【问题标题】:How to create nested routes with parameters using NestJS如何使用 NestJS 创建带参数的嵌套路由
【发布时间】:2018-10-30 12:40:36
【问题描述】:

我需要构建一个 API,其中大多数路由都以一个公共 URL 部分为前缀,该部分也有一个参数。

在我的具体情况下,我的路线需要如下所示:

/accounts/:account/resource1/:someParam

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

/accounts/:account/resource3/

/accounts/:account/resource4/subResource/

等等..

理想情况下,我会创建一个 parent 路由 /accounts/:account/,其中将包含 children 路由(resource1resource2resource3resource4等...)。

我还需要可以从所有子路由访问 :account 参数。

使用 NestJS 实现这一目标的最佳方法是什么?

【问题讨论】:

标签: javascript node.js typescript nestjs


【解决方案1】:

我认为你需要这个?

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

}

【讨论】:

  • 这是我目前正在使用的,但是如果我有很多子路由怎么办?我不想有一个单一的胖控制器类。理想情况下,我将能够在不同的 Controller 类中创建不同的子路由
【解决方案2】:

关于您的用例,您可能想看看这个路由器模块
=> https://github.com/shekohex/nest-router

按照这个模块的文档,你可以像这样定义你的路线:

... //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 {}

当然,路由会在单独的文件中定义,例如 routes.ts

鉴于您有一个按模块的控制器,前面的代码将在以下路由树中结束:

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


示例
如果您想到达 ketty 控制器的路由,您需要到达这个端点:
<your-api-host>/ninja/cats/ketty

【讨论】:

【解决方案3】:

父控制器:

@Controller('accounts')
export class AccountsController {
  // http://api.domaine.com/accounts
  @Get()

儿童控制器:

@Controller('accounts/:id')
export class ResourcesController {
  // http://api.domaine.com/accounts/1/resources
  @Get('resources')

【讨论】:

  • ...然后你是如何从第二个例子中得到 :id 的?
  • 方法(@param('id') id)
  • 这是我正在使用的模式,也是我见过的最好的模式,但对于企业 API 来说仍然不是很好。如果你想拥有/resources/:id,你需要定义另一个ResourcesController,也许将上面的重命名为AccountsResourcesController 以创建“空格”。我希望有一种机制来注释类方法以忽略父控制器路由/覆盖它。如果有人发现这一点并找到更好的方法,请告诉我!
【解决方案4】:

你可以这样做

@Controller('trainer/:trainerId/heroes')
 export class HeroesController {
    constructor(private readonly heroesService: HeroesService) {}

    @Get(':id')
    findOne(@Param('trainerId') trainerId:string,@Param('id') id: string) {
       return `This action returns a #${id} hero trainer id ${trainerId}`;
    }

 }

URI 是:

http://localhost:3000/trainer/4/heroes/5

thr 结果是

This action returns a #5 hero trainer id 4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多