'use strict'; // 路由配置 const routes = [ { method: 'get', path: '/user', controller: 'user.index', name: 'userQuery', zh: '用户列表查询' }, { method: 'get', path: '/user/:id', controller: 'user.show', name: 'userShow', zh: '用户查询' }, { method: 'post', path: '/user', controller: 'user.create', middleware: [ 'password' ], name: 'userCreate', zh: '创建用户' }, { method: 'post', path: '/user/:id', controller: 'user.update', middleware: [ 'password' ], name: 'userQuery', zh: '修改用户' }, { method: 'delete', path: '/user/:id', controller: 'user.destroy', name: 'userQuery', zh: '删除用户' }, ]; module.exports = app => { const { router, config } = app; const mwares = app.middleware; 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 (middleware.length > 0) middleware = middleware.map(i => mwares[i]()); // 注册路由 router[method](zh, allPath, ...middleware, ctl); } };