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)
C
不行。res.download(path) 只能针对的是本地文件。如果是远程文件,可以参考如下思路。
createReadStreamOfRemoteFile(url).pipe(res); // createReadStreamOfRemoteFile 自行实现
G
@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);
参与回复
登录后即可参与回复。登录