1.0.8 • Published 5 years ago
salak-router v1.0.8
salak-router
base on koa-router & joi
- Support request validate.
- Support response validate.
Installation
Install using npm:
npm install --save salak-router
API Reference
- salak-router
- Router (extends koa-router)
new Router(opts)
instance
* .addRoute(opts, handler)
- Router (extends koa-router)
Router
new Router(opts)
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 |
.addRoute(opts, handler)
add route for app
Param | Type | Description | |
---|---|---|---|
opts | Object | ||
opts.path | String | router path | |
opts.method | Array | String | router method |
opts.validate | Object | router validation schema | |
handler | Function | router handler |
Example
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)
LICENSE
MIT