0
0
Expressframework~20 mins

Multer middleware setup in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multer Mastery
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 this Multer setup in an Express app:

const multer = require('multer');
const upload = multer({
  limits: { fileSize: 1000 }
});

app.post('/upload', upload.single('file'), (req, res) => {
  res.send('File uploaded');
});

What will happen if a user uploads a file of 2000 bytes?

Express
const multer = require('multer');
const upload = multer({
  limits: { fileSize: 1000 }
});

app.post('/upload', upload.single('file'), (req, res) => {
  res.send('File uploaded');
});
AThe server crashes due to unhandled file size.
BThe server responds with 'File uploaded' and saves the file.
CThe file is partially saved up to 1000 bytes and then truncated.
DMulter throws a 'LIMIT_FILE_SIZE' error and the request is rejected.
Attempts:
2 left
💡 Hint

Think about how Multer handles file size limits and errors.

📝 Syntax
intermediate
2:00remaining
Which Multer setup correctly stores files in 'uploads/' folder?

Choose the correct Multer configuration to save uploaded files to the 'uploads/' directory with original filenames.

Aconst upload = multer({ storage: multer.diskStorage({ destination: 'uploads/', filename: (req, file, cb) => cb(null, file.originalname) }) });
Bconst upload = multer({ storage: multer.memoryStorage() });
Cconst upload = multer({ storage: multer.diskStorage({ destination: (req, file, cb) => cb(null, 'uploads/'), filename: (req, file, cb) => cb(null, file.originalname) }) });
Dconst upload = multer({ dest: 'uploads/' });
Attempts:
2 left
💡 Hint

Remember that destination can be a function or a string, but filename must be a function.

🔧 Debug
advanced
2:00remaining
Why does this Multer setup not save files to disk?

Review this Multer setup:

const multer = require('multer');
const storage = multer.diskStorage({
  destination: 'uploads/',
  filename: (req, file, cb) => {
    cb(null, file.originalname);
  }
});
const upload = multer({ storage });

Files are not saved to disk after upload. What is the cause?

Express
const multer = require('multer');
const storage = multer.diskStorage({
  destination: 'uploads/',
  filename: (req, file, cb) => {
    cb(null, file.originalname);
  }
});
const upload = multer({ storage });
AThe 'storage' object is not passed correctly to multer.
BThe 'filename' callback is missing the 'file' parameter.
CThe 'uploads/' folder does not exist, causing failure.
DThe 'destination' option must be a function, not a string.
Attempts:
2 left
💡 Hint

Multer requires the destination directory to exist beforehand.

state_output
advanced
2:00remaining
What is the value of req.file after upload?

Given this route:

const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('avatar'), (req, res) => {
  res.json(req.file);
});

A user uploads a file named 'photo.png'. What does req.file contain?

Express
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('avatar'), (req, res) => {
  res.json(req.file);
});
AA string with the file path 'uploads/photo.png'.
BAn object with fields like originalname: 'photo.png', filename: random string, path: 'uploads/filename', size, mimetype, etc.
CUndefined, because multer does not add req.file.
DAn array of files uploaded under 'avatar'.
Attempts:
2 left
💡 Hint

Check what multer.single adds to the request object.

🧠 Conceptual
expert
2:00remaining
Which Multer option prevents uploading files with wrong MIME types?

You want to accept only image files (jpeg, png) in your upload route. Which Multer option helps reject other file types before saving?

AUse the 'fileFilter' option with a function that checks file.mimetype and calls cb(null, false) for invalid types.
BSet 'limits.fileSize' to restrict file size to images only.
CUse 'storage' option with diskStorage and check file extension in 'filename' callback.
DUse 'dest' option to specify folder and rely on client-side validation.
Attempts:
2 left
💡 Hint

Think about how to reject files based on type before saving.