早期使用Node.js和Express刷新

早期使用Node.js和Express刷新

问题描述:

我如何实现Express的早期刷新(成块的传输编码)?

How do I realise early flush (chuncked transfer encoding) with Express?

我发现的所有示例都在处理http模块,您可以在其中调用响应对象的write()方法,并以这种方式分段发送数据.

All examples I have found are dealing with the http module, where you can call the write() method of the response object and that way send data piece-wise.

您仍然可以将write与Express一起使用:

You can still use write with Express:

app.get('/test', function(req, res) {
  var count     = 0;
  var interval  = setInterval(function() {
    if (count++ === 5) {
      clearInterval(interval);
      res.end();
      return;
    }
    res.write('This is line #' + count + '\n');
  }, 1000);
});