Question

Checking the simple code

var http  = require('http');
var server = http.createServer(function(req, res){
    console.log("Got Request");
    res.end("");
});

When I am sending request to server using Firefox 8.0.1, I am geting console output once

Got Request

Using Chrome 16.0

Got Request
Got Request

why createServer is running 2 times on chrome??? is it a bug or something wrong with my code?

Was it helpful?

Solution

Browsers may submit addtional requests to the site, in which the most notable one is favicon.ico. Its purpose is to get the favicon for the site. And some plugins will also make additional requests. To make clear exactly what is being requested, you may print the url for the requests:

var http  = require('http');
var server = http.createServer(function(req, res){
    console.log(req.url); // <<<<<<<<<<<<<<<<<<<<<<<<<<<< print the requested url
    res.end("");
});
server.listen(8000)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top