JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2171228
  • Score
    100M100P100Q199196F
  • License MIT

favicon serving middleware with caching

Package Exports

  • serve-favicon

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (serve-favicon) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

serve-favicon

NPM Version NPM Downloads Build Status Test Coverage Gittip

Node.js middleware for serving a favicon.

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.

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

Serve favicon accepts these properties in the options object.

maxAge

The cache-control max-age directive in ms, defaulting to 1 day. 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 app = express();
app.use(favicon(__dirname + '/public/favicon.ico'));

// Add your routes here, etc.

app.listen(3000);

connect

var connect = require('connect');
var favicon = require('serve-favicon');

var app = connect();
app.use(favicon(__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 _favicon = favicon(__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