egg-decorator-router
Define egg.js router and middleware use decorator.
Install
npm i egg-decorator-router --save
Usage
// {app_root}/config/plugin.jsexportsdecoratorRouter = enable: true package: 'egg-decorator-router'
基于 typescript 的 eggjs 项目可直接使用装饰器
如果是 js 项目,则需要手动安装babel-plugin-transform-decorators-legacy
和babel-plugin-transform-object-rest-spread
这两个包,并在项目里加入.babelrc
文件
.babelrc 定义如下:
Specification
The full path is combin between root-path and sub-path.
在 controller 中先引入依赖
const Route HttpAll HttpGet HttpPost HttpPut HttpPatch HttpDelete Middleware } =
如果使用 typescript
Use Route define a root-path on the controller
Define a root path on controller
// root path is '/'@ // root path is '/'@ // root path is '/routename'@ // root path is '/routename/action'@
Parameter is available
@
Use HttpMethod define a sub-path
HttpMethod include HttpGet
HttpPost
HttpPut
HttpPatch
HttpDelete
and HttpAll
Define a sub-path in controller's method
// sub-path is '/'@ // sub-path is '/'@ // sub-path is '/action'@ // sub-path is '/action/:id'@
Define middleware
@
Example
'use strict' const Controller = const Route HttpGet Middleware filters = const DefaultFilter = filters const routeM = { console } const actionM = { return { console }} @@ @ // path: / async { await { thisctxbody = 'ssss' } } @ // path: /func1 @ @ { ctxbody = 'hi, func1' } @ // path: /:id @ { ctxbody = 'hi, func2' + ctxparamsid } moduleexports = HomeController