Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nested routes with parameters using NestJS

Tags:

I need to build an API where most of the routes are prefixed with a common URL part which also has a parameter.

In my specific case, my routes need to look like:

/accounts/:account/resource1/:someParam

/accounts/:account/resource2/:someParam/whatever

/accounts/:account/resource3/

/accounts/:account/resource4/subResource/

and so on..

So ideally I would create a parent route /accounts/:account/ which will contain the children routes (resource1, resource2, resource3, resource4, etc...).

I also need the :account parameter to be accessible from all the children routes.

What is the best way to achieve this with NestJS ?

like image 997
Francesco Borzi Avatar asked May 20 '18 20:05

Francesco Borzi


People also ask

How do you use parameters in Nestjs?

You can use the @Req decorator, and use param object, see : @Get() findAll( @Req() req: Request ): Promise<any[]> { console. log(req. query); // another code .... }

How do I add nested routes to react?

Now, the last thing you need to do is tell React Router where in the parent Route ( Messages ) should it render the child Route ( Chats ). To do this, you use React Router's Outlet component. If the app's location matches the nested Route 's path , this Outlet component will render the Route 's element .

How do I redirect in Nestjs?

To redirect a response to a specific URL, you can either use a @Redirect() decorator or a library-specific response object (and call res. redirect() directly). @Redirect() takes two arguments, url and statusCode , both are optional.


Video Answer


2 Answers

i think you need this?

import {Controller, Get, Param} from "@nestjs/common";  @Controller('accounts/:account') export class TestController{      @Get('resource2/:someParam/whatever')     arsPW(@Param('account') account, @Param('someParam') someparam){         console.log(':account/resource2/:someParam/whatever',account,someparam)         return account+'_'+someparam+'___';     }      @Get('resource1/:someparam')     aRSP(@Param('account') account, @Param('someparam') someparam){         console.log(':account/resource1/:someParam',account,someparam)         return account+'_'+someparam;     }       @Get()     getget(){         console.log('get');         return 'aaa';     }  } 
like image 120
small white Avatar answered Sep 22 '22 13:09

small white


Regarding your use case, you might want to take a look at this router module
=> https://github.com/shekohex/nest-router

Following the documentation of this module, you can define your routes like so:

... //imports const routes: Routes = [     {       path: '/ninja',       module: NinjaModule,       children: [         {           path: '/cats',           module: CatsModule,         },         {           path: '/dogs',           module: DogsModule,         },       ],     },   ];  @Module({   imports: [       RouterModule.forRoutes(routes), // setup the routes       CatsModule,       DogsModule,       NinjaModule       ], // as usual, nothing new }) export class ApplicationModule {} 

Of course, the routes would be defined in a separate file like routes.ts

Given the fact you have a controller by module, the previous code would end in the following tree of routes:

ninja     ├── /     ├── /katana     ├── cats     │   ├── /     │   └── /ketty     ├── dogs         ├── /         └── /puppy 


Example:
If you want to reach the ketty controller's routes, you will need to reach this endpoint:
<your-api-host>/ninja/cats/ketty

like image 25
A. Maitre Avatar answered Sep 25 '22 13:09

A. Maitre