// path/to/main/koa/server.ts import { getRouterSync } from '@tng/koa-controller' const app = new Koa()
// inject all related controller files by given glob pattern app.use(getRouterSync({ files: path.resolve('path/to/controller/*/.[jt]s'), }).routes())
`
Router
$3
Register a route on a class for router as a controller.
NOTE The router will ignore the route if no route method is provided in the class. And for the contrast, the route method will be ignored unless the controller is registed.
$3
Register a route on a method for router. It will run other middleware as well when route is matched. The sequence is in middleware squence section.
Method's first parameter is the state of context, or ctx.state; second paramenter is the koa context or ctx for the request. Method should be a thenable async function which return the result for response body.
NOTE A method can be registered for multi routes. all the result and the middlewares will be shared.
example: `ts class SomeController { @request('get', '/login') async somePath(state, ctx: Context) { // state is for context.state // ctx is for context return result // equivalent to ctx.body = result } } `
$3
$3
$3
Register a before middleware, a after middleware or a middleware for a route which register either on method or controller class.
$3
if same decorator written 2 or more times, router will run in sequence from top to bottom. Please see test/router.test.ts file to learn more about it.