0
0
Expressframework~20 mins

Request size limits in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Size Limits Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a request exceeds the size limit in Express?

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?

Express
const express = require('express');
const app = express();
app.use(express.json({ limit: '1kb' }));
app.post('/data', (req, res) => {
  res.send('Received');
});
AThe server responds with status 413 Payload Too Large error.
BThe server truncates the request body to 1kb and processes it normally.
CThe server crashes with an unhandled exception.
DThe server ignores the size limit and processes the full request body.
Attempts:
2 left
💡 Hint

Think about HTTP status codes related to request size limits.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets a 500kb limit for JSON requests in Express?

Choose the correct way to set a JSON body size limit of 500 kilobytes using Express middleware.

Aapp.use(express.json({ sizeLimit: 500000 }));
Bapp.use(express.json({ limit: '500kb' }));
Capp.use(express.json({ maxSize: '500kb' }));
Dapp.use(express.json({ limit: 500 }));
Attempts:
2 left
💡 Hint

Check the official option name and format for size limits.

🔧 Debug
advanced
2:30remaining
Why does this Express app not enforce the request size limit?

Given the code below, why does the server accept requests larger than 2kb?

Express
const express = require('express');
const app = express();
app.post('/submit', (req, res) => {
  res.send('OK');
});
app.use(express.urlencoded({ limit: '2kb' }));
AThe limit option only applies to JSON, not URL-encoded data.
Bexpress.urlencoded does not support the limit option.
CThe middleware is applied after the route handler, so it has no effect.
DThe limit value should be a string with units like '2kb', not a number.
Attempts:
2 left
💡 Hint

Check the order of middleware and route definitions.

state_output
advanced
2:30remaining
What is the value of req.body after a too-large JSON 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?

Express
const express = require('express');
const app = express();
app.use(express.json({ limit: '1kb' }));
app.post('/test', (req, res) => {
  res.json({ body: req.body });
});
Anull, because the body parser returns null on error.
BAn empty object {}, because the body is cleared on error.
CThe full JSON object sent by the client, ignoring the limit.
Dundefined, because the middleware rejects the request before parsing.
Attempts:
2 left
💡 Hint

Consider what happens when the middleware rejects a request.

🧠 Conceptual
expert
3:00remaining
How to globally enforce request size limits for all content types in Express?

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?

AUse a custom middleware that checks <code>req.headers['content-length']</code> and rejects if over 10kb.
BUse <code>app.use(express.raw({ limit: '10kb' }))</code> only, which covers all types.
CUse <code>app.use(express.json({ limit: '10kb' }))</code> and <code>app.use(express.urlencoded({ limit: '10kb' }))</code> together.
DSet <code>app.use(express.text({ limit: '10kb' }))</code> only, which applies to all requests.
Attempts:
2 left
💡 Hint

Think about how Express parses different content types and what headers are available.