zaaack / Koa Dec Router
Licence: mit
An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
Stars: ✭ 19
Programming Languages
javascript
184084 projects - #8 most used programming language
es6
455 projects
Projects that are alternatives of or similar to Koa Dec Router
Koatty
Koa2 + Typescript = Koatty. Use Typescript's decorator implement IOC and AOP.
Stars: ✭ 67 (+252.63%)
Mutual labels: koa, koa2, router
Trafficlight
🚦 Flexible NodeJS Routing Decorators for API Routing
Stars: ✭ 69 (+263.16%)
Mutual labels: koa, koa2, decorators
koa-smart
A framework base on Koajs2 with Decorator, Params checker and a base of modules (cors, bodyparser, compress, I18n, etc…) to let you develop smart api easily
Stars: ✭ 31 (+63.16%)
Mutual labels: decorators, class, koa2
Joi Router
Configurable, input and output validated routing for koa
Stars: ✭ 418 (+2100%)
Mutual labels: koa, router
Koahub
KoaHub.js -- 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件。当前项目已停止维护,推荐使用Doodoo.js
Stars: ✭ 308 (+1521.05%)
Mutual labels: koa, koa2
Egg
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Stars: ✭ 17,616 (+92615.79%)
Mutual labels: koa, koa2
toxic-decorators
Library of Javascript decorators
Stars: ✭ 26 (+36.84%)
Mutual labels: decorators, class
Koa Rest Api Boilerplate
💯 Boilerplate for Node.js Koa RESTful API application with Docker, Swagger, Jest, CodeCov and CircleCI
Stars: ✭ 420 (+2110.53%)
Mutual labels: koa, koa2
Vue Koa Demo
🔰A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)
Stars: ✭ 730 (+3742.11%)
Mutual labels: koa, koa2
koa2-winston
koa2 version winston logger like express-winston
Stars: ✭ 37 (+94.74%)
Mutual labels: koa, koa2
route-decorators
ES7 decorators that simplify Koa and Express route creation
Stars: ✭ 71 (+273.68%)
Mutual labels: koa, decorators
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (+3552.63%)
Mutual labels: koa, koa2
Agile-Server
A simple, fast, complete Node.js server solution, based on KOA. 简单快速的 、性能强劲的、功能齐全的 node 服务器解决方案合集,基于 KOA。
Stars: ✭ 24 (+26.32%)
Mutual labels: koa, koa2
koa-dec-router
- An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
- Using koa-router under the hood.
- Examples
Install
npm i koa-dec-router
or
yarn add koa-dec-router
Demo
app.js
import Koa from 'koa'
import DecRouter from 'koa-dec-router'
const decRouter = DecRouter({
controllersDir: `${__dirname}/controllers`,
before: null, // global middleware
after: null, // global middleware
})
const app = new Koa()
// decRouter.router: `koa-router` instance
app.use(decRouter.router.routes())
app.use(decRouter.router.allowedMethods())
controllers/api.js
import { controller, get, post } from 'dec-router'
async function apiHandler(ctx, next) {
console.log('handle all api and subclass\'s')
await next()
}
@controller('/api', apiHandler)
export default class Api {
async getApiCommon(ctx) {
// ...
return // some common data
}
}
controllers/posts.js
import { controller, get, post } from 'dec-router'
import Api from './api'
async function postHandler(ctx, next) {
console.log('handle post')
await next()
}
// define multi controller class in one file. You can passing {expose: false} to disable exposing this controller, which can still be inherit.
@controller('/subpost')
export class Subpost {
@get('s')
async list(ctx) {
ctx.body = 'get subpost'
}
}
@controller('/post')
export default class Post extends Api {
@get('s') // final path = parent controller path + controller path + method path
async list(ctx) {
const commonData = await super.getApiCommon()
ctx.body = 'get posts'
}
@get('/:id', {priority: -1}) // wildcard using low priority, let `special` method handle first
async get(ctx) {
ctx.body = 'get' + ctx.params.id
}
@get('/special')
async special(ctx) {
ctx.body = 'special post'
}
}
Console output
To output all routes generated by dec-router, you can run your app like
DEBUG=dec-router,your-app:* node ./your-app.js
For windows, using cross-env
cross-env DEBUG=dec-router,your-app:* node ./your-app.js
API Reference
DecRouter(options)
- options: {object}
- controllersDir: {string} controllers directory
- before: {function} optional, first middleware for all controller methods
- after: {function} optional, last middleware for all controller methods (before controller method)
- autoLoadControllers: {bool} optional, default is true
@controller(path, opts, ...middlewares)
Controller decorator.
- path: {string} optional, path of this controller, default is
'/' + slug(cls.name)
, can be inherited - opts: {object} optional, options, cannot be inherited
- ignoreParentPath: {bool} optional, default is false
- ignoreParentMdws: {bool} optional, default is false
- expose: {bool} optional, expose as a route default is false
- middlewares: {Array} optional, koa middlewares, can be inherited
@route(method, path, opts, ...middlewares)
Controller method decorator, default would totally override superclass's method with same path (not same name), including method, opts, middlewares, etc.
- method: {string}, one of
'get', 'head', 'post', 'put', 'delete', 'patch', 'use'
; - path: {string} optional, path of this method, default is
'/' + slug(method.name)
- opts: {object} optional, options
- priority: {number} optional, larger before smaller, default is 0
- ignoreCtrlPath: {bool} optional, ignore all controller path, including superclass's, default is false
- middlewares: {Array} optional, koa middlewares
@get(path, opts, ..middlewares)
alias of @route('get', path, opts, ...middlewares)
@head(path, opts, ..middlewares)
alias of @route('head', path, opts, ...middlewares)
@post(path, opts, ..middlewares)
alias of @route('post', path, opts, ...middlewares)
@put(path, opts, ..middlewares)
alias of @route('put', path, opts, ...middlewares)
@del(path, opts, ..middlewares)
alias of @route('delete', path, opts, ...middlewares)
@patch(path, opts, ..middlewares)
alias of @route('patch', path, opts, ...middlewares)
@all(path, opts, ..middlewares)
alias of @route('use', path, opts, ...middlewares)
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].