H3 App Instance
You can create a new h3 app instance using new H3()
:
import { H3 } from "h3";
const app = new H3(/* optional config */);
H3 App Options
You can pass global app configuration when initializing an app.
Example: Create an app with verbose logging enabled.
const app = new H3({ debug: true });
Global Hooks
When initializing an h3 app, you can register global hooks:
onError
onRequest
onBeforeResponse
These hooks are called for every request and can be used to add global logic to your app such as logging, error handling, etc.
const app = new H3({
onError: (error) => {
console.error(error);
},
onRequest: (event) => {
console.log("Request:", event.path);
},
});
H3 App Methods
app.fetch(request, init?, context?)
A fetch-like API allowing to fetch app routes.
Input can be a URL, relative path or standard Request object.
Returned value is a standard Response or a promise resolving to a Response.
Example:
const response = await app.fetch("/");
console.log(response, await response.text());
app.handler(event)
An h3 compatible event handler useful to compose multiple h3 app instances.
app.resolve(method, path)
Resolve a route handler by method and path useful for custom purposes.
app.on(method, route, handler)
Register route handler for specific HTTP method.
app.[method](route, handler)
Register route handler for specific HTTP method (shortcut for app.on(method, ...)
).
app.all(route, handler)
Register route handler for all HTTP methods.
app.use(handler, opts?)
Register a global middleware handler.
app.use(route, handler, opts?)
Register a routed global middleware handler.
H3 App Properties
app.config
Global h3 instance config.
app.websocket
Websocket hooks compatible with 🔌 crossws.