app.register()
Loads a plugin, creating an encapsulated child context for its routes and decorators.
Syntax
app.register(plugin, options?) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
plugin | FastifyPluginCallback | No | A plugin function or imported plugin module. |
options | object | No | Plugin options, including `prefix` for route namespacing. |
Returns
FastifyInstance — The instance, for chaining.
Examples
import Fastify from 'fastify';
import cors from '@fastify/cors';
const app = Fastify();
await app.register(cors, { origin: true });
await app.register(
async (instance) => {
instance.get('/cats', async () => ['kitty']);
},
{ prefix: '/api/v1' },
);
Notes
Each registered plugin runs in its own encapsulated scope; decorators and
hooks added inside do not leak to the parent unless wrapped with
fastify-plugin. Use `prefix` to mount route groups under a base path.