'use strict'; /** * 注册路由 * @param {App} app 全局变量 * @param {Array} routes 路由数组 * @param {String} keyZh 路由主题 * @param {String} rkey 路由主位置的标识 * @param {String} ckey 路由对应的controller的标识 */ const _ = require('lodash'); module.exports = (app, routes, keyZh, rkey, ckey) => { const { router, config } = app; const mwares = app.middleware; // if (process.env.NODE_ENV === 'development') console.log(`${keyZh}: ${rkey}`); for (const route of routes) { const { method, path, controller: ctl, zh } = route; let { middleware = [] } = route; if (!method || !path || !ctl) continue; // 拼全路径 const allPath = `${config.routePrefix}/${path}`; // if (process.env.NODE_ENV === 'development') console.log(` ${zh}: ${allPath}`); // 处理中间件 if (middleware.length > 0) middleware = middleware.map(i => _.get(mwares, i)({ enable: true, model: ckey || rkey })); // 注册路由 router[method](zh, allPath, ...middleware, ctl); } };