murvinlai murvinlai - 1 year ago 61
Javascript Question

How to retrieve POST query parameters in Express

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
, etc. None of them work. They all return

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

Answer Source

Things have changed again in Express 4.0:

$ 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download