# minihono **Repository Path**: lrlanbhh/minihono ## Basic Information - **Project Name**: minihono - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-11 - **Last Updated**: 2026-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Mini Hono 一个简化版的 [Hono](https://hono.dev) 框架实现,用于学习和理解 Hono 的核心架构。 ## 项目结构 ``` mini-hono/ ├── src/ │ ├── types.ts # 核心类型定义 │ ├── router.ts # 简化版 RegExpRouter │ ├── context.ts # Context 和 HonoRequest 类 │ ├── compose.ts # 中间件编排 │ ├── hono.ts # Hono 主类 │ ├── index.ts # 入口文件 │ └── example.ts # 使用示例 ├── package.json ├── tsconfig.json └── README.md ``` ## 核心概念 ### 1. Context(上下文) Context 封装了 HTTP 请求和响应的所有信息: ```typescript app.get('/user/:id', (c) => { // 访问请求信息 const id = c.req.param('id'); const query = c.req.query('search'); const header = c.req.header('Authorization'); // 返回响应 return c.json({ id, query }); }); ``` ### 2. Router(路由器) 使用正则表达式进行高效路由匹配: ```typescript // 静态路由 app.get('/users', handler); // 动态路由 app.get('/user/:id', handler); // 通配符 app.use('/api/*', middleware); ``` ### 3. Compose(中间件编排) 实现洋葱模型中间件链: ```typescript app.use('*', async (c, next) => { console.log('before'); await next(); // 执行后续中间件 console.log('after'); }); ``` ## 支持的 API ### 路由 - `app.get(path, handler)` - `app.post(path, handler)` - `app.put(path, handler)` - `app.delete(path, handler)` - `app.options(path, handler)` - `app.patch(path, handler)` - `app.all(path, handler)` - `app.on(method, path, handler)` ### 中间件 - `app.use(path, middleware)` - `app.use(middleware)` ### 响应方法 - `c.text(string, status?)` - `c.json(object, status?)` - `c.html(string, status?)` - `c.body(data, status?)` - `c.redirect(location, status?)` ### 请求方法 - `c.req.param(key?)` - `c.req.query(key?)` - `c.req.header(key?)` - `c.req.json()` - `c.req.text()` ### 上下文变量 - `c.set(key, value)` - `c.get(key)` ### 路由分组 - `app.route(path, hono)` - `app.basePath(path)` ### 错误处理 - `app.onError(handler)` - `app.notFound(handler)` ## 运行示例 ```bash # 使用 Node.js 22+ 的实验性 TypeScript 支持 node --experimental-strip-types src/example.ts # 或者编译后运行 npx tsc node dist/example.js ``` ## 与原版的差异 | 特性 | Mini Hono | Hono | |------|-----------|------| | 代码量 | ~1500 行 | ~10000+ 行 | | 路由器 | 简化版 RegExpRouter | 多种路由器 | | 类型系统 | 基础类型 | 完整类型推导 | | 中间件 | 核心功能 | 30+ 内置中间件 | | 适配器 | 无 | Cloudflare, Lambda 等 | | JSX | 不支持 | 支持 | | 流式响应 | 不支持 | 支持 | ## 学习要点 1. **基于 Web 标准**:使用原生 Request/Response 对象 2. **洋葱模型中间件**:通过 compose 实现中间件链 3. **正则路由**:高效的路由匹配算法 4. **类型安全**:TypeScript 类型定义 5. **多运行时**:兼容 Node.js, Deno, Bun, Cloudflare Workers ## 许可证 MIT