What do Body Parser and cookie parser nodes do? And should they be used?

I read all the documentation I can find, but I cannot find a simple explanation of what these two middlewares do.

What about body in body-parser ? Why do you need to analyze the body?

Similarly for cookies. Is it right that cookie-parser parses or deletes cookies that accompany a web user?

Finally, I read that body-parser is unsafe and deprecated in Express4. Should I not use it?

+5
source share
2 answers

As you know, Node.js provides a very low level HTTP module by default. This is why you need β€œframeworks” like Express, etc. - they make it easy to handle common web server functions on other platforms (like Java and PHP).

body-parser will take the body of your request and analyze it for everything you want to receive on the POST / PUT server (JSON, URL, text, raw).
The only problem with body-parser (as far as I know) is that you cannot handle multi-part bodies (which usually load).

cookie-parser will parse the Cookie header and handle the separation and encoding of the cookie descriptor, perhaps even decrypt it!

It all comes down to the fact that you do not need to use these functions, and why Node is excellent.
You can simply ignore them and take care of the server :)

+6
source

In Express 4, body-parser and cookie-parser were moved to separate modules. The body and cookie analyzers that were outdated were the ones that came with Express 3.

The body analyzer analyzes the request bodies. They may contain data encoded by json or url. Then the form data will be displayed in req.body .

A cookie parser analyzes cookies and places cookie information in a req object in the middleware. It will also decrypt signed cookies if you know the secret.

+3
source

Source: https://habr.com/ru/post/1204888/


All Articles