nest-router: Nested routes do not keep prefix

I’m having a bit of trouble keeping my route definitions modular.

In this repro repo I have the AppModule that says hello when /prefix/hello is called. But I intended the SubModule to respond when /prefix/sub/hello is called. But if I just use @Controller('sub') in the SubController, then it responds only to /sub/hello.

Now, I have read a bit of the original discussion with kamilmysliwiec and understand that modules are not meant to map directly to the paths. But right now it feels like needing to declare all routes in a single module hurts modularity.

Is there a way to prefix all controllers of a module as well as their imported controllers?

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (10 by maintainers)

Most upvoted comments

Hi @Y0hy0h , unfortunately that’s not an option right now, it’s one of Nest design gools, the isolations.

when i was playing with nest, i have a fork of it, actually i achieved something similar to this https://github.com/nestjs/nest/issues/255#issuecomment-361568409

but there is something you may not have noticed, the children prop in Routes could be also an array of modules. so instead of doing something like this

// root.module.ts
...
RouterModule.forRoutes([
      { path: '/prefix', module: AppModule },
      { path: '/prefix', module: SubModule },
    ]),
...

you could just

// root.module.ts

RouterModule.forRoutes([ { path: '/prefix', children: [AppModule, SubModule] } ]),

i know, that’s not what you are looking for, but, well… hmmm… it will work as expected.

This may be the ticket:

      { path: 'nested/cats', module: CatsModule },
      { path: ':ninjaId/cats', module: CatsModule },

To map the same module twice…once with the parent id and once without.

Thanks for your explanation! I just wanted to check whether I didn’t understand something, but I get why it’s difficult to design and implement.

Just a quick remark: In your 2. example I would expect ModuleB to be accessible through both /a/b and /a/c. 😉

Thanks for your answer!