koa-ctrl
v2.1.0
Published
Router middleware for koa. Provides a way to define controller
Downloads
14
Maintainers
Readme
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