Request size limits help protect your server from very large requests that can slow it down or cause crashes.
Request size limits in Express
Start learning this pattern below
Jump into concepts and practice - no test required
app.use(express.json({ limit: 'size' }))
app.use(express.urlencoded({ limit: 'size', extended: true }))The limit option sets the maximum size of the request body.
You can use sizes like '100kb', '1mb', or just numbers for bytes.
app.use(express.json({ limit: '100kb' }))app.use(express.urlencoded({ limit: '1mb', extended: true }))app.use(express.json({ limit: 50000 }))This Express app limits incoming JSON data to 10 kilobytes. If a client sends more data, Express will reject it with an error.
When you send a POST request to /data with JSON data under 10kb, it responds with the keys received.
import express from 'express'; const app = express(); // Limit JSON body size to 10kb app.use(express.json({ limit: '10kb' })); app.post('/data', (req, res) => { res.send(`Received data with keys: ${Object.keys(req.body).join(', ')}`); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
If a request exceeds the limit, Express sends a 413 Payload Too Large error.
You can set different limits for JSON and URL-encoded data separately.
Always set limits to protect your server from overload and attacks.
Request size limits stop very large requests from slowing or crashing your server.
Use the limit option in express.json() and express.urlencoded() middleware.
Set limits based on what your app expects to receive to keep it safe and fast.
Practice
express.json()?Solution
Step 1: Understand the role of request size limits
Request size limits help protect the server from very large requests that can slow it down or cause crashes.Step 2: Identify the correct purpose in Express middleware
Thelimitoption inexpress.json()sets this size limit to keep the server safe and responsive.Final Answer:
To prevent very large requests from slowing down or crashing the server -> Option BQuick Check:
Request size limit = prevent server overload [OK]
- Thinking it speeds up requests by caching
- Confusing size limit with compression
- Assuming unlimited size is better
Solution
Step 1: Recall the correct option name for size limit
The correct option to set request size limit inexpress.json()islimit.Step 2: Check the correct syntax for setting 10kb
The value should be a string with units, like '10kb'. So{ limit: '10kb' }is correct.Final Answer:
app.use(express.json({ limit: '10kb' })) -> Option DQuick Check:
Uselimit: '10kb'option [OK]
- Using wrong option names like sizeLimit or maxSize
- Passing number without units
- Using assignment inside options object
app.use(express.json({ limit: '5kb' }));
app.post('/data', (req, res) => {
res.send('Received');
});What happens if a client sends a JSON body of 10kb to
/data?Solution
Step 1: Understand the limit setting effect
The limit is set to 5kb, so any request body larger than 5kb will be rejected.Step 2: Identify Express behavior on large requests
Express responds with a 413 Payload Too Large error when the body exceeds the limit.Final Answer:
The server rejects the request with a 413 Payload Too Large error -> Option AQuick Check:
Request > limit = 413 error [OK]
- Assuming server accepts large requests anyway
- Thinking server crashes instead of error response
- Believing server ignores body silently
app.use(express.json({ limit: 10000 }));What is the problem with this code regarding request size limits?
Solution
Step 1: Check the type of the limit option value
Thelimitoption accepts both numbers (in bytes) and strings with units like '10kb'.Step 2: Understand the effect of passing a number
Passing 10000 sets the limit to 10000 bytes (approximately 10kb), which works correctly.Final Answer:
There is no problem; this code sets a 10kb limit correctly -> Option AQuick Check:
limit: number = bytes [OK]
- Thinking it must be string with units only
- Thinking limit option is unsupported
- Assuming number means bytes automatically is wrong
Solution
Step 1: Set JSON request limit to 1mb correctly
Useexpress.json({ limit: '1mb' })to set JSON body limit to 1 megabyte.Step 2: Set URL-encoded form data limit to 100kb correctly
Useexpress.urlencoded({ limit: '100kb', extended: true })to set form data limit to 100 kilobytes.Step 3: Verify option names and values
Both use the correctlimitoption with string sizes and properextendedflag for URL-encoded.Final Answer:
app.use(express.json({ limit: '1mb' })); app.use(express.urlencoded({ limit: '100kb', extended: true })); -> Option CQuick Check:
Use limit strings per middleware type [OK]
- Swapping limits between JSON and URL-encoded
- Using numbers instead of strings for limits
- Using wrong option names like maxSize
