@whatwg-node/server 简单试用

就是一个简单测试,将hono 挂载到express 框架中

参考代码

  • index.ts
import express from "express"

import { Hono } from "hono"

import {createServerAdapter} from "@whatwg-node/server"

const ex = express();

// Hono 入口
const ho = new Hono();

// hono 子应用
const hoSubApp = new Hono();

hoSubApp.get("/ho", (c) => c.text("Hello from Hono!"));
hoSubApp.get("/ho/about", (c) => c.text("About page from Hono!"));

// 挂载子应用
ho.route("/mydemo", hoSubApp);

// 通过adapter 挂载到express 中
ex.use('/mydemo/', createServerAdapter(ho.fetch))

ex.get("/", (req, res) => {
    res.write("Hello from Express!");
    res.end();
});
ex.get("/about", (req, res) => {
    res.write("About page from Express!");
    res.end();
});

ex.listen(9090, () => {
  console.log("Express server running on http://localhost:9090");
})
  • 访问说明

  访问/ 以及/about 都是express 的请求,但是使用 /myapp/ho 以及/myapp/ho/about 将是hono的

说明

@whatwg-node/server 是一个和不错的包,可以让基于web fetch 标准的框架与周边框架很好的兼容

参考资料

https://github.com/ardatan/whatwg-node/tree/master/packages/server

posted on 2026-10-13 08:00  荣锋亮  阅读(10)  评论(0)    收藏  举报

导航