express.static()
Built-in middleware that serves static files from a directory.
Syntax
express.static(root[, options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
root | string | Yes | The directory to serve files from. |
options | object | No | `maxAge`, `index`, `dotfiles`, `etag`, `immutable`, `extensions`, `fallthrough`. |
Returns
function — A middleware function.
Examples
app.use(express.static('public'));
// public/style.css is now served at /style.css
Output
$ curl localhost:3000/style.css
body { margin: 0 }
app.use('/assets', express.static('dist', {
maxAge: '1y',
immutable: true,
}));
Output
Cache-Control: public, max-age=31536000, immutable
Notes
Sets caching headers and supports range requests automatically. Use
a long `maxAge` with content-hashed filenames for cache busting. By
default it does not serve dotfiles. Mount it after security
middleware and before catch-all routes.