我不明白为什么在Express应用程序中需要体解析器,因为我们可以不使用体解析器来获取数据。 它到底做什么,怎么做?
当前回答
保持简单:
如果你使用post请求,那么你需要请求的主体,所以你 将需要体解析器。 不需要安装带有express的body-parser,但如果您愿意,则必须使用它 接收岗位请求。
app.use (bodyParser。Urlencoded ({extended: false}));
{ extended: false }
错误的意思是,在body对象中没有嵌套数据。注意:请求数据内嵌在请求体对象中。
其他回答
编辑: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
如果你不想使用单独的npm包体解析器,最新的express(4.16+)有内置的体解析器中间件,可以这样使用:
const app = express();
app.use(express.json({ limit: '100mb' }));
附:并不是所有的体解析功能都在表达式中。请参考文档以获得完整的使用方法
这里的答案解释得非常详细和精彩,答案包括:
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来阅读他们的文档,它包含了关于其工作的信息。
保持简单:
如果你使用post请求,那么你需要请求的主体,所以你 将需要体解析器。 不需要安装带有express的body-parser,但如果您愿意,则必须使用它 接收岗位请求。
app.use (bodyParser。Urlencoded ({extended: false}));
{ extended: false }
错误的意思是,在body对象中没有嵌套数据。注意:请求数据内嵌在请求体对象中。
为了访问post数据,我们必须使用body解析器。基本上body-parser就是允许express读取body然后解析成我们能理解的Json对象。
推荐文章
- 很好的初学者教程socket.io?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- Node.js同步执行系统命令
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- 发送JWT令牌在头部与邮差
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成