身体解析器用express做什么?

时间:2022-12-19 07:48:38

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应用程序中需要body-parser,因为我们可以在不使用body解析器的情况下获得数据。它的作用是什么?

6 个解决方案

#1


74  

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

以快速方式处理HTTP POST请求。js版本4及以上,您需要安装名为body-parser的中间件模块。

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

解析器提取传入请求流的整个主体部分,并在req.body上公开它。

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

中间件是Express的一部分。之前是js,但现在必须分别安装。

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.

该解析器模块解析使用HTTP POST请求提交的JSON、缓冲区、字符串和URL编码数据。使用NPM安装体解析器,如下所示。

npm install body-parser --save

#2


8  

The answer here explain it very detailed and brilliantly, the answer contains:

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

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.

简而言之,body-parser提取传入请求流的整个主体部分,并在req上公开。身体是更容易接触的东西。你不需要它本身,因为你可以自己做所有的事情。然而,它很可能会做你想做的,并为你省去麻烦。


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).

再深入一点;body-parser为您提供了一个中间件,它使用nodejs/zlib解压缩传入的请求数据(如果它是压缩的,那么流-utils/ rawbody等待请求体的完整原始内容,然后再“解析它”(这意味着如果您不打算使用请求体,那么您只是浪费了一些时间)。

After having the raw contents, body-parser will parse it using one of four strategies, depending on the specific middleware you decided to use:

在获得原始内容之后,body-parser将使用四种策略之一来解析它,具体取决于您决定使用的特定中间件:

  • bodyParser.raw(): Doesn't actually parse the body, but just exposes the buffered up contents from before in a Buffer on req.body.

    bodyParser.raw():实际上并不解析主体,而是在req.body上的缓冲区中公开之前缓存的内容。

  • bodyParser.text(): Reads the buffer as plain text and exposes the resulting string on req.body.

    text():将缓冲区作为纯文本读取,并在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.

    urlencodes():将文本解析为URL编码的数据(这是浏览器从常规表单发送到POST的方式),并在req.body上公开结果对象(包含键和值)。进行比较;在PHP中,所有这些都是自动完成的,并在$_POST中公开。

  • bodyParser.json(): Parses the text as JSON and exposes the resulting object on req.body.

    JSON():将文本解析为JSON,并在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.

只有在设置了req之后。它将调用堆栈中的下一个中间件,该中间件可以访问请求数据,而不必考虑如何解压和解析它。

You can refer to body-parser github to read their documentation, it contains information regarding its working.

您可以参考body-parser github来阅读它们的文档,它包含关于其工作的信息。

#3


7  

It parses the HTTP request body. This is usually necessary when you need to know more than just the URL you hit, particular in the context of a POST or PUT PATCH HTTP request where the information you want is contains in the body.

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

Basically its a middleware for parsing JSON, plain text, or just returning a raw Buffer object for you to deal with as you require.

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

#4


4  

Yes we can work without body-parser. When you do that you get the raw request, and your body and headers are not in the root object. You will have to individually manipulate all the fields.

是的,我们可以不使用body-parser。当您这样做时,您将获得原始请求,并且您的主体和头部不在根对象中。您将不得不单独操作所有字段。

Or you can use body-parser, as the express team is maintaining it .

或者您可以使用body-parser,因为express团队正在维护它。

What body-parser can do for you: It simplifies the request.
How to use it: Here is example:

解析器能为您做什么:它简化了请求。如何使用:这里有一个例子:

Install npm install body-parser --save

安装npm安装解析器——保存

This how to use body-parser in 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 }));

Link.

链接。

https://github.com/expressjs/body-parser

https://github.com/expressjs/body-parser

#5


3  

In order to get access to the post data we have to use body-parser. Basically what the body-parser is which allows express to read the body and then parse that into a Json object that we can understand.

为了访问post数据,我们必须使用body解析器。基本上,body-parser允许express读取正文,然后将其解析为我们可以理解的Json对象。

#6


3  

These are all a matter of convenience.

这些都是方便的问题。

Basically, if the question were 'Do we need to use body-parser?' The answer is 'No'. We can come up with the same information from the client-post-request using a more circuitous route that will generally be less flexible and will increase the amount of code we have to write to get the same information.

基本上,如果问题是“我们需要使用body-parser吗?”答案是否定的。我们可以通过更迂回的路径从客户端后请求中获得相同的信息,这通常会更不灵活,并且会增加我们必须编写的代码数量以获得相同的信息。

This is kind of the same as asking 'Do we need to use express to begin with?' Again, the answer there is no, and again, really it all comes down to saving us the hassle of writing more code to do the basic things that express comes with 'built-in'.

这和问“我们需要使用express开始吗?”再一次,答案是否定的,实际上,这一切都归结于为我们省去了编写更多代码来完成“内置”的基本功能的麻烦。

On the surface - body-parser makes it easier to get at the information contained in client requests in a variety of formats instead of making you capture the raw data streams and figuring out what format the information is in, much less manually parsing that information into useable data.

