{"library":"sirv","title":"Sirv Static File Server Middleware","description":"Sirv is an optimized and lightweight middleware designed for serving static assets in Node.js applications, compatible with frameworks like Polka, Express, and native HTTP/S servers. The current stable version is 3.0.2. Its primary differentiator is a significant performance advantage over alternatives like `serve-static` because it pre-scans and caches file system information upfront (when not in 'dev' mode), avoiding costly per-request file system checks. This makes it very efficient for production deployments. Releases are active, with recent patches and a major version upgrade to v3.0.0 that introduced native ESM support and a higher Node.js baseline. It ships with TypeScript types, enhancing developer experience.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install sirv"],"cli":null},"imports":["import sirv from 'sirv';","import type { SirvOptions } from 'sirv';","import sirv from 'sirv';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import polka from 'polka';\nimport sirv from 'sirv';\nimport compression from 'compression';\nimport * as path from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\nconst __dirname = path.dirname(fileURLToPath(import.meta.url));\nconst publicDir = path.join(__dirname, 'public');\n\n// Create a dummy 'public' directory and an index.html file for demonstration\nimport { promises as fs } from 'node:fs';\nasync function setupPublicDir() {\n  await fs.mkdir(publicDir, { recursive: true });\n  await fs.writeFile(path.join(publicDir, 'index.html'), '<h1>Hello from Sirv!</h1><p>This is a static file.</p>');\n  await fs.writeFile(path.join(publicDir, 'styles.css'), 'body { font-family: sans-serif; background-color: #f0f0f0; }');\n}\n\n// Initialize sirv handler\nconst assets = sirv(publicDir, {\n  maxAge: 31536000, // 1 year\n  immutable: true,\n  gzip: true, // Look for precompiled .gz files\n  dotfiles: false, // Don't serve dotfiles\n  dev: process.env.NODE_ENV === 'development' // Enable dev mode for caching control\n});\n\nconst PORT = process.env.PORT ?? 3000;\n\nawait setupPublicDir();\n\npolka()\n  .use(compression()) // Apply compression before sirv\n  .use(assets) // Serve static assets\n  .use('/api', (req, res) => {\n    res.setHeader('Content-Type', 'application/json');\n    res.end(JSON.stringify({ message: 'This is an API endpoint', timestamp: Date.now() }));\n  })\n  .listen(PORT, (err) => {\n    if (err) throw err;\n    console.log(`> Ready on http://localhost:${PORT}!`);\n    console.log(`> Serving static files from ${publicDir}`);\n  });","lang":"typescript","description":"This quickstart demonstrates how to set up `sirv` with `polka` and `compression` to serve static files from a 'public' directory. It highlights ESM usage, configuration options like `maxAge` and `dev` mode, and shows how to integrate it alongside other middleware. It dynamically creates a 'public' directory and files for immediate execution.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}