routerRegister.js 1.0 KB

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. /**
  3. * 注册路由
  4. * @param {App} app 全局变量
  5. * @param {Array} routes 路由数组
  6. * @param {String} keyZh 路由主题
  7. * @param {String} rkey 路由主位置的标识
  8. * @param {String} ckey 路由对应的controller的标识
  9. */
  10. const _ = require('lodash');
  11. module.exports = (app, routes, keyZh, rkey, ckey) => {
  12. const { router, config } = app;
  13. const mwares = app.middleware;
  14. // if (process.env.NODE_ENV === 'development') console.log(`${keyZh}: ${rkey}`);
  15. for (const route of routes) {
  16. const { method, path, controller: ctl, zh } = route;
  17. let { middleware = [] } = route;
  18. if (!method || !path || !ctl) continue;
  19. // 拼全路径
  20. const allPath = `${config.routePrefix}/${path}`;
  21. // if (process.env.NODE_ENV === 'development') console.log(` ${zh}: ${allPath}`);
  22. // 处理中间件
  23. if (middleware.length > 0) middleware = middleware.map(i => _.get(mwares, i)({ enable: true, model: ckey || rkey }));
  24. // 注册路由
  25. router[method](zh, allPath, ...middleware, ctl);
  26. }
  27. };