Consider this Express route that handles POST requests with JSON body parsing enabled.
const express = require('express');
const app = express();
app.use(express.json());
app.post('/data', (req, res) => {
res.json({ received: req.body });
});If a client sends a POST request to /data with JSON body {"name": "Alice"}, what will the server respond with?
const express = require('express'); const app = express(); app.use(express.json()); app.post('/data', (req, res) => { res.json({ received: req.body }); });
Remember that express.json() middleware parses JSON body into an object.
The express.json() middleware parses the JSON body and sets req.body as an object. The response sends back this object inside received. So the output is a JSON object with received key holding the parsed object.
You want to parse URL-encoded form data sent in a POST request body in Express. Which middleware setup is correct?
const express = require('express'); const app = express(); // Which middleware to use here? app.post('/submit', (req, res) => { res.send(req.body); });
URL-encoded form data is not JSON. Use the middleware that parses URL-encoded bodies.
express.urlencoded({ extended: true }) parses URL-encoded bodies like form submissions. express.json() parses JSON bodies. bodyParser.json() is deprecated in modern Express. express.text() parses plain text bodies.
Look at this Express app code:
const express = require('express');
const app = express();
app.post('/api', (req, res) => {
res.json(req.body);
});
app.use(express.json());When sending a JSON POST request to /api, req.body is undefined. Why?
const express = require('express'); const app = express(); app.post('/api', (req, res) => { res.json(req.body); }); app.use(express.json());
Middleware order matters in Express.
Middleware like express.json() must be used before routes that need to access req.body. Here, the middleware is added after the route, so the body is not parsed when the route runs.
Given this Express setup:
const express = require('express');
const app = express();
app.use(express.text());
app.post('/text', (req, res) => {
res.send(req.body);
});If a client sends a POST request to /text with body Hello World (plain text), what will req.body contain?
const express = require('express'); const app = express(); app.use(express.text()); app.post('/text', (req, res) => { res.send(req.body); });
express.text() parses plain text bodies as strings.
The express.text() middleware parses the request body as a string and assigns it to req.body. So req.body will be the string Hello World.
You want your Express app to accept POST requests with either JSON or URL-encoded form data bodies. Which middleware setup correctly supports both?
const express = require('express'); const app = express(); // Middleware setup here app.post('/submit', (req, res) => { res.json(req.body); });
Order of middleware matters, but both JSON and URL-encoded parsers must be included.
To handle both JSON and URL-encoded bodies, include both express.json() and express.urlencoded() middleware. The order usually does not matter, but the common pattern is to add JSON parser first. Options A and D use text or raw parsers which do not parse JSON or URL-encoded data properly.