Nodejs Nestjs Fastify平台插件@nestjs/platform-fastify的使用

Nodejs Nestjs Fastify平台插件@nestjs/platform-fastify的使用
在使用 NestJS 框架时,[@nestjs](/user/nestjs)/platform-fastify 是一个非常有用的库,它允许你将 Fastify 作为 HTTP 库与 NestJS 集成。Fastify 是一个高性能的 Node.js 网络框架,它的设计目标是在提供良好开发体验的同时达到最快的响应速度。

安装

首先,你需要安装 [@nestjs](/user/nestjs)/platform-fastify 和 Fastify:

npm install [@nestjs](/user/nestjs)/platform-fastify fastify

如果你想要使用装饰器和 Fastify 插件,你还需要安装 @fastify/decorator

npm install @fastify/decorator

基本使用

以下是一个基本的例子来展示如何在 NestJS 中使用 Fastify:

  1. 创建一个新的 NestJS 项目(如果还没有的话):

    nest new my-fastify-app
    cd my-fastify-app
    
  2. 修改 main.ts 文件,使其使用 Fastify 平台:

    import { NestFactory } from '[@nestjs](/user/nestjs)/core';
    import { AppModule } from './app.module';
    import { FastifyAdapter, NestFastifyApplication } from '[@nestjs](/user/nestjs)/platform-fastify';
    
    async function bootstrap() {
      const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter(),
      );
      await app.listen(3000);
    }
    bootstrap();
    
  3. 现在你可以创建你的控制器,例如 app.controller.ts

    import { Controller, Get } from '[@nestjs](/user/nestjs)/common';
    
    [@Controller](/user/Controller)()
    export class AppController {
      @Get()
      getHello(): string {
        return 'Hello World!';
      }
    }
    
  4. 最后,确保在 app.module.ts 中声明了控制器:

    import { Module } from '[@nestjs](/user/nestjs)/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    
    [@Module](/user/Module)({
      imports: [],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    

使用 Fastify 特性

如果你想利用 Fastify 的一些特性,比如中间件或插件,你可以在 NestJS 中这样做:

import { NestFactory } from '[@nestjs](/user/nestjs)/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '[@nestjs](/user/nestjs)/platform-fastify';
import * as helmet from 'helmet'; // 使用 Helmet 安全中间件

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );

  // 使用 Fastify 的插件
  app.register(helmet);

  await app.listen(3000);
}
bootstrap();

通过这种方式,你可以在 NestJS 应用中集成 Fastify,并利用其高性能和丰富的功能。


3 回复

当然,要使用[@nestjs](/user/nestjs)/platform-fastify插件来创建一个Fastify服务器,你可以按照以下步骤操作:

  1. 首先,确保安装了必要的包:

    npm install [@nestjs](/user/nestjs)/platform-fastify fastify
    
  2. 在你的NestJS模块中导入FastifyAdapter并配置Fastify服务器。例如,在app.module.ts里:

    import { NestFactory } from '[@nestjs](/user/nestjs)/core';
    import { FastifyAdapter, NestFastifyApplication } from '[@nestjs](/user/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();
    

这样,你就用NestJS结合Fastify搭建了一个基本的服务端应用!是不是很简单呢?


@nestjs/platform-fastify 是 NestJS 提供的一个用于将 Fastify 作为 HTTP 平台适配器的库。它允许你在 NestJS 应用中使用 Fastify 而不是默认的 Express。

安装依赖

首先,你需要安装必要的包:

npm install @nestjs/platform-fastify fastify

创建 Fastify 适配器的应用

接下来,你可以创建一个基本的 NestJS 应用,并使用 Fastify 作为底层 HTTP 平台。

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

app.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}

main.ts

这是关键部分,使用 NestFactory.create 方法来创建一个基于 Fastify 的 NestJS 应用。

import { NestFactory } from '@nestjs/core';
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import fastify from 'fastify';
import { AppModule } from './app.module';

async function bootstrap() {
  const fastifyInstance = fastify({
    logger: true,
  });

  const app = await NestFactory.create<NestFastifyApplication>(AppModule, fastifyInstance);
  await app.listen(3000);
}
bootstrap();

运行应用

现在你可以运行你的 NestJS 应用了:

npm run start

访问 http://localhost:3000/,你应该能看到返回的 “Hello World!” 消息。

通过这种方式,你可以在 NestJS 中利用 Fastify 的高性能特性,同时仍然使用 NestJS 强大的依赖注入和模块化功能。

@nestjs/platform-fastify 是 NestJS 为 Fastify 框架提供的适配器。要使用它,首先安装必要的依赖:

npm install @nestjs/platform-fastify fastify

然后,在你的模块中导入 FastifyAdapter 并创建一个应用实例:

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    YourModule,
    new FastifyAdapter(),
  );
  await app.listen(3000);
}
bootstrap();

这样,你就配置好了使用 Fastify 的 NestJS 应用了。

回到顶部