-
-
Save kentbrew/763822 to your computer and use it in GitHub Desktop.
// 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/'); |
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 man
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.
π Thanks!
Still useful, thanks good Sir!!! π π
Thanks much π π π
Great man, thank you!
Exactly what i was looking for! Thanks ! π
I think the solution above don't avoid the favico requests @ least in my case! the solution that i came up with is adding an else statement in the end of my code to avoid all urls that i don't want
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url)
console.log('user hit the server')
if (req.url === '/') {
res.writeHead(200,
{
'content-type': 'text/html',
})
res.write(`<h1>home page</h1>`)
res.end()
}
else {
res.writeHead(
404,
{ 'Content-Type': 'text/html' }
)
res.write('<h2>Page not found!</h2>');
res.end()
}
})
server.listen(5000)`
Great Solution without express! Thanks!