node.js – Express.js req.body undefined

node.js – Express.js req.body undefined

UPDATE July 2020

express.bodyParser() is no longer bundled as part of express. You need to install it separately before loading:

npm i body-parser

// then in your app
var express = require(express)
var bodyParser = require(body-parser)
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies, urlencodedParser, function (req, res) {
  res.send(welcome,  + req.body.username)
// POST /api/users gets JSON bodies, jsonParser, function (req, res) {
  // create user in req.body

See here for further info

original follows

You must make sure that you define all configurations BEFORE defining routes. If you do so, you can continue to use express.bodyParser().

An example is as follows:

var express = require(express),
    app     = express(),
    port    = parseInt(process.env.PORT, 10) || 8080;


app.listen(port);, function(req, res) {
  res.send({ status: SUCCESS });

Latest versions of Express (4.x) has unbundled the middleware from the core framework. If you need body parser, you need to install it separately

npm install body-parser --save

and then do this in your code

var bodyParser = require(body-parser)
var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json

node.js – Express.js req.body undefined

No. You need to use app.use(express.bodyParser()) before app.use(app.router). In fact, app.use(app.router) should be the last thing you call.

Leave a Reply

Your email address will not be published.