express
import {controller, get, post} from 'router-control'
@controller('/api')
class Ctrl {
@get('/test')
test() {}
}
import express from 'express'
// Inside controller constructor
this.router = express.Router()
for (const {methodName, path, executer} of this.$$path) {
this.router[methodName](path, (req, res, next) => {
this[executer](req, res, next).catch(next)
})
}
或者像下面这样子
class BaseCtrl {
constructor() {
this.router = new Router()
for (const {methodName, path, executer} of this.$$path) {
this.router[methodName](path, (req, res, next) => {
this[executer](req, res, next).catch(next)
})
}
}
}
@controller(...)
class UserCtrl extends BaseCtrl {
// decorated methods as above
}
npm install
npm test