12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- 'use strict';
- // 测试接口
- const rKey = 'table'; // routerKey,路由前缀变量
- const cKey = 'table'; // controllerKey,controller名
- const zhKey = '桌台表';
- const routes = [
- {
- method: 'get',
- path: `/${rKey}`,
- controller: `${cKey}.index`,
- name: `${cKey}Query`,
- zh: `${zhKey}列表查询`,
- },
- {
- method: 'get',
- path: `/${rKey}/:id`,
- controller: `${cKey}.show`,
- name: `${cKey}Show`,
- zh: `${zhKey}查询`,
- },
- {
- method: 'post',
- path: `/${rKey}`,
- controller: `${cKey}.create`,
- name: `${cKey}Create`,
- zh: `创建${zhKey}`,
- },
- {
- method: 'post',
- path: `/${rKey}/:id`,
- controller: `${cKey}.update`,
- name: `${cKey}Update`,
- zh: `修改${zhKey}`,
- },
- {
- method: 'delete',
- path: `/${rKey}/:id`,
- controller: `${cKey}.destroy`,
- name: `${cKey}Delete`,
- zh: `删除${zhKey}`,
- },
- ];
- 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);
- }
- };
|