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();
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
registermethod from there. This can be done like so:The other option is to pass the type to the
NestFactory.createmethod and then useapp.register. This can bee seen hereBoth ways are valid, though only the second option is type safe.