Express JS阻止GET/favicon.ico [英] Express js prevent GET /favicon.ico
本文介绍了Express JS阻止GET/favicon.ico的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在每个请求中,即使是不包含html文件的REST API,我的服务器也正在接收对/favicon.ico的GET请求.为什么会发生这种情况,如何阻止该请求?
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?
推荐答案
浏览器默认情况下将尝试从主机名的根请求/favicon.ico
,以便在浏览器选项卡中显示图标.
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.
如果您想避免此请求返回404,则可以:
If you want to avoid this request returning a 404, you can either:
- 提供一个
favicon.ico
文件,该文件在您网站的根目录中可用. - 使用诸如 serve-favicon 之类的模块将请求指向特定文件. li>
-
捕获
favicon.ico
请求并发送204 No Content
状态:
- Supply a
favicon.ico
file that is available at the root of your site. - Use a module such as serve-favicon to point requests to a specific file.
Catch the
favicon.ico
request and send a204 No Content
status:
app.get('/favicon.ico', (req, res) => res.status(204));
这篇关于Express JS阻止GET/favicon.ico的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文