gpt4 book ai didi

node.js - 如何在 Nodejs 中创建代理下载

转载 作者:搜寻专家 更新时间:2023-10-31 22:46:46 24 4
gpt4 key购买 nike

我想创建一个 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)
}

}

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

最佳答案

您非常接近,您发送的是正确的 http 正文,但带有错误的 http header 。

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

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

虽然我会说你应该看看像 https://github.com/nodejitsu/node-http-proxy 这样的模块负责标题等。 . .给你。

关于node.js - 如何在 Nodejs 中创建代理下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com