Adapters

Web

Run your h3 apps in edge runtimes with Web API compatibility.

In order to run h3 apps in web compatible edge runtimes supporting fetch API with Request and Response, use toWebHandler adapter to convert h3 app into a fetch-like function.

Usage

First, create app entry:

app.mjs
import { createApp, defineEventHandler } from "h3";

export const app = createApp();

app.use(defineEventHandler(() => "Hello world!"));

Create web entry:

web.mjs
import { toWebHandler } from "h3";
import { app } from "./app.mjs";

// Create Web Adapter
export const handler = toWebHandler(app);

// Integrate handler with your runtime.
// Input is a Request and response is Promise<Response>

Local testing

You can test adapter using any compatible JavaScript runtime by passing a Request object.

web.test.mjs
import { handler } from "./web.mjs";

const response = await handler(new Request(new URL("/", "http://localhost")));

console.log(await response.text()); // Hello world!

Run with node ./web.test.mjs.