0

I'm playing around with node.js sample demo with HTTP Streams (Content-Type: chunked).

var http = require('http');

var server = http.Server(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});

  setInterval(function() {
     res.write('world\n');
  },2000);

  res.write('hello ');
});

server.listen(3000);

Now when I use chrome to view the page, it just times out and I never get to see anything on the screen. Whereas using cURL seems to show me the contents as it is received.

$ curl localhost:3000
hello world
world
world
world

Is this the browser default behavior where it won't show anything unless it has complete data? Seems like a waste to throw away the data and show a timeout error.

1
  • if I remove the '\n' during write then cURL also doesn't seem to display the data, more confusion - so cURL is flushing based on new lines? Commented Mar 24, 2014 at 4:46

1 Answer 1

0

You need to call res.end in order to see the output in the browser.

Try, for example, adding this after res.write('hello'):

setTimeout(function(){res.end();}, 5000);

After five seconds you'll see something like:

Hello world
world

My guess is that cURL shows output right away because it doesn't care about the content type, whereas the browser, which does care, may want more of the response before it shows anything.

You can also try streaming the response by changing the transfer encoding; check out this question for more info on that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.