Managing file downloads in express with the response download method

As I continue expanding on express.js this month today I thought I would write about the response download method, which is one of the many methods in express that are part of the standard response object that I might more about this week. This method is useful if you want to have some kind of path that will work as a way to deliver a file as a download when a link is clicked, or something to that effect. It is very easy to use, you do not have to worry about setting the proper headers or anything like that it does it all for you so all that has to be done basically is to just call a method in the response object.

What to know

This is a post on express.js a popular node.js framework that is used in many node.js projects. It is not a getting started post on express.js, node.js, or any additional skills required to get something of value out of reading this. I have my getting started post on express.js, but you might want to start elsewhere if you are new to express.js, node.js, and javaScript.

Also in this post I am working with express.sj version 4.16.3, which as of this writing is the latest version.

Basic example of res.download in express.js

For a basic example I have just a single file called doc1.txt in a folder called docs in the root of my demo folder. For this demo the only dependency that is needed is express itself.

1
2
3
4
5
$ mkdir response-download
$ cd response-download
$ npm init
$ npm install express --save
$ mkdir docs

The app.js file for the basic example of res.download in express.js

This demo has just one javaScript file that is of course the main app.js file that will be started with node in the command line to start the demo. I am just setting up two paths. One path for the root name space of the app, that will provide a link to a path that will result in a file download, and the other is that path that will result in the download.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let express = require('express'),
path = require('path'),
port = process.env.PORT || process.argv[2] || 8080,
app = express();
app.get('/', function (req, res) {
res.send('<a href="/getdoc">get a doc</a>');
});
app.get('/getdoc', function (req, res) {
res.download(path.join(__dirname, 'docs/doc1.txt'), function (err) {
console.log(err);
});
});
app.listen(port, function () {
console.log('response-download demo is up on port: ' + port);
});
``
When I start the app.

node app.js
```

And go to localhost:8080 in my browser I see the get a doc link, when I click it I end up downloading doc1.txt to the downloads folder.

Conclusion

The response download method works great if you want something to be treated as a file download. this makes the method useful for things like documents and platform dependent binaries thar are meant to be used outside of the browser in some capacity. There are many other such methods like res.sendFile that can also be used to just send a file to the client, although they do work differently. If you want to just host some files in a public folder of sorts you would want to use express.static. If you enjoyed this post consider reading my other posts on express.