// 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/'); |
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'));
Great Solution without express! Thanks!
Thank you! Exactly what I was looking for.
Thanks... Once again.
Thanks that's what I was looking for
thanks for sharing
Great !!!!!
Nice one, thanks
God Bless you~
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
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.
@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.
Hallelujah!!!
Thanks a million. This was driving me insane
Thank you very much! ^_^
Thanks so much .. I was wondering why my page's visitor
count was increasing twice on a single request.
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. :)
Great approach. Thank you
Thank You.
Still useful, thanks good Sir!!!
Thanks much
Great man, thank you!
Exactly what i was looking for! Thanks !
nice cock! ugh ugh I mean work :)
Thanks so much! Exactly what I was looking for.