Hapi
Quick Start
code
const server = Hapi.server({ port: 3000 });
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();
Critical Rules
- •Compose with decorations & methods - Expose services via decorations and reusable logic via methods
- •Follow the lifecycle - 24-step request flow; see lifecycle overview
- •Auth is three layers - scheme → strategy → default; see server auth
- •Validate at the route - Use joi schemas on params, query, payload, headers; see validation
- •Type routes with Refs - Use
ServerRoute<Refs>pattern; see route scaffold
Workflow
- •Create server - server overview for constructor options
- •Register plugins - plugins and plugin structure
- •Configure auth - auth schemes and route auth
- •Define routes - route overview with handlers
- •Add extensions - lifecycle hooks and pre-handlers
Key Patterns
| Topic | Reference |
|---|---|
| Request/response objects | request, response |
| Response toolkit (h) | toolkit |
| Caching & CORS | cache-cors, server cache |
| Security headers | security |
| Payload parsing | payload |
| Decorations & methods | decorations, methods |
| Events | events |
| Testing (server.inject) | network |
| TypeScript auth typing | auth-scheme, type-author |
| TypeScript plugins | plugin-scaffold |