How to retrieve POST query parameters?

2018-12-31 04:23发布

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?

18条回答
听够珍惜
2楼-- · 2018-12-31 04:35

Given some form:

<form action='/somepath' method='post'>
   <input type='text' name='name'></input>
</form>

Using express

app.post('/somepath', function(req, res) {

    console.log(JSON.stringify(req.body));

    console.log('req.body.name', req.body['name']);
});

Output:

{"name":"x","description":"x"}
req.param.name x
查看更多
情到深处是孤独
3楼-- · 2018-12-31 04:36

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:

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

You'll have to install the package body-parser separately from npm, then use something like the following (example taken from the GitHub page):

var express    = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser());

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next();
})
查看更多
泪湿衣
4楼-- · 2018-12-31 04:38

I could find all parameters by using following code for both POST and GET requests.

 var express = require('express');
    var app = express();
    const util = require('util');  
    app.post('/', function (req, res) {
        console.log("Got a POST request for the homepage");
        res.send(util.inspect(req.query,false,null));
    })

查看更多
ら面具成の殇う
5楼-- · 2018-12-31 04:39

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.

<input id='foo' type='text' value='1'/>             // req = {}
<input id='foo' type='text' name='foo' value='1' /> // req = {foo:1}

My idiot mistake is your benefit.

查看更多
忆尘夕之涩
6楼-- · 2018-12-31 04:40
Post Parameters can be retrieved as follows-

app.post('/api/v1/test',Testfunction);
http.createServer(app).listen(port, function(){
console.log("Express server listening on port " + port)
});

function Testfunction(request,response,next)
{
   console.log(request.param("val1"));

   response.send('HI');
}
查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 04:44

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

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

And in express code use 'app.get'

查看更多
登录 后发表回答