Skip to content

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');
Copy link

bloyot commented Feb 11, 2013

Thanks so much! Exactly what I was looking for.

Copy link

yooodooo commented Mar 30, 2013


Copy link

akochepasov commented Apr 6, 2013

Great and simple! Thank you.

Copy link

gbmhunter commented Oct 2, 2013

Thanks for this! Chrome causes this issue.

Copy link

pavelnikolov commented Dec 6, 2013

If you are using express you can do it like this

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

Copy link

flpms commented Jul 3, 2014

Great Solution without express! Thanks!

Copy link

radicalsauce commented Oct 9, 2014

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

Copy link

SarasArya commented Jun 14, 2015

Thanks... Once again.

Copy link

SC7639 commented Sep 24, 2015

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

Copy link

jcabasc commented Mar 11, 2016


Copy link

Shenglian commented Apr 5, 2016

thanks for sharing πŸ‘

Copy link

ronak-8699 commented Apr 7, 2016

Great !!!!!

Copy link

kstarzyk commented May 11, 2016

Nice one, thanks

Copy link

scc0309 commented May 30, 2016

God Bless you~

Copy link

mattiaerre commented Aug 26, 2016

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

// cc @kentbrew

Copy link

damirkotoric commented May 12, 2017

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.

Copy link

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.

Copy link

Fraasi commented Nov 13, 2017


Copy link

TeamParkIt commented May 24, 2018

Thanks a million. This was driving me insane

Copy link

YaninaProtskaya commented Jul 19, 2018

Thank you very much! ^_^

Copy link

vikkrantxx7 commented Jul 30, 2018

Thanks so much .. I was wondering why my page's visitor count was increasing twice on a single request.

Copy link

varun-manjunath commented Nov 8, 2018

I read up on StackOverFlow that 204 (No Content) is a better response status than 200 (Success) for silencing the favicon problem! Just saying. good solution, I just like the semantics implied by status 204. :)

Copy link

woody180 commented Apr 13, 2019

Great approach. Thank you

Copy link

Michael-Yimam commented May 4, 2019

Thank You.

Copy link

lucierabahi commented Sep 16, 2019

πŸ‘ Thanks!

Copy link

rhernandog commented Dec 1, 2019

Still useful, thanks good Sir!!! πŸ‘ πŸ‘

Copy link

skirankumar7 commented Feb 4, 2020

Thanks much πŸ‘ πŸ‘ πŸ‘

Copy link

leomiranda commented Feb 2, 2021

Great man, thank you!

Copy link

Alik64 commented May 2, 2022

Exactly what i was looking for! Thanks ! πŸ‘

Copy link

Piggyback13 commented May 2, 2022

nice cock! ugh ugh I mean work :)

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