Reputation: 598
I have a controller accessed from a route through a
router.post('/human',human.create);
human is imported through its controller file which contains the following :
var config = require(__dirname + '/../config/config'),
logger = require(__dirname + '/../lib/logger'),
util = require(__dirname + '/../helpers/util');
exports.create = function(request, response, next){
response.send({
id: "human_103tsG2eZvKYlo2CW5yEDyUe",
firstName: "Patricia",
middleName: null,
lastName: "Cesar",
sex: "Female",
birthdate: null
});
};
Inside this create function, I can do
console.log(request);
And a humongous JSON object will apear in the terminal, including the attribute I need : body.
However, when I do, console.log(request.body)
, it goes undefined.
Am I missing a particular functionality of Node or Express that needs to be coded out?
Upvotes: 0
Views: 924
Reputation: 2183
I guess you are using the express 4.x which decoupled lots of middleware packages, as @shredmill metioned, you should use
bodyParser = require ('body-parser')
...
app.use(bodyParser.json()); //i found this works for me comparing to use bodyParser()
Upvotes: 1
Reputation: 146064
req.body
is not pre-parsed for you automatically. You should explicitly use the connect.json()
middleware for this:
var connect = require('connect');
router.post('/human', connect.json(), human.create);
Upvotes: 0