05 / 12

How does NestJS achieve platform agnosticism between Express and Fastify?

NestJS wraps the underlying HTTP framework behind an HttpAdapter interface. Core functionality like DI, decorators, and lifecycle is framework-independent. The adapter translates NestJS abstractions to Express or Fastify internals. You switch platforms by passing a different adapter to NestFactory.create() with no application code changes.

Switching from Express to Fastify
How platform agnosticism works:
  1. 1

    NestJS defines an AbstractHttpAdapter interface that both Express and Fastify implement.

  2. 2

    Controllers, guards, interceptors, and pipes work identically on both platforms.

  3. 3

    Only platform-specific code (e.g., using @Req(), @Res() directly) breaks portability.

  4. 4

    Fastify is significantly faster than Express but has a smaller ecosystem of middleware.