Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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'} );
console.log('favicon requested');
// not the favicon? say hai
r.writeHead(200, {'Content-Type': 'text/plain'} );
r.write('Hello, world!');
console.log('Server running at');

shilov commented Oct 17, 2011

Simple and sweet, thanks for sharing!


kentbrew commented Oct 20, 2011

Glad it was useful!

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 commented Oct 30, 2011

Wonderful, thanks.

This was very helpful. Thank you.


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 commented Feb 11, 2013

Thanks so much! Exactly what I was looking for.


Great and simple! Thank you.

Thanks for this! Chrome causes this issue.

If you are using express you can do it like this

app.use(express.favicon(__dirname + '/public/favicon.ico'));

flpms commented Jul 3, 2014

Great Solution without express! Thanks!

Thank you! Exactly what I was looking for. πŸ‘

Thanks... Once again.

SC7639 commented Sep 24, 2015

Thanks that's what I was looking for πŸ‘

jcabasc commented Mar 11, 2016


thanks for sharing πŸ‘

Great !!!!!

Nice one, thanks

scc0309 commented May 30, 2016

God Bless you~

if you are interested I made and express middleware based on this gist; you can find it here:

// cc @kentbrew

Worth noting that this only works if you call return form the main app.js file. I put the above code in a separate router.js file, and it didn't work as expected.

DivideBySara commented Jul 29, 2017

@damirkotoric Where would I call return from my server.js file? I'm new to backend Node.js. Installing @mattiaerre 's express-favicon-short-circuit middleware did not help, and I've already posted to, so I am on the hunt for answers to this favicon problem. This is the project I'm working on.

Fraasi commented Nov 13, 2017


Awais-cb commented Dec 3, 2017

thanks man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment