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
Recall & Review
beginner
What is Multer in Express?
Multer is a middleware for Express that helps handle file uploads from forms. It processes files sent through HTTP requests and saves them to the server or memory.
Click to reveal answer
beginner
How do you set up Multer to store uploaded files in a folder named 'uploads'?
You create a Multer storage engine using multer.diskStorage() and set the destination to 'uploads'. Then, pass this storage to multer() to create the middleware.
Click to reveal answer
beginner
What does the 'single' method in Multer do?
The 'single' method tells Multer to accept a single file with the given field name from the form. It adds the file info to req.file.
Click to reveal answer
intermediate
How can you limit the file size of uploads using Multer?
You can set the 'limits' option in Multer configuration with a 'fileSize' property in bytes to restrict the maximum upload size.
Click to reveal answer
intermediate
What is the purpose of the 'fileFilter' option in Multer?
The 'fileFilter' option lets you control which files are accepted by checking the file type or other properties before saving.
Click to reveal answer
Which Multer method is used to handle a single file upload?
Asingle
Barray
Cfields
Dnone
✗ Incorrect
The 'single' method handles one file upload with the specified field name.
How do you specify the folder where Multer saves uploaded files?
AUsing the 'limits' option
BUsing the 'fileFilter' option
CUsing the 'destination' option in diskStorage
DUsing the 'single' method
✗ Incorrect
The 'destination' option inside diskStorage sets the folder path for saving files.
What property in Multer limits the maximum file size?
Astorage
BfileFilter
CmaxFiles
DfileSize in limits
✗ Incorrect
The 'fileSize' property inside 'limits' sets the max allowed file size in bytes.
Where does Multer store information about the uploaded file when using 'single'?
Areq.body
Breq.file
Creq.files
Dreq.upload
✗ Incorrect
For single file uploads, Multer adds the file info to req.file.
What does the 'fileFilter' function receive as arguments?
Areq, file, callback
Breq, res, next
Cfile, callback
Dreq, res
✗ Incorrect
'fileFilter' receives the request, the file object, and a callback to accept or reject the file.
Explain how to set up Multer middleware to accept a single file upload and save it to a specific folder.
Think about creating storage, then middleware, then using it in a route.
You got /4 concepts.
Describe how to control which files Multer accepts and how to limit file size.
Consider validation and size restrictions in Multer config.
You got /2 concepts.
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
Step 1: Understand middleware role
Multer is middleware designed specifically to handle multipart/form-data, which is used for uploading files.
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.
Final Answer:
To handle file uploads from client requests -> Option C
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
Step 1: Correct multer initialization
Multer must be called as a function to create an instance: multer().
Step 2: Use .single() for single file
To accept one file, use upload.single('fieldname'). Here, 'avatar' is the field name.
Final Answer:
const upload = multer().single('avatar'); -> Option A
Quick Check:
multer() + .single('avatar') = correct setup [OK]
Hint: Always call multer() before .single() or .array() [OK]
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
Step 1: Check multer method used
upload.single('file') handles one file and stores it in req.file (singular).
Step 2: Identify incorrect property usage
The code logs req.files (plural), which is undefined for single file uploads.
Final Answer:
Using req.files instead of req.file for single file upload -> Option B
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?