Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nest-router

shekohex53.3kMIT1.0.9TypeScript support: included

Router Module For Nestjs Framework

nestjs, router, addons

readme

Nest Router :vertical_traffic_light:

Greenkeeper badge Build Status npm version Coverage Status

Router Module For Nestjs Framework

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!

Resolve Full Controller Path:

Nestjs dosen't resolve or take into account MODULE_PATH metadata when it is coming to resolve Controller path in Middlewear 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.

Authors

  • Shady Khalifa - Initial work

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[Unreleased]

[1.0.9]

Bugs

[1.0.8]

Added

  • Resolve Full Controller Path from anywhere (#32)

Changed

  • Update Dev Dependencies

[1.0.7] - 2018-09-28

Changed

  • Project dependency refactor
  • add node v10 as a test target

[1.0.6] - 2018-06-20

Changed

  • Now Nest Router Module Using Nest V5+

    See examples folder, there is nest-v5x.

[1.0.5] - 2018-02-27

Deprecated

  • childrens, use children instead. see why?

[1.0.4] - 2018-02-12

Added

  • You can now Omit the module keyword and just using an arry of children and one path proparty.

[1.0.3] - 2018-02-10

Added

  • children array can be array with just modules. this means you can omit the path keyword.
  • Unreleased section to gather unreleased changes and encourage note keeping prior to releases.

[1.0.2] - 2018-02-08

Added

  • Routes now can be endless nested array.

[1.0.1] - 2018-02-05

Changed

  • children now an Array insted of object

[1.0.0] - 2018-02-05

  • Published to NPM :rocket:
  • add continuous integration "Travis CI"

[0.0.0] - 2018-01-31

Added

  • Greenkeeper badge
  • README
  • Good examples and basic guidelines, in example folder and README.
  • Build status badge