koa-ctrl

2.1.0 • Public • Published

koa-ctrl

[![License][license-image]][license-url]

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

Package Sidebar

Install

npm i koa-ctrl

Weekly Downloads

3

Version

2.1.0

License

MIT

Unpacked Size

4.76 kB

Total Files

5

Last publish

Collaborators

  • xsmallbird