user.js 1.1 KB

12345678910111213141516171819202122232425
  1. 'use strict';
  2. // 路由配置
  3. const routes = [
  4. { method: 'get', path: '/user', controller: 'user.index', name: 'userQuery', zh: '用户列表查询' },
  5. { method: 'get', path: '/user/:id', controller: 'user.show', name: 'userShow', zh: '用户查询' },
  6. { method: 'post', path: '/user', controller: 'user.create', middleware: [ 'password' ], name: 'userCreate', zh: '创建用户' },
  7. { method: 'post', path: '/user/:id', controller: 'user.update', middleware: [ 'password' ], name: 'userQuery', zh: '修改用户' },
  8. { method: 'delete', path: '/user/:id', controller: 'user.destroy', name: 'userQuery', zh: '删除用户' },
  9. ];
  10. module.exports = app => {
  11. const { router, config } = app;
  12. const mwares = app.middleware;
  13. for (const route of routes) {
  14. const { method, path, controller: ctl, zh } = route;
  15. let { middleware = [] } = route;
  16. if (!method || !path || !ctl) continue;
  17. // 拼全路径
  18. const allPath = `${config.routePrefix}${path}`;
  19. // 处理中间件
  20. if (middleware.length > 0) middleware = middleware.map(i => mwares[i]());
  21. // 注册路由
  22. router[method](zh, allPath, ...middleware, ctl);
  23. }
  24. };