在结束响应之前如何输出数据? [英] How can I output data before I end the response?

查看:91
本文介绍了在结束响应之前如何输出数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在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).

这篇关于在结束响应之前如何输出数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