代码之家  ›  专栏  ›  技术社区  ›  John Balvin Arias

读取流响应并将其写入另一个响应节点

  •  0
  • John Balvin Arias  · 技术社区  · 7 年前

    我在主处理器中得到了这个:

    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);
            })
    })
    

    我希望“结果”中的所有结果都转到“响应”

    1 回复  |  直到 7 年前
        1
  •  2
  •   NicoNing    5 年前

    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

    推荐文章