App.post () doesn't work with Express
I have a problem with Express, I am trying to use the app.post () function, but it doesn’t work and I don’t know why ...
Although I've included bodyParser () ...
Problem: Page load with no response, no error message. I can't see console.log () ...
app.js:
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, postProvider = require('./postProvider.js')
, http = require('http')
, path = require('path')
, compass = require('node-compass')
, hash = require('./auth').hash;
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(compass());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser('dsqdq edsds'));
app.use(express.session());
});
app.configure('development', function () {
app.use(express.errorHandler());
});
app.get('/admin', function(req, res){
res.redirect('login');
});
app.get('/login', function(req, res){
res.render('login');
});
app.post('/login', function(req, res){
console.log('test');
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
login.jade:
extends layout
block content
h1 Login
h3 "tj" and "foobar"
form(method="post", action="/login")
input(type="text", placeholder="Username", autofocus="true", name="username")
br
input(type="password", placeholder="Password", name="password")
br
input(type="submit", value="login")
source to share
Your post request is sent to /test
(since you used action
your form attribute ), while your express listener listens for any post requests to /login
.
Your code should look like this:
block content
h1 Login
h3 "tj" and "foobar"
form(method="post", action="/login")
If you want this to work!
source to share
While I didn't go through all the code mentioned here, I encountered a similar problem and was able to debug it.
I will share where I went wrong and maybe it can help someone.
Below is the HTML code that is out of order:
<form action="/" method="**post**">
<select name="crypto">
<option value="BTC">Bitcoin</option>
<option value="ETC">Ethereum</option>
<option value="LTC">Litecoin</option>
</select>
<select name="fiat">
<option value="USD">US Dollors</option>
<option value="GBP">GB Pounds</option>
<option value="EUR">EU Euros</option>
</select>
</form>
Check
The error is in this code, although (type) is "submit", it was not included in the tag. Now when you use the "app.post" method to get data or try to tell "console.log (value)" to a requirement, it cannot access it.
I was able to debug by including the button tag inside the form.
source to share