Skip to main content
AI Generated Content
πŸ€– This documentation was generated with AI assistance. Please report any issues you find.
Logo for Fastify

Fastify

Pikku provides two Fastify packages:

  • @pikku/fastify-plugin β€” drop Pikku into an existing Fastify app as a plugin
  • @pikku/fastify β€” full managed server with health checks and graceful shutdown

Fastify Plugin​

Add Pikku to an existing Fastify app. Your non-Pikku routes and plugins work alongside it.

npm install @pikku/fastify-plugin
import Fastify from 'fastify'
import pikkuFastifyPlugin from '@pikku/fastify-plugin'

import './.pikku/pikku-bootstrap.gen.js'

const app = Fastify()

const singletonServices = await createSingletonServices(config)

app.register(pikkuFastifyPlugin, {
pikku: {
singletonServices,
createWireServices,
respondWith404: true,
logRoutes: true,
loadSchemas: true,
}
})

// Your own Fastify routes still work
app.get('/custom', async () => ({ hello: 'world' }))

await app.listen({ port: 3000 })

Options​

OptionTypeDefaultDescription
respondWith404booleantrueReturn 404 for routes not matched by Pikku
logRoutesbooleanfalseLog registered Pikku routes on startup
loadSchemasbooleanfalseLoad JSON schemas for validation

Full Server (PikkuFastifyServer)​

For projects where Pikku handles everything:

npm install @pikku/fastify
import './.pikku/pikku-bootstrap.gen.js'
import { PikkuFastifyServer } from '@pikku/fastify'

const config = await createConfig()
const singletonServices = await createSingletonServices(config)

const server = new PikkuFastifyServer(config, singletonServices.logger)

// Access the underlying Fastify app if needed
// server.app.register(myPlugin)

await server.init({
singletonServices,
createWireServices,
logRoutes: true,
loadSchemas: true,
})

process.on('SIGINT', async () => {
await server.stop()
process.exit(0)
})

await server.start()

The full server config extends CoreConfig with:

OptionTypeDescription
portnumberPort to listen on
hostnamestringHostname to bind to
healthCheckPathstringHealth check endpoint (default: /health-check)

The underlying Fastify app is exposed as server.app if you need to add custom plugins or routes.