在表面上,body-parser使您更容易地获取各种格式的客户端请求中的信息,而不是让您捕获原始数据流并确定信息的格式,更不用说手动地将这些信息解析为可用的数据了。

#1


74  

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

以快速方式处理HTTP POST请求。js版本4及以上,您需要安装名为body-parser的中间件模块。

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

解析器提取传入请求流的整个主体部分,并在req.body上公开它。

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

中间件是Express的一部分。之前是js,但现在必须分别安装。

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.

该解析器模块解析使用HTTP POST请求提交的JSON、缓冲区、字符串和URL编码数据。使用NPM安装体解析器,如下所示。

npm install body-parser --save

#2


8  

The answer here explain it very detailed and brilliantly, the answer contains:

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

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.

简而言之,body-parser提取传入请求流的整个主体部分,并在req上公开。身体是更容易接触的东西。你不需要它本身,因为你可以自己做所有的事情。然而,它很可能会做你想做的,并为你省去麻烦。


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).

再深入一点;body-parser为您提供了一个中间件,它使用nodejs/zlib解压缩传入的请求数据(如果它是压缩的,那么流-utils/ rawbody等待请求体的完整原始内容,然后再“解析它”(这意味着如果您不打算使用请求体,那么您只是浪费了一些时间)。

After having the raw contents, body-parser will parse it using one of four strategies, depending on the specific middleware you decided to use:

在获得原始内容之后,body-parser将使用四种策略之一来解析它,具体取决于您决定使用的特定中间件:

  • bodyParser.raw(): Doesn't actually parse the body, but just exposes the buffered up contents from before in a Buffer on req.body.

    bodyParser.raw():实际上并不解析主体,而是在req.body上的缓冲区中公开之前缓存的内容。

  • bodyParser.text(): Reads the buffer as plain text and exposes the resulting string on req.body.

    text():将缓冲区作为纯文本读取,并在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.

    urlencodes():将文本解析为URL编码的数据(这是浏览器从常规表单发送到POST的方式),并在req.body上公开结果对象(包含键和值)。进行比较;在PHP中,所有这些都是自动完成的,并在$_POST中公开。

  • bodyParser.json(): Parses the text as JSON and exposes the resulting object on req.body.

    JSON():将文本解析为JSON,并在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.

只有在设置了req之后。它将调用堆栈中的下一个中间件,该中间件可以访问请求数据,而不必考虑如何解压和解析它。

You can refer to body-parser github to read their documentation, it contains information regarding its working.

您可以参考body-parser github来阅读它们的文档,它包含关于其工作的信息。

#3


7  

It parses the HTTP request body. This is usually necessary when you need to know more than just the URL you hit, particular in the context of a POST or PUT PATCH HTTP request where the information you want is contains in the body.

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

Basically its a middleware for parsing JSON, plain text, or just returning a raw Buffer object for you to deal with as you require.

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

#4


4  

Yes we can work without body-parser. When you do that you get the raw request, and your body and headers are not in the root object. You will have to individually manipulate all the fields.

是的,我们可以不使用body-parser。当您这样做时,您将获得原始请求,并且您的主体和头部不在根对象中。您将不得不单独操作所有字段。

Or you can use body-parser, as the express team is maintaining it .

或者您可以使用body-parser,因为express团队正在维护它。

What body-parser can do for you: It simplifies the request.
How to use it: Here is example:

解析器能为您做什么:它简化了请求。如何使用:这里有一个例子:

Install npm install body-parser --save

安装npm安装解析器——保存

This how to use body-parser in 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 }));

Link.

链接。

https://github.com/expressjs/body-parser

https://github.com/expressjs/body-parser

#5


3  

In order to get access to the post data we have to use body-parser. Basically what the body-parser is which allows express to read the body and then parse that into a Json object that we can understand.

为了访问post数据,我们必须使用body解析器。基本上,body-parser允许express读取正文,然后将其解析为我们可以理解的Json对象。

#6


3  

These are all a matter of convenience.

这些都是方便的问题。

Basically, if the question were 'Do we need to use body-parser?' The answer is 'No'. We can come up with the same information from the client-post-request using a more circuitous route that will generally be less flexible and will increase the amount of code we have to write to get the same information.

基本上,如果问题是“我们需要使用body-parser吗?”答案是否定的。我们可以通过更迂回的路径从客户端后请求中获得相同的信息,这通常会更不灵活,并且会增加我们必须编写的代码数量以获得相同的信息。

This is kind of the same as asking 'Do we need to use express to begin with?' Again, the answer there is no, and again, really it all comes down to saving us the hassle of writing more code to do the basic things that express comes with 'built-in'.

这和问“我们需要使用express开始吗?”再一次,答案是否定的,实际上,这一切都归结于为我们省去了编写更多代码来完成“内置”的基本功能的麻烦。

On the surface - body-parser makes it easier to get at the information contained in client requests in a variety of formats instead of making you capture the raw data streams and figuring out what format the information is in, much less manually parsing that information into useable data.

在表面上,body-parser使您更容易地获取各种格式的客户端请求中的信息,而不是让您捕获原始数据流并确定信息的格式,更不用说手动地将这些信息解析为可用的数据了。