Skip to content

SalakJS/salak-router

Repository files navigation

salak-router

NPM version build status David deps NPM download

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

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
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

About

Router for salak base on joi & koa.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published