Nest.js Nested Routing, Wildcard, Request

Shameel Uddin - Dec 30 '23 - - Dev Community

In previous blog, we understood about Nest.js controller and now we will be exploring a bit about nested routing, wildcard and starting with nest.js request.

Nest.js Nested Routing: A Modular Approach

Nest.js encourages a modular and organized structure for building applications. One key aspect is nested routing, where routes are organized 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 Nest.js controller:

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

@Controller('cats')
export class CatsController {
@Get()
  findAll(): string {
    return 'This action returns all cats of Shameel!';
  }
}
Enter fullscreen mode Exit fullscreen mode

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

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('/shameel/123')
Enter fullscreen mode Exit fullscreen mode

Now you can access this route with this:

/cats/shameel/123
Enter fullscreen mode Exit fullscreen mode

Wildcard Routes

Wildcard routes in Nest.js 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('/sha*eel/')
Enter fullscreen mode Exit fullscreen mode

Now you can access it with any character between "sha" and "meel". For example:

/cats/shameel
/cats/sha1eel
/cats/shaweel
Enter fullscreen mode Exit fullscreen mode

Nest.js Request

In Nest.js, 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
Enter fullscreen mode Exit fullscreen mode

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 Shameel!';
  }
}

Enter fullscreen mode Exit fullscreen mode

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.

Follow me for more such content:
YouTube: https://www.youtube.com/@ShameelUddin123
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player