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?
Given some form:
Using express
Output:
Note for Express 4 users:
If you try and put
app.use(express.bodyParser());
into your app, you'll get the following error when you try to start your Express server:You'll have to install the package
body-parser
separately from npm, then use something like the following (example taken from the GitHub page):I could find all parameters by using following code for both POST and GET requests.
I was searching for this exact problem. I was following all the advice above but req.body was still returning an empty object {}. In my case, it was something just as simple as the html being incorrect.
In your form's html, make sure you use the
'name'
attribute in your input tags, not just'id'
. Otherwise, nothing is parsed.My idiot mistake is your benefit.
You are using 'req.query.post' with wrong method 'req.query.post' works with 'method=get' 'method=post' works with body-parser.
Just try this by changing post to get
And in express code use 'app.get'