87
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

1

7 Answers 7

74

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.

0
40

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).

2
  • 15
    None of the answers in this post were satisfactory. Please refer to this post if you want a proper answer - stackoverflow.com/questions/29175465/…
    – AwsAnurag
    Commented Jan 1, 2021 at 15:19
  • Does this mean that numbers in string format will be parsed ? What about nulls in string fromat, will they be parsed too ? Commented Jul 21, 2021 at 4:00
26

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.

12

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.

0
11

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

1
  • 3
    Thank your breaking that down in a human readable way
    – klewis
    Commented Mar 16, 2021 at 13:18
-2

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}); 
-3

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 syntax in -extended:true- allows the use of other
 characteristics like rich objects & code arrangement inside the
 URL-encoded format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.