🌐 Nodejs.cn

serve-favicon 中间件

用于提供 favicon 的 Node.js 中间件。

🌐 Node.js middleware for serving a favicon.

网站图标是客户端软件(如浏览器)用来识别网站的视觉提示。要了解示例和更多信息,请访问关于网站图标的维基百科文章

🌐 A favicon is a visual cue that client software, like browsers, use to identify a site. For an example and more information, please visit the Wikipedia article on favicons.

为什么使用这个模块?

🌐 Why use this module?

  • 用户代理频繁且无差别地请求 favicon.ico,因此你可能希望在记录器中间件之前使用此中间件来排除这些请求。
  • 该模块将图标缓存到内存中,通过跳过磁盘访问来提高性能。
  • 此模块提供一个基于图标内容的 ETag,而不是基于文件系统属性。
  • 此模块将与最兼容的 Content-Type 一起使用。

注意 本模块专门用于提供“默认的、隐式的 favicon”,即 GET /favicon.ico。对于需要 HTML 标记的额外供应商特定图标,需要额外的中间件来提供相关文件,例如 serve-static

安装

🌐 Install

这是一个可以通过npm注册表获取的Node.js模块。安装可以使用npm install命令完成:

🌐 This is a Node.js module available through the npm registry. Installation is done using the npm install command:

Terminal window
npm install serve-favicon

Note

serve-favicon 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/serve-favicon

API

favicon(path, options)

创建新的中间件,从给定的 path 提供 favicon 到 favicon 文件。path 也可以是要提供的图标的 Buffer

🌐 Create new middleware to serve a favicon from the given path to a favicon file. path may also be a Buffer of the icon to serve.

选项

🌐 Options

Serve favicon 在 options 对象中接受这些属性。

🌐 Serve favicon accepts these properties in the options object.

maxAge

ms 中的 cache-control max-age 指令,默认值为 1 年。这也可以是 ms 模块接受的字符串。

🌐 The cache-control max-age directive in ms, defaulting to 1 year. This can also be a string accepted by the ms module.

例子

🌐 Examples

通常,这个中间件会很早出现在你的堆栈中(甚至可能是第一个),以避免在我们已经知道请求是针对 /favicon.ico 时处理任何其他中间件。

🌐 Typically this middleware will come very early in your stack (maybe even first) to avoid processing any other middleware if we already know the request is for /favicon.ico.

express

var express = require('express');
var favicon = require('serve-favicon');
var path = require('path');
var app = express();
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// Add your routes here, etc.
app.listen(3000);

connect

var connect = require('connect');
var favicon = require('serve-favicon');
var path = require('path');
var app = connect();
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// Add your middleware here, etc.
app.listen(3000);

原生 HTTP 服务器

🌐 vanilla http server

这个中间件可以在任何地方使用,即使在 express/connect 之外。它接受 reqrescallback

🌐 This middleware can be used anywhere, even outside express/connect. It takes req, res, and callback.

var http = require('http');
var favicon = require('serve-favicon');
var finalhandler = require('finalhandler');
var path = require('path');
var _favicon = favicon(path.join(__dirname, 'public', 'favicon.ico'));
var server = http.createServer(function onRequest(req, res) {
var done = finalhandler(req, res);
_favicon(req, res, function onNext(err) {
if (err) return done(err);
// continue to process the request here, etc.
res.statusCode = 404;
res.end('oops');
});
});
server.listen(3000);

许可证

🌐 License

MIT