装饰器是 TypeScript 提供的一种强大特性,它允许我们在编译时向类、方法、属性或参数添加元数据。在此框架中,装饰器主要用于定义路由和 HTTP 方法,这极大地简化了路由的配置过程,使路由定义更加直观和灵活。
要在你的项目中使用装饰器,你需要在你的 tsconfig.json 文件中配置:
{ "experimentalDecorators": true }
@Controller@Controller("test")
export class Test {}
app.use(controllers("/api", Test));
@Get, @Post, @Put, @Delete, @Patch  @Get("post")
  handle(ctx: Context) {}
路径匹配规则:
@Get(":id")。@Get("*")。示例
@Controller("test")
export class Test {
  // GET 请求,路径为 /test
  @Get()
  [sfn()](ctx: Context) {
    console.log(this.a);
    ctx.json(ctx.query);
  }
  // POST 请求,路径为 /test/post
  @Post("post")
  [sfn()](ctx: Context) {
    const body = await ctx.body("json");
    ctx.json(body);
  }
  // GET 请求,路径为 /test/rand/123
  @Get("rand/*")
  [sfn()](ctx: Context) {
    ctx.text("hello world");
  }
  // GET 请求,路径为 /test/123
  @Get(":id")
  [sfn()](ctx: Context) {
    ctx.json(ctx.params);
  }
}
@Use如何在 RESTful API 控制器中使用 AOP 来添加横切关注点逻辑。
@Controller("test")
export class Test {
  @Use(async (ctx, next) => {
    console.log("before");
    await next();
    console.log("after");
  })
  @Get()
  async [sfn()](ctx: Context) {
    ctx.json(ctx.query);
  }
}