What is the meaning of "bodyParser.urlencoded({ extended: true }))" and "bodyParser.json()" in Express.js?

node.js

node.js Problem Overview


const bp = require("body-parser");
const express = require("express");
const app = express();

app.use(bp.json());
app.use(bp.urlencoded({ extended: true }));

I need to know what they do. I couldn't find any detailed information. Can you help me? And what is the difference between extended:true and extended:false

node.js Solutions


Solution 1 - node.js

body-parser is an NPM package that parses incoming request bodies in a middleware before your handlers, available under the req.body property.

app.use(bp.json()) looks at requests where the Content-Type: application/json header is present and transforms the text-based JSON input into JS-accessible variables under req.body. app.use(bp.urlencoded({extended: true}) does the same for URL-encoded requests. the extended: true precises that the req.body object will contain values of any type instead of just strings.

Solution 2 - node.js

Full documentation of body-parser library can be found here.

bp.json() - middleware for parsing json objects - options can be found here. Source code can be found here.

> Returns middleware that only parses JSON and only looks at requests > where the Content-Type header matches the type option. This parser > accepts any Unicode encoding of the body and supports automatic > inflation of gzip and deflate encodings.

bp.urlencoded({ extended: true }) - middleware for parsing bodies from URL. Options can be found here. Source code can be found here.

> 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. > > A new body object containing the parsed data is populated on the > request object after the middleware (i.e. req.body). This object will > contain key-value pairs, where the value can be a string or array > (when extended is false), or any type (when extended is true).

Solution 3 - node.js

In Express >= 4.16, body parser has been re-added under the methods express.json(). The replacement for the above question is

const express = require("express");

const app = express();

app.use(express.json());

app.use(express.urlencoded({ extended: true }));

The extended 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. For more information, please see the qs library here.

Defaults to true, but using the default has been deprecated. Please research into the difference between qs and querystring and choose the appropriate setting.

Solution 4 - node.js

It helps you to create object from the form input

  <input type="text" class="form-control" placeholder='Text' name="comment[text]" value="<%=comment.text%>">

those two line will help you produce a object directly without the hassle to set variables and create your own object.if you set extended property to false it will not produce the object and will return undefined.try it for yourself you will know

Solution 5 - node.js

You don't need to use require("body-parser") in express. Because express from v4 has body-parser implemented. And you can use:

app.use(express.json())
app.use(express.urlencoded({extended: true}))

It is the same.

Solution 6 - node.js

from: https://apuntes.de/nodejs-desarrollo-web/body-parser/#gsc.tab=0

> Extended false utiliza la librería querystring mientras que true la librería qs. La sintaxis de extended:true permite el uso de otras características como rich objects y arreglos codificados dentro del formato URL-encoded.

which means (roughly translated)

> extended false utilizes the library 'querystring' while true uses > 'qs'. The sintax in -extended:true- allows the use of other > characteristics like rich objets & code arrengenment inside the > URL-encoded format

Solution 7 - node.js

bodyparser has modes this below one parse data comes from HTML form

app.use(bp.urlencoded({ extended: true }));

and also can parse data like text and json .

app.use(bp.text({extend:true});
app.use(bp.json({extend:true}); 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondoğukanView Question on Stackoverflow
Solution 1 - node.jsNino FiliuView Answer on Stackoverflow
Solution 2 - node.jsIvan VasiljevicView Answer on Stackoverflow
Solution 3 - node.jsmintukrishnan sView Answer on Stackoverflow
Solution 4 - node.jsAjahi himalilView Answer on Stackoverflow
Solution 5 - node.jsuser6742636View Answer on Stackoverflow
Solution 6 - node.jsconector_chView Answer on Stackoverflow
Solution 7 - node.jsMahmoud AhmedView Answer on Stackoverflow