route-decorators for Koa/Express
ES7 decorators that simplify Koa and Express route creation. Using these decorators, you can write your controllers like below and have all the routes populated.
Koa
@ @ async {} @ async {}
Express
@ @ async {} @ async {}
Once the decorators are applied, every controller instance will receive a $routes
array, which you can use to define actual Koa/Express routes.
Assume the above UserCtrl
definition, you can define routes in UserCtrl
's constructor (although really you can put the code anywhere) as follows:
Koa
// Inside controller constructorthisrouter = for const method url middleware fnName of this$routes thisroutermethodurl ...middleware thisfnName
Express
// Inside controller constructorthisrouter = expressfor const method url middleware fnName of this$routes thisroutermethodurl ...middleware { thisfnNamereq res next }
You can move the above logic to some base controller in your app and reuse it for every controller. For example:
{ thisrouter = for const method url middleware fnName of this$routes thisroutermethodurl ...middleware thisfnName } @ // decorated methods as above
Decorators
@controller(path: optional, ...middleware: optional)
@route(method, path: optional, ...middleware: optional)
@head
,@options
,@get
,@post
,@put
,@patch
,@del
,@delete
,@all
: wrappers of@route
that automatically supply themethod
argument.
Test
npm installnpm test