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
Multiple file uploads
📖 Scenario: You are building a simple Express server that allows users to upload multiple files at once. This is useful for applications like photo galleries or document management where users need to send several files in one go.
🎯 Goal: Create an Express server that accepts multiple file uploads using the multer middleware. You will set up the data structure, configure multer for multiple files, write the route to handle uploads, and finalize the server to listen on a port.
📋 What You'll Learn
Create an Express app instance
Configure multer to accept multiple files with the field name 'photos'
Write a POST route '/upload' that handles multiple file uploads
Start the Express server on port 3000
💡 Why This Matters
🌍 Real World
Many web applications need to let users upload multiple files at once, such as photo albums, resumes, or project documents.
💼 Career
Understanding how to handle multiple file uploads is a common requirement for backend developers working with Node.js and Express.
Progress0 / 4 steps
1
DATA SETUP: Create Express app and import multer
Write code to import express and multer, then create an Express app instance called app.
Express
Hint
Use require('express') and require('multer') to import the modules, then call express() to create the app.
2
CONFIGURATION: Set up multer storage and upload handler
Create a multer storage configuration using multer.diskStorage() that saves files to a folder named uploads. Then create an upload variable using multer({ storage }).
Express
Hint
Use multer.diskStorage() to set destination and filename. Then pass this storage to multer().
3
CORE LOGIC: Create POST route to handle multiple file uploads
Add a POST route at /upload that uses upload.array('photos', 5) middleware to accept up to 5 files with the field name photos. Inside the route handler, send a JSON response with { message: 'Files uploaded successfully' }.
Express
Hint
Use app.post with upload.array('photos', 5) middleware and send a JSON response inside the handler.
4
COMPLETION: Start the Express server on port 3000
Add code to make the Express app listen on port 3000 and log 'Server started on port 3000' when running.
Express
Hint
Use app.listen(3000, () => { console.log(...) }) to start the server.
Practice
(1/5)
1. In Express, which method from multer is used to handle multiple file uploads at once?
easy
A. upload.array()
B. upload.single()
C. upload.fields()
D. upload.none()
Solution
Step 1: Understand multer upload methods
Multer provides different methods: single() for one file, array() for multiple files, and fields() for multiple fields.
Step 2: Identify method for multiple files
To upload multiple files under the same field name, upload.array() is used.
Final Answer:
upload.array() -> Option A
Quick Check:
Multiple files = upload.array() [OK]
Hint: Multiple files use upload.array() method in multer [OK]
Common Mistakes:
Using upload.single() for multiple files
Confusing upload.fields() with upload.array()
Not specifying max count in upload.array()
2. Which of the following is the correct syntax to accept up to 5 files named 'photos' using multer in Express?
easy
A. upload.array('photos', 5)
B. upload.single('photos', 5)
C. upload.fields([{ name: 'photos', maxCount: 5 }])
D. upload.none('photos', 5)
Solution
Step 1: Recall multer syntax for multiple files
The method upload.array(fieldname, maxCount) accepts multiple files with the same field name and limits the count.
Step 2: Match syntax to question
To accept up to 5 files named 'photos', upload.array('photos', 5) is correct.
Final Answer:
upload.array('photos', 5) -> Option A
Quick Check:
Max 5 files = upload.array('photos', 5) [OK]
Hint: Use upload.array('fieldname', maxCount) for multiple files [OK]
A. upload.array() cannot be used for multiple files
B. upload.array() requires a maxCount parameter
C. Should use req.files instead of req.file to access multiple files
D. res.send() must send JSON, not string
Solution
Step 1: Check multer usage for multiple files
When using upload.array(), uploaded files are stored in req.files (an array), not req.file.
Step 2: Identify error in code
The code logs req.file, which will be undefined or incorrect for multiple files.
Final Answer:
Should use req.files instead of req.file to access multiple files -> Option C
Quick Check:
Multiple files = req.files [OK]
Hint: Use req.files (plural) for multiple files, not req.file [OK]
Common Mistakes:
Forgetting req.files vs req.file difference
Assuming maxCount is mandatory
Thinking upload.array() is invalid
5. You want to accept multiple files from two different fields: 'images' (max 3 files) and 'documents' (max 2 files). Which multer setup correctly handles this?
hard
A. upload.array('images', 3), upload.array('documents', 2)