-2

UPDATE: Even though another request is being sent for favicon.ico and function is getting executed again (two console.log(data) outputs), why isn't the response object being sent twice?

Also, is there a way to not send a request for favcion.ico as I wouldn't want my function to uselessly run twice?


I have created a very simple servlet to accept a GET (default) and return the contents to the browser after reading a file

var http = require("http");
var fs = require("fs");
var num=0;

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
var data = '';
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');

// Set the encoding to be utf8. 
readerStream.setEncoding('UTF8');

// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
   data += chunk;
});

readerStream.on('end',function(){
console.log(data); //To print same file content in the console
response.end(data);
});

readerStream.on('error', function(err){
   console.log(err.stack);
   data+="\n\n"+err.stack;
});


}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

But every time I run this servlet and make a request at http://localhost:/8081, I get the content of the file printed twice on the console but only once at the browser window (this is the expected behaviour).

What is the cause for the console behaviour?

Also, I modified the program by adding a counter to count how many times the function is run for every request (appended the value as data+num for both console and response) and it was found to be two but the response from the first execution of the function was the one received by the browser, not the second one(browser window printed data+1 for the first ever request).

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66

3 Answers3

4

As pointed out by @sForSujit, there are apparently two requests made for every page that the browser wants as a response - the other being a file called favicon.ico which is the icon file next to the title of your page displayed on a browser tab.

That leads to two requests also being caught by the server which again makes the file read operation and prints it out on the console the second time. The response object also made the second time is rendered useless at the client side as it is looking for your favicon.ico file as the response object.

How is it fetched?

The browser tries to obtain that icon by first requesting for "favicon.ico" from the directory of your web page. If it cannot find such a file, it will try to obtain it from the root directory of your website, failing which, it will simply use a default icon.

Chrome searches for a favicon.ico file in the root directory.

Firefox needs this on each page:

<link rel="icon" type="image/png" href="/favicon.png" />

Most, if not all, modern browsers also have the ability to use the favicon.ico file. In fact, many of them also accept the file in other graphics formats, such as the PNG format.

But, this again, needs to be set for every webpage of your website where you'd want your custom favicon to be displayed. To know how to take care of that, read the SO answer here


How to avoid the favico.ico request?

From vog's answer,

The following solution is very short, valid HTML5 and does not incur any quirks from IE 8 and older.

Just add the following line to the section of your HTML file:

<link rel="icon" href="data:,">
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
1

Most browsers make a call to grab /favicon.ico for example.

Try to log the url and you can see what's being called.

console.log(req.url);
sForSujit
  • 987
  • 1
  • 10
  • 24
  • Also, from the browser's network tab, you can see the call that's being made to favicon.ico. – Jeremy Thille Jul 05 '17 at 06:43
  • @JeremyThille Yes.& And thanks for making it more clear – sForSujit Jul 05 '17 at 06:43
  • I don't understand. Obviously, my function is getting executed twice (as the readerStream's `on` event's function is being called) as the `console.log(data)` is being printed twice. Why isn't the request also being sent twice from `response.end` as it is in the same function?? – Aakash Verma Jul 05 '17 at 06:51
  • And also how do I make my browser not do that? – Aakash Verma Jul 05 '17 at 06:58
0

I found this effective URL link to get rid of favicon errors.

<link rel="icon" href="data:;base64,iVBORw0KGgo=">

Hope you find it useful.

Raj Kanchan
  • 450
  • 6
  • 11