Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access POST form fields

Here is my simple form:

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

Here is my Express.js/Node.js code:

app.post('/userlogin', function(sReq, sRes){         var email = sReq.query.email.;    } 

I tried sReq.query.email 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?

like image 561
murvinlai Avatar asked Apr 19 '11 00:04

murvinlai


People also ask

How do I get Express body data?

Before we can easily access this data on the server side in Express, we need to use some middleware, like the body-parser package, to parse the data in to a format that we can easily access. Once the data from the raw HTTP request is parsed, it can then be accessed via the body property of the req object.

How do you post data from a form?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

What is Post () in Express?

js POST Method. Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.


2 Answers

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  app.post('/test-page', function(req, res) {     var name = req.body.name,         color = req.body.color;     // ... }); 

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

app.use(express.bodyParser()); 

...is equivalent to:

app.use(express.json()); app.use(express.urlencoded()); app.use(express.multipart()); 

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.

like image 149
Drew Noakes Avatar answered Sep 19 '22 16:09

Drew Noakes


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 (http://expressjs.com/api.html#bodyParser). 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()); 

...you should use

app.use(express.urlencoded()); 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.

like image 25
Sean Lynch Avatar answered Sep 19 '22 16:09

Sean Lynch