6

Note: this is not a replicated post for those about settimeout, the key answer here is browser design options.

I am starting study node.js: A simple example to test async:

var http=require('http');

http.createServer(
    function(request, response){


        response.writeHead(200);
        response.write("Hello, dog is running");
        setTimeout(
            function(){
                response.write("Dog is done");
                response.end();
            },
            10000
        );

    }
).listen(8080);
console.log("Listen on port 8080") 

One interesting thing is its behavior is differernt when in command lind with curl and in browser: In Ubuntu 12.10, I use curl localhost:8080 in two consoles, they response in almost same 10 sends.

However, I open two browsers, make the request at almost same time, but the whole procedure took me 20 seconds?

thanks.

4
  • possible duplicate of Nodejs one request blocks another requests Commented Apr 6, 2013 at 14:21
  • @MattBall, I don't think it's a duplicate. This one doesn't do CPU intensive processing. Also, it works correctly using CLI (curl) for me, but not in Firefox. Commented Apr 6, 2013 at 14:25
  • Looks like browser isn't sending the second request until the first one ends. If you add a log statement to the start of the handler, it'll demonstrate this. Commented Apr 6, 2013 at 14:29
  • I edit the post because of new finding, it is differernt when use console with command curl and in browsers. Guys, please help me. Commented Apr 6, 2013 at 15:29

1 Answer 1

19

It's the browser waiting, not node.js

If you run the server and request http://localhost:8080/ in two tabs it takes 20 seconds because the browser waits for the first request to the same url before starting the second.

If you run the server and request http://localhost:8080/1 and http://localhost:8080/2 in two tabs it takes 10 seconds again.

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

2 Comments

Can you tell me more about how the browser works? Why they can not open the same url with tabs at the same time?
Chrome does not issue any requests simultaneously to the same URL, it is an implementation choice.

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.