What is the ExecutionContext in NestJS?
Answer
The ExecutionContext is an interface that provides methods to access the arguments passed to the current handler and extends the context beyond the HTTP layer. It is available in Guards, Interceptors, and Filters via the ArgumentsHost or ExecutionContext argument. Methods: switchToHttp() gets the HTTP request/response. switchToWs() for WebSockets. switchToRpc() for microservices. getType() returns the transport type ('http', 'ws', 'rpc'). getHandler() returns the route handler function — used with Reflector to read custom metadata. getClass() returns the controller class. Using ExecutionContext makes guards and interceptors work across HTTP, WebSocket, and microservice contexts.
Previous
What are NestJS microservices and what transports are supported?
Next
How do you implement role-based access control (RBAC) in NestJS?
More NestJS Questions
View all →- Intermediate How does NestJS dependency injection scoping work?
- Intermediate What are NestJS microservices and what transports are supported?
- Intermediate How do you implement role-based access control (RBAC) in NestJS?
- Intermediate What is the Reflector in NestJS and how is it used?
- Intermediate How do you implement caching in NestJS?