4

I am using the library nest-router and I want to split router structure by many files attached to controllers or just file stored near controller. In Angular i can use loadChildren in Router definition and make lazy loading module. How i can create router structure like Angular style? Or just separate router file by child routes?

const routes: Routes = [
    {
      path: '/ninja',
      module: NinjaModule,
      childrens: NinjaRouterModule // <- like here
    },
  ];

in NinjaRouterModule

const routes: Routes = [
        {
          path: '/cats',
          module: CatsModule,
        },
        {
          path: '/dogs',
          module: DogsModule,
          childrens: DogsRouterModule // <-like here
        }
  ];
5
  • That's would work, in fact the children array is just another Routes object, so that's a valid structure, i don't see any reasons why that would not work for you! Commented Aug 4, 2019 at 15:02
  • @ShadyKhalifa how can i import routes to NinjaRouterModule? imports: [ RouterModule.forRoutes(routes),...], exports: [RouterModule] ? Commented Aug 5, 2019 at 7:50
  • Routes should be registered once in the RootModule (aka AppModule), on the other hand you could split up your routes.ts file between different files or modules. Commented Aug 5, 2019 at 10:10
  • @Gopard, could you post an official answer to this question? Commented Dec 9, 2019 at 20:46
  • @JWess I still have no solution Commented Dec 17, 2019 at 5:43

3 Answers 3

2
NestJS 8x

import { Module } from '@nestjs/common'
import { RouterModule } from '@nestjs/core'

@Module({
  imports: [RouterModule.register([{
    path: 'admin',
    module: AdminModule,
    children: [
      {
        path: 'dashboard',
        module: DashboardpModule
      },
      {
        path: 'metrics',
        module: MetricsModule
      }
    ]
  }])]
})

export default class AppModule {
}

https://docs.nestjs.com/recipes/router-module#router-module

Sign up to request clarification or add additional context in comments.

1 Comment

How i can move children routes in separate file?
2

You need to import all your children routes in main module(AppModule) along with RouterModule.register

@Module({
  imports: [
    NinjaRouterModule,
    CatsModule,
    DogsModule,
    DogsRouterModule,
    RouterModule.register(routes),
  ],

Comments

1

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)]
  ...

2 Comments

Looks like what I was looking for. Let me try
Nest.js v8 has been released few days ago which contains lazy loading modules for specific use case (mostly serverless applications which needs fast startup time): trilon.io/blog/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.