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 🙏

© 2025 – Pkg Stats / Ryan Hefner

koa2-router-decors

v0.0.5

Published

koa2路由装饰器

Downloads

23

Readme

一、关于重复造轮子解释下

npmjs上搜索关于koa路由装饰器的已经有那么几个包了,但是我从几个包中发现作者的思维仅仅限制于前端开发的思想,项目分层不明确,我们开发kow-web项目可以根据java-web中项目分层的思想来写项目,项目结构清晰明了,本人封装这个包也是参考了java-web开发过程中把项目分为四层架构。

  • 1、controllers:路由的控制
  • 2、servers:常用于一些业务逻辑的判断
  • 3、dao:操作数据库的
  • 4、models:关于建表的数据模型

二、关于koa2-router-decors包的使用步骤

  • 1、构建一个项目,并创建分层目录

  • 2、安装

    npm install koa2-router-decors
    // or
    yarn add koa2-router-decors
  • 3、在中间件中使用我们安装的包

    import { resolve } from 'path';
    import Route from 'koa2-router-decors';
    // 可以写到config中统一配置
    const API_VERSION = '/api/v1';
    /**
     * @Description: 反转路径的方法
     * @param {String} 
     * @return: 
     */
    const dir = path => resolve(__dirname, path);
    
    /**
     * @Description: 路由中间件读取controllers中的装饰器配置
     * @param {type} 
     * @return: 
     */
    export default (app) => {
      // 这个地方是要读取的文件夹目录
      const apiPath = dir('../controllers/*');
      // 实例化类并调用方法
      const route = new Route(app, apiPath, API_VERSION);
      route.init();
    };
  • 4、使用中间件

  • 5、在controllers的文件夹中使用装饰器

    @controller('/user')
    export class UserController extends BaseController {
      constructor() {
        super();
      }
      /**
       * 
       * @api {post} /api/v1/user/create/ 添加用户
       * @apiDescription 创建用户的接口
       * @apiName createUser
       * @apiGroup users
       * @apiVersion  0.1.0
       * @apiParam {string} username="张三" 用户名
       * @apiParam {string} mobile 手机号码
       * @apiParam {string} email 邮箱
       * @apiParam {string} password 密码
       */
      @post('/create')
      @required({ body: ['username', 'mobile', 'password'] })
      async createUser(ctx) {
        const result = await UserServer.createUser(ctx.request.body);
        ctx.success(result);
      }
      ....
    }
  • 6、具体代码可以参考example中写的

三、关于example代码跑起来的说明

  • 1、使用的是mysql

  • 2、mysql建表sql

    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(30) NOT NULL,
      `mobile` varchar(11) DEFAULT NULL,
      `email` varchar(20) DEFAULT NULL,
      `password` varchar(255) NOT NULL,
      `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
      `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
  • 3、在example的根目录下创建一个.env的文件

    DB_HOST=数据库地址
    DB_USERNAME=数据库连接名
    DB_PASSWORD=数据库连接密码
    DB_DATABASE=数据库名