SOuřaan Gřg
SOuřaan Gřg

Reputation: 399

Node.js created server makes 3 request on first page load and 2 request after each page refresh?

I have created a server by help of node.js like :

var http = require('http'); //http comes build into the node

var server = http.createServer(function(request, response){
console.log('got a request!');
response.write('Hello sanjit!');
response.end();
});
server.listen(3000);

I run my server from command promote like:

c:\nodejs\node module1 

nodejs is my main folder where everything lies. module1 is that file which has my code. Now to know it works fine in every page load there should have 1 request at a time but I don't know why in my case when I first load localhost:3000 it gives me 2 requests as it logs got a request.

I know something isn't right there. I mean if I was gone right way then I would have seen 1 request in first load of localhost:3000 and after then also 1 request on every single page load. That's not happening with me and I'm confused.

I am using Chrome.

Again I wanna say that after I run my server I get 2 requests on first load of localhost:3000 and after that 2 requests on every single page load on Chrome.

In Firefox things get little different. In first load localhost:3000 I get 2 requests and after that in every single page load it works fine and I get only 1 request at every single page load.

What might be the problem?
How can I make it request 1 request at one page load?

Upvotes: 0

Views: 182

Answers (2)

Miguel Valentine
Miguel Valentine

Reputation: 35

When you visit a page with chrome.

It will try to get /favecon.ico.

and you will have two request

/
/favecon.ico

Upvotes: 1

Andrius
Andrius

Reputation: 5939

This is intended behaviour. The browser actually does multiple requests by default.

The second request is usually a favicon.ico request. You can log which URL was called using console.log(request.url);

To make it work as you want it, you will want to work with a router. This could be one of your options. Then you have all the controls of all the URLs that the browser requests.

Upvotes: 1

Related Questions