为什么不是这个简单的节点程序非阻塞?

我已经安装了节点,正在运行一些简单的“hello world”风格程序,以便更好地掌握正在发生的事情。

我很困惑,为什么下面的代码似乎以阻塞的方式运行。 当我在5秒钟后打开我的浏览器到本地主机:8080“进程启动…”和“进程完成”。 出现在屏幕上。 我期望“stream程开始…”立即出现,然后“stream程完成”。 5秒钟之后。 任何想法,为什么超时会影响这两个代码段? 这段代码保存在一个名为“hello.js”的文件中,我只需要使用“node hello.js”来运行。

var http = require('http'); http.createServer(function(request,response) { response.writeHead(200); response.write("Process started..."); setTimeout(function() { response.write("Process complete."); response.end(); }, 5000); }).listen(8080); 

您的浏览器最有可能缓冲响应。 试着用curl( curl -N http://localhost:8080 )来代替它,你会看到不同之处。