@fastify/static
Serves static files from a directory over HTTP.
Syntax
app.register(fastifyStatic, { root, prefix? }) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | root path, optional prefix, and caching options. |
Returns
Promise<void> — Resolves when the plugin is registered.
Examples
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import { join } from 'node:path';
const app = Fastify();
await app.register(fastifyStatic, {
root: join(import.meta.dirname, 'public'),
prefix: '/assets/',
});
Notes
`root` must be an absolute path. Register multiple times with different
prefixes by setting `decorateReply: false` on all but the first. Supports
reply.sendFile() for explicit file responses.