Express.js – express.json() function

Express JSServer Side ProgrammingProgramming

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

Syntax

express.json([options])

Parameters

Following are the different options available with this method

  • options

    • inflate – This enables or disables the handling of the deflated or compressed bodies. Default: true

    • limit – This controls the maximum size of the request body.

    • reviver – This option is passed to the JSON.parse method as the second argument.

    • strict – This enables or disables the accepting arrays or objects.

    • type – This determines the media type for the middleware that will be parsed.

Example 1

Create a file with the name "express.js" and copy the following code snippet. After creating the file, use the command "node express.js" to run this code.

// express.json() Demo Example

// Importing the express module
var express = require('express');

// Initializing the express and port number
var app = express();
var PORT = 3000;

// Calling the express.json() method for parsing
app.use(express.json());

// Reading content-type
app.post('/', function (req, res) {
   console.log(req.body.name)
   res.end();
})

// Listening to the port
app.listen(PORT, function(err){
   if (err) console.log(err);
   console.log("Server listening on PORT", PORT);
});

Before hitting the API endpoint, set the following two properties

  • Set the content-type as application/json in headers.

  • Pass the following body in the POST request – {"name": "TutorialsPoint"}

Output

C:\home\node>> node express.js
Server listening on PORT 3000
tutorialspoint

Example 2

Let’s take a look at one more example.

// express.json() Demo Example

// Importing the express module
var express = require('express');

// Initializing the express and port number
var app = express();
var PORT = 3000;

// Without using the json middleware

// app.use(express.json());

// Reading content-type
app.post('/', function (req, res) {
   console.log(req.body.name)
   res.end();
})

// Listening to the port
app.listen(PORT, function(err){
   if (err) console.log(err);
   console.log("Server listening on PORT", PORT);
});

Before hitting the API endpoint, set the following two properties

  • Set the content-type as application/json in headers.

  • Pass the following body in the POST request – {"name": "TutorialsPoint"}

Output

C:\home\node>> node express.js
Server listening on PORT 3000
TypeError: Cannot read property 'name' of undefined
at /home/node/test/express.js:15:23
at Layer.handle [as handle_request]
(/home/node/test/node_modules/express/lib/router/layer.js:95:5)
at next
(/home/node/test/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch
(/home/node/test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request]
(/home/node/test/node_modules/express/lib/router/layer.js:95:5)
raja
Published on 01-Oct-2021 06:32:10
Advertisements