【发布时间】:2021-06-23 05:12:57
【问题描述】:
从一个后端服务器想要从另一个后端 api 服务器下载文件:
请求远程文件的第一个服务器
requestDownload() {
const ws = fs.createWriteStream('file.mp3');
this.httpService.get('http://MYSECONDSERVER:3000/music/download/189').subscribe(
(result:any) => {
ws.write(result.data)
},
(error) => {
},
() => {
}
)
}
提供文件的第二台服务器
async getFile(id, response) {
let music = await this.findOne(id);
if(music) {
let filePath = path.join(__dirname, '../../../', music.path);
response.set('content-type', `audio/${music.format}`);
// response.set('accept-ranges', 'bytes');
fs.createReadStream(filePath)
.on('error', () => {
response.end();
}).pipe(response);
}
}
对于两个服务器,我都在使用 nestjs 框架。
我下载的 mp3 文件损坏了。
这里是对订阅函数提供的结果对象的调试
我想念什么?
提前致谢
【问题讨论】:
-
什么是
this.httpService? -
由封装Axios包的nestjs框架提供
标签: node.js downloadfile nodejs-stream