问题
I get a problem with Express, I try to use the app.post() function but it's not working and I don't know why...
Though I included the bodyParser()...
The problem: Page load without response, no error message. I don't see the 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")
回答1:
I don't find any issue with app.post code !!
app.post('/login', function(req, res){
console.log('test');
res.end(); // end the response
});
One suggestion is that you should end each response send to client , otherwise your server become unresponsive.
回答2:
Your post request is send to /test
( since you've used action
attribute of your form ), while your express listener is listening to any post requests on /login
.
Your code should look like :
block content
h1 Login
h3 "tj" and "foobar"
form(method="post", action="/login")
If you want this to work!
回答3:
I copy 'router.post()...' to another js file,and change the route in app.js,it worked!But 'router.post()' method still a warning.sometimes it seems like a warning in webstorm,but it can actually work!
回答4:
Though I haven't gone through the entire code that was mentioned here, I had found a similar issue and was able to debug it.
I'll share where I've gone wrong and maybe that might help someone.
Below code is the part of HTML which went wrong:
<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
Bug in this code is, Though (type) of the is "submit", it has not been included inside tag. Now while you use "app.post" method to get data or trying to "console.log(value)" of the requirement, it can't access.
I was able to debug by including button tag inside the form.
来源:https://stackoverflow.com/questions/14786756/app-post-not-working-with-express