Skip to content
Create a gist now

Instantly share code, notes, and snippets.

How to short-circuit those annoying favicon requests in node.js
// early experiments with node had mysterious double requests
// turned out these were for the stoopid favicon
// here's how to short-circuit those requests
// and stop seeing 404 errors in your client console
var http = require('http');
http.createServer(function (q, r) {
// control for favicon
if (q.url === '/favicon.ico') {
r.writeHead(200, {'Content-Type': 'image/x-icon'} );
r.end();
console.log('favicon requested');
return;
}
// not the favicon? say hai
console.log('hello');
r.writeHead(200, {'Content-Type': 'text/plain'} );
r.write('Hello, world!');
r.end();
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
@shilov
shilov commented Oct 17, 2011

Simple and sweet, thanks for sharing!

@kentbrew
Owner

Glad it was useful!

@shilov
shilov commented Oct 20, 2011

In case someone stumbles upon this in the future.. If you are running nginx in front of node.js you can also block favicon requests by adding this line:

location = /favicon.ico { access_log off; log_not_found off; }
@kentbrew
Owner

Wonderful, thanks.

@jimthedev

This was very helpful. Thank you.

@leandrodeleite

Thanks!

@billyeh
billyeh commented Nov 28, 2012

Thanks. This is exactly what I was looking for. Wish there were more useful, small code snippets like this around the Net.

@bloyot
bloyot commented Feb 11, 2013

Thanks so much! Exactly what I was looking for.

@yooodooo

helpful,thanks!

@akssnsk
akssnsk commented Apr 6, 2013

Great and simple! Thank you.

@gbmhunter

Thanks for this! Chrome causes this issue.

@pavelnikolov

If you are using express you can do it like this

app.use(express.favicon(__dirname + '/public/favicon.ico'));
@flpms
flpms commented Jul 3, 2014

Great Solution without express! Thanks!

@radicalsauce

Thank you! Exactly what I was looking for. :+1:

@SarasArya

Thanks... Once again.

@SC7639
SC7639 commented Sep 24, 2015

Thanks that's what I was looking for :+1:

@jcabasc
jcabasc commented Mar 11, 2016

:+1:

@Shenglian

thanks for sharing :+1:

@ronak-8699

Great !!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.