npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-router-simple

v2.0.1

Published

Simple router for koa2 and base on @koa/router、koa-body

Downloads

4

Readme

koa-router-simple

基于koa2的路由封装中间件,只需按照统一格式配置路由对象即可,并集成了参数类型校验功能。

安装

使用NPM

  npm i -S koa-router-simple

使用YARN

  yarn add koa-router-simple

使用方式:

CommonJS模块引入

  const simpleRouter = require('koa-router-simple');
  // 使用 koa-router-simple
  const app = new Koa();
  app.use(simpleRouter(allRoutes));

其中allRoutes表示项目中所有的路由对象数组,其格式如下:

  allRoutes = [...UserRouter, ...OtherRouter]

其中UserRouter、OtherRouter表示需要用户配置的路由对象,格式如下:

  UserRouter = [
    {
      url: '/user/login',
      method: 'post',
      // params: null,
      params: {
        username: {
          type: ['string', 'number'],
          required: true
        },
        password: {
          type: 'string',
          required: true
        }
      },
      handler: async(ctx, next) {
        // url路由对应的具体处理逻辑
      }
    }
  ]

请求参数校验:

  • params属性表示需要对请求参数进行校验的规则,其中表示要校验的参数名,该键所对应的对象为具体的规则。
  • 其中type表示参数值的类型,值为字符串或者字符串数组,字符串内容为使用运算符typeof能够得到的值。
  • required表示该参数是否必须传递。

当校验完之后,会出现如下两种情况:

  • 一种是参数规则匹配正确,此时无任何副作用(影响)。
  • 第二种情况就是参数规则匹配不对应,此时,将产生副作用:
    • ctx.response.status 被设置为400
    • ctx.request.paramsErrors 被设置为具体的校验异常信息数组(errors)

errors的格式如下:

  errors = [
    {
      code: number,  //0 | 1,0表示参数值类型异常,1表示参数缺失异常。
      key: string,   // "校验失败的字段名"
      value: any,    // "校验失败的字段值"
      type: string   // "该字段值的正确类型"
    }
  ]

开发者可以通过检测ctx.response.status来判断参数校验是否有异常;或者可以通过ctx.request.paramsErrors的长度来判断是否有参数校验异常,且可以根据该字段中的具体的校验异常对象信息来组装返回信息给接口调用方。

注意

此中间件基于包@koa/routerkoa-body,如需使用此中间件,需要先安装以上两个包!