likes.js 1.4 KB

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. // 测试接口
  3. const rKey = 'likes'; // routerKey,路由前缀变量
  4. const cKey = 'likes'; // controllerKey,controller名
  5. const zhKey = '我的喜欢';
  6. const routes = [
  7. { method: 'get', path: `/${rKey}`, controller: `${cKey}.index`, name: `${cKey}Query`, zh: `${zhKey}列表查询` },
  8. { method: 'get', path: `/${rKey}/:id`, controller: `${cKey}.show`, name: `${cKey}Show`, zh: `${zhKey}查询` },
  9. { method: 'post', path: `/${rKey}/selikes`, controller: `${cKey}.selikes`, name: `${cKey}Selikes`, zh: `${zhKey}查询喜欢` },
  10. { method: 'post', path: `/${rKey}`, controller: `${cKey}.create`, name: `${cKey}Create`, zh: `创建${zhKey}` },
  11. { method: 'post', path: `/${rKey}/:id`, controller: `${cKey}.update`, name: `${cKey}Update`, zh: `修改${zhKey}` },
  12. { method: 'delete', path: `/${rKey}/:id`, controller: `${cKey}.destroy`, name: `${cKey}Delete`, zh: `删除${zhKey}` },
  13. ];
  14. module.exports = app => {
  15. const { router, config } = app;
  16. const mwares = app.middleware;
  17. for (const route of routes) {
  18. const { method, path, controller: ctl, zh } = route;
  19. let { middleware = [] } = route;
  20. if (!method || !path || !ctl) continue;
  21. // 拼全路径
  22. const allPath = `${config.routePrefix}${path}`;
  23. // 处理中间件
  24. if (middleware.length > 0) middleware = middleware.map(i => mwares[i]());
  25. // 注册路由
  26. router[method](zh, allPath, ...middleware, ctl);
  27. }
  28. };