Skip to content

ES7 decorators that control koa and express router

Notifications You must be signed in to change notification settings

Liqihan/router-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

router-control

用es7的装饰器来实现koa和express的路由功能,

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
}

test

npm install 
npm test

About

ES7 decorators that control koa and express router

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published