简体   繁体   English

node.js经典示例奇怪的行为

[英]node.js classic example strange behavior

there is classic node example: 有一个经典的节点示例:

var http = require('http');
var s = http.createServer(function(req, res){
    res.writeHead(200, {'content-type':'text/plain'});
    res.write('hi there\n');
    setTimeout(function(){
        res.end('and here');
    },2000);
});
s.listen(8000);

and when i run 当我跑步时

curl http://127.0.0.1:8000

its going fine, "hi there" and over 2 sec - "and here". 一切正常,“嗨,在那里”,并超过2秒-“和这里”。 ok. 好。

but when i change the string#4 and remove \\n: 但是当我更改字符串#4并删除\\ n时:

 res.write('hi there');

i see delay (2sec) and simultaneously output without any timeout between "hi there" and "and here" 我看到延迟(2秒),并且同时在“ hi there”和“ and here”之间没有任何超时地输出

node -v = 4.2.6 节点-v = 4.2.6

this is good, this is bad or this is unimportant? 这是好事,这是坏事还是不重要?

It seems to be browser specific behavior. 似乎是浏览器特定的行为。 firefox shows the data immediately while chrome seems to buffer and wait until the response is ended. 在chrome似乎缓冲并等待响应结束之前,firefox会立即显示数据。

答案似乎在注释中: it's a line-wise buffer, a buffer that flushes when it sees a line terminator.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM