channel.js 1.4 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. // 路由配置
  3. const rkey = 'channel';
  4. const ckey = 'channel.channel';
  5. const keyZh = '科技频道';
  6. const routes = [
  7. { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}Login`, zh: `${keyZh}登陆` },
  8. { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
  9. { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
  10. { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
  11. { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
  12. { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
  13. ];
  14. module.exports = app => {
  15. const { router, config } = app;
  16. const mwares = app.middleware;
  17. console.log(`${keyZh}: ${rkey}`);
  18. for (const route of routes) {
  19. const { method, path, controller: ctl, zh } = route;
  20. let { middleware = [] } = route;
  21. if (!method || !path || !ctl) continue;
  22. // 拼全路径
  23. const allPath = `${config.routePrefix}/${path}`;
  24. // 处理中间件
  25. if (middleware.length > 0) middleware = middleware.map(i => mwares[i]({ enable: true }));
  26. // 注册路由
  27. router[method](zh, allPath, ...middleware, ctl);
  28. }
  29. };