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?
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 }); });
Look at the second argument to upload.array. It limits the number of files; exceeding it causes a LIMIT_FILE_COUNT error.
The upload.array('photos', 3) middleware limits uploads to 3 files. Uploading 4 files exceeds the limit, triggering a MulterError 'LIMIT_FILE_COUNT'. The route handler is not reached, resulting in an error instead of a JSON response.
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?
Check multer's documentation for handling multiple fields with different file counts.
The upload.fields method accepts an array of objects specifying each field name and maxCount. Other options are invalid multer syntax.
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?
app.post('/upload', upload.single('photos'), (req, res) => { console.log(req.files); res.send('Upload complete'); });
Check multer's difference between single and array methods.
upload.single handles one file and sets req.file. For multiple files, upload.array must be used, which sets req.files. Hence, req.files is undefined here.
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?
app.post('/upload', upload.fields([{ name: 'doc', maxCount: 2 }, { name: 'img', maxCount: 3 }]), (req, res) => { res.json(req.files); });
Recall how multer structures req.files when using fields method.
When using upload.fields, req.files is an object with keys for each field name. Each key maps to an array of file objects uploaded under that field.
Choose the correct statement about multer's handling of multiple file uploads in Express.
Review multer's documentation on how it stores uploaded files in the request object.
upload.array stores multiple files under the same field name in req.files as an array. upload.single stores one file in req.file. upload.fields stores files in req.files as an object keyed by field names. Multer does not merge files from different fields into one array.