In Express.js, res.download() transfers a file from disk as an attachment. Browsers usually show a Save dialog. The download name comes from the path’s base name, or from an optional filename argument.
Relative paths resolve from the process working directory, or from options.root when you set it. Options are forwarded to res.sendFile(). For headers only (no file read), use res.attachment() instead.
Syntax
Call res.download in your Express.js app. Start with this line.
res.download() transfers a file as an attachment. The method finishes the response (unless you supply an error callback you must handle). Treat the return value like other terminating res helpers — do not send twice.
Express
res.download(path.join(__dirname, 'files/a.zip'), 'a.zip', (err) => {
if (err) {
if (!res.headersSent) res.status(404).end();
}
});
Express.js Examples
Example 1 — Download a Static File
Send a PDF from disk; the download name comes from the path.
GET /download-report
→ Reads: reports/monthly.csv (on disk)
→ Content-Disposition: attachment; filename="sales-report.csv"
→ Body: (CSV bytes)
User sees sales-report.csv in the Save dialog
How It Works
path chooses which bytes to stream from disk.
filename overrides only the Content-Disposition name.
Disk path stays private. Clients never need to know monthly.csv.
Type still follows the file. Content-Type comes from the real file’s extension/type handling.
Example 3 — Callback Errors + root Option
Contain downloads under one folder and handle missing files safely.
Express
const express = require('express');
const path = require('path');
const app = express();
const FILES = path.join(__dirname, 'files');
app.get('/download-safe/:name', (req, res, next) => {
// Only the base name — no "../" from the client
const name = path.basename(req.params.name);
res.download(name, name, { root: FILES }, (err) => {
if (err) {
if (!res.headersSent) {
res.status(404).send('File not found');
}
return;
}
console.log('Download completed');
});
});
app.listen(3000);
Output
GET /download-safe/guide.pdf
→ Resolves under FILES → files/guide.pdf
→ attachment; filename="guide.pdf"
→ Download completed
GET /download-safe/missing.pdf
→ err in callback, headers not sent
→ 404 File not found
GET /download-safe/../secret.txt (basename → secret.txt)
→ Still resolved only under FILES (no escape)
How It Works
basename first. Strips directory segments from user input before download.
root contains access. Express resolves name inside FILES and rejects escapes.
Callback on finish/error. Official docs: you must end the cycle or pass control on error.
Check headersSent. If the stream already started, do not try a fresh 404 body blindly.
Compatibility
Runtime Support
res.download() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+) and reads the local filesystem. Browsers only receive the attachment response.
✓ Server · Express 4 & 5
res.download()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · Express 4 supported
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.download()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Place sample files on disk before testing downloads.
Frequently Asked Questions
It transfers a file from disk as an attachment. Browsers typically prompt to save it. Content-Disposition filename comes from the path, or from the optional filename argument.
Both send a file from disk. res.download() also sets Content-Disposition to attachment so the client treats it as a download. res.sendFile() often opens inline unless you set attachment yourself.
res.attachment() only sets headers. res.download() sets attachment headers and streams the file in one call.
With root, Express resolves the relative path inside that directory and rejects paths that escape it. Use it when any part of the path comes from user input.
Handle the response yourself: end the cycle or call next(err). If headers may already be sent, check res.headersSent before writing a new status body.