Skip to content
Router Module For Nestjs Framework ๐Ÿšฆ ๐Ÿš€
Branch: master
Clone or download
shekohex Merge pull request #50 from shekohex/greenkeeper/ts-jest-24.0.0
Update ts-jest to the latest version ๐Ÿš€
Latest commit fd1e1d4 Feb 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples add an example of how to deal with nested routes, many 2 many modules. Dec 29, 2018
scripts chore: split tsconfig in order to avoid error in spec.ts file during โ€ฆ Nov 7, 2018
src rem rxjs import Dec 21, 2018
.gitignore
.npmignore feat(npm): add .npmignore Feb 5, 2018
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE
README.md (docs): update readme Nov 29, 2018
jest.json (test): use ts-jest from 'node_modules' Oct 14, 2018
package-lock.json
package.json chore(package): update ts-jest to version 24.0.0 Feb 18, 2019
tsconfig.json
tsconfig.prod.json
tslint.json
wallaby.js test(wallabyjs): add a great test runner feedback for my editor vscode Feb 6, 2018
yarn.lock chore(package): update rxjs to version 6.0.0 Jun 19, 2018

README.md

Nest Router ๐Ÿšฆ

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 {}

๐Ÿ‘ 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!! ๐Ÿ˜ฎ 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.

You canโ€™t perform that action at this time.