Bird
Raised Fist0
Expressframework~20 mins

File 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
🎖️
File 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 file larger than the limit is uploaded?
Consider an Express app using the express.json() middleware with a file size limit set to 1MB. What will happen if a client uploads a JSON payload of 2MB?
Express
const express = require('express');
const app = express();
app.use(express.json({ limit: '1mb' }));
app.post('/upload', (req, res) => {
  res.send('Upload successful');
});
AThe server accepts the upload and processes the full 2MB payload without error.
BThe server truncates the payload to 1MB and processes only that part.
CThe server rejects the request and responds with a 413 Payload Too Large error.
DThe server crashes due to memory overflow.
Attempts:
2 left
💡 Hint
Think about how Express middleware handles payloads exceeding the set limit.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a 5MB file size limit for JSON payloads?
Choose the correct way to configure Express's JSON parser to accept payloads up to 5 megabytes.
Aapp.use(express.json({ limit: 5 }));
Bapp.use(express.json({ maxSize: 5 * 1024 * 1024 }));
Capp.use(express.json({ sizeLimit: '5MB' }));
Dapp.use(express.json({ limit: '5mb' }));
Attempts:
2 left
💡 Hint
Check the official option name and format for size limits in Express.
🔧 Debug
advanced
2:00remaining
Why does this Express app not enforce the file size limit?
Given the code below, why does the server accept payloads larger than 1MB without error?
Express
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post('/data', (req, res) => {
  res.send('Received');
});
AThe server needs a separate middleware to enforce size limits.
BThe file size limit is missing from express.json(), so the default unlimited size is used.
CThe limit option must be set on both express.urlencoded() and express.json() to work.
Dexpress.urlencoded() overrides express.json() and disables size limits.
Attempts:
2 left
💡 Hint
Look at the middleware configuration for express.json().
🧠 Conceptual
advanced
2:00remaining
How does Express handle file uploads exceeding the limit with multer?
When using multer middleware to handle file uploads with a size limit set, what happens if a user uploads a file larger than the limit?
AMulter throws a MulterError with code 'LIMIT_FILE_SIZE' and does not save the file.
BMulter ignores the limit and saves the full file.
CMulter automatically truncates the file to the limit and saves it.
DMulter crashes the server with an unhandled exception.
Attempts:
2 left
💡 Hint
Think about how multer signals errors for file size violations.
state_output
expert
2:00remaining
What is the value of req.file after a too-large file upload with multer?
In an Express route using multer with a 1MB file size limit, a user uploads a 2MB file. What will be the value of req.file inside the route handler?
Express
const multer = require('multer');
const upload = multer({ limits: { fileSize: 1 * 1024 * 1024 } });
app.post('/upload', upload.single('file'), (req, res) => {
  res.json({ file: req.file });
});
Aundefined, because multer rejects the file and does not set req.file.
BAn object containing file details for the uploaded file.
Cnull, indicating no file was uploaded.
DAn empty object {}.
Attempts:
2 left
💡 Hint
Consider multer's behavior when a file exceeds the size limit.

Practice

(1/5)
1. What is the main purpose of setting a file size limit in an Express app using body parsers?
easy
A. To change the file type of uploads
B. To allow unlimited file uploads without restrictions
C. To automatically compress uploaded files
D. To prevent users from uploading files that are too large and slow down the server

Solution

  1. Step 1: Understand file size limits in Express

    File size limits stop very large files from being uploaded, protecting server resources.
  2. Step 2: Identify the main reason for limits

    Limits keep the app safe and fast by preventing overload from big files.
  3. Final Answer:

    To prevent users from uploading files that are too large and slow down the server -> Option D
  4. Quick Check:

    File size limits = prevent large uploads [OK]
Hint: File size limits protect server from big uploads [OK]
Common Mistakes:
  • Thinking limits compress files automatically
  • Believing limits allow unlimited uploads
  • Confusing file type changes with size limits
2. Which of the following is the correct way to set a 1MB file size limit using Express's built-in JSON body parser?
easy
A. app.use(express.json({ sizeLimit: 1 }))
B. app.use(express.json({ limit: '1mb' }))
C. app.use(express.json({ maxFileSize: '1MB' }))
D. app.use(express.json({ limit: 1000 }))

Solution

  1. Step 1: Recall Express JSON parser options

    The correct option to set size limit is 'limit' with a string like '1mb'.
  2. Step 2: Check each option's syntax

    Only app.use(express.json({ limit: '1mb' })) uses 'limit' with correct string format '1mb'. Others use wrong keys or units.
  3. Final Answer:

    app.use(express.json({ limit: '1mb' })) -> Option B
  4. Quick Check:

    Use 'limit' with string size in Express [OK]
