tones31
tones31

Reputation: 167

Why does Chrome send so many HTTP requests?

I am running a barebones Nodejs server only using the HTTP module. I've created an HTTP server and am listening on socket connections and on requests. I noticed that when I use chrome and go to localhost, three sockets connect, and two requests are made to "/". I know that, using some other webservers, I've seen Chrome request the same thing multiple times if it does not receive a quick response (about 5 seconds), but I am sending a response right away and still Chrome is connecting/requesting multiple times.

Is this expected, and if it is, should I be expected to handle duplicate requests?

My relevant code

    let server = http.createServer();
    server.listen({
        host: host,
        port: port
    });
    server.on('connection', function(socket){
         // gets printed 3 times
         console.log('connection')
    });
    server.on('request', function(request, response){
        // gets printed two times
        console.log('hi')
        // yet chrome only receives one response (seemingly)
        response.end('hi')
    });

Edit: Half solved. Now I am printing request.url and I see / and favicon.ico

So there are 2 requests, but still 3 socket connections. I guess every single request is on a new socket?

Upvotes: 0

Views: 278

Answers (2)

gaurav deep
gaurav deep

Reputation: 1

It typically makes 2 request and one preconnection

  1. GET /
  2. GET /favicon.ico
  3. In the third it makes a connection in advance without sending anything to the server, I presume this is because if the html is parsed it can get the file much more quickly because it already has the connection to the server, now it can send the request to get the file

The third request has a time out of 1 min

I observed this behavior when I was building my own http server in c++

Upvotes: 0

Mithun Shreevatsa
Mithun Shreevatsa

Reputation: 3619

All individual images, css and javascript will definitely make http requests. No doubt about it.

Upvotes: 1

Related Questions