Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Core Concepts/Lifecycle Hooks

Lifecycle Hooks

Register the startup, shutdown, and error hooks that Volt currently supports.

Supported hooks

Use app.onStart(fn), app.onStop(fn), and app.onError(fn). onError receives an Error and native Request and must return a Response. For validation errors and HttpError handling, see Error Handling.

Complete example

index.ts
import { Volt } from 'voltts';

const app = new Volt();
app.onStart(() => console.log('Starting Volt'));
app.onStop(() => console.log('Stopped Volt'));
app.onError((error, request) => {
  console.error(request.method, request.url, error);
  return Response.json({ error: 'Unexpected error' }, { status: 500 });
});
app.get('/health', () => ({ ok: true }));
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Callback timing

Register hooks before listen(). The current startup callback is invoked without awaiting its result, so perform asynchronous initialization before listen(), rather than inside onStart. stop() awaits plugin cleanup and onStop. Each hook setter replaces the previous callback of that kind. Per-request hooks such as preValidation and onResponse are not implemented; use middleware around a handler.