admin管理员组文章数量:1415654
Surely I'm doing something stupid, because this should be the easiest thing in the world.
All I'm trying to do is perform a POST in an Express route.
My app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
My index.js route:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', {title: 'Express'});
});
router.post("/test", function(req, res) {
console.log("Hello...anyone!?");
res.end();
});
module.exports = router;
The GET works fine. I can pull http:/localhost:3000 right up in a browser.
When I fire a POST against http:/localhost:3000/test it results in a 400 Bad Gateway.
Surely I'm doing something stupid, because this should be the easiest thing in the world.
All I'm trying to do is perform a POST in an Express route.
My app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
My index.js route:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', {title: 'Express'});
});
router.post("/test", function(req, res) {
console.log("Hello...anyone!?");
res.end();
});
module.exports = router;
The GET works fine. I can pull http:/localhost:3000 right up in a browser.
When I fire a POST against http:/localhost:3000/test it results in a 400 Bad Gateway.
Share Improve this question asked Feb 12, 2017 at 17:46 Tsar BombaTsar Bomba 1,1066 gold badges31 silver badges65 bronze badges 8- Have you tried putting post above get? – Vsevolod Goloviznin Commented Feb 12, 2017 at 17:51
- @VsevolodGoloviznin Yes...same result. – Tsar Bomba Commented Feb 12, 2017 at 17:52
- 1 @TsarBomba Could you please share how you are firing the POST request? – Nehal J Wani Commented Feb 12, 2017 at 18:10
- @NehalJWani I'm using both Postman and Fiddler, and executing a POST against http:/localhost:3000/test – Tsar Bomba Commented Feb 12, 2017 at 18:16
- Strange. Your code runs for me without any problems. – Nehal J Wani Commented Feb 12, 2017 at 18:18
2 Answers
Reset to default 3In the end, this had nothing to do with Node or the code posted. I rebooted my PC and the issue went away. I can't find anything that explains it.
In case anyone finds this helpful, I ran into the same issue and the culprit turned out to be missing headers. I knew I needed the "Content-Type": "application/json"
header, which I already had in place, but I didn't know that I was missing two other headers.
The solution for me was also adding the "Content-Length"
and "Host"
headers in Postman.
I see some others have questioned the need for the "Content-Length"
header, but in my case, the minimum three needed were "Content-Type"
, "Content-Length"
, and "Host"
or it would always fail.
本文标签: javascriptNode and Express simple POST400 Bad RequestStack Overflow
版权声明:本文标题:javascript - Node and Express simple POST - 400 Bad Request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745182366a2646524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论