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 the purpose of the multer middleware in Express?
Multer is used to handle file uploads in Express apps. It processes incoming files and stores them temporarily or permanently.
Click to reveal answer
beginner
How do you configure multer to accept a single file upload with the field name 'avatar'?
Use upload.single('avatar') where upload is the multer instance. This tells multer to expect one file with the field name 'avatar'.
Click to reveal answer
beginner
What does req.file contain after a successful single file upload?
req.file holds information about the uploaded file like its original name, size, encoding, and the path where it was saved.
Click to reveal answer
intermediate
Why is it important to handle errors when uploading files in Express?
Errors can happen if the file is too large, the wrong type, or the upload fails. Handling errors helps keep the app stable and informs users what went wrong.
Click to reveal answer
intermediate
What is a simple way to limit the size of uploaded files using multer?
Set the limits.fileSize option in multer configuration to restrict the maximum file size allowed.
Click to reveal answer
Which middleware is commonly used for handling single file uploads in Express?
Amulter
Bbody-parser
Ccors
Dexpress-session
✗ Incorrect
Multer is designed specifically for handling multipart/form-data, which is used for file uploads.
What does upload.single('photo') do in an Express route?
ABlocks file uploads
BAccepts multiple files with any field name
CAccepts one file with the field name 'photo'
DUploads files to a cloud service automatically
✗ Incorrect
It tells multer to expect a single file upload with the field name 'photo'.
Where can you find the uploaded file's information after multer processes it?
Areq.file
Breq.body.file
Creq.files
Dreq.upload
✗ Incorrect
req.file contains the uploaded file's details for single file uploads.
How can you limit the size of an uploaded file using multer?
AUse express.static middleware
BSet the limits.fileSize option
CSet maxUploadSize in Express settings
DUse body-parser size limit
✗ Incorrect
Multer's configuration allows setting limits.fileSize to restrict file size.
What happens if a file larger than the limit is uploaded with multer?
AThe file uploads but is truncated
BThe server crashes
CThe file uploads normally without issues
DAn error is thrown and can be caught in error handling
✗ Incorrect
Multer throws an error when the file size exceeds the limit, which should be handled properly.
Explain how to set up a simple Express route to handle a single file upload using multer.
Think about the steps from importing multer to accessing the file in the route.
You got /5 concepts.
Describe why handling errors during file upload is important and how you might do it in Express with multer.
Consider what can go wrong and how to keep the app stable.
You got /4 concepts.
Practice
(1/5)
1. What does upload.single('file') do in an Express app using multer?
easy
A. Uploads multiple files with the form field name 'file'.
B. Validates the file type without uploading.
C. Uploads a single file but does not save it.
D. Handles uploading a single file with the form field name 'file'.
Solution
Step 1: Understand multer's single file upload
The method upload.single('file') tells multer to accept one file with the field name 'file' from the form.
Step 2: Confirm behavior of single file upload
This middleware processes the file and attaches its info to req.file, enabling access in the route handler.
Final Answer:
Handles uploading a single file with the form field name 'file'. -> Option D
Quick Check:
upload.single('file') = single file upload [OK]
Hint: Remember single() is for one file with given field name [OK]
Common Mistakes:
Confusing single() with array() for multiple files
Thinking it uploads files without saving
Assuming it validates file type automatically
2. Which of the following is the correct syntax to set up multer for single file upload with field name 'avatar'?
easy
A. app.post('/upload', multer.single('avatar'), (req, res) => { ... })
B. app.post('/upload', upload.single('avatar'), (req, res) => { ... })
C. app.post('/upload', multer.upload.single('avatar'), (req, res) => { ... })
D. app.post('/upload', upload.single.avatar(), (req, res) => { ... })
Solution
Step 1: Recognize multer setup pattern
You first create an upload instance with multer, e.g., const upload = multer({ dest: 'uploads/' }). Then use upload.single('avatar') in route.
A. upload.single('file') is not called as middleware, so file is not processed.
B. The destination folder 'uploads/' is missing.
C. The route should use upload.array() for single file.
D. res.send() must be called before upload.single().
Solution
Step 1: Check how multer middleware is used
The method upload.single('file') must be passed as middleware in the route definition, not just called inside the handler.
Step 2: Identify the mistake in route setup
Here, upload.single('file') is called but not used as middleware, so multer never processes the file.
Final Answer:
upload.single('file') is not called as middleware, so file is not processed. -> Option A
Quick Check:
Use upload.single() as middleware in route [OK]
Hint: Pass upload.single() as middleware, not call inside handler [OK]
Common Mistakes:
Calling upload.single() inside route handler instead of middleware
Confusing upload.single() with upload.array()
Ignoring multer setup before route
5. You want to customize the filename of uploaded files to include the current timestamp before the original name. Which multer storage option correctly achieves this?