MCPcopy Index your code
hub / github.com/eggjs/egg-router-plus

github.com/eggjs/egg-router-plus @2.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 2.0.0 ↗ · + Follow
45 symbols 75 edges 20 files 1 documented · 2% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

egg-router-plus

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

The missing router features for eggjs

Install

$ npm i egg-router-plus --save

Then mount plugin:

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

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

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 '/'.

// {app_root}/app/router.js
module.exports = app => {
  const subRouter = app.router.namespace('/sub');
  // curl localhost:7001/sub/test
  subRouter.get('/test', app.controller.sub.test);
  subRouter.get('sub_upload', '/upload', app.controller.sub.upload);

  // const subRouter = app.router.namespace('/sub/:id');
  // const subRouter = app.router.namespace('/sub', app.middleware.jsonp());

  // output: /sub/upload
  console.log(app.url('sub_upload'));
};

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

js module.exports = app => { const apiRouter = app.router.namespace('/api'); const apiWebRouter = app.router.namespace('/api/web'); const apiWebAdminRouter = app.router.namespace('/api/web/admin'); apiRouter.get('/:a/:b/:c', controller.api.one); apiWebRouter.get('/:a/:b', controller.api.two); apiWebAdminRouter.get('/:a', controller.api.three); // /api/web/admin/hello => controller.api.three // /api/web/foo/hello => controller.api.two // /api/foo/bar/hello => controller.api.one };

Known issues

  • sub redirect is not support, use app.router.redirect() or redirect to a named router.
const subRouter = app.router.namespace('/sub');

// will redirect `/sub/go` to `/anyway`, not `/sub/anyway`
subRouter.redirect('/go', '/anyway');

// just use router
router.redirect('/sub/go', '/sub/anyway');

// or redirect to a named router
subRouter.get('name_router', '/anyway', app.controller.sub.anyway);
// will redirect `/sub/go_name` to `/sub/anyway` which is named `name_router`
subRouter.redirect('/sub/go_name', 'name_router');

Questions & Suggestions

Please open an issue here.

License

MIT

Extension points exported contracts — how you extend this code

RouterPlus (Interface)
(no doc) [1 implementers]
index.d.ts
Application (Interface)
(no doc)
index.d.ts

Core symbols most depended-on inside this repo

get
called by 39
test/fixtures/example/app/controller/sub.js
namespace
called by 19
index.d.ts
post
called by 11
test/fixtures/example/app/controller/sub.js
url
called by 5
lib/router.js
put
called by 5
test/fixtures/example/app/controller/sub.js
options
called by 4
test/fixtures/example/app/controller/sub.js
patch
called by 4
test/fixtures/example/app/controller/sub.js
get
called by 2
lib/router.js

Shape

Method 24
Class 14
Function 5
Interface 2

Languages

TypeScript100%

Modules by API surface

test/fixtures/example/app/controller/sub.js10 symbols
lib/router.js10 symbols
test/fixtures/example/app/controller/override.js5 symbols
test/fixtures/example/app/controller/posts.js4 symbols
test/fixtures/example/app/controller/home.js4 symbols
test/fixtures/example/app/controller/admin.js4 symbols
test/fixtures/dir/app/controller/home.js4 symbols
index.d.ts3 symbols
app.js1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add egg-router-plus \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact