ElMassas
ElMassas

Reputation: 407

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?

Upvotes: 10

Views: 7373

Answers (3)

jfriend00
jfriend00

Reputation: 707326

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.

Upvotes: 6

ElMassas
ElMassas

Reputation: 407

I have found the answer, although it is a weird one. Somehow this line of code works, even though it takes in an option from a different method.

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

This option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The “extended” syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded.

Upvotes: 4

Robert
Robert

Reputation: 20286

You are not. This seems clearly to be a mistake. As [options] you can pass the following: inflate, limit, reviver, strict, type, verify

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

Upvotes: 2

Related Questions