What is sirv?
The sirv npm package is a simple, high-performance, single-purpose HTTP server for serving static files. It is designed to be fast and efficient, making it ideal for serving the static assets of web applications.
What are sirv's main functionalities?
Serving static files
This code sample demonstrates how to use sirv with Polka (a lightweight web server) to serve static files from the 'public' directory.
const sirv = require('sirv');
const polka = require('polka');
const server = polka();
server.use(sirv('public'));
server.listen(3000, err => {
if (err) throw err;
console.log('> Running on localhost:3000');
});
Customizing options
This code sample shows how to customize sirv with options such as cache control headers and enabling single-page application (SPA) mode.
const sirv = require('sirv');
const options = {
maxAge: 31536000, // 1 year in seconds
immutable: true,
etag: true,
single: true
};
const serve = sirv('public', options);
Using with middleware
This code sample illustrates how to use sirv as middleware in an Express application to serve static files with development options enabled.
const sirv = require('sirv');
const express = require('express');
const app = express();
app.use(sirv('public', { dev: true }));
app.listen(3000, () => console.log('Server running on port 3000'));
Other packages similar to sirv
express-static
Express' built-in middleware function to serve static files. It is part of the Express.js framework and is widely used for serving static assets. Compared to sirv, it is more feature-rich but also heavier due to being part of a larger framework.
serve-static
A Node.js middleware for serving static files that is compatible with many frameworks like Express. It is similar to sirv but offers more configuration options and is part of the larger Express ecosystem.
koa-static
A static file serving middleware for Koa, which is another web framework for Node.js. Koa-static is designed to work within the Koa ecosystem and provides a similar feature set to sirv but is tailored for Koa's middleware pattern.