Bird
Raised Fist0
Expressframework~20 mins

Multer middleware setup 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
🎖️
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.

Practice

(1/5)
1. What is the primary purpose of the multer middleware in an Express application?
easy
A. To serve static files like images and CSS
B. To parse JSON data in request bodies
C. To handle file uploads from client requests
D. To manage user sessions and cookies

Solution

  1. Step 1: Understand middleware role

    Multer is middleware designed specifically to handle multipart/form-data, which is used for uploading files.
  2. Step 2: Compare with other middleware

    Other middleware like body-parser handle JSON, and express.static serves static files, but multer focuses on file uploads.
  3. Final Answer:

    To handle file uploads from client requests -> Option C
  4. Quick Check:

    Multer = file upload handler [OK]
Hint: Multer is for file uploads, not JSON or sessions [OK]
Common Mistakes:
  • Confusing multer with body-parser for JSON parsing
  • Thinking multer manages sessions or cookies
  • Assuming multer serves static files
2. Which of the following is the correct way to set up multer to accept a single file upload with the field name 'avatar'?
easy
A. const upload = multer().single('avatar');
B. const upload = multer.single('avatar');
C. const upload = multer().array('avatar');
D. const upload = multer().multi('avatar');

Solution

  1. Step 1: Correct multer initialization

    Multer must be called as a function to create an instance: multer().
  2. Step 2: Use .single() for single file

    To accept one file, use upload.single('fieldname'). Here, 'avatar' is the field name.
  3. Final Answer:

    const upload = multer().single('avatar'); -> Option A
  4. Quick Check:

    multer() + .single('avatar') = correct setup [OK]
Hint: Always call multer() before .single() or .array() [OK]
Common Mistakes:
  • Calling multer.single() without parentheses
  • Using .array() instead of .single() for one file
  • Using non-existent .multi() method
3. Given this Express route setup with multer:
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/profile', upload.single('photo'), (req, res) => {
  res.send(req.file.path);
});
What will be the response when a user uploads a file named 'pic.jpg' in the 'photo' field?
medium
A. Undefined, because req.file is not set
B. The original filename 'pic.jpg'
C. An error because 'dest' is not a valid option
D. The path where the file is saved, like 'uploads/abc123'

Solution

  1. Step 1: Understand multer storage with 'dest'

    Setting 'dest' tells multer to save files to that folder with a generated filename, not the original name.
  2. Step 2: Check what req.file.path contains

    req.file.path contains the full path to the saved file, e.g., 'uploads/abc123'.
  3. Final Answer:

    The path where the file is saved, like 'uploads/abc123' -> Option D
  4. Quick Check:

    dest option saves file with generated name = path returned [OK]
Hint: dest saves file with random name; req.file.path shows saved path [OK]
Common Mistakes:
  • Expecting original filename in req.file.path
  • Thinking 'dest' is invalid option
  • Assuming req.file is undefined without upload
4. Consider this code snippet:
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.files);
  res.send('Upload complete');
});
What is the problem with this code?
medium
A. Incorrect destination folder name
B. Using req.files instead of req.file for single file upload
C. Missing call to multer() function
D. upload.single() should be upload.array() for single file

Solution

  1. Step 1: Check multer method used

    upload.single('file') handles one file and stores it in req.file (singular).
  2. Step 2: Identify incorrect property usage

    The code logs req.files (plural), which is undefined for single file uploads.
  3. Final Answer:

    Using req.files instead of req.file for single file upload -> Option B
  4. Quick Check:

    single() sets req.file, not req.files [OK]
Hint: Use req.file for single, req.files for multiple uploads [OK]
Common Mistakes:
  • Confusing req.file and req.files
  • Not calling multer() before .single()
  • Using upload.array() when single file expected
5. You want to customize multer to store uploaded files in a folder named 'images' and rename each file to include the current timestamp before the original filename. Which setup correctly achieves this?
hard
A. const storage = multer.diskStorage({ destination: 'images', filename: (req, file, cb) => { cb(null, Date.now() + '-' + file.originalname); } }); const upload = multer({ storage });
B. const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, images); }, filename: (req, file, cb) => { cb(null, Date.now() + '-' + file.originalname); } }); const upload = multer({ storage });
C. const storage = multer.diskStorage({ destination: 'images', filename: (req, file, cb) => { cb(null, file.originalname); } }); const upload = multer({ storage });
D. const upload = multer({ dest: 'images', filename: (req, file, cb) => { cb(null, Date.now() + '-' + file.originalname); } });

Solution

  1. Step 1: Understand diskStorage options

    diskStorage accepts destination as string or function(req, file, cb); filename must be function(req, file, cb).
  2. Step 2: Verify timestamp renaming

    Filename function must call cb(null, Date.now() + '-' + file.originalname).
  3. Step 3: Identify correct setup

    const storage = multer.diskStorage({ destination: 'images', filename: (req, file, cb) => { cb(null, Date.now() + '-' + file.originalname); } }); const upload = multer({ storage }); uses valid string destination and correct filename.
  4. Final Answer:

    const storage = multer.diskStorage({ destination: 'images', filename: (req, file, cb) => { cb(null, Date.now() + '-' + file.originalname); } }); const upload = multer({ storage }); -> Option A
  5. Quick Check:

    destination string + filename(Date.now()) [OK]
Hint: destination: string or fn(); filename: fn() always [OK]
Common Mistakes:
  • Using unquoted path in destination function (images vs 'images')
  • Passing filename option directly to multer instead of diskStorage
  • Not using callback cb in destination or filename