0.2.6 • Published 1 year ago

koa-controller-register v0.2.6

Weekly downloads
-
License
BSD
Repository
github
Last release
1 year ago

koa-controller-register

An ES6 decorator based router, support auto load controllers.

$ npm install koa-controller-register

decorators

class decorators:

  • @Middlewares()
  • @Controller()

method decorators:

  • @Get()
  • @Post()
  • @Delete()
  • @Put()
  • @Patch()
  • @All()
  • @Middlewares()
  • @Before()

example

  • example/index.ts :
import Application from 'koa'
import { useControllers } from 'koa-controller-register'
import PingController from './ping.controller'

const controllers = [PingController]
const app = new Application()
// ...
useControllers(app, controllers)
app.listen(8080)
  • example/middlewares.ts :
import { Context, Next } from 'koa'

export async function middleware0(ctx: Context, next: Next) {
    console.log('middleware 0')
    await next()
}

export async function middleware1(ctx: Context, next: Next) {
    console.log('middleware 1')
    await next()
}
  • example/ping.controller.ts :
import { Context } from 'koa'
import { Before, Controller, Get, Middlewares } from 'koa-controller-register'
import { middleware0, middleware1 } from './middlewares'

@Controller('/')
@Middlewares(middleware0)
export default class PingController {
    @Get('/ping')
    async ping(ctx: Context) {
        ctx.body = 'pong' // will print "middleware 0"
    }

    @Get('/example')
    @Before(middleware1)
    async test(ctx: Context) {
        ctx.body = 'test' // will print "middleware 0\n middleware 1"
    }
}
0.2.6

1 year ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago