Express.js res.download() Function

Last Updated : 24 Sep, 2026

The res.download() function is used to transfer a file from the server as an attachment. Typically, browsers prompt the user to download the file instead of displaying it directly.

frame_3479

Syntax:

res.download(path [, filename] [, options] [, fn])

Parameters:

  • path: The path of the file to be downloaded.
  • filename (Optional): The name to use for the downloaded file. If not provided, the filename is derived from the path.
  • options (Optional): An object used to configure the file transfer, such as root, maxAge, headers, and other res.sendFile() options.
  • fn (Optional): A callback function that is called when the file transfer is complete or an error occurs.

Installation of the express module

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

The res.download() function in Express.js allows you to download files from the server.

Example 1: Downloading a File

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.download('Hello.txt');
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Place any file in the root directory of the project which can be downloaded, like here we have used Hello.txt. 

Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Screenshot-2026-08-17-124132

Browser Output: Open your browser and go to http://localhost:3000/, now you can see the "Hello.txt" file being downloaded on your screen.

Screenshot-2026-08-17-124429

Example 2: Downloading a File with a Custom Filename

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.download('Hello.txt', 'GeeksforGeeks.txt');
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

Browser Output:

Open your browser and go to http://localhost:3000/, then you will see the following output on your browser:

Screenshot-2026-08-17-124429

Working of res.download()

  • An Express application receives a client request.
  • The route handler gets access to the response object through res.
  • res.download() locates the specified file on the server.
  • Express sets the response as an attachment and derives the download filename from the file path or the provided filename.
  • The file is transferred to the client, and the browser typically prompts the user to download it.

Use Cases of res.download()

  • Downloading files from an Express application.
  • Providing documents, reports, or other files to users.
  • Allowing users to download files with a custom filename.
  • Serving generated files as downloadable attachments.
  • Providing file downloads through web applications and APIs.

Reference: https://expressjs.com/en/4x/api.html#res.download

Comment