body-parser 用 express 做什么? [英] What does body-parser do with express?

查看:33
本文介绍了body-parser 用 express 做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在 Express 应用程序中需要 body-parser,因为我们可以在不使用 body-parser 的情况下获取数据.它实际上做了什么以及如何做?

I don't understand why we need body-parser in an Express application, as we can get data without using body-parser. And what does it do actually and how?

推荐答案

要在 Express.js 版本 4 及更高版本中处理 HTTP POST 请求,您需要安装中间件模块称为body-parser.

To handle HTTP POST requests in Express.js version 4 and above, you need to install the middleware module called body-parser.

body-parser 提取传入请求流的整个主体部分并将其公开在 req.body 上.

body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body.

中间件之前是 Express.js 的一部分,但现在您必须单独安装它.

The middleware was a part of Express.js earlier but now you have to install it separately.

这个 body-parser 模块解析使用 HTTP POST 请求提交的 JSON、缓冲区、字符串和 URL 编码数据.使用 NPM 安装 body-parser,如下所示.

This body-parser module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request. Install body-parser using NPM as shown below.

npm install body-parser --save

在 2019 年 4 月 2 日在 express@4.16.0 中,与 express 捆绑在一起的 body-parser 中间件.有关详细信息,请参阅此

edit in 2019-april-2: in express@4.16.0 the body-parser middleware bundled with express. for more details see this

这篇关于body-parser 用 express 做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