Node.js Question:

Do you know how to get POST query in express node.js?

Tweet Share WhatsApp

Answer:

Things have changed in express 3.0.

Firstly you need to add some middleware to parse the post data of the body.

Add one or both of the following lines of code:

app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies

Then, in your handler, use the req.body object:

// assuming POST: name=foo&color=red <-- URL encoding
//
// or POST: {"name":"foo","color":"red"} <-- JSON encoding

app.post('/test-page', function(req, res) {
var name = req.body.name,
color = req.body.color;
// ...
});

Download Node.js PDF Read All 15 Node.js Questions
Previous QuestionNext Question
Tell me how to use underscore.js as a template engine?Do you know how to debug node.js applications?