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?
For Express 4.1 and above
As most of the answers are using to Express, bodyParser, connect; where multipart is deprecated. There is a secure way to send post multipart objects easily.
Multer can be used as replacement for connect.multipart().
To install the package
Load it in your app:
And then, add it in the middleware stack along with the other form parsing middleware.
connect.json() handles application/json
connect.urlencoded() handles application/x-www-form-urlencoded
multer() handles multipart/form-data
use express-fileupload package
Then for
app.post
request you can get post values viareq.body.{post request variable}
.Backend:
Frontend:
You shoudn't use app.use(express.bodyParser()). BodyParser is a union of json + urlencoded + mulitpart. You shoudn't use this because multipart will be removed in connect 3.0.
To resolve that, you can do this:
It´s very important know that app.use(app.router) should be used after the json and urlencoded, otherwise it does not work!
Things have changed once again starting Express 4.16.0, you can now use
express.json()
andexpress.urlencoded()
just like in Express 3.0.This was different starting Express 4.0 to 4.15:
and then:
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:
Then, in your handler, use the
req.body
object:Note that the use of
express.bodyParser()
is not recommended....is 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.