🦅 NestJS Intermediate

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.