【发布时间】:2013-06-30 16:51:02
【问题描述】:
我是 node.js 的初学者,我对 setTimeout(function(){.....},time); thingo 有一些问题。当我卷曲本地服务器但在谷歌浏览器中不符合预期时,这工作正常(我尚未测试其他浏览器)
我的代码是:
/* simple node.js createserver */
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200);
response.write('Somthing\r\n');
setTimeout(function(){
response.end('\n This came out after 5 seconds :) ');
}, 5000);
response.write('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
当我 curl 127.0.0.1:8124 时,一切都按预期工作。但是当我在浏览器中指出它时,它会保持空闲一段时间(我猜是 5 秒)并一次显示所有内容?这是 node.js 的预期行为还是我遗漏了什么?浏览器可以像 curl 那样做(即先打印两行,等待 5 秒再打印另一行)吗?
【问题讨论】:
-
Possible solution 是添加
Transfer-Encoding: chunked -
那没有解决:(
标签: node.js