jeudi 26 juin 2014

Why isn't this simple Node program non-blocking?


Vote count:

0




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);


asked 42 secs ago






Aucun commentaire:

Enregistrer un commentaire