1

I'm using Nestjs (7.x) and Fastify (with @nestjs/platform-fastify). I'm trying to install Helmet in my project (fastify-helmet), but I'm not able to figure how to integrate/configure it with Nestjs. What's the proper way to have it onboard?

Here is my Nestjs bootstrap:

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();
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Etienne
  • 2,257
  • 3
  • 27
  • 41

2 Answers2

6

You've got two options when it comes to registering middleware for fastify. The first is to get the instance of the HttpAdapter and use the register method from there. This can be done like so:

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();

The other option is to pass the type to the NestFactory.create method and then use app.register. This can bee seen here

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();

Both ways are valid, though only the second option is type safe.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thanks for your 2 solutions and your explanations. However in both way I'm still getting an error where *plugin must be a function or a promise*. Also does it mean that `app.getHttpAdapter().getInstance().register()` is the same as `app.register()` but typing here is managed differently? – Etienne May 17 '20 at 09:32
  • Interesting. I tested both of these locally and had no problems with either set up. As for your question about if `app.register` is really `app.getHttpAdapter().getInstance().register`, the answer is yes. Nest just gives you the typings for it instead of having to go through the call chain. – Jay McDoniel May 17 '20 at 19:19
1
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();

try this instead. import fastifyHelmet from the package, use that to register.

jonscyriac
  • 74
  • 6