0
0
NestJSframework~10 mins

Interceptor interface in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interceptor interface
Request received
Interceptor intercept() called
Execute pre-processing logic
Call next.handle() to continue
Wait for response Observable
Execute post-processing logic on response
Return modified or original response
Response sent to client
The interceptor intercepts the request, runs code before and after the main handler, then returns the response.
Execution Sample
NestJS
intercept(context, next) {
  console.log('Before handler');
  return next.handle().pipe(
    tap(() => console.log('After handler'))
  );
}
Logs a message before and after the main request handler runs.
Execution Table
StepActionEvaluationResult
1Interceptor intercept() calledcontext and next receivedReady to process request
2Log 'Before handler'Console logs messageMessage shown before handler
3Call next.handle()Returns Observable of handler responseWaiting for response
4Response emittedtap operator triggersLog 'After handler'
5Return responseResponse sent to clientRequest completed
💡 Response sent to client, interceptor completes execution
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
contextProvided by NestJSSameSameSameSame
nextProvided by NestJSSameSameSameSame
Observable responseNot startedNot startedWaitingEmittedCompleted
Key Moments - 2 Insights
Why do we call next.handle() inside intercept()?
Calling next.handle() continues the request to the next handler or interceptor. Without it, the request would stop here. See execution_table step 3.
How can we run code after the main handler finishes?
We use operators like tap() on the Observable returned by next.handle() to run code after the response emits. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe main handler is called
BThe response is sent to the client
CThe interceptor logs a message before the handler runs
DThe interceptor finishes execution
💡 Hint
Check the 'Action' and 'Result' columns at step 2 in the execution_table
At which step does the interceptor wait for the handler's response?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look for when next.handle() is called and the Observable is waiting
If we remove next.handle() call, what will happen?
AThe interceptor will log messages twice
BThe request will stop and client gets no response
CThe request will continue normally
DThe response will be sent immediately
💡 Hint
Refer to key_moments about why next.handle() is necessary
Concept Snapshot
Interceptor interface in NestJS:
- intercept(context, next) method runs on each request
- Call next.handle() to continue request pipeline
- Use RxJS operators (e.g., tap) to run code after handler
- Can modify request or response
- Useful for logging, caching, transforming responses
Full Transcript
In NestJS, an Interceptor implements the intercept() method. When a request comes in, intercept() runs first. It can run code before the main handler by placing logic before calling next.handle(). Calling next.handle() continues the request to the next handler or interceptor and returns an Observable of the response. We can use RxJS operators like tap() on this Observable to run code after the handler finishes, such as logging or modifying the response. Finally, the response is sent back to the client. If next.handle() is not called, the request stops and the client does not get a response. This flow allows interceptors to add behavior around request handling, like logging or caching.