使用 Express/Node.js 的 favicon 中间件有什么好处?

What is the advantage of using favicon middleware for Express / Node.js?

我想在我的网站上添加一个网站图标(用 JavaScript 和 Node.JS & Express 编写)。互联网上的资源指向使用 favicon-serve 中间件。 Express 生成器也包含这个中间件。

目前我正在使用以下代码行提供静态文件。

app.use(express.static('./public'));

所以我将 favicon.ico 放入 /public 文件夹中,favicon 正常工作(不使用 favicon-serve 中间件)。

所以我的问题是:为什么要使用 favicon 中间件?为什么不把它和其他静态资源 (.css / .png / ...) 一起放在 public 文件夹中?

编辑:我收到了一些反对票,抱歉,如果这个 "RTM" 问题太多了,但作为初学者(在 JavaScript / Node.js 中)很容易迷失在外部模块中。很难为 Node.js 找到 "install this module, learn to use this module, done" 之外的资源 - 作为初学者,我经常想知道有人如何在没有另一个外部模块的情况下解决问题,即使您最终还是使用了该模块。

the documentation for the module回答了这个问题:

  • User agents request favicon.ico frequently and indiscriminately, so you may wish to exclude these requests from your logs by using this middleware before your logger middleware.
  • This module caches the icon in memory to improve performance by skipping disk access.
  • This module provides an ETag based on the contents of the icon, rather than file system properties.
  • This module will serve with the most compatible Content-Type.