Consider an Express app using express.json({ limit: '1kb' }) middleware. What is the behavior when a client sends a JSON payload larger than 1 kilobyte?
const express = require('express'); const app = express(); app.use(express.json({ limit: '1kb' })); app.post('/data', (req, res) => { res.send('Received'); });
Think about HTTP status codes related to request size limits.
Express's body parser middleware rejects requests exceeding the specified limit by sending a 413 Payload Too Large response.
Choose the correct way to set a JSON body size limit of 500 kilobytes using Express middleware.
Check the official option name and format for size limits.
The correct option is limit with a string value specifying size units like 'kb'.
Given the code below, why does the server accept requests larger than 2kb?
const express = require('express'); const app = express(); app.post('/submit', (req, res) => { res.send('OK'); }); app.use(express.urlencoded({ limit: '2kb' }));
Check the order of middleware and route definitions.
Middleware and routes are executed in the order they are added to the app. Here, the route handler is added first and executes before the body parser middleware, so the size limit check does not occur before the route processes the request.
In an Express app with express.json({ limit: '1kb' }), if a client sends a JSON payload of 2kb, what will req.body be inside the route handler?
const express = require('express'); const app = express(); app.use(express.json({ limit: '1kb' })); app.post('/test', (req, res) => { res.json({ body: req.body }); });
Consider what happens when the middleware rejects a request.
If the request exceeds the size limit, the middleware sends an error response and does not populate req.body.
You want to limit the size of all incoming requests regardless of content type (JSON, URL-encoded, text, etc.) to 10kb. Which approach achieves this correctly?
Think about how Express parses different content types and what headers are available.
Express body parsers handle specific content types. To enforce a global size limit, checking the content-length header in a custom middleware is the reliable way.