Thariama
Thariama

Reputation: 50840

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)

Upvotes: 0

Views: 550

Answers (2)

0101
0101

Reputation: 2712

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());

Upvotes: 4

Scimonster
Scimonster

Reputation: 33409

You need to do app.use(bodyParser()).

bodyParser() will return a function with what to use. bodyParser on its own is an invalid function for Express.

Upvotes: 0

Related Questions