base on koa-router & joi
- Support request validate.
- Support response validate.
Install using npm:
npm install --save salak-router
- salak-router
- Router (extends koa-router)
* new Router([opts])
* instance
* .addRoute(opts, handler)
- Router (extends koa-router)
Param | Type | Description |
---|---|---|
[opts] | Object |
|
[opts.prefix] | String |
prefix router paths |
[opts.requestFailure] | Number |
request validation error status |
[opts.responseFailure] | String |
response validation error status |
add route for app
Param | Type | Description |
---|---|---|
opts | Object |
|
opts.path | String |
router path |
opts.method | Array |
String |
opts.validate | Object |
router validation schema |
handler | Function |
router handler |
const Koa = require('koa')
const Router = require('salak-router')
const Joi = Router.Joi
const app = new Koa()
const router = new Router()
router.addRoute({
path: '/test',
method: 'GET',
validate: {
query: {
id: Joi.number().required().description('文章id')
},
responses: {
200: {
body: Joi.object().keys({
code: Joi.number(),
msg: Joi.string()
}).description('文章详情')
}
}
}
}, async (ctx) => {
ctx.body = {
code: 0,
msg: 'ok'
}
})
app.use(router.routes())
app.listen(3000)
MIT