📖 Scenario: You are building a simple Express server that allows users to download a file from your server.
🎯 Goal: Create an Express route that uses res.download to send a file named example.txt to the user when they visit /download.
📋 What You'll Learn
Create an Express app instance
Create a route handler for GET requests to /download
Use res.download to send the file example.txt
Set a custom filename for the downloaded file as user-file.txt
💡 Why This Matters
🌍 Real World
File downloads are common in web apps for delivering reports, images, or documents to users.
💼 Career
Knowing how to serve files securely and correctly is important for backend developers working with Express.
Progress0 / 4 steps
1
Set up Express app and import modules
Write code to import express and create an Express app instance called app.
Express
Hint
Use require('express') to import Express and then call express() to create the app.
2
Create a GET route for /download
Add a GET route handler on app for the path /download that takes req and res as parameters.
Express
Hint
Use app.get('/download', (req, res) => { ... }) to create the route.
3
Use res.download to send example.txt
Inside the /download route handler, use res.download to send the file example.txt from the current directory. Set the downloaded filename to user-file.txt.
Express
Hint
Use path.join(__dirname, 'example.txt') to get the file path and then call res.download(filePath, 'user-file.txt').
4
Start the Express server
Add code to start the Express server on port 3000 using app.listen.
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What does res.download() do in an Express app?
easy
A. Uploads a file from the user to the server
B. Sends a file to the user for download
C. Deletes a file on the server
D. Reads a file and logs its content
Solution
Step 1: Understand the purpose of res.download
res.download() is used to send files to users so they can download them easily.
Step 2: Compare with other file operations
Uploading, deleting, or reading files are different operations and not what res.download() does.
Final Answer:
Sends a file to the user for download -> Option B
Quick Check:
res.download() = Sends file [OK]
Hint: Remember: download means sending file to user [OK]
Common Mistakes:
Confusing download with upload
Thinking it deletes files
Assuming it reads file content
2. Which is the correct syntax to send a file named report.pdf for download using res.download?
easy
A. res.download('report.pdf')
B. res.download(report.pdf)
C. res.download('report.pdf', 'download')
D. res.download('report.pdf', {filename: 'download.pdf'})
Solution
Step 1: Check the basic usage of res.download
The first argument is the file path as a string, so res.download('report.pdf') is correct.
Step 2: Analyze other options
res.download(report.pdf) misses quotes making it invalid syntax. res.download('report.pdf', 'download') adds a second argument incorrectly as a string. res.download('report.pdf', {filename: 'download.pdf'}) uses an object which is not valid for filename.
Final Answer:
res.download('report.pdf') -> Option A
Quick Check:
File path string only = res.download('report.pdf') [OK]
A. res.download requires a callback as first argument
B. res.download cannot be used inside app.get
C. No error, code works fine
D. Missing file path argument in res.download
Solution
Step 1: Check res.download required arguments
res.download() requires at least the file path as the first argument.
Step 2: Analyze given code
The code calls res.download() with no arguments, causing an error.
Final Answer:
Missing file path argument in res.download -> Option D
Quick Check:
res.download needs file path [OK]
Hint: Always provide file path to res.download [OK]
Common Mistakes:
Calling res.download without arguments
Thinking res.download needs callback first
Assuming no error if arguments missing
5. You want to send a file report.pdf for download but also handle errors if the file is missing. Which code correctly does this?
hard
A. res.download('report.pdf', (err) => { if (err) throw err; });
B. res.download('report.pdf').catch(() => res.status(404).send('File not found'));
C. res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); });
D. res.download('report.pdf', 'report.pdf', err => { if (err) res.send('Error'); });
Solution
Step 1: Understand error handling in res.download
res.download accepts an optional callback as the last argument to handle errors.
Step 2: Check each option
res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); }); uses the callback correctly to send 404 if error occurs. res.download('report.pdf').catch(() => res.status(404).send('File not found')); wrongly uses .catch which is not supported. res.download('report.pdf', 'report.pdf', err => { if (err) res.send('Error'); }); has an extra filename argument which is unnecessary here. res.download('report.pdf', (err) => { if (err) throw err; }); throws error instead of handling gracefully.
Final Answer:
res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); }); -> Option C
Quick Check:
Use callback to handle errors in res.download [OK]
Hint: Use callback function to catch errors in res.download [OK]
Common Mistakes:
Using promises or .catch with res.download
Not handling errors at all
Passing wrong number of arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.