有没有用Fastify在Nest.js中获取请求、响应的类型接口



我正在学习Nest.js,在开始阅读文档时,我读到它不仅可以与express一起使用,还可以与fastify使用,所以我用fastify设置了我的第一个项目,然后我开始阅读有关控制器的内容,发现了一个问题。例如,如果我想获得更多关于用户请求的信息,我可以稍微使用@Req req: Reguest,而这个reqrequest的类型,并且很容易从基于express应用程序中获得这个接口,你只需要安装@types/express,然后你就可以从express导入Request接口,但如果我使用fastify,我如何(如果可能的话(获得Request界面?

因此,我确定fasify的类型已经在Nest项目中,因为它们来自@types/node。如果您想使用fastify的接口,只需从fastify模块导入即可。示例:

import { Controller, Get, Query, Req } from '@nestjs/common';
import { AppService } from './app.service';
import { DefaultQuery } from 'fastify';
@Controller('math')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('add')
addTwoNumbers(@Query() query: DefaultQuery): number {
return this.appService.addTwoNumbers(query.value);
}
}

如果您想阅读更多关于fastify中类型的信息,请访问此链接:fastify types

应该有来自@types/fastify的类型可以安装。我相信Fastify使用RequestReply作为请求和响应。

安装fastify包并从中导入FastifyRequestFastifyReply

import { Controller, Get, Req, Res } from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
@Controller('feature')
export class FeatureController {
@Get()
async handler(
@Req() req: FastifyRequest,
@Res() reply: FastifyReply,
) { }
}

最新更新