Masiar Masiar - 4 months ago 542
Node.js Question

Express.js req.body undefined

I have this as configuration of my Express server

app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("view options", { layout: true });
//Handles post requests
app.use(express.bodyParser());
//Handles put requests
app.use(express.methodOverride());


But still when I ask for
req.body.something
in my routes I get some error pointing out that
body is undefined
. Here is an example of a route that uses
req.body
:

app.post('/admin', function(req, res){
console.log(req.body.name);
});


I read that this problem is caused by the lack of
app.use(express.bodyParser());
but as you can see I call it before the routes.

Any clue?

Answer

As already posted under one comment, I solved it using

app.use(require('connect').bodyParser());

instead of

app.use(express.bodyParser());

I still don't know why the simple express.bodyParser() is not working...

Comments