1. Overview

In this article, we will learn how to download file in NodeJs with example . There are several ways to download files.

  1. Download File In http Server Using Stream.
  2. Download File In  express.js Using Download Function
  3. Using Third Party Module.


2.1 Download  File Without Express.

2.1.1 Server.js

Using stream we can download file in node.js. We just need to set headers. Browser will automatically download the file. First header is 'Content-disposition': 'attachment; filename=package.json" . If this header will present in response then browser will force fully download the file. We can also  specify the file name in this header. Another header is Content-Type , this contains the type of content or file type.

var http = require('http');
var fs = require('fs');
//create a server 
http.createServer(function (req, res) {
  var files = fs.createReadStream("package.json");
  res.writeHead(200, {'Content-disposition': 'attachment; filename=package.json"}'}); //here you can specify file name
  files.pipe(res); // also you can set content-type
}).listen(8080); //the server object listens on port 8080

Now run server.js and hit the url http://localhost:8080/.Your file will be download.

2.2 Download File In Express Server Using Download Function.

2.2.1 Server.js

In Express.js  we can directly download file using response.download(filepath, [filename],[function]). Here [] indicates the optional parameter.First parameter is. file path. We can use both relative as well as absolute path. Another parameter is filename and the last parameter is function we will use to handle errors. We can use that like this (error) => { console.log('Some error')}.

var express  = require('express')
var app  = express()
var server = app.listen(8081, () => {
    console.log('Server is started on')
app.get('/downloadFile/', (req, res) => {

When you run the server and send request on http://localhost:8081/downloadFilethen browser will prompt the user for download the file.

NodeJs download file example - output

NodeJs download file example – output

2.2.2 Server.js

Another way is by using stream same as we used in 2.1.1.

var express  = require('express')
var fs = require('fs')
var app  = express()
var server = app.listen(8081, () => {
    console.log('Server is started on')
app.get('/downloadFile/', (req, res) => {
  var files = fs.createReadStream("demo.pdf");
  res.writeHead(200, {'Content-disposition': 'attachment; filename=demo.pdf'}); //here you can add more headers

2.3 Download File Using Third Party Module.

There are so many npm packages are available for this. Based on your requirements you can use them. Here, I used downloadjs. In this case server send the response in form of stream or blob likewise. Then at client side using this module we can download the file.For more info you can visit this.

3. Conclusion

In this article we learned how to download the files in node.js. There the most efficient way is by using the stream and by using the request.download method. While this two are most used ways for download the file furthermore in some case you can use download by any third party modules.  

4. References



Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *