What does a parser body do with an expression?

I don’t understand why we need body-parser in the Express application, since we can get data without using body-parser . And what is he really doing and how?

+228
express body-parser
Jul 11 '16 at 12:08
source share
9 answers

To process the HTTP POST request in Express.js version 4 and later, you need to install a middleware module called body-parser .

body-parser extracts the entire body part of the incoming request stream and sets it to req.body .

Middleware was previously part of Express.js, but now it needs to be installed separately.

This body-parser module parses JSON encoded data, buffers, strings, and URLs sent using an HTTP POST request. Install body-parser using NPM as shown below.

 npm install body-parser --save 

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

+155
Apr 26 '17 at 6:47
source share

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

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

What body-parser can do for you: This simplifies the query.
How to use this: Here is an example:

Install npm install body-parser --save

Here's how to use body-parser in express:

 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 .

+41
Jan 21 '18 at 7:53
source share

The answer here explains it in great detail and brilliantly, the answer contains:

In short; body-parser extracts the entire body part of the incoming request stream and provides it to req.body as something easier to interact with. You do not need this on your own, because you could do all this yourself. However, he is likely to do what you want and save you trouble.




Go a little deeper; body-parser gives you middleware that uses nodejs / zlib to unzip the incoming request data if it is archived, and stream-utils / raw-body waits for the full, raw content of the request body to be parsed (this means if you are not going to use the request body, you just wasted some time).

After the content is raw, body-parser will analyze it using one of four strategies, depending on the particular middleware that you decide to use:

  • bodyParser.raw () : does not actually parse the body, but simply req.body buffer contents from the buffer in req.body .

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

  • bodyParser.urlencoded () : parses text as URL-encoded data (how browsers typically send form data from regular forms set to POST) and provide a result object (containing keys and values) for req.body . For comparison; in PHP, all this is automatically executed and displayed in $_POST .

  • bodyParser.json () : parses the text as JSON and the req.body result object on req.body .

Only after setting req.body to the desired content will it call the next middleware on the stack, which can then access the request data without thinking about how to unpack and analyze it.

You can contact github body-parser to read their documentation, it contains information about its work.

+39
Nov 25 '17 at 12:52 on
source share

Let's try to keep it the least technical.

Suppose you submit html form data to a node-js server, i.e. You have made a request to the server. The server file will receive your request under the request object. Now, logically, if you consolidate this request object in your server file, you should see the data of your form somewhere in it, which could be extracted then, but whoa! you really don't!

So where is our data? How do we extract it, if it’s not only in my request.

A simple explanation for this is that http sends your form data into bits and pieces that are intended to be assembled as they are reached. So how would you retrieve your data.

But why not cause this pain every time manually disassembling your data for pieces and assembly. Use something called "body-parser" that will do it for you.

body-parser analyzes your request and converts it into a format from which you can easily extract the information you need.

For example, let's say you have a registration form in your interface. You fill it in and request a server to save data somewhere.

Retrieving the username and password from your request is as simple as shown below if you use body-parser.

 var loginDetails = { username : request.body.username, password : request.body.password }; 

So body-parser analyzed your incoming request, collected pieces containing your form data, and then created this body object for you and populated it with form data.

+21
Aug 19 '18 at 21:54
source share

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

This is mainly middleware for parsing JSON, plain text, or just returning a raw Buffer object that you need to deal with as you need.

+9
Jul 26 '17 at 17:31
source share

Thought it was an old question, but it might help someone, instead of using bodyParser middleware, we can use these utilities from express .

If you want to express.json() json at the endpoint, use express.json() middleware. If you want to use the json and urlencoded endpoint, use [express.json(), express.urlencoded()] for your middleware.

If you want users to upload files to the endpoint, you can use express.multipart() and don't forget to clear all created temporary files.

+7
Nov 29 '16 at 17:05
source share

To access messages, we must use body-parser . Basically, that body-parser , which allows the express to read the body, and then parse it into a Json object that we can understand.

+7
Jul 26 '17 at 12:23
source share

All this is a matter of convenience.

Basically, if the question is: "Do I need to use body-parser ?" The answer is no. ' We can come up with the same information from the client post request using a steeper route, which will usually be less flexible and increase the amount of code we need to write to get the same information.

This is similar to the question: "Do I need to use express to start?" Again, there is no answer, and again, indeed, it all comes down to saving us from the hassle of writing more code to do the basic things that are expressed with "built-in".

On the surface - body-parser simplifies access to information contained in client requests in different formats, instead of capturing raw data streams and figuring out what format the information is in, and even more so manually, this information into usable data.

+7
Nov 01 '17 at 9:31 on
source share

let me explain why we use a body parser?

Node.js. Body Parser

Parse incoming requests in middleware in front of your handlers, available under the req.body property.

Note. Since the req.body form is based on user-controlled input, all properties and values ​​in this object are untrusted and must be checked before trusting. For example, req.body.foo.toString () may fail in several ways, for example, the foo property may not be there or may not be a string, and toString may not be a function, but instead a string or another user.

Installation (localization in the project folder)

npm install body-parser OR npm body-parser

Now add this to your main or js file that you defined in package.json

 var express = require('express') var bodyParser = require('body-parser') var app = express() // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) 

For more clarification, go to https://www.npmjs.com/package/body-parser

0
Nov 30 '18 at 15:24
source share



All Articles