更多实用程序

¥More utils

更多 H3 实用程序。

¥More H3 utilities.

基础

¥Base

withBase(base, input)

Returns a new event handler that removes the base url of the event before calling the original handler.

Example:

const api = new H3()
 .get("/", () => "Hello API!");
const app = new H3();
 .use("/api/**", withBase("/api", api.handler));

事件

¥Event

isEvent(input)

Checks if the input is an H3Event object.

mockEvent(_request, options?)

中间件

¥Middleware

onError(hook)

Define a middleware that runs when an error occurs.

You can return a new Response from the handler to gracefully handle the error.

onRequest(hook)

Define a middleware that runs on each request.

onResponse(hook)

Define a middleware that runs after Response is generated.

You can return a new Response from the handler to replace the original response.

WebSocket

defineWebSocket(hooks)

Define WebSocket hooks.

defineWebSocketHandler(hooks)

Define WebSocket event handler.

适配器

¥Adapters

defineNodeHandler(handler)

defineNodeMiddleware(handler)

fromNodeHandler(handler)

fromWebHandler()

toNodeHandler(app)

Convert H3 app instance to a NodeHandler with (IncomingMessage, ServerResponse) => void signature.