fastify介绍
fastify官网
NestJS支持2种底层的模式,1种是express,另外一种是fastify。 express的优点是框架时间比较久,各种对应的方案和插件在网上都能找到资料,可以避免重复造轮子。fastify主要的优点就是高性能低开销,我喜欢快,所以决定接入fastify。
接入
- 首先安装依赖包
pnpm i --save @nestjs/platform-fastify
- 写适配器
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
await app.listen(3000);
}
bootstrap();
- 删除不要的依赖
pnpm uni @nestjs/platform-express @types/express
helloworld
然后我们用默认的路由来验证下配置是否成功。
npm start
> juejin-shenblog@0.0.1 start
> nest start
[Nest] 2061 - 2023/03/29 09:06:15 LOG [NestFactory] Starting Nest application...
[Nest] 2061 - 2023/03/29 09:06:15 LOG [InstanceLoader] AppModule dependencies initialized +14ms
[Nest] 2061 - 2023/03/29 09:06:15 LOG [RoutesResolver] AppController {/}: +3ms
[Nest] 2061 - 2023/03/29 09:06:15 LOG [RouterExplorer] Mapped {/, GET} route +3ms
[Nest] 2061 - 2023/03/29 09:06:15 LOG [NestApplication] Nest application successfully started +2ms
访问接口
curl http://localhost:3000
> Hello World!%