description and source-codefunction favicon(path, options) {
var opts = options || {}
var icon // favicon cache
var maxAge = calcMaxAge(opts.maxAge)
if (!path) {
throw new TypeError('path to favicon.ico is required')
}
if (Buffer.isBuffer(path)) {
icon = createIcon(copyBuffer(path), maxAge)
} else if (typeof path === 'string') {
path = resolveSync(path)
} else {
throw new TypeError('path to favicon.ico must be string or buffer')
}
return function favicon (req, res, next) {
if (parseUrl(req).pathname !== '/favicon.ico') {
next()
return
}
if (req.method !== 'GET' && req.method !== 'HEAD') {
res.statusCode = req.method === 'OPTIONS' ? 200 : 405
res.setHeader('Allow', 'GET, HEAD, OPTIONS')
res.setHeader('Content-Length', '0')
res.end()
return
}
if (icon) {
send(req, res, icon)
return
}
fs.readFile(path, function (err, buf) {
if (err) return next(err)
icon = createIcon(buf, maxAge)
send(req, res, icon)
})
}
}