In every request, my server is receiving GET request to /favicon.ico, even when it's REST api that not include html file. Why is this happening and how can I prevent this request?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Axios OPTIONS instead of POST Request. Express Res
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- fetch: Getting cookies from fetch response
Browsers will by default try to request
/favicon.ico
from the root of a hostname, in order to show an icon in the browser tab.If you want to avoid this request returning a 404, you can either:
favicon.ico
file that is available at the root of your site.Catch the
favicon.ico
request and send a204 No Content
status:app.get('/favicon.ico', (req, res) => res.status(204));
my preferred method is middleware
put this somewhere:
then:
I agree with @Blair Anderson that middleware is the best course of action here but
204
should not return a body. Also, you may want to catch all favicon request e.g.: https://example.com/some/path/favicon.ico. In which case something like this works best:I think you mean that your server receives such a request. Usually, the browser will try to get an icon to display on the tab. Especially Chrome is very aggressive (at least that's my observation).
What you can do is properly handle the request for your HTML pages and ignore it for API calls (as they will probably normally not happen in the foreground with a browser)