2.1.0 • Published 5 years ago

koa-ctrl v2.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

koa-ctrl

License

koa-ctrl is used to define controller base koa@v2.

Install

$ npm i koa-ctrl --save

API

Controller.prototype.action(name, action)

Example

example-1

const controller = new Controller()

controller.action('doHello', async (ctx, next) => {
  ctx.body = 'hello'
  await next()
})

// response.body -> 'hello'

example-2

const controller = new Controller()

controller.action('doHello', {
  before: async (ctx, next) => {
    ctx.body = 'before'
    await next()
  },
  action: async (ctx, next) => {
    ctx.body += '->action'
    await next()
  },
  after: async (ctx, next) => {
    ctx.body += '->after'
    await next()
  }
})

// response.body -> 'before->action->after

example-3

const controller = new Controller()

controller.action('doHello', {
  before: async (ctx, next) => {
    ctx.body = 'before'
    await next()
  },
  action: async (ctx, next) => {
    ctx.body += '->action'
    await next()
  }
})

// response.body -> 'before->action

Test

$ npm test (coverage 100%)

License

MIT