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.
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.
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.
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).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With