I don't think you want to lazy load routes server side as you would do client side with angular as the loading logic is not the same.
In the client side, you want to display the main page as fast as possible and load others page as the user visit the webapp (lazy loading)
In the server, you want to load everything at launch time to be able to respond as fast as possible without having to load external resources (as much as possible) once the server initialized.
So, I think it is for this reason in NestJS, it is possible to load routes only once in the root module but nothing prevent you to externalize routes in another file using classic export/import.
Personally, I use this kind of structure for my app:
ninja/
cat/
cat.module.ts
dog/
dog.module.ts
ninja.module.ts
ninja.routes.ts
index.ts
app.module.ts
app.routes.ts
in app.routes.ts
imports {ninjaRoutes, NinjaModule} from './ninja';
export const appRoutes: Routes = [
{
path: 'ninja',
module: NinjaModule,
childrens: ninjaRoutes
},
];
in ninja.routes.ts
import { CatModule } from './cat/cat.module';
import { DogModule } from './dog/dog.module';
export const ninjaRoutes = [
{
path: 'cats',
module: CatModule
}, {
path: 'dogs',
module: DogModule
}
];
in ninja/index.ts
export * from './ninja.module';
export * from './ninja.routes';
in app.module.ts
import { Module } from '@nestjs/common';
import { RouterModule } from '@nestjs/core';
import { appRoutes } from './app.routes';
import { NinjaModule } from './ninja';
@Module({
imports: [NinjaModule, RouterModule.register(appRoutes)]
...
childrenarray is just anotherRoutesobject, so that's a valid structure, i don't see any reasons why that would not work for you!imports: [ RouterModule.forRoutes(routes),...], exports: [RouterModule]?routes.tsfile between different files or modules.