建议采用:集中式路由,
不要把路由规则逻辑散落在多个地方,会给排查问题带来困扰

路由分组

image.png
路由拆分,路由映射 app/router/user.js

  1. /**
  2. * User: lulongwen
  3. * Date: 2021-06-20 18:28
  4. * Description:
  5. */
  6. 'use strict';
  7. module.exports = app => {
  8. const { router, controller } = app;
  9. console.log('router-child', router);
  10. router.get('/api/users', controller.user.index);
  11. };

router.js

app/router.js

  1. 'use strict';
  2. /**
  3. * @param {Egg.Application} app - egg application
  4. * app 就是 Egg的实例
  5. */
  6. module.exports = app => {
  7. const { router, controller } = app;
  8. require('./router/user')(app);
  9. router.get('/404.html', controller.error.notFound);
  10. };

router拆分路由

app/router.js

  1. 'use strict';
  2. /**
  3. * @param {Egg.Application} app - egg application
  4. */
  5. module.exports = app => {
  6. require('./router/home')(app);
  7. require('./router/news')(app);
  8. require('./router/admin')(app);
  9. };

router/*

  1. // app/router/news.js
  2. module.exports = app => {
  3. app.router.get('/news/list', app.controller.news.list);
  4. app.router.get('/news/detail', app.controller.news.detail);
  5. };
  6. // app/router/admin.js
  7. module.exports = app => {
  8. app.router.get('/admin/user', app.controller.admin.user);
  9. app.router.get('/admin/log', app.controller.admin.log);
  10. };

路由重定向

路由内部重定向

  1. // app/router.js
  2. module.exports = app => {
  3. app.router.get('index', '/home/index', app.controller.home.index);
  4. app.router.redirect('/', '/home/index', 302);
  5. };
  6. // app/controller/home.js
  7. exports.index = async ctx => {
  8. ctx.body = 'hello controller';
  9. };
  10. // curl -L http://localhost:7001

外部重定向

  1. // app/router.js
  2. module.exports = app => {
  3. app.router.get('/search', app.controller.search.index);
  4. };
  5. // app/controller/search.js
  6. exports.index = async ctx => {
  7. const type = ctx.query.type;
  8. const q = ctx.query.q || 'nodejs';
  9. if (type === 'bing') {
  10. ctx.redirect(`http://cn.bing.com/search?q=${q}`);
  11. } else {
  12. ctx.redirect(`https://www.google.co.kr/search?q=${q}`);
  13. }
  14. };
  15. // curl http://localhost:7001/search?type=bing&q=node.js
  16. // curl http://localhost:7001/search?q=node.js

egg-router-plus

egg-router-plus 按命名空间划分路由
自己的业务域下进行管理
https://github.com/eggjs/egg-router-plus

  1. yarn add egg-router-plus

config/plugin.js

  1. exports.routerPlus = {
  2. enable: true,
  3. package: 'egg-router-plus',
  4. };

自动注册路由

读取 controller 下的文件和 router 下的文件,进行注册路由

  1. const Router = router.namespace('/api/v1');
  2. const Controller = controller;
  3. const RouterBasePath = path.resolve(__dirname, './routes/v1');
  4. const requireRouters = async (basePath, router, controller, middleware) => {
  5. const files = await fsPromises.readdir(basePath);
  6. files.forEach(file => {
  7. require(path.join(basePath, file))(router, controller, middleware);
  8. });
  9. };
  10. requireRouters(RouterBasePath, Router, Controller, middleware);