NestJS Nested Routing, Wildcard, Request

hasabTechhasabTech
2 min read

In previous blog, we talked about about NestJS controllers and now we will be exploring a bit about nested routing, wildcard and starting with NestJS request.

NestJS Nested Routing: A Modular Approach

NestJS encourages a modular and organised structure for building applications. One key aspect is nested routing, where routes are organised hierarchically, mirroring the structure of your application. This approach provides clarity and maintainability, making it easier to manage the complexity of larger applications.

This is a simple NestJS controller:

import { Controller, Get, Param, Post } from '@nestjs/common';

@Controller('cats')
export class CatsController {
@Get()
  findAll(): string {
    return 'This action returns all cats of hasabTech!';
  }
}

If you send GET request to /cats/ then it will return 'This action returns all cats of hasabTech!'.

In order to make nested routes within this controller for GET request handler for this case, all you have to make changes is in @Get() decorator to something like this:

@Get('/hasabTech/123')

Now you can access this route with this:

/cats/hasabTech/123

Wildcard Routes

Wildcard routes in NestJS provide a way to capture dynamic or unpredictable segments in a URL, enabling the handling of various scenarios within a single route.

You can make changes like this:

@Get('/hasab*ech/')

Now you can access it with any character between "hasab" and "ech". For example:

/cats/hasabTech
/cats/hasabMech
/cats/hasabRech

NestJS Request

In NestJS, the @Req() decorator is used to access the underlying Express.js request object. This decorator allows developers to tap into the details of an incoming HTTP request, gaining access to headers, parameters, and other request-specific information.

For better typing in TypeScript, please install this as dev dependency:

npm install @types/express --save-dev

Now you need to replace the controller code with this:

import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

@Controller('cats')
export class CatsController {
  @Get()
  findAll(@Req() req: Request): string {
    console.log(req);
    return 'This action returns all cats of hasabTech!';
  }
}

As you can see that we have added @Req as a decorator and when you check your terminal after hitting /cats/, then you will observe request object similar to Express.js.

If you’re a visual learner, check out the following video tutorial for this blog:

https://www.youtube.com/watch?v=-7oCm3I0544

Follow us for more such content:

https://www.linkedin.com/company/hasabtech

https://www.youtube.com/@hasabTech

https://www.instagram.com/hasab.tech/

https://www.facebook.com/hasabTech

0
Subscribe to my newsletter

Read articles from hasabTech directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

hasabTech
hasabTech

HasabTech is a developer-focused educational platform committed to simplifying tech learning. We share practical coding tutorials, programming guides, and tech insights to help beginners and aspiring developers build real world skills. Our mission is to make technical education accessible, clear, and community driven. We are currently building our in house products, experimenting with real world tech solutions, and sharing what we learn along the way.