项目作者: eggjs

项目描述 :
The missing router feature for eggjs
高级语言: JavaScript
项目地址: git://github.com/eggjs/egg-router-plus.git
创建时间: 2017-12-08T16:24:56Z
项目社区:https://github.com/eggjs/egg-router-plus

开源协议:MIT License

下载


egg-router-plus

NPM version
build status
Test coverage
David deps
Known Vulnerabilities
npm download

The missing router features for eggjs

Install

  1. $ npm i egg-router-plus --save

Then mount plugin:

  1. // {app_root}/config/plugin.js
  2. exports.routerPlus = {
  3. enable: true,
  4. package: 'egg-router-plus',
  5. };

Features

load app/router/**/*.js

this plugin will auto load router define at app/router/**/*.js.

Notice: all sub routers will be loaded before app/router.js, please ensure all the sub router definitions are not conflict(better to use app.router.namespace to create different namespaces for each sub router file).

app.router.namespace

  1. app.router.namespace(prefix, ...middlewares);
  • prefix - {String}, the prefix string of sub router
  • middlewares - {…Function}, optional group middlewares

Support same as Router:

  • router.verb('path-match', app.controller.action);
  • router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
  • router.verb('router-name', 'path-match', app.controller.action);
  • router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);

prefix and path are not allow to be regex, and prefix can’t be ‘/‘.

  1. // {app_root}/app/router.js
  2. module.exports = app => {
  3. const subRouter = app.router.namespace('/sub');
  4. // curl localhost:7001/sub/test
  5. subRouter.get('/test', app.controller.sub.test);
  6. subRouter.get('sub_upload', '/upload', app.controller.sub.upload);
  7. // const subRouter = app.router.namespace('/sub/:id');
  8. // const subRouter = app.router.namespace('/sub', app.middleware.jsonp());
  9. // output: /sub/upload
  10. console.log(app.url('sub_upload'));
  11. };

Every different prefix will bind to different router instance, and all the namespaces will sort by trie tree to ensure best match.

  1. module.exports = app => {
  2. const apiRouter = app.router.namespace('/api');
  3. const apiWebRouter = app.router.namespace('/api/web');
  4. const apiWebAdminRouter = app.router.namespace('/api/web/admin');
  5. apiRouter.get('/:a/:b/:c', controller.api.one);
  6. apiWebRouter.get('/:a/:b', controller.api.two);
  7. apiWebAdminRouter.get('/:a', controller.api.three);
  8. // /api/web/admin/hello => controller.api.three
  9. // /api/web/foo/hello => controller.api.two
  10. // /api/foo/bar/hello => controller.api.one
  11. };

Known issues

  • sub redirect is not support, use app.router.redirect() or redirect to a named router.
  1. const subRouter = app.router.namespace('/sub');
  2. // will redirect `/sub/go` to `/anyway`, not `/sub/anyway`
  3. subRouter.redirect('/go', '/anyway');
  4. // just use router
  5. router.redirect('/sub/go', '/sub/anyway');
  6. // or redirect to a named router
  7. subRouter.get('name_router', '/anyway', app.controller.sub.anyway);
  8. // will redirect `/sub/go_name` to `/sub/anyway` which is named `name_router`
  9. subRouter.redirect('/sub/go_name', 'name_router');

Questions & Suggestions

Please open an issue here.

License

MIT