Skip to content

Add a name to the routes #73

Open
Open
@amehat

Description

@amehat

Hello,

it might be interesting to add names to each route, to use the name in the controllers and not a path.
So we could easily change a route without having to go into the controller.

In module:

const routes: Routes = [
    {
      path: '/api',
      name: 'api',
      children: [
        {
          path: '/cats',
          module: CatsModule,
          name: 'cats',
        },
        {
          path: '/cats/:id',
          module: CatsModule,
          name: 'catsById',
        },
        {
          path: '/dogs',
          module: DogsModule,
          name: 'dogs',
        },
      ],
    },
  ];

In controller:

// ...
@Controller()
export class CatsController {
  private cats: string[] = [];
  @Get('cats')
  sayHello() {
    return `Hello From CatsController`;
  }

  @Get('catsById')
  getCat(@Param('catId') id: string) {
    const idx = parseInt(id, 10) || 0;
    return { cat: this.cats[idx - 1] || null };
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions