3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Linux Build][travis-image]][travis-url]
6 [![Windows Build][appveyor-image]][appveyor-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
8 [![Gittip][gittip-image]][gittip-url]
10 Node.js middleware for serving a favicon.
12 A favicon is a visual cue that client software, like browsers, use to identify
13 a site. For an example and more information, please visit
14 [the Wikipedia article on favicons](https://en.wikipedia.org/wiki/Favicon).
18 - User agents request `favicon.ico` frequently and indiscriminately, so you
19 may wish to exclude these requests from your logs by using this middleware
20 before your logger middleware.
21 - This module caches the icon in memory to improve performance by skipping
23 - This module provides an `ETag` based on the contents of the icon, rather
24 than file system properties.
25 - This module will serve with the most compatible `Content-Type`.
27 **Note** This module is exclusively for serving the "default, implicit favicon",
28 which is `GET /favicon.ico`. For additional vendor-specific icons that require
29 HTML markup, additional middleware is required to serve the relevant files, for
30 example [serve-static](https://npmjs.org/package/serve-static).
34 This is a [Node.js](https://nodejs.org/en/) module available through the
35 [npm registry](https://www.npmjs.com/). Installation is done using the
36 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
39 npm install serve-favicon
44 ### favicon(path, options)
46 Create new middleware to serve a favicon from the given `path` to a favicon file.
47 `path` may also be a `Buffer` of the icon to serve.
51 Serve favicon accepts these properties in the options object.
55 The `cache-control` `max-age` directive in `ms`, defaulting to 1 year. This can
56 also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
61 Typically this middleware will come very early in your stack (maybe even first)
62 to avoid processing any other middleware if we already know the request is for
68 var express = require('express');
69 var favicon = require('serve-favicon');
72 app.use(favicon(__dirname + '/public/favicon.ico'));
74 // Add your routes here, etc.
82 var connect = require('connect');
83 var favicon = require('serve-favicon');
86 app.use(favicon(__dirname + '/public/favicon.ico'));
88 // Add your middleware here, etc.
93 ### vanilla http server
95 This middleware can be used anywhere, even outside express/connect. It takes
96 `req`, `res`, and `callback`.
99 var http = require('http');
100 var favicon = require('serve-favicon');
101 var finalhandler = require('finalhandler');
103 var _favicon = favicon(__dirname + '/public/favicon.ico');
105 var server = http.createServer(function onRequest(req, res) {
106 var done = finalhandler(req, res);
108 _favicon(req, res, function onNext(err) {
109 if (err) return done(err);
111 // continue to process the request here, etc.
113 res.statusCode = 404;
125 [npm-image]: https://img.shields.io/npm/v/serve-favicon.svg
126 [npm-url]: https://npmjs.org/package/serve-favicon
127 [travis-image]: https://img.shields.io/travis/expressjs/serve-favicon/master.svg?label=linux
128 [travis-url]: https://travis-ci.org/expressjs/serve-favicon
129 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-favicon/master.svg?label=windows
130 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-favicon
131 [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-favicon.svg
132 [coveralls-url]: https://coveralls.io/r/expressjs/serve-favicon?branch=master
133 [downloads-image]: https://img.shields.io/npm/dm/serve-favicon.svg
134 [downloads-url]: https://npmjs.org/package/serve-favicon
135 [gittip-image]: https://img.shields.io/gittip/dougwilson.svg
136 [gittip-url]: https://www.gittip.com/dougwilson/