What is serve-favicon?
The serve-favicon npm package is a middleware for serving a favicon, a small icon associated with a particular website or web page. It is commonly used in Node.js applications to define and serve the favicon efficiently, handling caching and other concerns.
What are serve-favicon's main functionalities?
Serving a favicon
This code sample demonstrates how to use serve-favicon to serve a favicon from a specified path in an Express.js application. The favicon.ico file is located in the 'public' directory, and the middleware is set up to serve it.
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const app = express();
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.listen(3000);
Caching
This code sample shows how to set a cache-control max-age directive for the favicon, which tells browsers to cache the favicon for a specified amount of time (in this case, 30 days).
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const app = express();
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'), { maxAge: 2592000000 }));
app.listen(3000);
Other packages similar to serve-favicon
static-favicon
Similar to serve-favicon, static-favicon was used to serve favicon files. However, it is now deprecated in favor of serve-favicon, which is more up-to-date and maintained.
express-favicon
This is another package that can be used to serve favicons in Express applications. It is less popular and not as actively maintained as serve-favicon, which is the more commonly recommended package for this purpose.
connect-favicons
Connect-favicons is a middleware for Connect and Express that serves favicons. It is an alternative to serve-favicon but has fewer downloads and less frequent updates, suggesting that serve-favicon is the preferred choice for most developers.
serve-favicon
Node.js middleware for serving a favicon.
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
may also be a Buffer
of the icon to serve.
options
maxAge
- cache-control max-age directive in ms
, defaulting to 1 day.
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 3.x/4.x
var express = require('express');
var favicon = require('serve-favicon');
var app = express();
app.use(favicon(__dirname + '/public/favicon.ico'));
app.listen(3000);
connect
var connect = require('connect');
var favicon = require('serve-favicon');
var app = connect();
app.use(favicon(__dirname + '/public/favicon.ico'));
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 _favicon = favicon(__dirname + '/public/favicon.ico');
var server = http.createServer(function onRequest(req, res) {
_favicon(req, res, function onNext(err) {
if (err) {
res.statusCode = 500;
res.end();
return;
}
res.statusCode = 404;
res.end('oops');
});
});
server.listen(3000);
License
MIT