Hint: Use 'limit' option with string size like '1mb' [OK]
Common Mistakes:
  • Using wrong option names like sizeLimit or maxFileSize
  • Passing number without units as limit
  • Using uppercase units incorrectly
3. Given this Express code snippet, what happens if a client uploads a JSON body larger than 500kb?
app.use(express.json({ limit: '500kb' }));
app.post('/upload', (req, res) => {
  res.send('Upload successful');
});
medium
A. The server throws an error and does not call the route handler
B. The server ignores the size limit and processes the full upload
C. The server truncates the JSON to 500kb and processes it
D. The server accepts the upload and responds with 'Upload successful'

Solution

  1. Step 1: Understand Express JSON parser behavior with limits

    If the JSON body exceeds the limit, Express throws a 'PayloadTooLargeError' and skips the route handler.
  2. Step 2: Analyze the code flow

    The route handler sends 'Upload successful' only if parsing succeeds, which won't happen if size is too big.
  3. Final Answer:

    The server throws an error and does not call the route handler -> Option A
  4. Quick Check:

    Exceeding limit causes error, no handler call [OK]
Hint: Uploads over limit cause error, no success response [OK]
Common Mistakes:
  • Assuming server truncates large JSON
  • Thinking handler runs despite error
  • Believing limit is ignored
4. Identify the error in this Express setup that tries to limit JSON body size to 2MB:
app.use(express.json({ limit: 2 * 1024 * 1024 }));
medium
A. The limit value should be a string like '2mb', not a number
B. The limit option is not supported by express.json()
C. The multiplication is incorrect; it should be 2 * 1000 * 1000
D. The limit should be set in bytes as a Buffer, not a number

Solution

  1. Step 1: Check express.json() limit option type

    The 'limit' option expects a string with units like '2mb', not a raw number.
  2. Step 2: Analyze the given code

    Passing a number causes Express to ignore or misinterpret the limit, so it should be '2mb'.
  3. Final Answer:

    The limit value should be a string like '2mb', not a number -> Option A
  4. Quick Check:

    Limit must be string with units, not number [OK]
Hint: Use string with units for limit, not number [OK]
Common Mistakes:
  • Passing number instead of string for limit
  • Misunderstanding unit conversion
  • Thinking limit option is unsupported
5. You want to limit file uploads to 3MB using the multer middleware in Express. Which code snippet correctly sets this limit and handles errors to inform users when files are too large?
hard
A. const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', upload.single('file'), (req, res) => { res.send('File uploaded'); });
B. const upload = multer({ limit: '3mb' }); app.post('/upload', upload.single('file'), (req, res) => { res.send('File uploaded'); });
C. const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', (req, res) => { upload.single('file')(req, res, err => { if (err) return res.status(400).send('File too large'); res.send('File uploaded'); }); });
D. const upload = multer({ fileSizeLimit: 3 * 1024 * 1024 }); app.post('/upload', upload.single('file'), (req, res) => { res.send('File uploaded'); });

Solution

  1. Step 1: Set file size limit correctly in multer

    The correct option is 'limits: { fileSize: size_in_bytes }'. const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', (req, res) => { upload.single('file')(req, res, err => { if (err) return res.status(400).send('File too large'); res.send('File uploaded'); }); }); and A use this correctly.
  2. Step 2: Handle errors to inform users

    const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', (req, res) => { upload.single('file')(req, res, err => { if (err) return res.status(400).send('File too large'); res.send('File uploaded'); }); }); wraps upload.single in route handler with error callback to catch file size errors and respond properly.
  3. Step 3: Compare options

    const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', upload.single('file'), (req, res) => { res.send('File uploaded'); }); lacks error handling, B uses wrong option 'limit', D uses invalid 'fileSizeLimit'.
  4. Final Answer:

    Option C correctly sets limit and handles errors to inform users -> Option C
  5. Quick Check:

    Multer limits + error callback = const upload = multer({ limits: { fileSize: 3 * 1024 * 1024 } }); app.post('/upload', (req, res) => { upload.single('file')(req, res, err => { if (err) return res.status(400).send('File too large'); res.send('File uploaded'); }); }); [OK]
Hint: Use limits.fileSize and error callback to handle large files [OK]
Common Mistakes:
  • Using wrong option names like limit or fileSizeLimit
  • Not handling errors to inform users
  • Assuming multer auto-handles file size errors