What do nodes Body Parser and cookie parser do? And should I use them?

Startec picture Startec · Oct 17, 2014 · Viewed 7.2k times · Source

I have read all the documentation I can find, but I can not find a simple explanation of what these two middleware do.

What does the body in body-parser refer to? Why does the body need to be parsed?

Similarly for cookies. Am I correct that cookie-parser "parses" or beaks down the cookies that accompany a web user?

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

Answer

gustavohenke picture gustavohenke · Oct 17, 2014

As you may know, Node.js provides by default a very low level HTTP module. That's why you need "frameworks" like Express and such - they let you easily handle common features of web servers in other platforms (like Java and PHP, for example).

body-parser will take the body of your request and parse it to whatever you want your server to receive in POST/PUT requests (JSON, URL encoded, text, raw).
The only problem with body-parser (the far I know) is that you can't handle multipart bodies (which are commonly uploads).

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

This all comes down to the fact that you don't need to use these features, and that's why Node is great.
You can simply ignore them and have your server less busy :)