我不明白为什么在Express应用程序中需要体解析器,因为我们可以不使用体解析器来获取数据。 它到底做什么,怎么做?


当前回答

历史:

早期版本的Express曾经捆绑了许多中间件。bodyParser是附带的中间件之一。当Express 4.0发布时,他们决定从Express中移除捆绑的中间件,并将它们作为单独的包。在安装bodyParser模块后,语法从app.use(express.json())改为app.use(bodyParser.json())。

bodyParser在4.16.0版本中被添加回Express,因为人们希望它像以前一样与Express捆绑在一起。这意味着如果您使用的是最新版本,则不必再使用bodyParser.json()。您可以使用express.json()代替。

对于那些感兴趣的人来说,4.16.0的发布历史在这里,拉请求在这里。

好吧,言归正传,

实现:

你需要加的就是加,

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

在路由声明之前,

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

快递公司会处理您的要求。:)

完整的例子是这样的,

const express       = require('express')
const app           = express()

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

app.post('/test-url', (req, res) => {
    console.log(req.body)
    return res.send("went well")
})

app.listen(3000, () => {
    console.log("running on port 3000")
})

其他回答

它解析HTTP请求体。这通常是必要的,当您需要知道的不仅仅是您所点击的URL时,特别是在POST或PUT PATCH HTTP请求的上下文中,您想要的信息包含在正文中。

基本上,它是一个中间件,用于解析JSON,纯文本,或只是返回一个原始的Buffer对象供您根据需要处理。

这里的答案解释得非常详细和精彩,答案包括:

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对象。

历史:

早期版本的Express曾经捆绑了许多中间件。bodyParser是附带的中间件之一。当Express 4.0发布时,他们决定从Express中移除捆绑的中间件,并将它们作为单独的包。在安装bodyParser模块后,语法从app.use(express.json())改为app.use(bodyParser.json())。

bodyParser在4.16.0版本中被添加回Express,因为人们希望它像以前一样与Express捆绑在一起。这意味着如果您使用的是最新版本,则不必再使用bodyParser.json()。您可以使用express.json()代替。

对于那些感兴趣的人来说,4.16.0的发布历史在这里,拉请求在这里。

好吧,言归正传,

实现:

你需要加的就是加,

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

在路由声明之前,

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

快递公司会处理您的要求。:)

完整的例子是这样的,

const express       = require('express')
const app           = express()

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

app.post('/test-url', (req, res) => {
    console.log(req.body)
    return res.send("went well")
})

app.listen(3000, () => {
    console.log("running on port 3000")
})