Nest-router: Router Module For Nestjs Framework

Nest Router šŸš¦

Router Module For Nestjs Framework

Important Note

As of Nestjs v8.0.0 This module got added into the @nestjs/core. see the docs with that being said, this package is still maintained (for now).

Quick Overview

RouterModule helps you organize your routes and lets you create a routes tree.

How ?

Every module could have a path property. That path will be a prefix for all controllers in this module. If that module has a parent, it will be a child of it and again all controllers in this child module will be prefixed by parent module prefix + this module prefix

see issue #255 .

Install

IMPORTANT: you need Nest > v4.5.10+

npm install nest-router --save

OR

yarn add nest-router

Setup

See how easy it is to set up.

... //imports
const routes: Routes = [
    {
      path: '/ninja',
      module: NinjaModule,
      children: [
        {
          path: '/cats',
          module: CatsModule,
        },
        {
          path: '/dogs',
          module: DogsModule,
        },
      ],
    },
  ];

@Module({
  imports: [
      RouterModule.forRoutes(routes), // setup the routes
      CatsModule,
      DogsModule,
      NinjaModule
  ], // as usual, nothing new
})
export class ApplicationModule {}

:+1: TIP: Keep all of your routes in a separate file like routes.ts

In this example, all the controllers in NinjaModule will be prefixed by /ninja and it has two childs, CatsModule and DogsModule.

Will the controllers of CatsModule be prefixed by /cats? NO!! :open_mouth: The CatsModule is a child of NinjaModule so it will be prefixed by /ninja/cats/ instead. And so will DogsModule.

See examples folder for more information.

Example Folder Project Structure

.
ā”œā”€ā”€ app.module.ts
ā”œā”€ā”€ cats
ā”‚   ā”œā”€ā”€ cats.controller.ts
ā”‚   ā”œā”€ā”€ cats.module.ts
ā”‚   ā””ā”€ā”€ ketty.controller.ts
ā”œā”€ā”€ dogs
ā”‚   ā”œā”€ā”€ dogs.controller.ts
ā”‚   ā”œā”€ā”€ dogs.module.ts
ā”‚   ā””ā”€ā”€ puppy.controller.ts
ā”œā”€ā”€ main.ts
ā””ā”€ā”€ ninja
    ā”œā”€ā”€ katana.controller.ts
    ā”œā”€ā”€ ninja.controller.ts
    ā””ā”€ā”€ ninja.module.ts

And here is a simple, nice route tree of example folder:

ninja
    ā”œā”€ā”€ /
    ā”œā”€ā”€ /katana
    ā”œā”€ā”€ cats
    ā”‚   ā”œā”€ā”€ /
    ā”‚   ā””ā”€ā”€ /ketty
    ā”œā”€ā”€ dogs
        ā”œā”€ā”€ /
        ā””ā”€ā”€ /puppy

Nice!

Params in nested routes

In a standard REST API, you probably would need to add some params to your nested routes. Here is an example of how you can achieve it:

... //imports
const routes: Routes = [
    {
      path: '/ninja',
      module: NinjaModule,
      children: [
        {
          path: '/:ninjaId/cats',
          module: CatsModule,
        },
        {
          path: '/:ninjaId/dogs',
          module: DogsModule,
        },
      ],
    },
  ];

The ninjaId param will be available inside CatsModule controllers and DogsModule controllers. Please, find the instruction how to handle params in the official documentation. It might be a good practice to use a pipe for transformation use case to have an access to ninja object instead of just id.

Resolve Full Controller Path:

Nestjs dosen't resolve or take into account MODULE_PATH metadata when it is coming to resolve Controller path in Middleware resolver for example, so that i introduced a new fancy method RouterModule#resolvePath that will resolve the full path of any controller so instead of doing so:

consumer.apply(someMiddleware).forRoutes(SomeController);

you should do

consumer.apply(someMiddleware).forRoutes(RouterModule.resolvePath(SomeController));

see #32 for more information about this.

CHANGELOG

See CHANGELOG for more information.

Contributing

You are welcome to contribute to this project, just open a PR.


Download Details:

Author: Nestjsx
Source Code: https://github.com/nestjsx/nest-router 
License: MIT license

#nest #nestjs #typescript 

Nest-router: Router Module For Nestjs Framework
1.15 GEEK