I am experimenting with various responses from a simple NodeJS HTTP server.
The effect I am trying to achieve is faster visual rendering of a web page. Since the response is streamed to the browser with transfer-encoding: chunked
(right?) I was thinking I could render the page layout first and the rest of the data after a delay.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
, 'Transfer-Encoding': 'chunked'
});
res.write('<html>
');
res.write('<body>
');
res.write('hello ');
res.write('</body>
');
res.write('</html>
');
setTimeout(function () {
res.end('world');
},1500);
}).listen(3000, '127.0.0.1');
The thing is that it seems as if the response isn't sent until res.end('world')
unless the already written data is long enough, so for instanceres.write(new Array(2000).join('1'))
instead of thatres.write('hello')
, would do the trick.
Is Node buffering my writes until the data is sizable enough to be sent? If that is the case, is the chunk size configurable?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…