NestJS Logo

Performance (Fastify)

By default, Nest uses the Express framework. Nest is also compatible with other libraries, such as Fastify. It achieves this framework independence through a framework adapter, whose primary job is to proxy middleware and handlers to the appropriate library-specific implementations.

Hint A framework adapter can only be implemented for a library that provides request/response pipeline processing similar to Express.

Fastify is a good alternative framework for Nest because it solves design issues in a similar way to Express. However, Fastify is significantly faster than Express, achieving nearly twice the benchmark results. Nest still uses Express as the default HTTP provider because Express is widely used, well known, and has an enormous set of compatible middleware, which is available to Nest users out of the box.

Because Nest is framework independent, you can migrate between the two. Fastify can be a better choice when raw speed is a priority. To use Fastify, choose the built-in FastifyAdapter, as shown in this chapter.

Installation#

First, install the required package:


$ npm i --save @nestjs/platform-fastify

Adapter#

Once the Fastify platform is installed, you can use the FastifyAdapter.

main.ts
JS TS

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

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter()
  );
  await app.listen(process.env.PORT ?? 3000);
}
await bootstrap();

By default, Fastify listens only on the localhost interface (see the Fastify getting started guide). To accept connections on other hosts, specify '0.0.0.0' in the listen() call:


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

Platform specific packages#

When you use the FastifyAdapter, Nest uses Fastify as the HTTP provider. This means that recipes that rely on Express may not work. Use Fastify-equivalent packages instead.

Redirect response#

Fastify handles redirect responses slightly differently than Express. To redirect with Fastify, set the status code with status(), then call redirect() with the URL:


@Get()
index(@Res() res) {
  res.status(302).redirect('/login');
}

Fastify options#

You can pass options to the Fastify instance through the FastifyAdapter constructor. For example:


new FastifyAdapter({ logger: true });

Middleware#

Middleware functions receive the raw req and res objects instead of Fastify's wrappers. This is how the @fastify/middie package, which Nest uses under the hood, works. See the Fastify middleware documentation for more information.

logger.middleware.ts
JS TS

import { Injectable, NestMiddleware } from '@nestjs/common';
import { FastifyRequest, FastifyReply } from 'fastify';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) {
    console.log('Request...');
    next();
  }
}

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

@Injectable()
export class LoggerMiddleware {
  use(req, res, next) {
    console.log('Request...');
    next();
  }
}

Route Config#

Use the @RouteConfig() decorator to apply Fastify's route config feature.


@RouteConfig({ output: 'hello world' })
@Get()
index(@Req() req) {
  return req.routeConfig.output;
}

Route Constraints#

Use the @RouteConstraints() decorator to apply Fastify's route constraints feature.


@RouteConstraints({ version: '1.2.x' })
newFeature() {
  return 'This works only for version >= 1.2.x';
}
Hint@RouteConfig() and @RouteConstraints() are imported from @nestjs/platform-fastify.

Example#

A working example is available in the nestjs/nest repository.

Edit on GitHub

Support us

Nest is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you'd like to join them, please read more here.

Principal Sponsors

SerpApi LogoTrilon LogoMojam Logo

Sponsors / Partners

Become a sponsor