1.0.4 • Published 4 years ago
koa-captor v1.0.4
koa-captor
Installation
npm install --save koa-captor
Usage
const Koa = require('koa')
const Joi = require('@hapi/joi')
const Router = require('koa-router')
const captor = require('koa-captor')
const app = new Koa()
const router = new Router()
const middleware1 = async (ctx, next) => {
console.log('middleware 1')
await next()
}
const middleware2 = async (ctx, next) => {
console.log('middleware 2')
await next()
}
router.get('/:name', captor({
validations: {
// or params or query or files or file
body: Joi.object({
name: Joi.string().required()
})
},
middlewares: [
middleware1,
middleware2
],
handler: ({ request, response }) => {
return response.code(201).json({ hello: "world" })
}
}))
Other ways to use the handler method:
handler: ({ request, response }) => {
return { hello: "world" }
}
handler: ({ request, response }) => {
return response.json({ hello: "world" })
}
handler: ({ request, response }) => {
return response.code(404)
}