Node.js Express express.json 和 express.urlencoded 与表单提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22143105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:57:18  来源:igfitidea点击:

Node.js Express express.json and express.urlencoded with form submit

node.jsexpress

提问by huggie

Express (or Connect's) bodyParser middleware is marked deprecated and users are advised to use instead:

Express(或 Connect 的) bodyParser 中间件被标记为已弃用,建议用户改用:

app.use(connect.urlencoded())
app.use(connect.json())  

However, when I run the an example from Node.js in Action, I use curl to fill out the form as suggested by the book:

但是,当我运行Node.js in Action 中的一个示例时,我使用 curl 来按照本书的建议填写表单:

curl -F entry[title]='Ho ho ho' -F entry[body]='santa loves you' http://abc:[email protected]:3000/api/entry

It doesn't work. req.bodyis not defined. Am I missing something? It works fine with bodyParser.

它不起作用。req.body没有定义。我错过了什么吗?它适用于 bodyParser。

EDIT: SOLUTION as of Express 4

编辑:自 Express 4 起的解决方案

Parse json this way:

以这种方式解析 json:

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

...

app.use(bodyParser.json());

Parse urlencoded body this way:

以这种方式解析 urlencoded 正文:

app.use(bodyParser.urlencoded({extended: true}));

Then there is no deprecation warning. The extended: true (default) uses the qs module and false uses the querystring module to parse the body.

然后没有弃用警告。extended: true(默认)使用 qs 模块,false 使用 querystring 模块来解析正文。

Don't use app.use(bodyParser()), that usage is now deprecated.

不要使用app.use(bodyParser()),该用法现已弃用。

回答by Paul Mougel

bodyParseris in fact the composition of three middlewares (see documentationand relevant source code): json, urlencodedand multipart:

bodyParser实际上是三个中间件的组合(参见文档相关源代码):jsonurlencodedmultipart

  • jsonparses application/jsonrequest bodies
  • urlencodedparses x-ww-form-urlencodedrequest bodies
  • and multipartparses multipart/form-datarequest bodies, which is what you're interested in.
  • json解析application/json请求体
  • urlencoded解析x-ww-form-urlencoded请求体
  • multipart解析multipart/form-data您感兴趣的请求正文。

If you only specify jsonand urlencodedmiddlewares, the form data won't be parsed by any middleware, thus req.bodywon't be defined. You then need to add a middleware that is able to parse form data such as formidable, busboy or multiparty (as stated in connect's documentation).

如果只指定jsonurlencoded中间件,则表单数据不会被任何中间件解析,因此req.body不会被定义。然后,您需要添加一个能够解析表单数据的中间件,例如 formidable、busboy 或 multiparty(如connect的文档中所述)。

Here is an example, using multiparty:

这是一个示例,使用multiparty

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.use('/url/that/accepts/form-data', multipartMiddleware);
app.post('/url/that/accepts/form-data', function(req, resp) {
    console.log(req.body, req.files);
});

Don't forget that by using such middlewares you allow anyone to upload files to your server: it then your responsibility to handle (and delete) those files.

不要忘记,通过使用此类中间件,您允许任何人将文件上传到您的服务器:然后您有责任处理(和删除)这些文件。