nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

NestJs middlewares don't run when fastify is the HTTP adapter #13657

Closed caolanb10 closed 3 months ago

caolanb10 commented 3 months ago

Is there an existing issue for this?

Current behavior

NestJs middlewares don't run when fastify is the HTTP adapter

When implementing fastify middleware from the docs: https://docs.nestjs.com/techniques/performance#middleware It doesn't run

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-fvyxvn

Steps to reproduce

  1. npm ci
  2. npm start
  3. Observe the middleware in main.ts runs but the LoggerMiddleware applied in the app.module.ts doesn't work.

Expected behavior

The logger middleware should work

Package

Other package

No response

NestJS version

10.3.2

Packages versions

    "@nestjs/common": "^10.3.2",
    "@nestjs/core": "^10.3.2",
    "@nestjs/platform-fastify": "10.3.9",
    "reflect-metadata": "^0.2.1",
    "rxjs": "^7.8.1"

Node.js version

v18.20.3

In which operating systems have you tested?

Other

No response

caolanb10 commented 3 months ago

Solution is

    consumer.apply(LoggerMiddleware).forRoutes('(.*)');

Default behaviour is different between express and fastify, this doesn't work for fastify:

    consumer.apply(LoggerMiddleware);
kamilmysliwiec commented 3 months ago

Not sure if I'm following. The provided code snippet:

consumer.apply(LoggerMiddleware);

is incorrect for both adapters.

caolanb10 commented 3 months ago

My mistake