JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q42059F
  • License MIT

Router for salak. Provide validation routes.

Package Exports

  • salak-router

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (salak-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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