我不明白为什么在Express应用程序中需要体解析器,因为我们可以不使用体解析器来获取数据。 它到底做什么,怎么做?
当前回答
这里的答案解释得非常详细和精彩,答案包括:
In short; body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body as something easier to interface with. You don't need it per se, because you could do all of that yourself. However, it will most likely do what you want and save you the trouble. To go a little more in depth; body-parser gives you a middleware which uses nodejs/zlib to unzip the incoming request data if it's zipped and stream-utils/raw-body to await the full, raw contents of the request body before "parsing it" (this means that if you weren't going to use the request body, you just wasted some time). After having the raw contents, body-parser will parse it using one of four strategies, depending on the specific middleware you decided to use: bodyParser.raw(): Doesn't actually parse the body, but just exposes the buffered up contents from before in a Buffer on req.body. bodyParser.text(): Reads the buffer as plain text and exposes the resulting string on req.body. bodyParser.urlencoded(): Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST) and exposes the resulting object (containing the keys and values) on req.body. For comparison; in PHP all of this is automatically done and exposed in $_POST. bodyParser.json(): Parses the text as JSON and exposes the resulting object on req.body. Only after setting the req.body to the desirable contents will it call the next middleware in the stack, which can then access the request data without having to think about how to unzip and parse it.
你可以参考body-parser github来阅读他们的文档,它包含了关于其工作的信息。
其他回答
这里的答案解释得非常详细和精彩,答案包括:
In short; body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body as something easier to interface with. You don't need it per se, because you could do all of that yourself. However, it will most likely do what you want and save you the trouble. To go a little more in depth; body-parser gives you a middleware which uses nodejs/zlib to unzip the incoming request data if it's zipped and stream-utils/raw-body to await the full, raw contents of the request body before "parsing it" (this means that if you weren't going to use the request body, you just wasted some time). After having the raw contents, body-parser will parse it using one of four strategies, depending on the specific middleware you decided to use: bodyParser.raw(): Doesn't actually parse the body, but just exposes the buffered up contents from before in a Buffer on req.body. bodyParser.text(): Reads the buffer as plain text and exposes the resulting string on req.body. bodyParser.urlencoded(): Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST) and exposes the resulting object (containing the keys and values) on req.body. For comparison; in PHP all of this is automatically done and exposed in $_POST. bodyParser.json(): Parses the text as JSON and exposes the resulting object on req.body. Only after setting the req.body to the desirable contents will it call the next middleware in the stack, which can then access the request data without having to think about how to unzip and parse it.
你可以参考body-parser github来阅读他们的文档,它包含了关于其工作的信息。
编辑:2019年4月2日 在express@4.16.0中,express中包含了体解析器中间件,因此您不再需要单独安装体解析器。更多细节请看这个
OLD:
要在Express.js版本4及更高版本中处理HTTP POST请求,需要安装名为body-parser的中间件模块。
body解析器提取传入请求流的整个body部分,并在req.body上公开它。
中间件之前是Express.js的一部分,但现在你必须单独安装它。
这个体解析器模块解析使用HTTP POST请求提交的JSON、缓冲区、字符串和URL编码的数据。使用NPM安装体解析器,如下所示。
npm install body-parser --save
这些都是为了方便起见。
基本上,如果问题是“我们需要使用体解析器吗?”答案是否定的。我们可以使用更迂回的路线从请求后的客户端获得相同的信息,这种路线通常不太灵活,并且会增加我们必须编写的代码数量来获得相同的信息。
这有点像问“我们一开始需要用express吗?”同样,答案是否定的,同样,这一切都归结为为我们省去了编写更多代码来完成“内置”表达的基本事情的麻烦。
从表面上看,体解析器使以各种格式获取客户端请求中包含的信息变得更容易,而不是让您捕获原始数据流并确定信息的格式,更不用说手动将信息解析为可用的数据了。
是的,我们可以在没有体解析器的情况下工作。当你不使用它时,你得到原始请求,你的主体和头不在request参数的根对象中。您必须单独操作所有字段。
或者您可以使用body解析器,因为express团队正在维护它。
body-parser能为您做什么:它简化了请求。 如何使用:下面是例子:
安装body-parser
这是如何在express中使用body-parser:
const express = require('express'),
app = express(),
bodyParser = require('body-parser');
// support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
链接。
https://github.com/expressjs/body-parser。
然后你可以在根请求对象中得到body和header。例子
app.post("/posturl",function(req,res,next){
console.log(req.body);
res.send("response");
});
如果你不想使用单独的npm包体解析器,最新的express(4.16+)有内置的体解析器中间件,可以这样使用:
const app = express();
app.use(express.json({ limit: '100mb' }));
附:并不是所有的体解析功能都在表达式中。请参考文档以获得完整的使用方法
推荐文章
- 如何在vue-cli项目中更改端口号
- 同步和异步编程(在node.js中)的区别是什么?
- 如何编辑通过npm安装的节点模块?
- “node_modules”文件夹应该包含在git存储库中吗
- 使用package.json在全局和本地安装依赖项
- this.libOptions.parse不是一个函数
- 对嵌套文件夹运行npm install的最好方法是什么?
- 节点Multer异常字段
- 很好的初学者教程socket.io?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- Node.js同步执行系统命令