serve-favicon

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

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?为什么要使用这个模块?

Note This module is exclusively for serving the "default, implicit favicon", which is GET /favicon.ico. 注意:此模块专门用于服务“默认隐式favicon”,即GET /favicon.icoFor additional vendor-specific icons that require HTML markup, additional middleware is required to serve the relevant files, for example serve-static.对于需要HTML标记的其他特定于供应商的图标,需要额外的中间件来为相关文件提供服务,例如serve-static

Install安装

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

API

favicon(path, options)

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也可以是要服务的图标的缓冲区。

Options

Serve favicon accepts these properties in the options object.Serve favicon在options对象中接受这些属性。

maxAge

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模块接受的字符串。

Examples例子

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的,那么该中间件将在堆栈中很早出现(甚至可能是第一个),以避免处理任何其他中间件。

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)

vanilla http server香草http服务器

This middleware can be used anywhere, even outside express/connect. 该中间件可以在任何地方使用,甚至在express/connect之外。It takes req, res, and callback.它需要reqrescallback

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