Adapters

Node.js

Natively run h3 servers with Node.js.

In order to start h3 apps in Node.js, use toNodeListener adapter to convert h3 app into a Node.js requestListener.

Usage

First, create an h3 app:

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

export const app = createApp();

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

Create Node.js server entry:

server.mjs
import { createServer } from "node:http";
import { toNodeListener } from "h3";
import { app } from "./app.mjs";

createServer(toNodeListener(app)).listen(process.env.PORT || 3000);

Now, you can run you h3 app natively with Node.js:

terminal
node ./server.mjs

Using listhen

Alternatively, you can use unjs/listhen. In this method, you only need to make app.mjs with a default or app export.

Run this command to run your servers:

npx --yes listhen ./app.ts

WebSocket support

Read more in crossws.unjs.io/adapters/node.
When using listhen method, websocket is supported out of the box!
import wsAdapter from "crossws/adapters/node";

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

server.on("upgrade", handleUpgrade);