从流响应中读取并将其写入另一个响应 Node js
read from a stream response and write it to another response Node js
我在主要处理程序中得到了这个:
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);
})
})
我希望 "result" 的所有结果都转到 "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
我在主要处理程序中得到了这个:
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);
})
})
我希望 "result" 的所有结果都转到 "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