Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between node's bodyParser and express's urlencoded middleware?

After reading about both i still can't wrap my head about it, could be language differences bu please help clarify.

express.urlencoded()

Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

And

and body-parser middleware

Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

i understand that express.urlencoded is based on Nodejs body-parser. and both pages,

https://expressjs.com/en/api.html#express.urlencoded

and

https://expressjs.com/en/resources/middleware/body-parser.html

even say the same warning note:

As req.body’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, req.body.foo.toString() may fail in multiple ways, for example foo may not be there or may not be a string, and toString may not be a function and instead a string or other user-input.

but eventually both give me a req.body with params sent in requests body object. so why should i use body-parser (which i have to install separatly) instead of always using express.urlencoded()

I know this isn't a code problem but i thank in advance for anyone who can list up to the main differences.

like image 653
fedesc Avatar asked Apr 15 '19 15:04

fedesc


People also ask

What is body-parser in Node JS?

Body-parser is the Node.js body parsing middleware. It is responsible for parsing the incoming request bodies in a middleware before you handle it. You can visit the link to Install body-parser module.

What is urlencoded body parser?

bodyParser.urlencoded([options]) Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

Is there a middleware like body parser available in express?

This middleware is available in Express v4.16.0 onwards. If you're using the latest version, there's almost no reason to. body-parser provides a few additional utilities like bodyParser.raw ( [options]) or bodyParser.text ( [options]) which almost nobody uses (never seen use one myself).

How to use urlencoded payloads in express?

The express.urlencoded () function is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body-parser. Return Value: It returns an Object. You can visit the link to Install express module.


1 Answers

so why should i use body-parser (which i have to install separatly) instead of always using express.urlencoded()

For the simple reason that it was not available in older version of express

This middleware is available in Express v4.16.0 onwards.

If you're using the latest version, there's almost no reason to.

body-parser provides a few additional utilities like bodyParser.raw([options]) or bodyParser.text([options]) which almost nobody uses (never seen use one myself).

like image 82
1565986223 Avatar answered Oct 20 '22 15:10

1565986223