02 / 03

What is middleware in NestJS and how does it differ from Express middleware?

Difficulty: 5/10
NestJS middleware, Express compatibility, request pipeline

NestJS middleware is functionally equivalent to Express middleware — it has access to req, res, and next(). The key difference is that NestJS middleware can be a class implementing NestMiddleware decorated with @Injectable(), giving it full access to the DI container. Functional middleware is still supported for simple stateless cases.

Class-based middleware with DI
NestJS middleware vs Express middleware:
  1. 1

    Express middleware — plain functions, no DI, registered with app.use().

  2. 2

    NestJS class middleware — @Injectable() class, full DI support, applied via configure().

  3. 3

    NestJS functional middleware — plain function, no DI, lighter weight for simple cases.

  4. 4

    Both must call next() to pass control to the next handler or the request will hang.

  5. 5

    NestJS middleware runs before routing — it has no knowledge of which controller handles the request.

Scenario Questions

0-2 years experience

  1. 1How would you add a logging middleware to a NestJS controller that prints the request method and URL?
  2. 2If you place a middleware after the route handler in NestJS, what will happen to the response?
  3. 3Can you explain how you would register a middleware that should run for all routes except '/health'?

2-5 years experience

  1. 1You added a custom NestJS middleware to validate JWT tokens, but some routes are still accessible without a token. Walk me through how you'd debug this.
  2. 2When migrating an existing Express app to NestJS, you need to reuse an Express middleware that sets CORS headers. How would you integrate it, and what differences do you need to watch for?
  3. 3Suppose you need to conditionally apply a rate‑limiting middleware only for POST requests on the '/orders' endpoint. How would you implement that in NestJS, and why might the same code behave differently in plain Express?

5-8 years experience

  1. 1Our microservice uses NestJS and we have a chain of middlewares for authentication, logging, and request transformation. A new performance bottleneck appears under load. How would you evaluate whether NestJS middleware or Express middleware is contributing, and what refactoring options exist?
  2. 2We need to support both HTTP and GraphQL in the same NestJS app, but some existing Express middlewares only work with HTTP. How would you design a solution that respects NestJS's middleware lifecycle while keeping the Express middleware reusable?
  3. 3During a rollout, a bug in a custom NestJS middleware caused request bodies to be lost for multipart/form-data uploads. Explain how NestJS's middleware execution differs from Express in this scenario and how you'd fix it.

8+ years experience

  1. 1Our organization is moving a large monolithic Express codebase into a modular NestJS architecture. What strategy would you use to migrate existing Express middlewares, and how would you decide which to keep as NestJS middleware versus wrapping as providers?
  2. 2We have cross‑team contracts that require request tracing middleware to be injected at the framework level. Discuss the trade‑offs of implementing this as a global NestJS middleware versus an Express middleware plugged into the underlying platform, considering maintainability and performance.
  3. 3Imagine we need to support dynamic runtime addition/removal of middlewares based on feature flags in a NestJS service that also runs on serverless platforms. How would you architect this, and what pitfalls arise from NestJS's static module system compared to Express's more dynamic nature?

Follow-up Questions

  • What are the pros and cons of using NestJS middleware versus plain Express middleware in a large codebase?
  • How does Nest's dependency injection affect testing of middleware?
  • Can you describe a scenario where you'd prefer an interceptor over middleware in NestJS?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.