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?
Update for Express 4.4.1
Middleware of the following is removed from Express.
When you use the middleware directly like you did in express 3.0. You will get the following error:
In order to utilize those middleware, now you need to do npm for each middleware separately.
Since bodyParser is marked as deprecated, so I recommend the following way using json, urlencode and multipart parser like formidable, connect-multiparty. (Multipart middleware is deprecated as well).
Also remember, just defining urlencode + json, the form data will not be parsed and req.body will be undefined. You need to define a middleware handle the multipart request.
Request streaming worked for me
Security concern using express.bodyParser()
While all the other answers currently recommend using the
express.bodyParser()
middleware, this is actually a wrapper around theexpress.json()
,express.urlencoded()
, andexpress.multipart()
middlewares (http://expressjs.com/api.html#bodyParser). The parsing of form request bodies is done by theexpress.urlencoded()
middleware and is all that you need to expose your form data onreq.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 theexpress.bodyParser()
wrapper but instead use only the middlewares you need.Note:
connect.bodyParser()
will soon be updated to only includeurlencoded
andjson
when Connect 3.0 is released (which Express extends).So in short, instead of ...
...you should use
and if/when you need to handle multipart forms (file uploads), use a third party library or middleware such as multiparty, busboy, dicer, etc.
This will do it if you want to build the posted query without middleware:
That will send this to the browser
It's probably better to use middleware though so you don't have to write this over and over in each route.
Note: this answer is for Express 2. See here for Express 3.
If you're using connect/express, you should use the bodyParser middleware: It's described in the Expressjs guide.
Here's the original connect-only version:
Both the querystring and body are parsed using Rails-style parameter handling (
qs
) rather than the low-levelquerystring
library. In order to parse repeated parameters withqs
, the parameter needs to have brackets:name[]=val1&name[]=val2
. It also supports nested maps. In addition to parsing HTML form submissions, the bodyParser can parse JSON requests automatically.Edit: I read up on express.js and modified my answer to be more natural to users of Express.