In this express tutorial we will see how to download a file. The idea is to have user download a file after hitting the service url. In order to transfer the file, it uses the method download() which is part of the response object. It is quite similar to the usage of sendFile() in express. Let us explore it with an example.

Download File Example

Create a folder “files” and place an image file (techeplanet.png) in it in the root folder of the project. The below code will create an express GET service where the user can download the file.

var express = require("express");
var app = express();
//Express Download File

Run the above code and make a request to http://localhost:8080/download. This will download the image file to user’s computer.

How to download a file using express?