【问题标题】:How do I stream data to browsers with Hapi?如何使用 Hapi 将数据流式传输到浏览器?
【发布时间】:2015-09-21 12:57:33
【问题描述】:

我正在尝试使用流通过 Hapi 将数据发送到浏览器,但不知道我们如何。具体来说,我正在使用request 模块。根据文档,reply 对象接受一个流,所以我尝试过:

reply(request.get('https://google.com'));

抛出一个错误。在文档中它说流对象必须与streams2 兼容,所以我尝试了:

reply(streams2(request.get('https://google.com')));

现在这不会引发服务器端错误,但在浏览器中请求永远不会加载(使用 chrome)。

然后我尝试了这个:

var stream = request.get('https://google.com');
stream.on('data', data => console.log(data));
reply(streams2(stream));

并且在控制台数据输出,所以我知道流不是问题,而是 Hapi。如何让 Hapi 中的流媒体工作?

【问题讨论】:

    标签: node.js request streaming hapijs


    【解决方案1】:

    尝试使用Readable.wrap

    var Readable = require('stream').Readable;
    ...
    function (request, reply) {
    
      var s = Request('http://www.google.com');
      reply(new Readable().wrap(s));
    }
    

    使用 Node 0.10.x 和 hapi 8.x.x 进行测试。在我的代码示例中,Request 是节点请求模块,request 是传入的 hapi 请求对象。

    更新

    另一种可能的解决方案是从Request 监听'response' event,然后使用http.IncomingMessage 监听reply,这是一个正确的读取流。

    function (request, reply) {
    
         Request('http://www.google.com')
         .on('response', function (response) {
            reply(response);
         });
    }
    

    这需要更少的步骤,并且还允许开发人员在传输之前将用户定义的属性附加到流中。这对于设置 200 以外的状态代码很有用。

    【讨论】:

    • hapi 17.9.0 有这个答案的版本吗?
    • 我在响应中发现了压缩方法gzip的问题。所以现在我研究如何通过flush方法绕过它或禁用
    【解决方案2】:

    2020

    我找到了!!问题是gzip 压缩

    要仅为event-stream 禁用它,您需要向Happi 服务器提供下一个配置

    const server = Hapi.server({
      port: 3000, 
      ...
      mime:{
        override:{
          'text/event-stream':{
            compressible: false
          }
        }
      }
    });
    

    在处理程序中我使用axios,因为它支持新的流2协议

    async function handler(req, h) {
        const response = await axios({
            url: `http://some/url`,
            headers: req.headers,
            responseType: 'stream'
        });
    
        return response.data.on('data',function (chunk) {
            console.log(chunk.toString());
        })
    
        /* Another option with h2o2, not fully checked */
        // return h.proxy({
        //     passThrough:true,
        //     localStatePassThrough:true,
        //     uri:`http://some/url`
        // });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 2010-09-14
      • 2012-07-02
      相关资源
      最近更新 更多