我正在尝试来自一个简单的 NodeJS HTTP 服务器的各种响应。我想要达到的效果是更快的网页视觉渲染。由于响应通过(对吗?)流式传输到浏览器,transfer-encoding: chunked
我想我可以先渲染页面布局,然后延迟渲染其余数据。
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
, 'Transfer-Encoding': 'chunked'
});
res.write('<html>\n');
res.write('<body>\n');
res.write('hello ');
res.write('</body>\n');
res.write('</html>\n');
setTimeout(function () {
res.end('world');
},1500);
}).listen(3000, '127.0.0.1');
res.end('world')
问题是,除非已经写入的数据足够长,否则似乎直到没有发送响应,所以例如res.write(new Array(2000).join('1'))
代替 that res.write('hello')
,就可以了。
Node 是否会缓冲我的写入,直到数据足够大才能发送?如果是这种情况,块大小是否可配置?