Download files for the client from the Node.js server using this quick way

Sharmila S
3 min readNov 30, 2021

Add a download option for files on your Node.js web application for users to download them to their local filesystem.

System Requirements: npm, node.js

Using Express (framework of node.js), it is easier to download files for users. If you are new to express, refer to the link below to get started.

Set up the project

  • Create a new directory
$ mkdir download-test$ cd download-test
  • Initiate npm and install the express module.
$ npm init$ npm i express
  • Create an index.js file.
  • Create a directory ‘public’ with a sub-directory ‘assets’, save the file that you need to download in the assets folder (here, ‘book.png’).
Folder Structure
  • Add the following code in index.js
const express = require("express")
const app = express();
const PORT = 3000;//
// GET
http://localhost:3000/download/book.png
//
app.get("/download/:filename", (req, res) => {
const filePath = __dirname + "/public/assets/" + req.params.filename; res.download(
filePath,
"downloaded-book.png", // Remember to include file extension
(err) => {
if (err) {
res.send({
error : err,
msg : "Problem downloading the file"
})
}
});
});
app.listen( PORT, () => console.log("Server listening to port " + PORT))

Code Explanation

We define an API with the endpoint “download/:filename” for the GET method. When a request is sent, the file that is mentioned in the URL will be downloaded.

Let’s look at the API definition.

app.get("/download/:filename", (req, res) => {     const filePath = __dirname + "/public/assets/" + req.params.filename;    res.download(
filePath,
"downloaded-book.png", // Remember to include file extension
(err) => {
if (err) {
res.send({
error : err,
msg : "Problem downloading the file"
})
}
});
});

filepath -> variable to hold the full path of the file which will be downloaded.

req.params.filename -> to obtain the name of the file to be downloaded from URL parameter. Eg. in "/download/book.png” , the file name is ‘book.png’.

res.download() is used to download a file on the user’s computer. It takes 3 parameters.

In the above usage,

  1. filepath-> Path to the file.
  2. “downloaded-book.png” -> Alternate name for the file when a user downloads it. (optional)
  3. (err) => {} -> Error callback. (optional)

Run the app

$ node index

Try making a GET request to http://localhost:3000/download/book.png to download the file(here, book.png) to your system. (Hit the URL in the browser)

There are several ways to make an API call from the front end. Since we are dealing with the GET request, a simple way is to use an anchor tag.

The following code creates a button and when a user clicks on the button, the file is downloaded.

<a href="http://localhost:3000/download/book.png">
<button>Click to Download</button>
</a>

Github Link 👇

Thank you! Hope you find this article useful!

Follow for more!!

More Node.js articles: 👇

--

--

Sharmila S

Software Engineer | Writes about Full Stack Web Development | 1 X AWS | CKA http://sharmilas.bio.link/