Back to H3

Plain

docs/3.adapters/3.plain.md

1.15.111.1 KB
Original Source

Plain

Run h3 servers into any unknown runtime!

There might be cases where your runtime is neither Node.js or Web compatible. Using plain adapter you can have an object input/output interface.

[!NOTE] This can be also be particularly useful for testing your server or running inside lambda-like environments.

Usage

First, create app entry:

js
import { createApp, defineEventHandler } from "h3";

export const app = createApp();

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

Create plain entry:

js
import { toPlainHandler } from "h3";
import { app } from "./app.mjs";

export const handler = toPlainHandler(app);

Local testing

You can test adapter using any JavaScript runtime.

js
import { handler } from "./plain.mjs";

const response = await handler({
  method: "GET",
  path: "/",
  headers: {
    "x-test": "test",
  },
  body: undefined,
  context: {},
});

Example response:

js
{
  status: 200,
  statusText: '',
  headers: [ [ 'content-type', 'text/html' ] ],
  body: 'Hello world!'
}