Where does node search for /favicon.ico ? #52019
-
Hi all! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Hi @digimeas care to elaborate a little? 🙂 Assuming your webpage includes a link for |
Beta Was this translation helpful? Give feedback.
-
Hi @atlowChemi ! Thanks |
Beta Was this translation helpful? Give feedback.
-
This is the root route of the server. Here is an example for handling such an endpoint using const { createServer } = require('node:http');
const { join } = require('path');
const fs = require('fs');
const url = require('url');
// Location of your favicon in the filesystem.
const FAVICON = join(__dirname, 'public', 'favicon.ico');
const server = createServer(function(req, res) {
const pathname = url.parse(req.url).pathname;
// If this request is for the favicon, stream it in response.
if (req.method === 'GET' && pathname === '/favicon.ico') {
res.setHeader('Content-Type', 'image/x-icon');
fs.createReadStream(FAVICON).pipe(res);
return;
}
// This request was not asking for our favicon, handle it with further handlers.
res.end();
});
server.listen(3000); Does this answer your question? |
Beta Was this translation helpful? Give feedback.
-
I thought maybe, like apache uses /var/www/html as '/', that node might do something similar Thanks again |
Beta Was this translation helpful? Give feedback.
-
Node doesn't offer such a behavior by default 🙂 |
Beta Was this translation helpful? Give feedback.
This is the root route of the server.
Node doesn't automatically handle such an endpoint if not explicitly defined, so the endpoint would just "freeze" in pending state until the timeout. This means you need to explicitly handle this endpoint.
Here is an example for handling such an endpoint using
http.createServer