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

Submitted by: Administrator
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;
// ...
});
Submitted by: Administrator

Read Online Node.js Job Interview Questions And Answers