简体   繁体   English

对Express / Node.js使用favicon中间件有什么优势?

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

I want to add a favicon to my website (written in JavaScript with Node.JS & Express). 我想在我的网站上添加一个图标(使用Node.JS&Express用JavaScript编写)。 Resources on the internet point towards using the favicon-serve middleware. 互联网上的资源指向使用favicon-serv中间件。 The Express generator also includes this middleware. Express生成器还包括此中间件。

At the moment I'm serving static files with the following line of code. 目前,我正在使用以下代码行提供静态文件。

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

So I dropped favicon.ico in the /public folder and the favicon works like it should (without using the favicon-serve middleware). 因此,我将favicon.ico放到/public文件夹中,并且favicon可以正常工作(不使用favicon-serv中间件)。

So my question is: why would one want to use the favicon-middleware? 所以我的问题是:为什么要使用Favicon中间件? Why not just put it in a public folder with other static resources (.css / .png / ...)? 为什么不将其与其他静态资源(.css / .png / ...)放在公用文件夹中?

EDIT: I'm getting some downvotes, sorry if this is too much of a "RTM"-question, but as a beginner (in JavaScript / Node.js) it's easy to get lost in the external modules. 编辑:我有些反对意见,对不起,如果这是过多的“ RTM”问题,但是作为一个初学者(在JavaScript / Node.js中),它很容易在外部模块中迷失方向。 It's hard to find resources for Node.js that aren't "install this module, learn to use this module, done" - as a beginner I often want know how someone would solve a problem without another external module, even if you end up using the module anyway. 很难找到不是“安装此模块,学习使用该模块,完成”的Node.js资源-作为初学者,我经常想知道如果没有其他外部模块,即使没有结果,也可以解决问题无论如何都使用模块。

This is answered by 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. 用户代理经常且不加区分地请求favicon.ico,因此您可能希望通过在记录器中间件之前使用此中间件,将这些请求从日志中排除。
  • 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. 该模块基于图标的内容而不是文件系统属性提供一个ETag。
  • This module will serve with the most compatible Content-Type. 该模块将使用最兼容的Content-Type。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM