How to Use express.json Middleware in Express.js
Use
express.json() as middleware in your Express app to automatically parse incoming JSON request bodies. Add it with app.use(express.json()) before your route handlers to access req.body as a JavaScript object.Syntax
The express.json() middleware is a built-in function in Express that parses incoming requests with JSON payloads. You add it to your app using app.use(express.json()). This tells Express to convert JSON data in the request body into a JavaScript object accessible via req.body.
app.use(): Adds middleware to the Express app.express.json(): Middleware that parses JSON request bodies.req.body: The parsed JSON object available in route handlers.
javascript
const express = require('express'); const app = express(); app.use(express.json()); app.post('/data', (req, res) => { console.log(req.body); // Access parsed JSON res.send('JSON received'); });
Example
This example shows a simple Express server that uses express.json() middleware to parse JSON sent in a POST request. The server logs the parsed object and responds with a confirmation message.
javascript
const express = require('express'); const app = express(); const port = 3000; // Use express.json middleware to parse JSON bodies app.use(express.json()); app.post('/submit', (req, res) => { // req.body contains the parsed JSON object console.log('Received JSON:', req.body); res.json({ message: 'JSON received successfully', data: req.body }); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Output
Server running on http://localhost:3000
Received JSON: { name: 'Alice', age: 30 }
Common Pitfalls
Common mistakes when using express.json() include:
- Not adding
express.json()middleware before routes that need to readreq.body, causingreq.bodyto beundefined. - Sending requests with incorrect
Content-Typeheaders (should beapplication/json), so the middleware does not parse the body. - Using older body parsing middleware like
body-parserwithout realizingexpress.json()is built-in since Express 4.16.
Example of wrong and right usage:
javascript
// Wrong: middleware added after route const express = require('express'); const app = express(); app.post('/test', (req, res) => { console.log(req.body); // undefined res.send('Check console'); }); app.use(express.json()); // Too late // Right: middleware before route const app2 = express(); app2.use(express.json()); app2.post('/test', (req, res) => { console.log(req.body); // Parsed JSON res.send('Check console'); });
Quick Reference
- Use:
app.use(express.json())to parse JSON bodies. - Place: Add middleware before routes that read
req.body. - Content-Type: Clients must send
Content-Type: application/jsonheader. - Access: Parsed data is in
req.body. - Error Handling: Invalid JSON causes a 400 error by default.
Key Takeaways
Add express.json() middleware with app.use() before routes to parse JSON request bodies.
Parsed JSON data is available in req.body inside route handlers.
Clients must send requests with Content-Type: application/json for parsing to work.
express.json() is built-in since Express 4.16; no need for extra body-parser package.
Invalid JSON in requests results in a 400 error automatically.