1.0.6 • Published 4 years ago
egg-yhsd-router v1.0.6
egg router decorator -> 使用注解(decorator)定义API,简化路由配置。
!! 注意: 本插件只支持typescript版本的egg项目,js版本的暂未测试
安装
npm i egg-yhsd-router
使用方法
在router
入口中注入
// app/router.ts
import { Application } from 'egg';
import { RouterHandler } from 'egg-yhsd-router';
export default (app: Application) => {
RouterHandler(app);
};
在controller
中使用注解(decorator)
// app/controller
import { Controller } from 'egg';
import { Get, Use, Prefix } from 'egg-yhsd-router';
@Prefix('/api')
export default class HomeController extends Controller {
@Use([ 'middlewareName' ]) // 使用中间件。传入需要应用到该路由级别的中间件列表
@Get('/') // 将该函数注册为path: /api ; method: GET 的api处理函数
async index() {
const { ctx } = this;
ctx.body = await ctx.service.test.sayHi('egg');
}
}
// 完成,访问 `http://host:port/api` 即可