{"library":"serve-favicon","title":"Serve Favicon Middleware","description":"serve-favicon is a Node.js middleware designed for efficiently serving the `favicon.ico` file in web applications, primarily within the Express.js and Connect.js ecosystems. Currently at version 2.5.1, this package is actively maintained, receiving updates primarily for dependency management and CI improvements, rather than frequent feature additions. Its core purpose is to optimize favicon delivery by caching the icon in memory, generating robust ETags based on file content, and correctly setting the `Content-Type` header. A key differentiator is its specific focus on the default `/favicon.ico` path, ensuring that these high-frequency requests are handled quickly and bypass subsequent middleware in the stack, thereby improving overall application performance by reducing unnecessary processing for static assets.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install serve-favicon"],"cli":null},"imports":["const favicon = require('serve-favicon')","const favicon = require('serve-favicon')","favicon(path.join(__dirname, 'public', 'favicon.ico'), { maxAge: '1d' })"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const express = require('express');\nconst favicon = require('serve-favicon');\nconst path = require('path');\nconst fs = require('fs');\n\nconst app = express();\nconst publicDir = path.join(__dirname, 'public');\nconst faviconPath = path.join(publicDir, 'favicon.ico');\n\n// Ensure the 'public' directory exists\nif (!fs.existsSync(publicDir)) {\n  fs.mkdirSync(publicDir);\n}\n\n// Create a dummy favicon.ico if it doesn't exist for the example to run\nif (!fs.existsSync(faviconPath)) {\n  // A minimal, valid ICO file (1x1 transparent pixel)\n  const dummyFavicon = Buffer.from('0000010001001010000001000400000028000000010000000100000001000400000000001600000000000000000000000000000000000000', 'hex');\n  fs.writeFileSync(faviconPath, dummyFavicon);\n}\n\n// Use serve-favicon middleware\n// It should be placed early in your middleware stack\napp.use(favicon(faviconPath, { maxAge: '30d' }));\n\napp.get('/', (req, res) => {\n  res.send('Hello from your Express app with a favicon!');\n});\n\napp.listen(3000, () => {\n  console.log('Server running on http://localhost:3000');\n});","lang":"javascript","description":"This example sets up a basic Express server, uses `serve-favicon` to serve a favicon from a 'public' directory, and includes logic to create a dummy favicon file if one doesn't exist, making the example runnable out-of-the-box.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}