# NestJS Nested Routing, Wildcard, Request

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:

```javascript
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:

```javascript
@Get('/hasabTech/123')
```

Now you can access this route with this:

```javascript
/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:

```javascript
@Get('/hasab*ech/')
```

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

```javascript
/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:

```javascript
npm install @types/express --save-dev
```

Now you need to replace the controller code with this:

```javascript
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](https://www.youtube.com/watch?v=-7oCm3I0544)  

***Follow us for more such content:***

[**https://www.linkedin.com/company/hasabtech**](https://www.linkedin.com/company/hasabtech)

[https://www.youtube.com/@hasabTech](https://www.youtube.com/@hasabTech)

[https://www.instagram.com/hasab.tech/](https://www.instagram.com/hasab.tech/)

[https://www.facebook.com/hasabTech](https://www.facebook.com/hasabTech)
