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
Single file upload
📖 Scenario: You are building a simple web server that allows users to upload one file at a time.This is useful for profile pictures, documents, or any single file submission.
🎯 Goal: Build an Express server that accepts a single file upload from a form and saves it to a folder on the server.
📋 What You'll Learn
Create an Express app with a POST route to handle file uploads
Use the multer middleware to process single file uploads
Save the uploaded file to a folder named uploads
Respond with a success message after upload
💡 Why This Matters
🌍 Real World
Single file uploads are common in web apps for profile pictures, documents, or attachments.
💼 Career
Backend developers often implement file upload endpoints using Express and multer in real projects.
Progress0 / 4 steps
1
Set up Express app and import multer
Create a file named app.js. Import express and multer. Initialize an Express app by writing const app = express().
Express
Hint
Use require('express') and require('multer') to import the packages.
Then create the app with express().
2
Configure multer storage and upload variable
Create a multer storage configuration 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.
Destination should be the uploads folder.
Filename should use the original file name.
3
Create POST route to handle single file upload
Add a POST route at /upload that uses upload.single('file') middleware to handle a single file upload with the form field name file. Inside the route, send a response with res.send('File uploaded successfully').
Express
Hint
Use app.post with path /upload.
Use upload.single('file') as middleware.
Send a success message in the response.
4
Start the Express server on port 3000
Add code to start the Express server listening on port 3000 using app.listen(3000). Inside the listen callback, log 'Server started on port 3000'.
Express
Hint
Use app.listen(3000, () => { ... }) to start the server.
Inside the callback, log a message to confirm the server is running.
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?