Express.js res.download() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

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.

Express
res.download(path [, filename] [, options] [, fn])
res.download('/report-12345.pdf');
res.download('/report-12345.pdf', 'report.pdf');
res.download('/report-12345.pdf', 'report.pdf', (err) => {
if (err) {
} else {
}
});
CallWhat it does
res.download(path [, filename] [, options] [, fn])Primary signature.
res.download('/report-12345.pdf');Related call form.
res.download('/report-12345.pdf', 'report.pdf');Related call form.
res.download('/report-12345.pdf', 'report.pdf', (err) => {Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.download(path [, filename] [, options] [, fn])
res.download('/report-12345.pdf');
res.download('/report-12345.pdf', 'report.pdf');
res.download('/report-12345.pdf', 'report.pdf', (err) => {
if (err) {
} else {
}
});

Parameters

ParameterDescription
pathRequired. File to transfer. Relative paths use CWD or options.root.
filenameRequired. Overrides Content-Disposition filename= (the name the user sees).
optionsOptional. Passed through to res.sendFile() (for example root, maxAge, headers).
fnRequired. fn(err) when done or on error. On error you must finish the response or call next.
rootRequired. Absolute directory; relative path must resolve inside it.
maxAgeRequired. Cache-Control max-age (ms or ms string). Default 0.
headersRequired. Extra response headers to send with the file.
dotfilesRequired. "allow" | "deny" | "ignore" (default "ignore").

Return Value

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.

Express
const express = require('express');
const path = require('path');
const app = express();

app.get('/download-manual', (req, res) => {
  const filePath = path.join(__dirname, 'files', 'manual.pdf');
  res.download(filePath);
});

app.listen(3000);

How It Works

  1. Resolve a real path. path.join(__dirname, …) points at a file next to your app.
  2. download streams it. Express reads the file and sets attachment headers.
  3. Name from the path. Base name manual.pdf becomes filename=.
  4. Client saves. The browser treats the response as a download, not an inline view.

Example 2 — Custom Download Filename

Keep the disk name private; offer a friendly name in the Save dialog.

Express
const express = require('express');
const path = require('path');
const app = express();

app.get('/download-report', (req, res) => {
  const filePath = path.join(__dirname, 'reports', 'monthly.csv');
  res.download(filePath, 'sales-report.csv');
});

app.listen(3000);

How It Works

  1. path chooses which bytes to stream from disk.
  2. filename overrides only the Content-Disposition name.
  3. Disk path stays private. Clients never need to know monthly.csv.
  4. 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);

How It Works

  1. basename first. Strips directory segments from user input before download.
  2. root contains access. Express resolves name inside FILES and rejects escapes.
  3. Callback on finish/error. Official docs: you must end the cycle or pass control on error.
  4. Check headersSent. If the stream already started, do not try a fresh 404 body blindly.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via 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.

Next: res.end()

End the response, optionally with a final chunk of data.

res.end() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful