【问题标题】:Subroute in NestJsNestJs 中的子路由
【发布时间】: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'

标签: javascript typescript backend nestjs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2021-09-21
    • 2018-09-27
    • 2020-11-21
    • 2021-01-15
    • 1970-01-01
    • 2021-08-17
    • 2021-02-27
    相关资源
    最近更新 更多