在结束响应之前如何输出数据?

问题描述:

这是我在Chrome 11和Firefox 4中对其进行测试的代码段:

Here is my snippet I tested it in Chrome 11, and Firefox 4:

var http = require('http');

http.createServer(function(request, response){
   // Write Headers
   response.writeHead(200);

   // Write Hello World!
   response.write("Hello World!");

   // End Response after 5 seconds
   setTimeout(function(){ 
        response.end(); 
   }, 5000);

}).listen(8000);

如您所见,我使response.end()超时,因此我可以测试response.write是否在response.end之前输出.以我的经验,虽然不是.

As you can see I timed out the response.end() so I can test if response.write is outputted before the response.end. In my experience though it is not.

是否有一种方法可以在结束响应之前输出数据,就像以数据包形式发送数据一样?

Is there a way to output the data before ending the response, something like sending the data in packets?

如果将内容类型更改为text/plain,例如:

If you change the content type to text/plain -- e.g:

// Write Headers
response.writeHead(200, {'Content-Type': 'text/plain'});

然后firefox将立即显示内容. Chrome似乎仍然可以缓冲(如果您编写更多内容,则chrome会立即显示它).

then firefox will show the content immediately. Chrome still seems to buffer (if you write a bunch more content, chrome will show it immediately).