问题描述
当我出于某种原因尝试利用http流连接时,直到我调用时才会刷新写入response.end()
我直接从演示中获取代码,不明白我的问题是什么.
当我卷曲到服务器时,标题是正确的.
When I try to utilize http stream connection for some reason write does not flush until I callresponse.end()
I am taking the code straight from the demo and do not understand what my problem is.
When I curl to the server my headers are correct.
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello');
res.write(':');
setTimeout(function(){
res.end('World\n')},
2000);
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
服务器为什么不发送写数据?
Why is the server not sending the write data?
推荐答案
我似乎是浏览器特有的行为-Firefox会立即显示数据("Hello:"),而chrome似乎会缓冲并等待响应结束.请注意,如果您一开始写入更多数据(例如,我写了1000个"Hello"),chrome也会立即显示数据.
I seems to be browser specific behavior -- firefox shows the data ("Hello:") immediately while chrome seems to buffer and wait until the response is ended. Note that chrome also shows the data immediately if you write more data at first (e.g. I wrote 1000 "Hello"s).
这篇关于Node Js的response.write问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!