01 / 03

What can middleware NOT do that guards or interceptors can in NestJS?

Middleware runs before routing and has no knowledge of which handler will process the request. It cannot read @SetMetadata() values, know which controller or method will handle the request, make decisions based on route-level metadata, or access the response after the handler completes the way interceptors can.

Middleware limitations vs guards and interceptors:
  1. 1

    Cannot read @SetMetadata() values — no handler reference means no Reflector access.

  2. 2

    Cannot know which controller class or method will handle the request.

  3. 3

    Cannot short-circuit based on route-level metadata like @Public() or @Roles().

  4. 4

    Cannot access the response after the handler completes — interceptors can.

  5. 5

    Cannot observe the final response body — only guards and interceptors have this ability.

Use cases: middleware vs guards vs interceptors