How to Download files From Server in ExpressJS (Node.js)

Express.JS a web framework for Node.js and one of the most downloaded npm module of all time. It provides all the solutions to build and run Progressive web applications and Rest APIs and It also has built-in HTTP requests module to do POST, GET, PUT, DELETE etc options and In this Express.js How to guide we are going to learn how to serve file of any type such as pdf doc etc on Post request made by client or API. SO lets assume you’re building a rest API for generating report of sales of a particular company and after generating that report obvisouly client want to download the report in pdf or any other document to analyse and present, so at that point you can use this guide on How to Download a file using POST or GET request in ExpressJS (a Node.js Framework).

There are multiple possibilities of doing this, we will discuss all one by one:

Article Contents

How to Use the Express.js res.download helper function to download a file using post or get request with an example?

  • Assuming You have installed Nodejs and express.js on your pc.
  • We will use res.download function of express.js.
  • it will send the file to requesting client automatically and you can see the code below
    var express = require('express');
    var app= express();
    
    //your http requests
    
    app.post('/downloads/:id', function (req, res, next) {
        var filePath = "/s3/file/path/..."; // Or format the path using the `id` rest param
        var fileName = "salesReport.pdf"; // file name 
        res.download(filePath, fileName);
        next();    
    });
    
    //or you can also use get request
    app.get('/downloads/:id', function (req, res, next) {
        var filePath = "/s3/file/path/..."; // Or format the path using the `id` rest param
        var fileName = "salesReport.pdf"; // file name 
        res.download(filePath, fileName);    
        next();
    });
    
    //or using direct file URI
    app.get('/download', function(req, res){
      var file = __dirname + '/file-folder/salesReport.pdf';
      res.download(file);
    });
    
    //for using post request
    app.post('/download', function(req, res){
      var file = __dirname + '/file-folder/salesReport.pdf';
      res.download(file);
    });
  • You can use get and post request both to send the file to the client using res.download helper function of express.js.
See also  Fix node is not recognized as an internal or external command Error

How to Download Static files from the server using Express.js (Nodejs) Static folder function?

  • You can also make use of express.static() function to serve static files like pdf, doc, excel files etc
  • Just specify the folder of files to download and you will get URL to download files from server using file names
    const express = require('express');
    const path = require('path');
    const app= express();
    
    //...
    //set static path to serve static files
    app.use(express.static(path.join(__dirname, "public")));
    
    app.get('/download', (req, res) => {
    res.download(path.join(__dirname, "/downloads/report.pdf"));
    
    });
    
    
    //...
  • You can also use express’s res.attachment() function
    const express = require('express');
    const path = require('path');
    const app= express();
    
    //...
    //set static path to serve static files
    app.use(express.static(path.join(__dirname, "public")));
    
    app.get('/download', (req, res) => {
    res.attachment(path.join(__dirname, "/downloads/report.pdf"));
    });
    
    
    //...

     

  • You should prefer res.download as it’s the right function to that task because it sets the content disposition header and sends the file and res.attachment sets only the content disposition header.

Thanks for reading the How to guide on Express.js and please comment below if you have any queries and don’t forget to subscribe our newsletter to get awesome freebies and latest updates regarding nodejs and much more.

Leave a Comment