Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@shilov

shilov Oct 17, 2011

Simple and sweet, thanks for sharing!

shilov commented Oct 17, 2011

Simple and sweet, thanks for sharing!

@kentbrew

This comment has been minimized.

Show comment
Hide comment
@kentbrew

kentbrew Oct 20, 2011

Glad it was useful!

Owner

kentbrew commented Oct 20, 2011

Glad it was useful!

@shilov

This comment has been minimized.

Show comment
Hide comment
@shilov

shilov 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; }

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

This comment has been minimized.

Show comment
Hide comment
@kentbrew

kentbrew Oct 30, 2011

Wonderful, thanks.

Owner

kentbrew commented Oct 30, 2011

Wonderful, thanks.

@jimthedev

This comment has been minimized.

Show comment
Hide comment
@jimthedev

jimthedev Oct 16, 2012

This was very helpful. Thank you.

This was very helpful. Thank you.

@leandrodeleite

This comment has been minimized.

Show comment
Hide comment
@leandrodeleite

leandrodeleite Oct 28, 2012

Thanks!

Thanks!

@billyeh

This comment has been minimized.

Show comment
Hide comment
@billyeh

billyeh 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.

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

This comment has been minimized.

Show comment
Hide comment
@bloyot

bloyot Feb 11, 2013

Thanks so much! Exactly what I was looking for.

bloyot commented Feb 11, 2013

Thanks so much! Exactly what I was looking for.

@yooodooo

This comment has been minimized.

Show comment
Hide comment
@yooodooo

yooodooo Mar 30, 2013

helpful,thanks!

helpful,thanks!

@akochepasov

This comment has been minimized.

Show comment
Hide comment
@akochepasov

akochepasov Apr 6, 2013

Great and simple! Thank you.

Great and simple! Thank you.

@gbmhunter

This comment has been minimized.

Show comment
Hide comment
@gbmhunter

gbmhunter Oct 2, 2013

Thanks for this! Chrome causes this issue.

Thanks for this! Chrome causes this issue.

@pavelnikolov

This comment has been minimized.

Show comment
Hide comment
@pavelnikolov

pavelnikolov Dec 6, 2013

If you are using express you can do it like this

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

If you are using express you can do it like this

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

This comment has been minimized.

Show comment
Hide comment
@flpms

flpms Jul 3, 2014

Great Solution without express! Thanks!

flpms commented Jul 3, 2014

Great Solution without express! Thanks!

@radicalsauce

This comment has been minimized.

Show comment
Hide comment
@radicalsauce

radicalsauce Oct 9, 2014

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

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

@SarasArya

This comment has been minimized.

Show comment
Hide comment
@SarasArya

SarasArya Jun 14, 2015

Thanks... Once again.

Thanks... Once again.

@SC7639

This comment has been minimized.

Show comment
Hide comment
@SC7639

SC7639 Sep 24, 2015

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

SC7639 commented Sep 24, 2015

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

@jcabasc

This comment has been minimized.

Show comment
Hide comment
@jcabasc

jcabasc Mar 11, 2016

๐Ÿ‘

jcabasc commented Mar 11, 2016

๐Ÿ‘

@Shenglian

This comment has been minimized.

Show comment
Hide comment
@Shenglian

Shenglian Apr 5, 2016

thanks for sharing ๐Ÿ‘

thanks for sharing ๐Ÿ‘

@ronak-8699

This comment has been minimized.

Show comment
Hide comment
@ronak-8699

ronak-8699 Apr 7, 2016

Great !!!!!

Great !!!!!

@kstarzyk

This comment has been minimized.

Show comment
Hide comment
@kstarzyk

kstarzyk May 11, 2016

Nice one, thanks

Nice one, thanks

@scc0309

This comment has been minimized.

Show comment
Hide comment
@scc0309

scc0309 May 30, 2016

God Bless you~

scc0309 commented May 30, 2016

God Bless you~

@mattiaerre

This comment has been minimized.

Show comment
Hide comment
@mattiaerre

mattiaerre Aug 26, 2016

if you are interested I made and express middleware based on this gist; you can find it here: https://github.com/mattiaerre/express-favicon-short-circuit

// cc @kentbrew

if you are interested I made and express middleware based on this gist; you can find it here: https://github.com/mattiaerre/express-favicon-short-circuit

// cc @kentbrew

@damirkotoric

This comment has been minimized.

Show comment
Hide comment
@damirkotoric

damirkotoric 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.

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

This comment has been minimized.

Show comment
Hide comment
@DivideBySara

DivideBySara 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 stackoverflow.com, so I am on the hunt for answers to this favicon problem. This is the project I'm working on.

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 stackoverflow.com, so I am on the hunt for answers to this favicon problem. This is the project I'm working on.

@Fraasi

This comment has been minimized.

Show comment
Hide comment
@Fraasi

Fraasi Nov 13, 2017

Hallelujah!!!

Fraasi commented Nov 13, 2017

Hallelujah!!!

@Awais-cb

This comment has been minimized.

Show comment
Hide comment
@Awais-cb

Awais-cb Dec 3, 2017

thanks man

Awais-cb commented Dec 3, 2017

thanks man

@TeamParkIt

This comment has been minimized.

Show comment
Hide comment
@TeamParkIt

TeamParkIt May 24, 2018

Thanks a million. This was driving me insane

Thanks a million. This was driving me insane

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