I know that with Node.js you can stream data.
But why doesn’t this code work:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("hello");
res.write("world");
}).listen(80);
Seems that I have to have res.end() after the last res.write() to be able to send the data to the browser.
Actually, it seems more like the output has to be a certain size before the browser renders it. Probably the buffer at the browser end. When you send
res.end()the browser flushes this buffer and renders all that’s in the buffer straightaway. However, if nores.end()is send, the browser awaits till its buffer is filled.I tried experimenting the following block of code with Firefox and curl.
You can try it yourself by heading to http://localhost:3000/ after running node with the above code.
I then tried the same thing with curl, and every line appeared straightaway as it was sent from the server.
The curl command I used was simply:
Note the newlines I inserted in the res.write as curl will wait for the newlines before outputting to stdout.