如何检索POST查询参数?

本文翻译自:How to retrieve POST query parameters?

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: 这是我的Express.js /Node.js代码:

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. 我尝试了sReq.query.emailsReq.query['email']sReq.params['email']等。它们都不起作用。 They all return undefined . 他们都返回undefined

When I change to a Get call, it works, so .. any idea? 当我改为Get电话时,它有效,所以..任何想法?


#1楼

参考:https://stackoom.com/question/NxWY/如何检索POST查询参数


#2楼

app.use(express.bodyParser());

然后对于app.post请求,您可以通过req.body.{post request variable}获取帖子值req.body.{post request variable}


#3楼

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 . 事情已经改变 ,再次启动Express 4.16.0 ,您现在可以像Express 3.0一样使用express.json()express.urlencoded()

This was different starting Express 4.0 to 4.15 : Express 4.0到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 : 其余的就像在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: 然后,在您的处理程序中,使用req.body对象:

// 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. 请注意,不建议使用express.bodyParser()

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. express.multipart()存在安全问题,因此最好显式添加对所需特定编码类型的支持。 If you do need multipart encoding (to support uploading files for example) then you should read this . 如果您确实需要多部分编码(例如,支持上传文件),那么您应该阅读此内容


#4楼

Security concern using express.bodyParser() 使用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 ). 虽然目前所有其他答案都建议使用express.bodyParser()中间件,但这实际上是express.json()express.urlencoded()express.multipart()中间件( http://expressjs.com express.multipart()的包装器express.json() /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. 表单请求主体的解析由express.urlencoded()中间件完成,并且是在req.body对象上公开表单数据req.body

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. 由于安全问题express.multipart() / connect.multipart()如何为所有上传的文件创建临时文件(并且不是垃圾回收),现在建议不要使用express.bodyParser()包装,而是使用只有你需要的中间件。

Note: connect.bodyParser() will soon be updated to only include urlencoded and json when Connect 3.0 is released (which Express extends). 注意: connect.bodyParser()很快将更新为仅在发布Connect 3.0(Express扩展)时包含urlencodedjson


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. 如果/当您需要处理多部分表单(文件上传)时,请使用第三方库或中间件,如multiparty,busboy,dicer等。


#5楼

You shoudn't use app.use(express.bodyParser()) . 你不应该使用app.use(express.bodyParser()) BodyParser is a union of json + urlencoded + mulitpart. BodyParser是json + urlencoded + mulitpart的联合。 You shoudn't use this because multipart will be removed in connect 3.0. 你不应该使用它,因为multipart将在connect 3.0中被删除。

To resolve that, you can do this: 要解决此问题,您可以执行以下操作:

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

It´s very important know that app.use(app.router) should be used after the json and urlencoded, otherwise it does not work! 非常重要的是知道应该在json和urlencoded之后使用app.use(app.router) ,否则它不起作用!


#6楼

Note for Express 4 users: Express 4用户注意事项:

If you try and put app.use(express.bodyParser()); 如果你试着把app.use(express.bodyParser()); into your app, you'll get the following error when you try to start your Express server: 在您的应用程序中,当您尝试启动Express服务器时,您将收到以下错误:

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. 错误:大多数中间件(如bodyParser)不再与Express捆绑在一起,必须单独安装。 Please see https://github.com/senchalabs/connect#middleware . 请参阅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 ): 你必须从npm单独安装包body-parser ,然后使用类似下面的东西(例子取自GitHub页面 ):

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();
})
发布了0 篇原创文章 · 获赞 137 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105454304