When a user sends data to your server, you need to read it. Parsing the request body means turning that data into something your code can use easily.
Parsing request body (JSON and form data) in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
import express from 'express'; const app = express(); // To parse JSON data app.use(express.json()); // To parse URL-encoded form data app.use(express.urlencoded({ extended: true })); app.post('/submit', (req, res) => { const data = req.body; res.send(data); });
express.json() reads JSON data sent in the request body.
express.urlencoded() reads form data sent as URL-encoded (like from HTML forms).
app.use(express.json());
application/x-www-form-urlencoded.app.use(express.urlencoded({ extended: true }));req.body and sends a response.app.post('/data', (req, res) => { console.log(req.body); res.send('Got your data!'); });
This simple Express server listens on port 3000. It can accept POST requests with JSON or form data at the /submit route. It sends back the data it received as JSON.
import express from 'express'; const app = express(); const port = 3000; // Middleware to parse JSON app.use(express.json()); // Middleware to parse form data app.use(express.urlencoded({ extended: true })); app.post('/submit', (req, res) => { // req.body contains parsed data res.json({ receivedData: req.body }); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Always use these middleware before your routes to ensure req.body is populated.
For JSON, the client must send the header Content-Type: application/json.
For form data, the client sends Content-Type: application/x-www-form-urlencoded.
Parsing request body lets your server understand data sent by users or apps.
Use express.json() for JSON data and express.urlencoded() for form data.
Access the parsed data in your route handlers via req.body.
Practice
express.json() middleware do in a Node.js Express app?Solution
Step 1: Understand express.json() purpose
This middleware parses JSON data sent in the request body, converting it into a JavaScript object.Step 2: Know where parsed data is stored
The parsed object is assigned toreq.bodyso route handlers can access it easily.Final Answer:
It parses incoming JSON request bodies and makes the data available in req.body. -> Option DQuick Check:
express.json() parses JSON body = C [OK]
- Confusing express.json() with parsing URL query parameters
- Thinking it serves static files
- Assuming it encrypts data
Solution
Step 1: Identify middleware for form data
express.urlencoded() parses URL-encoded form data sent by HTML forms.Step 2: Check correct usage
Using{ extended: true }allows rich objects and arrays to be encoded.Final Answer:
app.use(express.urlencoded({ extended: true })) -> Option AQuick Check:
express.urlencoded() parses form data = A [OK]
- Using express.json() for form data
- Confusing static file serving with parsing
- Using bodyParser.raw() instead of urlencoded
console.log(req.body) output if the client sends JSON {"name":"Alice"}?
app.use(express.json());
app.post('/user', (req, res) => {
console.log(req.body);
res.send('Received');
});Solution
Step 1: Middleware parses JSON body
express.json() converts JSON string into a JavaScript object and assigns it to req.body.Step 2: Logging req.body shows parsed object
console.log prints the object { name: 'Alice' }.Final Answer:
{ name: 'Alice' } -> Option AQuick Check:
express.json() parses JSON to object = B [OK]
- Expecting req.body to be a string
- Not using express.json() middleware
- Assuming req.body is undefined
req.body remain undefined in this Express app?
const express = require('express');
const app = express();
app.post('/submit', (req, res) => {
console.log(req.body);
res.send('Done');
});
app.listen(3000);Solution
Step 1: Check middleware usage
The app does not use express.json() or express.urlencoded(), so request bodies are not parsed.Step 2: Understand req.body behavior
Without parsing middleware, req.body is undefined because Express does not parse request bodies by default.Final Answer:
Because the app is missing middleware to parse the request body. -> Option CQuick Check:
Missing body parser middleware = D [OK]
- Assuming req.body is always available
- Blaming route path or console.log
- Ignoring middleware setup
req.body works for all POST requests?Solution
Step 1: Identify middleware for both data types
express.json() parses JSON bodies; express.urlencoded({ extended: true }) parses form data with rich objects.Step 2: Confirm correct order and options
Both middlewares should be used; extended: true allows nested objects in form data.Final Answer:
app.use(express.json()); app.use(express.urlencoded({ extended: true })); -> Option BQuick Check:
Use both express.json() and express.urlencoded({ extended: true }) = A [OK]
- Using extended: false limits form data parsing
- Using deprecated bodyParser package
- Not including both middlewares
