如何在Node.js中创建代理下载

Jen*_*Jen 5 proxy download node.js

我想创建一个代理代理来下载文件的nodejs服务器,即用户单击一个下载按钮,从nodejs服务器调用get,nodejs服务器从另一个远程服务器获取链接并开始下载(以TB为单位)。然后,此下载将转发给用户。太字节文件不应存储在nodejs服务器上,然后发送。

这是我的尝试:

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) 
    }

}
Run Code Online (Sandbox Code Playgroud)

在使用之前,res.download(file, function(err)) {}但必须从远程服务器完全下载文件

gen*_*nry 5

您的位置非常接近,正在发送正确的http正文,但带有错误的http标头。

这是一个最小的工作示例:

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']);
    resp.pipe(res);
  });
});

app2.listen(9000);
Run Code Online (Sandbox Code Playgroud)

虽然我会说你应该看看像https://github.com/nodejitsu/node-http-proxy这样的模块,它们负责标头等。。。为了你。