Performance: res.download for file downloads
This affects how quickly a file starts downloading and how server resources are used during the download process.
app.get('/file', (req, res) => { res.download('/path/to/file.pdf', 'file.pdf', err => { if (err) res.status(500).send('Error downloading file'); }); });
app.get('/file', (req, res) => { const file = fs.readFileSync('/path/to/file.pdf'); res.setHeader('Content-Disposition', 'attachment; filename="file.pdf"'); res.send(file); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| res.send with fs.readFileSync | 0 (server-side only) | 0 | 0 | [X] Bad |
| res.download streaming file | 0 (server-side only) | 0 | 0 | [OK] Good |
res.download() do in an Express app?res.download() is used to send files to users so they can download them easily.res.download() does.report.pdf for download using res.download?res.download('report.pdf') is correct.app.get('/file', (req, res) => {
res.download('files/data.txt', 'info.txt');
});app.get('/download', (req, res) => {
res.download();
});res.download() requires at least the file path as the first argument.res.download() with no arguments, causing an error.report.pdf for download but also handle errors if the file is missing. Which code correctly does this?res.download accepts an optional callback as the last argument to handle errors.Pass the test and this pattern moves unknown