feat: 添加路由装饰器系统和全局异常处理
- 新增装饰器支持(@Get, @Post, @Put, @Delete, @Patch, @Controller) - 实现路由自动注册机制(RouteScanner) - 添加全局异常处理中间件(Exception) - 实现统一响应体格式(ApiResponse) - 新增请求体解析中间件(BodyParser) - 重构控制器为类模式,支持装饰器路由 - 添加示例用户控制器(UserController) - 更新TypeScript配置支持装饰器 - 添加reflect-metadata依赖 - 完善项目文档 Breaking Changes: - 控制器现在返回数据而不是直接设置ctx.body - 新增统一的API响应格式
This commit is contained in:
@@ -1,10 +1,25 @@
|
||||
import { Router } from './router.ts';
|
||||
import { ResponseTime } from './responseTime.ts';
|
||||
import { Exception } from './exception.ts';
|
||||
import { BodyParser } from './body-parser.ts';
|
||||
import type Koa from 'koa';
|
||||
|
||||
export function registerMiddlewares(app: Koa) {
|
||||
const router = new Router();
|
||||
/**
|
||||
* 初始化中间件
|
||||
* @param app Koa
|
||||
*/
|
||||
export function initMiddlewares(app: Koa) {
|
||||
// 全局异常处理中间件必须最先注册
|
||||
const exception = new Exception();
|
||||
exception.apply(app);
|
||||
|
||||
// 请求体解析中间件
|
||||
const bodyParser = new BodyParser();
|
||||
bodyParser.apply(app);
|
||||
|
||||
const responseTime = new ResponseTime();
|
||||
responseTime.apply(app);
|
||||
|
||||
const router = new Router();
|
||||
router.apply(app);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user