安装
pnpm install require-directory
  • 1.
路由加载
static initRouters() {
    // 绝对路径
    const apiDir = `${process.cwd()}/router`;

    // 自动加载路由
    requireDirectory(module, apiDir, {
      visit: whenLoadModule
    });

    // 判断加载模块是否是路由
    function whenLoadModule(obj) {
      if (obj instanceof Router) {
        /**
         * 用中间件启动路由
         * router.routes() 启动路由
         * router.allowedMethods() 允许任何请求
         */
        InitManager.app.use(obj.routes(), obj.allowedMethods());
      }
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.