Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

How to short-circuit those annoying favicon requests in node.js

View favicon-interceptor.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// 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/');

Simple and sweet, thanks for sharing!

Owner

Glad it was useful!

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

Wonderful, thanks.

This was very helpful. Thank you.

Thanks!

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

Thanks so much! Exactly what I was looking for.

helpful,thanks!

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

Great Solution without express! Thanks!

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

Thanks... Once again.

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.