Waterfr Villa Waterfr Villa - 3 years ago 218
Node.js Question

Why the callback function gets called twice for each request?

Guys I am new to node js and this behavior is strange to me!
In the code snippet below,

'use strict';
var http = require('http');
var numberOfRequests = 0;
http.createServer(function (request, responce) {
console.log('Request number ' + numberOfRequests + ' received!');
responce.write("Here is responce to your request..");
console.log('listening ...');

for each


call at Chrome, the app writes twice onto console? e.i
for a single 8080 call, it prints out:

Request number 0 received!
Request number 1 received!

I am using Visual studio to run this node js app.

Answer Source

Usually, when you see two requests for each page request, one is for the desired page and one is for the favicon for the website. This is what most browsers do unless there is meta tag in the page that tells the browser not to request a favicon resource. If you do this in your handler:


That will likely show you what's going on. In general, you don't want to have a web server where you never look at what resource is being requested. If you based your logic on a specific resource being requested such as /, then you would easily be able to ignore other types of requests such as the favicon.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download