javascript - Node.js 经典示例奇怪的行为

标签 javascript node.js

有经典的 Node 示例:

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);

当我运行时

curl http://127.0.0.1:8000

一切顺利,“嗨,那里”和超过 2 秒的时间 - “还有这里”。好的。

但是当我更改 string#4 并删除\n 时:

 res.write('hi there');

我看到延迟(2秒)和同时输出,在“hi there”和“and here”之间没有任何超时

Node -v = 4.2.6

这很好,这很糟糕,或者这不重要?

最佳答案

这似乎是浏览器特定的行为。 firefox 立即显示数据,而 chrome 似乎会缓冲并等待响应结束。

关于javascript - Node.js 经典示例奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179366/

相关文章:

javascript - Electron 收据热敏打印机

javascript - 需要使用 moment js 的日期帮助(2015-06-02T00 :00:00Z)

node.js - 检索音频 - 二进制文件 - 存储在我的 Mlab 中

node.js - 如何捆绑 "express"node.js 应用程序? (使用网页包)

mysql - Sequelize 关联出现错误

javascript - 使用 JavaScript/JQuery 应用 css 值

javascript - 类型错误 : Cannot read property currentTime of undefined

mysql - 如何使用 Node 的 Sequelize 更新记录?

javascript - Node.js 和 Express.js 中通过向前端请求数据来创建动态链接

javascript - imageButton 的服务器标记格式不正确