Adapters

Bun

Run your h3 apps with Bun

In order to run h3 apps in Bun, use the Web Adapter.

Alternatively you can use Node.js adapter as Bun is fully compatible with Node.js API!

Usage

Create app entry:

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

export const app = createApp();

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

Create Bun server entry:

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

const server = Bun.serve({
  port: 3000,
  fetch: toWebHandler(app),
});

Now, your can run Bun server:

bun --bun ./server.mjs

WebSocket support

Read more in crossws.unjs.io/adapters/bun.
import wsAdapter from "crossws/adapters/bun";

const { websocket, handleUpgrade } = wsAdapter(app.websocket);

const handler = toWebHandler(app);

const server = Bun.serve({
  port: 3000,
  websocket,
  fetch(req, server) {
    if (await handleUpgrade(req, server)) {
      return;
    }
    return handler(req);
  },
});