How to retrieve POST query parameters?


Here is my simple form:

<form id="loginformA" action="userlogin" method="post">
        <label for="email">Email: </label>
        <input type="text" id="email" name="email"></input>
<input type="submit" value="Submit"></input>

Here is my Express.js/Node.js code:'/userlogin', function(sReq, sRes){    
    var email =;   

I tried or sReq.query['email'] or sReq.params['email'], etc. None of them work. They all return undefined.

When I change to a Get call, it works, so .. any idea?

12/13/2016 6:39:40 PM

Accepted Answer

Things have changed once again starting Express 4.16.0, you can now use express.json() and express.urlencoded() just like in Express 3.0.

This was different starting Express 4.0 to 4.15:

$ npm install --save body-parser

and then:

var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true

The rest is like 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'/test-page', function(req, res) {
    var name =,
        color = req.body.color;
    // ...

Note that the use of express.bodyParser() is not recommended.

app.use(express.bodyParser()); equivalent to:


Security concerns exist with express.multipart(), and so it is better to explicitly add support for the specific encoding type(s) you require. If you do need multipart encoding (to support uploading files for example) then you should read this.

12/27/2017 6:38:10 PM

Security concern using express.bodyParser()

While all the other answers currently recommend using the express.bodyParser() middleware, this is actually a wrapper around the express.json(), express.urlencoded(), and express.multipart() middlewares ( The parsing of form request bodies is done by the express.urlencoded() middleware and is all that you need to expose your form data on req.body object.

Due to a security concern with how express.multipart()/connect.multipart() creates temporary files for all uploaded files (and are not garbage collected), it is now recommended not to use the express.bodyParser() wrapper but instead use only the middlewares you need.

Note: connect.bodyParser() will soon be updated to only include urlencoded and json when Connect 3.0 is released (which Express extends).

So in short, instead of ...

app.use(express.bodyParser()); should use

app.use(express.json());      // if needed

and if/when you need to handle multipart forms (file uploads), use a third party library or middleware such as multiparty, busboy, dicer, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow