4

I've installed node and am running some simple 'hello world' style programs to better grasp what's going on.

I'm confused as to why the following code seems to run in a blocking fashion. When I open my browser to localhost:8080 after 5 seconds both "Process started..." and "Process complete." appear on the screen. I would expect "Process started..." to appear immediately and then "Process complete." to follow 5 seconds later. Any ideas as to why the timeout effects both pieces of this code? This code is saved in a file called 'hello.js' that I simply run with 'node hello.js'.

var http = require('http');

http.createServer(function(request,response) {
    response.writeHead(200);
    response.write("Process started...");
    setTimeout(function() {
        response.write("Process complete.");
        response.end();
    }, 5000);
}).listen(8080);
MattDionis
  • 3,534
  • 10
  • 51
  • 105

1 Answers1

3

Your browser is most likely buffering the response. Try hitting it with curl (curl -N http://localhost:8080) instead and you'll see the difference.

mscdex
  • 104,356
  • 15
  • 192
  • 153