javascript - Node and Express simple POST - 400 Bad Request - Stack Overflow

Surely I'm doing something stupid, because this should be the easiest thing in the world.All I

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
 |  Show 3 more ments

2 Answers 2

Reset to default 3

In 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745182367a4615477.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信