Answers

Question and Answer:

  Home  Node.js

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

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;

// ...

});

 153 views

More Questions for you: