Here is my snippet I tested it in Chrome 11, and Firefox 4:
var http = require('http'); http.createServer(function(request, response){ // Write Headers response.writeHead(200); // Write Hello World! response.write("Hello World!"); // End Response after 5 seconds setTimeout(function(){ response.end(); }, 5000); }).listen(8000);
As you can see I timed out the response.end()
so I can test if response.write
is outputted before the response.end
. In my experience though it is not.
Is there a way to output the data before ending the response, something like sending the data in packets?
There actually is a way that you can do this without setting Content-Type: text/plain
and still use text/html
as the Content-Type
, but you need to tell the browser to expect chunks of data.
This can be done easily like this:
var http = require('http'); http.createServer(function(request, response) { response.setHeader('Connection', 'Transfer-Encoding'); response.setHeader('Content-Type', 'text/html; charset=utf-8'); response.setHeader('Transfer-Encoding', 'chunked'); response.write('hello'); setTimeout(function() { response.write(' world!'); response.end(); }, 10000); }).listen(8888);
You should be aware though, that until response.end()
is called the request is still taking place and blocking other requests to your nodejs server.
You can easily test this by opening calling this page (localhost:8888) on two different tabs. One of them will wait 10 seconds, and the other will only get the beginning of the response once the first response ends (meaning you'll wait 10 seconds for the beginning of the response and another 10 seconds till the end of the response, using this code).
You can probably pass this obstacle too by running a couple of nodejs processes and load balancing between them, but then this begins to get much more complicated, and is a thread that should be taken else where... :)
If you change the content type to text/plain -- e.g:
// Write Headers response.writeHead(200, {'Content-Type': 'text/plain'});
then firefox will show the content immediately. Chrome still seems to buffer (if you write a bunch more content, chrome will show it immediately).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With