midway icon indicating copy to clipboard operation
midway copied to clipboard

动态添加的路由优先级不符合预期

Open zhyupe opened this issue 3 years ago • 0 comments

  • Node Version: v16.15.0
  • Midway Version(Decorator/Core): 3.4.11 / 3.5.1
  • Component Name/Version: -
  • Platform: Mac OS
  • Mini Showcase Repository: -
// controller/home.ts
@Controller('/')
export class HomeController {
  @Get('/**')
  async home() {
    return 'main';
  }

  @Get('/test/**')
  async test() {
    return 'catch-all';
  }
}

// configuration.ts
@Configuration({
  imports: [egg],
  importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle implements ILifeCycle {
  @App()
  app: Application;

  async onReady() {
    const router = this.app.applicationContext.get(MidwayWebRouterService);

    // (a)
    const info = await router.getFlattenRouterTable();
    console.log(info);

    router.addRouter(
      ctx => {
        ctx.body = 'test';
      },
      {
        requestMethod: 'GET',

        // (b.1) This works as expected.
        url: '/asd',
        prefix: '/test',

        // (b.2) This is buggy
        // url: '/test/asd',
      }
    );

    // (c)
    (router as any).sortPrefixAndRouter();
  }
}
  1. 当使用 (b.2) 的代码时,预期返回 test,实际返回 catch-all;使用 (b.1) 的代码时,返回 test
  2. 在使用 (b.2) 的代码时,当存在 (a) 调用,必须增加 (c) 调用,否则返回 catch-all

zhyupe avatar Sep 20 '22 02:09 zhyupe