Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 635
Jen Avatar asked May 10 '18 20:05

Jen


1 Answers

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.

like image 119
generalhenry Avatar answered Dec 08 '22 01:12

generalhenry