0
0
Expressframework~20 mins

File size limits in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.