http - Stream with Node.js doesn't work -


I know that with Node.js you can stream data.

But why this code does not work:

  var sys = require ('sys'), http = requirement ('http'); Http.createServer (function (req, res) {res.writeHead (200, {'content-type': 'text / html'}); res.write ("hello"); res.write ("world"); .}) Listen (80);  

It looks like I have res.end () after the last res.write () so that the browser can send data.

Basically, it looks like a browser should have a certain size before presenting the browser. Maybe buffer at the end of the browser when you send res.end () , then the browser flushes this buffer and sends it directly to buffer. However, if someone sends res.end () , the browser is waiting until its buffer is full.

I tried to use the following block of code with Firefox and Curl.

  var sys = require ('sys'), http = need ('http'); Http.createServer (function (req, res) {res.writeHead (200, {'content-type': 'text / html'}); res.write ("hello \ n"); (var i = 0; I & Lt; 100; i ++) {setTimeout (function () {res.write ("many languages ​​too many text \ n");}, 400 * i);}}). Listen (3000);  

You can try yourself by going to the top after running the node with the above code.

Then I tried to do the same thing with the cue, and every line was visible directly from the server.

The curl command I used was just:

  curl http: // localhost: 3000 /  

Note that I In the underlined cast, new lines are written, likewise will wait for the curl before output to the CRL.


Comments