Fastify
Fastify register()
Fastify plugins organize routes and dependencies into encapsulated scopes. A child scope can inherit parent decorations without automatically exposing its own decorations to siblings. Register a plugin with register and optionally a route prefix. A decoration adds a named capability to the instance or request as appropriate. Normal plugin scopes inherit from ancestors; special plugins can deliberately change encapsulation, so inspect plugin packaging. Prefer explicit scope and avoid accidental global state. The plugin below is an ordinary async function, not wrapped by fastify-plugin. Static example only; runtime NOT RUN. Source: https://fastify.dev/docs/v5.6.x/Reference/Plugins/.
Formal Syntax & Structure
nodejs syntaxnodejs
app.register(plugin, options)Working Example
Example Snippetnodejs
import Fastify from 'fastify';const app = Fastify();app.register(async child => { child.decorate('catalogLabel', 'books'); child.get('/label', async () => ({label:child.catalogLabel}));}, {prefix:'/catalog'});try { const response = await app.inject('/catalog/label'); console.log(response.statusCode, response.body, app.hasDecorator('catalogLabel'));} finally { await app.close(); }