我在主处理程序中得到了这个:
app.get('/static', (request, response) => {
const id=request.query.id;
const url=endpoint+id;
request({url:url, resolveWithFullResponse: true})
.then(result => {
//"result" is a stream response,I want all data from here goes to "response"
//"result -> "response"
})
流光代码为:
app.get('/getfile', (request, response) =>{
//get the client somewhere
client.createReadStream()
}).then(stream => {
stream.on('end', () => {
response.end();
});
stream.on('error', (error) => {
response.end();
});
stream.pipe(response);
})
})
我希望从"结果"的所有结果都转到"响应">
如果结果是流,您可以简单地使用带有响应的管道,不要忘记添加正确的文件名和内容类型:
request({url:url})
.then(result => {
const filename = 'data.json';
const contentType = 'application/json'
res.set('Content-disposition', `attachment; filename*=${filename}`); // set a filename for your response f.e. data.json
res.set(Content-Type', contentType); // set a content type f.e. application/json
result.pipe(response);
})
注意:如果附加到任意二进制文件,请使用 Content-Type=application/octet-stream