NestJS provides a cool feature where we can create multiple routes with the same NestJS Controller.

What’s the use of such a feature?

Let’s say you want to change the route name for a particular entity from A to B. However, you also want to keep A endpoint working to support temporary backward compatibility.

With the help of this feature, you can have multiple routes for the same controller very easily without the need to duplicate a lot of code.

If you are totally new to controllers, I recommend you to go through my post on NestJS Controllers.

Let’s look at a couple of examples.

1 – NestJS Controller Multiple Routes Demo

Let’s say we want the same NestJS controller to support two routes /products and /items.

Here’s how you can do so:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller(['products', 'items'])
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getProducts(): any[] {
    // Hardcoded list of products
    const products = [
      { name: 'Product 1', price: 10.99 },
      { name: 'Product 2', price: 19.99 },
      { name: 'Product 3', price: 7.49 },
    ];

    return products;
  }
}

As you can see the NestJS @Controller() decorator can take an array of string names for the routes. We can mention all the route names that the controller will support.

If you run the application with the above controller, you should see the same output (the product list) from http://localhost:3000/products and http://localhost:3000/items.

In other words, we are serving multiple routes from the same NestJS controller.

2 – NestJS Multiple Routes for a Single Handler

You can take things with NestJS multiple routes even further by having multiple routes for a single handler.

See the below example:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller(['products', 'items'])
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get(['/v1', '/v2'])
  getProducts(): any[] {
    // Hardcoded list of products
    const products = [
      { name: 'Product 1', price: 10.99 },
      { name: 'Product 2', price: 19.99 },
      { name: 'Product 3', price: 7.49 },
    ];

    return products;
  }
}

As you can see, this creates another layer of branching within our route hierarchy.

In other words, you can have /products/v1 and products/v2 along with /items/v1 and /items/v2.

Basically, All HTTP methods in NestJS support either a single string route or an array of string routes.

We could use this technique to deprecate a bad route and roll out a better route without breaking the consumers immediately.

Conclusion

In this post, we looked at a quick little trick on how to support multiple routes with a single NestJS Controller.

We also extended this to the route handler level and saw the implementation.

If you have any comments or queries about this post, please mention them in the comments section below.

Categories: BlogNestJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *