univuniv1
univuniv1

Reputation: 63

Why does refreshing localhost send/log two requests?

I'll construct the following basic example that abstracts away a bunch of implementation details of my program to convey the core question. Let's say I have:

const http = require("http");

const server = http.createServer((req, res) => {
    console.log("request event");
    res.end("Hello World");
});
server.listen(5000, () => {
    console.log(server.listening); // true
    console.log("Server listening on port: 5000...");
});

Obviously my terminal shows Server listening on port: 5000..., but every time I refresh my browser I get two "request event" instances logged. I've had this happen a lot of times in the past and never really bothered with it, but why does this happen?

Upvotes: 0

Views: 480

Answers (3)

ak100
ak100

Reputation: 255


const server = http.createServer((req, res) => {
    console.log(req.url)
    console.log("request event");
    res.end("Hello World");
});


server.listen(5000, () => {
    console.log(server.listening); // true
    console.log("Server listening on port: 5000...");
});

One for your request and one for favicon.ico sent by default from browser

Upvotes: 0

CrackYourFace
CrackYourFace

Reputation: 21

There are always two requests that by default browser requests.

  • One is the blank HTML page request. This loads the HTML page and then gets rendered by the browser.
  • Second is the icon (Also known as favicon.ico) that you see beside a tab. Now if your page has a link to this icon, the browser will load otherwise will fail to load.

I hope this answers your question.

Upvotes: 2

h-sifat
h-sifat

Reputation: 1605

The reason is, browsers by default requests for favicon.ico. That's why you see two request event.

enter image description here

Upvotes: 2

Related Questions