Hooks Reference
Overview
Hooks allow you to tap into the application lifecycle and extend functionality.
Lifecycle Hooks
onInit
Called when the application initializes.
import { onInit } from 'my-package';
onInit((app) => { console.log('App initialized!'); // Setup code here});onReady
Called when the application is ready to handle requests.
import { onReady } from 'my-package';
onReady((app) => { console.log(`Server running on port ${app.port}`);});onRequest
Called for every incoming request.
import { onRequest } from 'my-package';
onRequest((request, response, next) => { console.log(`${request.method} ${request.url}`); next();});onError
Called when an error occurs.
import { onError } from 'my-package';
onError((error, request, response) => { console.error('Error:', error.message); response.status(500).json({ error: 'Internal Server Error' });});onShutdown
Called when the application is shutting down.
import { onShutdown } from 'my-package';
onShutdown(async () => { // Cleanup code await database.disconnect(); console.log('Graceful shutdown complete');});Event Hooks
on
Subscribe to custom events.
import { on, emit } from 'my-package';
// Subscribeon('user:created', (user) => { console.log('New user:', user.name); sendWelcomeEmail(user.email);});
// Emitemit('user:created', { id: 1, name: 'John', email: 'john@example.com' });once
Subscribe to an event only once.
import { once } from 'my-package';
once('app:ready', () => { console.log('This only runs once');});off
Unsubscribe from an event.
import { on, off } from 'my-package';
const handler = (data) => console.log(data);
on('event', handler);off('event', handler);Hook Order
onInit ↓onReady ↓onRequest (per request) ↓onError (if error) ↓onShutdown