Search code examples
node.jsexpressexpress-4

Nodejs: How can i simply get the request body using express4?


Now that express is not shipped anymore with middleware that fills the req.body variable i am fighting to get req.body filled again. I am sending a POST request to /xyz/:object/feedback.

here my code:

app.post('/xyz/:object/feedback', function(req, res)
{
    console.log('Feedback received.');

    console.log('Body: ', req.body); // is not available :(

    res.set('Content-Type', 'text/plain; charset=utf8');
    res.send(result ? JSON.stringify(req.body) : err);
});

I tried to use body-parser already, but "Feedback received." never got logged to my console. So something seems to get stuck here:

var bodyParser = require('body-parser');
app.use(bodyParser);

How can i get req.body filled? (i need some working code)


Solution

  • The problem is that you pass the whole module to the use method not the required instance.

    Instead of this:

    app.use(bodyParser);
    

    do

    app.use(bodyParser());