- 快速入门
- 指南
- 参考手册
- 高级主题
- 资源
- Express 词汇表
- Express 社区
- 模板引擎
- Express 中间件
- Express body-parser middleware
- Express compression middleware
- Express connect-rid middleware
- Express cookie-parser middleware
- Express cookie-session middleware
- Express cors middleware
- Express csurf middleware
- Express errorhandler middleware
- Express method-override middleware
- Express morgan middleware
- Express multer middleware
- Express response-time middleware
- Express serve-favicon middleware
- Express serve-index middleware
- Express serve-static middleware
- Express session middleware
- Express timeout middleware
- Express vhost middleware
- Express 实用模板
- 推荐框架
- 推荐书籍和博客
Express serve-favicon middleware
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?
- 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
.
Note This module is exclusively for serving the “default, implicit favicon”, which is 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.
Install
This is a Node.js module available through the npm registry. Installation is done using the npm install
command:
$ npm install serve-favicon
API
favicon(path, options)
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 accepts these properties in the options object.
maxAge
The cache-control
max-age
directive in ms
, defaulting to 1 year. This can also be a string accepted by the ms module.
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
.
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
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论