res.download lets your server send files to users so they can save them on their device. It makes downloading files easy and automatic.
res.download for file downloads in Express
res.download(path, [filename], [options], [callback])
path is the file location on your server.
filename is optional and sets the name users see when saving.
res.download('/files/report.pdf');res.download('/files/report.pdf', 'monthly-report.pdf');
res.download('/files/image.png', (err) => { if (err) console.log('Download error:', err); });
This Express server has a route '/download' that sends the file 'sample.txt' from the 'public' folder. The file will be downloaded as 'example.txt'. If the file is missing, it sends an error message.
import express from 'express'; const app = express(); app.get('/download', (req, res) => { const filePath = './public/sample.txt'; res.download(filePath, 'example.txt', (err) => { if (err) { res.status(500).send('Sorry, file not found.'); } }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Make sure the file path is correct and accessible by your server.
Use the callback to handle errors like missing files or permission issues.
res.download sets headers so browsers know to download, not display, the file.
res.download sends files to users for easy downloading.
You can rename the file for the user with an optional filename.
Always handle errors to improve user experience.
Practice
res.download() do in an Express app?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 whatres.download()does.Final Answer:
Sends a file to the user for download -> Option BQuick Check:
res.download() = Sends file [OK]
- Confusing download with upload
- Thinking it deletes files
- Assuming it reads file content
report.pdf for download using res.download?Solution
Step 1: Check the basic usage of res.download
The first argument is the file path as a string, sores.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 AQuick Check:
File path string only = res.download('report.pdf') [OK]
- Omitting quotes around filename
- Passing wrong argument types
- Using object instead of string for filename
app.get('/file', (req, res) => {
res.download('files/data.txt', 'info.txt');
});Solution
Step 1: Understand res.download parameters
The first argument is the path on the server, the second is the filename for the user.Step 2: Apply to given code
The server sends 'files/data.txt' but the user will see it saved as 'info.txt'.Final Answer:
The file 'files/data.txt' is sent and saved as 'info.txt' by the user -> Option AQuick Check:
res.download(path, filename) renames file [OK]
- Thinking second argument is server file path
- Assuming no renaming happens
- Believing second argument causes error
app.get('/download', (req, res) => {
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 callsres.download()with no arguments, causing an error.Final Answer:
Missing file path argument in res.download -> Option DQuick Check:
res.download needs file path [OK]
- Calling res.download without arguments
- Thinking res.download needs callback first
- Assuming no error if arguments missing
report.pdf for download but also handle errors if the file is missing. Which code correctly does this?Solution
Step 1: Understand error handling in res.download
res.downloadaccepts 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 CQuick Check:
Use callback to handle errors in res.download [OK]
- Using promises or .catch with res.download
- Not handling errors at all
- Passing wrong number of arguments
Ready to prove it?
Pass the test and this pattern moves unknown
