提问者:小点点

在res. end被调用(分块)之前,Node.js不能res.write


这是书中的一个例子:“Professional Node. js”。它应该每秒输出一个时间戳,持续3s。但是在调用res.end()之前没有任何东西出现:(页面加载而3s,在所有出现在屏幕上之前…)

require('http').createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    var left = 3;
    var interval = setInterval(function () {
        res.write(Date.now() + " ");

        left -= 1;
        if (left === 0) {
            clearInterval(interval);
            res.end();
        }
    }, 1000);
}).listen(4000);

你也一样吗?

[编辑:]我发现这个简单的Node. js服务器以分块传输编码响应,它也不适合我…(end()call~5s后显示所有内容)


共1个答案

匿名用户

对我来说很好。所以这取决于你如何测试它,因为浏览器、旋度和类似的东西会缓冲东西。

GET / HTTP/1.1

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Fri, 27 Sep 2013 11:21:22 GMT
Connection: keep-alive
Transfer-Encoding: chunked

e
1380280883375 
e
1380280884378 
e
1380280885380 
0