Hi everyone.
Let's continue with the core concept of nestjs.
And in this video we will be an interceptor.
Well interceptor is also a dependency injection class.
And now you can see, when we send the request, it will accept by the interceptor.
And when we respond it will catch.
It is a interceptor right here.
Okay.
Now.
Interceptor is an aspect oriented programming techniques.
If you already know the framework like Spring Framework, Java Spring.
We will familiar with the concept of aspect oriented programming, but if not, don't worry about that.
I will show you how we can understand the interceptor.
This will be the extra logic before and after method execution, or it can be used something like this.
We can read more about us inside the documentation.
And now you can see, well it's similar with middleware right.
But not really.
If I click on the middleware it's only have a one way okay.
It only have an one way HTTP request okay.
But with the interceptor is also the request and response right here.
And let me show Scroll down to how to create.
Inside the aspect interception.
Let me copy everything over here.
And again this is just a syntax, we don't need to memorize.
Okay.
Just copy last and go here.
Create the interceptors folder.
And in here we will be logging.interceptor.ts
And it very similar with logger middleware right.
But I will show you what is different.
Now you can see, in here.
Let me go on and organize it a little bit.
Firstly, with "AuthGuard"
I will change the "false", firstly will be "false"
Okay.
And watch me, in order to use the logging interceptor.
I will use the @UseInterceptor
Okay.
Decorator like this.
And pass the logging interceptor into the post controller.
I will use the @UseInterceptor(LoggingInteceptor)
And actually, I can use. I can move that inside the get method right here below the get method.
It will only work for the post with the GET method right.
We can use that later.
But right now let me use inside the controller level.
Okay.
This is the controller level and this will be the method level.
And when we use here, it will run "all of methods" inside that controller.
Anyway let's see what's the interceptor.
With the "AuthGuard" will be "false", what happened right now?
Inside the log middleware I will console log the middleware.
Okay, I will console.log on middleware inside the log in middleware and inside the interceptor, I will say
this is the interceptor.
And let's ignore that for now.
I will explain, and post, I think I will remove the post for messy code inside the app modules.
I will remove the post right here.
Now let's see if I open the terminal.
If I send a request inside the post with forbidden.
It will return the forbidden.
But notice it.
Return the middleware.
Right, interesting
Right?
It mean when we send a request, it always go to the middleware and after it run the middleware, it
will run the "guard", right?
Can you see "Guard" and after the "Guard", it will run the "Interceptor".
After go to the interceptor, it will run the handler.
Right.
The route handler
And interceptor right here
And this here will be response.
Okay.
That's the flow.
That's the flow of the Nestjs.
Now, if I go to the "AuthGuard" and return "true"
Now you can see, if I send a request, it will run the middleware, the guard right.
And interceptor.
This is the interceptor.
I will remove here.
I don't need to test the performance.
And inside the pipe.
Inside the pipe right here It will run.
Let me remove the tap
This is the next
.handle().pipe()
What's inside a pipe right here?
Here, the pipe right here it will run before the route return.
Okay.
Before it return the result for the client.
It will go to the pipe right
Here it means I will console.log.
Before response
Uh, sorry.
for that
We must use the "tap"
I already removed the tap.
Okay.
We must use the tap
Make sure, we use the "tap" right here.
Just a syntax
Don't worry about that.
But instead of pipe tap, it will return.
it will go to the logging interceptor before it response the result.
If I send, can you see, it mean, before it returned the data here, it will go to here first "before response"
result, and hopefully we already know what is the logging interceptor and what is different between
the guard or the middleware.
Right.
And flow
The flow.
So I will see you in the next video.