Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of argument in express.json({ extended: false})

I'm working with express to set up an API, and I came across this line of code: app.use(express.json( { extended: false } ));

I've seen the documentation from express, but I didn't find this method, is the documentation lacking or am I missing something?

like image 607
ElMassas Avatar asked Sep 02 '19 21:09

ElMassas


People also ask

What does app use Express Urlencoded ({ extended false do?

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.

What is Express Urlencoded ({ Extended true })?

urlencoded() is a built-in middleware in Express. js. The main objective of this method is to parse the incoming request with urlencoded payloads and is based upon the body-parser. This method returns the middleware that parses all the urlencoded bodies.

What is Express JSON () used for?

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.

What function arguments are available to express JS route handlers?

The arguments available to an Express. js route handler function are: req - the request object. res - the response object.


1 Answers

Answers come from looking at the actual Express and body-parser code...

If you go look at the Express code for the express.json() method here, you can see that it is a direct pass-through of the .json() method from the body-parser module.

 // from express.js
 exports.json = bodyParser.json;

So, if you then go look at the body-parser doc, there is nothing there for the extended option for the body-parser.json() middleware.

As you have discovered, the extended option is documented for the body-parser.urlencoded() middleware. But, since that is different than the .json() middleware method, it appears that this code is mistaken to be using the extended option with the .json() middleware.

If you go look at the code for the body-parser.json() middleware, you will find no references at all to the extended option in the code.

So, it appears to be an option that is mistakenly passed in the code you show and is subsequently ignored by the express/body-parser json middleware.

like image 173
jfriend00 Avatar answered Oct 11 '22 00:10

jfriend00