NestJs 中的子路由

Posted

技术标签:

【中文标题】NestJs 中的子路由【英文标题】:Subroute in NestJs 【发布时间】:2021-07-17 23:36:03 【问题描述】:

如何使用 Nest 创建子路由?

我正在为localhost:3000/payment/stripe/get_customer_by_email 尝试类似的方法:

import  Controller, Get  from '@nestjs/common';
import StripeService from './stripe.service';

@Controller('/payments/stripe')
export default class StripeController 
  constructor(private readonly stripeService: StripeService) 

  @Get('/get_customer_by_email')
  getCustomerByEmail(): string 
    return this.stripeService.getCustomerByEmail();
  

但我在启动 e2e 测试时遇到 404:

    expected 200 "OK", got 404 "Not Found"

      19 |     return request(app.getHttpServer())
      20 |       .get('/payment/stripe/get_customer_by_email')
    > 21 |       .expect(200)
         |        ^
      22 |       .expect('Hello Customer!');
      23 |   );
      24 | );

【问题讨论】:

使用'/payments/stripe/get_customer_by_email' 而不是'/payment/stripe/get_customer_by_email' 【参考方案1】:

在您的控制器中,您将 payments/stripe 作为路由,将 GET 作为 get_customer_by_email,但在您的 e2e 中,您调用的是 payment/stripe/get_customer_by_emailpayments vs payment 路线不匹配

【讨论】:

以上是关于NestJs 中的子路由的主要内容,如果未能解决你的问题,请参考以下文章