0
0
Expressframework~20 mins

Multiple file uploads in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when uploading multiple files with this Express route?

Consider this Express route using multer middleware to handle multiple file uploads:

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

app.post('/upload', upload.array('photos', 3), (req, res) => {
  res.json({ count: req.files.length });
});

If a client uploads 4 files named 'photos', what will be the JSON response?

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

app.post('/upload', upload.array('photos', 3), (req, res) => {
  res.json({ count: req.files.length });
});
A{"count":3}
B{"count":4}
CError: Too many files uploaded
D{"count":0}
Attempts:
2 left
💡 Hint

Look at the second argument to upload.array. It limits the number of files; exceeding it causes a LIMIT_FILE_COUNT error.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets up multer to accept multiple files with different field names?

You want to accept multiple files uploaded under two different fields: 'avatar' (1 file) and 'gallery' (up to 5 files). Which code snippet correctly configures multer middleware for this?

Aupload.array('avatar', 1, 'gallery', 5)
Bupload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 5 }])
Cupload.single('avatar', 1).array('gallery', 5)
Dupload.multiple(['avatar', 'gallery'], [1, 5])
Attempts:
2 left
💡 Hint

Check multer's documentation for handling multiple fields with different file counts.

🔧 Debug
advanced
2:00remaining
Why does this Express route fail to save multiple uploaded files?

Examine this Express route snippet:

app.post('/upload', upload.single('photos'), (req, res) => {
  console.log(req.files);
  res.send('Upload complete');
});

When uploading multiple files under the field 'photos', req.files is undefined. Why?

Express
app.post('/upload', upload.single('photos'), (req, res) => {
  console.log(req.files);
  res.send('Upload complete');
});
ABecause the field name 'photos' is incorrect and must be 'photo'
BBecause req.files is only set when no files are uploaded
CBecause multer requires upload.array for single file uploads
DBecause upload.single only processes one file and sets req.file, not req.files
Attempts:
2 left
💡 Hint

Check multer's difference between single and array methods.

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

Given this Express route:

app.post('/upload', upload.fields([{ name: 'doc', maxCount: 2 }, { name: 'img', maxCount: 3 }]), (req, res) => {
  res.json(req.files);
});

If a client uploads 1 file under 'doc' and 2 files under 'img', what is the structure of req.files?

Express
app.post('/upload', upload.fields([{ name: 'doc', maxCount: 2 }, { name: 'img', maxCount: 3 }]), (req, res) => {
  res.json(req.files);
});
A{"doc": [FileObject], "img": [FileObject, FileObject]}
B[FileObject, FileObject, FileObject]
C{"doc": FileObject, "img": FileObject}
D[]
Attempts:
2 left
💡 Hint

Recall how multer structures req.files when using fields method.

🧠 Conceptual
expert
2:00remaining
Which statement about handling multiple file uploads in Express with multer is TRUE?

Choose the correct statement about multer's handling of multiple file uploads in Express.

Aupload.array(fieldname, maxCount) stores files in req.files as an array of file objects
Bupload.single(fieldname) stores multiple files in req.files as an array
Cupload.fields([{name, maxCount}]) stores files in req.file as a single object
DMulter automatically merges files from different fields into a single array in req.files
Attempts:
2 left
💡 Hint

Review multer's documentation on how it stores uploaded files in the request object.