Bird
Raised Fist0
Expressframework~20 mins

Request size limits in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of setting a request size limit in Express middleware like express.json()?
easy
A. To increase the speed of the server by caching requests
B. To prevent very large requests from slowing down or crashing the server
C. To automatically compress large requests for faster processing
D. To allow unlimited request sizes for flexibility

Solution

  1. 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.
  2. Step 2: Identify the correct purpose in Express middleware

    The limit option in express.json() sets this size limit to keep the server safe and responsive.
  3. Final Answer:

    To prevent very large requests from slowing down or crashing the server -> Option B
  4. Quick Check:

    Request size limit = prevent server overload [OK]
Hint: Request size limits protect server from overload [OK]
Common Mistakes:
  • Thinking it speeds up requests by caching
  • Confusing size limit with compression
  • Assuming unlimited size is better
2. Which of the following is the correct way to set a 10kb limit on JSON request bodies in Express?
easy
A. app.use(express.json({ sizeLimit: 10 }))
B. app.use(express.json(limit = 10))
C. app.use(express.json({ maxSize: '10kb' }))
D. app.use(express.json({ limit: '10kb' }))

Solution

  1. Step 1: Recall the correct option name for size limit

    The correct option to set request size limit in express.json() is limit.
  2. Step 2: Check the correct syntax for setting 10kb

    The value should be a string with units, like '10kb'. So { limit: '10kb' } is correct.
  3. Final Answer:

    app.use(express.json({ limit: '10kb' })) -> Option D
  4. Quick Check:

    Use limit: '10kb' option [OK]
Hint: Use limit option with string size like '10kb' [OK]
Common Mistakes:
  • Using wrong option names like sizeLimit or maxSize
  • Passing number without units
  • Using assignment inside options object
3. Given this Express setup:
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?
medium
A. The server rejects the request with a 413 Payload Too Large error
B. The server ignores the body and responds with an empty object
C. The server crashes due to memory overflow
D. The server accepts the request and responds with 'Received'

Solution

  1. Step 1: Understand the limit setting effect

    The limit is set to 5kb, so any request body larger than 5kb will be rejected.
  2. Step 2: Identify Express behavior on large requests

    Express responds with a 413 Payload Too Large error when the body exceeds the limit.
  3. Final Answer:

    The server rejects the request with a 413 Payload Too Large error -> Option A
  4. Quick Check:

    Request > limit = 413 error [OK]
Hint: Requests over limit get 413 error response [OK]
Common Mistakes:
  • Assuming server accepts large requests anyway
  • Thinking server crashes instead of error response
  • Believing server ignores body silently
4. Consider this Express code snippet:
app.use(express.json({ limit: 10000 }));

What is the problem with this code regarding request size limits?
medium
A. There is no problem; this code sets a 10kb limit correctly
B. The limit option is not supported by express.json()
C. The limit value is too small and will reject all requests
D. The limit value should be a string with units like '10kb', not a number

Solution

  1. Step 1: Check the type of the limit option value

    The limit option accepts both numbers (in bytes) and strings with units like '10kb'.
  2. Step 2: Understand the effect of passing a number

    Passing 10000 sets the limit to 10000 bytes (approximately 10kb), which works correctly.
  3. Final Answer:

    There is no problem; this code sets a 10kb limit correctly -> Option A
  4. Quick Check:

    limit: number = bytes [OK]
Hint: Limit accepts number (bytes) or string with units [OK]
Common Mistakes:
  • Thinking it must be string with units only
  • Thinking limit option is unsupported
  • Assuming number means bytes automatically is wrong
5. You want to accept JSON requests up to 1mb but URL-encoded form data only up to 100kb in your Express app. Which setup correctly applies these limits?
hard
A. app.use(express.json({ limit: 100 * 1024 }));\napp.use(express.urlencoded({ limit: 1 * 1024 * 1024, extended: false }));
B. app.use(express.json({ maxSize: '1mb' }));\napp.use(express.urlencoded({ maxSize: '100kb', extended: true }));
C. app.use(express.json({ limit: '1mb' }));\napp.use(express.urlencoded({ limit: '100kb', extended: true }));
D. app.use(express.json({ limit: '100kb' }));\napp.use(express.urlencoded({ limit: '1mb', extended: true }));

Solution

  1. Step 1: Set JSON request limit to 1mb correctly

    Use express.json({ limit: '1mb' }) to set JSON body limit to 1 megabyte.
  2. Step 2: Set URL-encoded form data limit to 100kb correctly

    Use express.urlencoded({ limit: '100kb', extended: true }) to set form data limit to 100 kilobytes.
  3. Step 3: Verify option names and values

    Both use the correct limit option with string sizes and proper extended flag for URL-encoded.
  4. Final Answer:

    app.use(express.json({ limit: '1mb' })); app.use(express.urlencoded({ limit: '100kb', extended: true })); -> Option C
  5. Quick Check:

    Use limit strings per middleware type [OK]
Hint: Set limits separately with correct units and options [OK]
Common Mistakes:
  • Swapping limits between JSON and URL-encoded
  • Using numbers instead of strings for limits
  • Using wrong option names like maxSize