Node.js middleware for serving a favicon.Node.js中间件,用于为favicon提供服务。
A favicon is a visual cue that client software, like browsers, use to identify a site. favicon是客户端软件(如浏览器)用来识别站点的视觉提示。For an example and more information, please visit the Wikipedia article on favicons.有关示例和更多信息,请访问维基百科关于favicons的文章。
Why use this module?为什么要使用这个模块?
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
,因此您可能希望在日志记录器中间件之前使用此中间件从日志中排除这些请求。ETag
based on the contents of the icon, rather than file system properties.ETag
。Content-Type
.Content-Type
。Note This module is exclusively for serving the "default, implicit favicon", which is 注意:此模块专门用于服务“默认隐式favicon”,即GET /favicon.ico
. GET /favicon.ico
。For additional vendor-specific icons that require HTML markup, additional middleware is required to serve the relevant files, for example serve-static.对于需要HTML标记的其他特定于供应商的图标,需要额外的中间件来为相关文件提供服务,例如serve-static。
This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表获得。Installation is done using the 使用npm install
command:npm install
命令完成安装:
$ npm install serve-favicon
Create new middleware to serve a favicon from the given 创建新的中间件以服务于从给定path
to a favicon file. path
到favicon文件的favicon。path
may also be a Buffer
of the icon to serve.path
也可以是要服务的图标的缓冲区。
Serve favicon accepts these properties in the options object.Serve favicon在options对象中接受这些属性。
The cache-control
max-age
directive in ms
, defaulting to 1 year. cache-control
max-age
指令,以毫秒为单位,默认为1年。This can also be a string accepted by the ms module.这也可以是ms模块接受的字符串。
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的,那么该中间件将在堆栈中很早出现(甚至可能是第一个),以避免处理任何其他中间件。/favicon.ico
.
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)
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)
This middleware can be used anywhere, even outside express/connect. 该中间件可以在任何地方使用,甚至在express/connect之外。It takes 它需要req
, res
, and callback
.req
、res
和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)