请问 res.download 可以直接下载远程文件吗
发布于 6 年前 作者 hezhizheng 3179 次浏览 来自 问答

app.get(’/download’,function(req, res, next){ res.download(“https://o4j806krb.qnssl.com/public/images/cnodejs_light.svg”); });

运行提示 找不到目录 Error: ENOENT: no such file or directory

2 回复

不行。res.download(path) 只能针对的是本地文件。如果是远程文件,可以参考如下思路。

createReadStreamOfRemoteFile(url).pipe(res); // createReadStreamOfRemoteFile 自行实现

@chyingp 前辈提供的方向很好,我这边做了一个简易实例供楼主参考看看:

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

const url = 'https://o4j806krb.qnssl.com/public/images/cnodejs_light.svg';

const app = http.createServer((req, res) =>
    request.get(url).pipe(res)
);
app.listen(3000);
回到顶部