https://github.com/buunguyen/route-decorators
ES7 decorators that simplify Koa and Express route creation
https://github.com/buunguyen/route-decorators
decorators es7-decorators koa routes
Last synced: about 1 month ago
JSON representation
ES7 decorators that simplify Koa and Express route creation
- Host: GitHub
- URL: https://github.com/buunguyen/route-decorators
- Owner: buunguyen
- License: mit
- Created: 2015-11-19T21:24:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-28T19:17:37.000Z (about 5 years ago)
- Last Synced: 2025-03-19T07:03:40.468Z (about 1 month ago)
- Topics: decorators, es7-decorators, koa, routes
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 75
- Watchers: 4
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
## route-decorators for Koa/Express
[](https://www.npmjs.com/package/route-decorators)
[](https://travis-ci.org/buunguyen/route-decorators)
[ES7 decorators](https://github.com/wycats/javascript-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__
```js
import {controller, get, post} from 'route-decorators'@controller('/users', middleware1)
class UserCtrl {@get('/:id', middleware2, middleware3)
async get(context, next) {}@post(middleware2)
async post(context, next) {}
}
```__Express__
```js
import {controller, get, post} from 'route-decorators'@controller('/users', middleware1)
class UserCtrl {@get('/:id', middleware2, middleware3)
async get(req, res, next) {}@post(middleware2)
async post(req, res, next) {}
}
```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__
```js
import Router from 'koa-66'// Inside controller constructor
this.router = new Router()
for (const {method, url, middleware, fnName} of this.$routes) {
this.router[method](url, ...middleware, this[fnName].bind(this))
}
```__Express__
```js
import express from 'express'// Inside controller constructor
this.router = express.Router()
for (const {method, url, middleware, fnName} of this.$routes) {
this.router[method](url, ...middleware, (req, res, next) => {
this[fnName](req, res, next).catch(next)
})
}
```You can move the above logic to some base controller in your app and reuse it for every controller. For example:
```js
class BaseCtrl {
constructor() {
this.router = new Router()
for (const {method, url, middleware, fnName} of this.$routes) {
this.router[method](url, ...middleware, this[fnName].bind(this))
}
}
}@controller(...)
class UserCtrl extends BaseCtrl {
// 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 the `method` argument.### Test
```bash
npm install
npm test
```