
Middleware & Interceptors
Middleware, interceptors, RxJS operators, request/response transformation, caching
1What is a middleware in NestJS?
What is a middleware in NestJS?
Answer
A middleware is a function that executes before the route handler. It has access to request and response objects, and can modify the request, the response, end the request-response cycle, or pass to the next middleware with next(). Middlewares are ideal for logging, basic authentication, or header modifications. They execute in the order they are declared.
2What is an interceptor in NestJS?
What is an interceptor in NestJS?
Answer
An interceptor is a class with @Injectable() decorator that implements NestInterceptor. It allows intercepting requests before and after route handler execution. Using RxJS, it can transform results, handle errors, add caching, measure performance, or log responses. Interceptors provide more control than middlewares over the complete lifecycle.
3What is the main difference between a middleware and an interceptor?
What is the main difference between a middleware and an interceptor?
Answer
Middlewares execute before the handler and don't have access to the final response. Interceptors use RxJS and can act before AND after execution, allowing response transformation. Middlewares are simpler for basic tasks like request logging, while interceptors are more powerful for data transformation, error handling, or caching implementation.
In what order do components execute in NestJS?
How to apply a middleware globally in NestJS?
+17 interview questions
Other Node.js / NestJS interview topics
Node.js Fundamentals
Node.js Core APIs
Asynchronous Programming
Express.js Basics
NestJS Fundamentals
REST API Design
Validation & DTO
API Documentation & Contracts
Error Handling
Unit Testing
Task Scheduling
NestJS Modules & DI
Configuration & Environment Management
JWT Authentication
Authorization & RBAC
Database with TypeORM
Prisma ORM
File Upload
WebSockets
GraphQL with NestJS
End-to-End Testing
Caching with Redis
Queues with Bull
DevOps, Logging & CI/CD
Docker & Containerization
Microservices
Security Best Practices
Performance & Cloud Deployment
Master Node.js / NestJS for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free