Context: I have the following modules in my nestjs app. Which have just the boilerplate code. These modules were generated using command nest g resource resourceName.
- CatModule
- DogModule
- FoodModule
Issue: Nestjs is not allowing to share the FoodModule for two distinct routes. I was under the impression that it can be achieved using RouterModule.register(). But nestjs is only registering the routes /food for DogModule and not for CatModule.
New Understanding: Which ever module comes last in the RouterModule is able to have /food routes
Outcome: I want to achieve the following routing:
/cats/food
/dogs/food
Such that both /cats and /dogs route can access /food routes.
Code: I have use the below code to try achieve my outcome but it doesn't work:
-
app.module.ts@Module({ imports: [ CatsModule, DogsModule, FoodModule, RouterModule.register([ { path: "cats", module: CatsModule, children: [ { path: "/food", module: FoodModule, }, ], }, { path: "dogs", module: DogsModule, children: [ { path: "/food", module: FoodModule, }, ], }, ]), ], controllers: [AppController], providers: [AppService], }) export class AppModule {} -
cats.module.ts@Module({ imports: [FoodModule], controllers: [CatsController], providers: [CatsService], }) export class CatsModule {} -
dogs.module.ts@Module({ imports: [FoodModule], controllers: [DogsController], providers: [DogsService], }) export class DogsModule {} -
Attached logs in screenshot which show the routes.