【问题标题】:How configure Helmet on a Nestjs project using Fastify?如何使用 Fastify 在 Nestjs 项目中配置 Helmet?
【发布时间】:2021-04-16 23:13:34
【问题描述】:

我正在使用 Nestjs (7.x) 和 Fastify(使用 @nestjs/platform-fastify)。 我正在尝试在我的项目 (fastify-helmet) 中安装 Helmet,但我不知道如何将它与 Nestjs 集成/配置。将其载入的正确方法是什么?

这是我的 Nestjs 引导程序:

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { MainModule } from './main.module';
import * as helmet from 'fastify-helmet';

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

【问题讨论】:

    标签: nestjs fastify helmet.js nestjs-fastify


    【解决方案1】:

    在为 fastify 注册中间件时,您有两种选择。首先是获取 HttpAdapter 的实例并从那里使用 register 方法。可以这样做:

    import { NestFactory } from '@nestjs/core';
    import {
      FastifyAdapter,
      NestFastifyApplication,
    } from '@nestjs/platform-fastify';
    import * as helmet from 'fastify-helmet';
    import { AppModule } from './app.module';
    
    async function bootstrap() {
      const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter(),
      );
      app
        .getHttpAdapter()
        .getInstance()
        .register(helmet);
      await app.listen(3000);
    }
    bootstrap();
    

    另一个选项是将类型传递给NestFactory.create 方法,然后使用app.register。这可以在这里看到

    import { NestFactory } from '@nestjs/core';
    import {
      FastifyAdapter,
      NestFastifyApplication,
    } from '@nestjs/platform-fastify';
    import * as helmet from 'fastify-helmet';
    import { AppModule } from './app.module';
    
    async function bootstrap() {
      const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter(),
      );
      app.register(helmet);
      await app.listen(3000);
    }
    bootstrap();
    

    两种方式都有效,但只有第二种方式是类型安全的。

    【讨论】:

    • 感谢您的 2 个解决方案和您的解释。然而,在这两种方式中,我仍然遇到 plugin 必须是函数或 promise 的错误。这是否也意味着app.getHttpAdapter().getInstance().register()app.register() 相同,但此处键入的管理方式不同?
    • 有趣。我在本地测试了这两个,并且任何设置都没有问题。至于你关于app.register是否真的是app.getHttpAdapter().getInstance().register的问题,答案是肯定的。 Nest 只是为您提供它的类型,而不必通过调用链。
    【解决方案2】:
    import { NestFactory } from '@nestjs/core';
    import {
      FastifyAdapter,
      NestFastifyApplication,
    } from '@nestjs/platform-fastify';
    import { fastifyHelmet } from 'fastify-helmet';
    import { AppModule } from './app.module';
    
    async function bootstrap() {
      const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter(),
      );
      app.register(fastifyHelmet)
      await app.listen(3000);
    }
    bootstrap();
    

    试试这个。从包中导入 fastifyHelmet,用它来注册。

    【讨论】:

    • 这对我有用,而@Jay 的回答却不行。
    猜你喜欢
    • 2023-03-16
    • 2020-09-18
    • 2020-05-09
    • 2020-05-22
    • 2019-09-16
    • 2020-08-30
    • 2020-12-22
    • 1970-01-01
    • 2020-08-09
    相关资源
    最近更新 更多