Why does this simple Node program not block?

I installed node and run some simple hello world programs to better understand what is going on.

I am confused why the following code seems to work in blocking mode. When I open my browser on localhost: 8080 after 5 seconds, both "Process started ..." and "Process completed" are started. appear on the screen. I would expect the "Process started ..." to appear immediately, and then "Process completed." after 5 seconds. Any ideas as to why the timeout affects both pieces of this code? This code is saved in a file called "hello.js", which I just run using "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);
+4
1

, , . curl (curl -N http://localhost:8080), .

+3

Source: https://habr.com/ru/post/1546228/


All Articles