Egg: [RFC] sub router

Created on 10 May 2017  ·  16Comments  ·  Source: eggjs/egg

Background

currently egg don't support sub router, but koa-router provide prefix() feature.

Proposal

  • provide app.namespace([prefix], [middlewares]).verb()
  • app.url() will traverse all the router, return the first one

example:

// app/router.js
module.exports = app => {
  app.get('/', 'home.index');

  const adminRouter = app.namespace('/admin');
  // GET /admin
  adminRouter.get('/', 'admin.index');
  // GET /admin/user
  adminRouter.get('/user', 'admin.user');
}
feature proposals

Most helpful comment

https://github.com/atian25/egg-router-plus/pull/1

写了个插件,下周再完善下

All 16 comments

prefix change to routerPrefix?

or just getRouter

发自我的 iPhone

在 2017年5月10日,21:17,Haoliang Gao notifications@github.com 写道:

prefix change to routerPrefix?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

https://github.com/eggjs/egg-core/blob/master/lib/utils/router.js#L191 没考虑 router.url(name) 怎么还原的问题?

@fengmk2 traverse all the router, return the first one?

@popomore @fengmk2 RFC and PR is updated, check again

邀请最有经验的 @huacnlee 来看看

  1. 不想在 egg-core 里面提供这个功能,希望是一个扩展的 router 插件,这样也便于大家选择自己喜欢的 router 使用方式,egg-core 只提供基础的 app.verb()
  2. 需要考虑中间件的支持
const router = app.getRouter('/sub', middleware);
router.get('/test1', 'test1');
router.get('/test2', 'test2');

// or

app.namespace('/sub', middleware, router => {
  router.get('/test1', 'test1');
  router.get('/test2', 'test2');
});

我考虑之后单独做个 router 插件,实现一个 rails 实用的特性。

我建议加一个 advance-router 这一类的插件来实现这个需求

作为插件在Application上扩展呢?

  • standalone plugin
  • api name: app.namespace()
  • support middleware

如果更新了,麻烦通知下,很期待这个小功能

https://github.com/atian25/egg-router-plus/pull/1

写了个插件,下周再完善下

@atian25 你好,我用用TS写router时,提示app.router上不存在namespace这个属性。

自己写 d.ts merge 进去。还可以 PR 给这个插件。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

whlsxl picture whlsxl  ·  3Comments

wujianling picture wujianling  ·  3Comments

dizhifeng picture dizhifeng  ·  3Comments

weijiatan456 picture weijiatan456  ·  3Comments

Webjiacheng picture Webjiacheng  ·  3Comments