koa-router-class
koa 路由,将路由转发到 class 方法上
基于 path-to-regexp 库
usage
const { Router } = require('koa-router-class');
app.use(Router());
IndexController.ts
import Result from '../../src/core/Result';
import { Path, RequestMethod, BaseController, Param, Around, middlewareToAround, Inject } from '../../src/index';
import Test from '../service/Test';
@Around(async (point) => {
console.log('----around before----')
const { proceed, args } = point;
const result = await proceed(args);
console.log('----around after----')
return result;
})
export default class Index extends BaseController {
@Inject(Test)
t: Test;
@Path('/')
index() {
console.log(this.t.test());
return this.send('This is index');
}
@Path({ value: '/post', method: RequestMethod.POST })
post() {
return this.send('This is post page.');
}
@Path('/test/:name')
@Around(middlewareToAround((ctx, next) => {
console.log('》》', ctx.request.path);
return next();
}))
test(@Param('name') name: string) {
console.log(`进入方法,参数:${JSON.stringify(name)}`)
return Result.send(`这里是测试页面,地址 ${this.req.path}`);
}
hehe() {
return this.send('hehe');
}
}