是否可以在nestJS控制器中正确输入Http请求?

问题描述 投票:0回答:1

我正在配置一个 NestJS 控制器,当前使用

any
类型来请求。我可以使用一种类型来使其类型更加安全吗?

@Controller('endpoint')
@UsePipes(new MongoSanitizePipe())
export class RequestController {
    constructor(private service: Service) {}

    @Post('post')
    async onAction(
        @Body() body: RequestBody,
        @Request() request: any,
    ): Promise<void> {
        return this.service.onAction(body, request.headers);
    }
}

您尝试了什么以及您期待什么?

我找不到我的 HttpRequest 的类型。
我希望正确输入它。

typescript nestjs
1个回答
0
投票

NestJs 网站上的example使用了express中的

Request
类,我在我公司的项目中也是如此:

import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

@Controller('cats')
export class CatsController {
  @Get()
  findAll(@Req() request: Request): string {
    return 'This action returns all cats';
  }
}

如果您使用 fastify,该软件包包含一个

FastifyRequest
,您可以改为导入。

© www.soinside.com 2019 - 2024. All rights reserved.