How to create a proxy download in Nodejs

I want to create a nodejs server which is acting a proxy to download files i.e. user clicks
on the a download button, call get from nodejs server, nodejs server fetches link from a different
remote server and starts the download (in terabytes). This download is then forwarded to the user.
The terabyte file should not be stored on the nodejs server and then sent.

Here is my attempt:

function (request, response) {

 // anything related to the remote server having the file
 var options= {
        path: "./bigData",
        hostname:"www.hugeFiles.net"
    }

    // get the file from the remote server hugefiles and push to user's response
    https.get(options, function(downFile)) {
        downFile.pipe(response) 
    }

}

Before I was using res.download(file, function(err)) {} but file has to be downloaded completely from the remote server

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You’re very close, you’re sending the right http body but with the wrong http headers.

Here’s a minimal working example:

const express = require('express');
const http = require('http');

const app1 = express();

app1.get('/', function (req, res) {
  res.download('server.js');
});

app1.listen(8000);

const app2 = express();

app2.get('/', function (req, res) {
  http.get({ path: '/', hostname: 'localhost', port: 8000}, function (resp) {
    res.setHeader('content-disposition', resp.headers['content-disposition']);
    res.setHeader('Content-type', resp.headers['content-type']);
    resp.pipe(res);
  });
});

app2.listen(9000);

Though I would say you should take a look at modules like https://github.com/nodejitsu/node-http-proxy which take care of the header etc . . . for you.

Solution 2

There is no way for your server to provide a file to the client without the server downloading it first.

What you may want to do instead is provide the client with a download link to the huge file. To make it seem automatic, you can create html which starts a download from the content provider automatically and serve that to the client.

In other words, in the scenario you are describing, the server is acting as a middleman between your client and the content provider. Unless the server needs to process the data or the client isn’t allowed to retrieve the data themselves, it makes more sense to cut out the middleman.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply