Hi everyone.
Let's continue with the core concept of the lectures will be the middleware.
Okay.
You can see, now what is a middleware?
If you already know the express.js framework we will understand middleware.
But anyway middleware is a function it will call before the route handler.
Okay.
Now for example we have a request.
Okay.
And we have a response right here.
And in the middle it has a something called middleware okay.
The middleware.
That's the idea of the middleware.
And by default the NestJS will use the express middleware by default.
Right.
So for that reason if you already understand what is the middleware inside the expressjs
You will be familiar with this.
And talk enough.
I think I will go through the code this time.
Now you can see.
In this example it will create something called logger middleware and it will log the request.
And now my advice is never remember the code, okay.
Always go to the documentation and copy the code okay.
Because you might be doesn't know implement methods or don't understand what is the use or
something like this right.
This is just the syntax of Nestjs.
We as a developer don't worry about that.
And let me copy everything okay.
Never remember that code.
For now in the src, I will create a middleware.
Okay.
Actually middlewares, and I will create a logger.middleware.ts and I will paste everything right here.
And now how we can use this middleware.
For example, if I open the terminal I want when I access into the route, for example go to the post.
It will console.log something inside the terminal.
How we can do that?
And let's scroll down into apply middleware.
This can be applied in the app module.
And use something like this.
Configure.
Okay now let me show you how we can do that.
I will open the app module.
Open the app module inside the class module.
Right here I will enter.
And inside here we must implement the nest module.
Okay.
Never in my life I remember that.
So I will implement nest module inside NestJS common right here, and for that reason it will suggest
for me implement the interface called configure.
Okay.
And inside configure we will apply the middleware for the some routes
Okay.
If I copy.
Okay I will copy.
And you can always go to the documentation.
And I will copy this and paste it here.
And I will import the logger middleware, I will import it from the middleware right here.
And if I run it not work.
Right.
The reason it not work because the forRoutes right here, it doesn't have any route call "cats".
If I change the post let's see work or not.
Now you can see it console.log from the request right here, right?
Very cool.
And if I go to the /home, you can see it work or not.
It's not work, right?
It's only have the previous one.
Now how I can use the every row.
Oh, sorry.
Okay.
How I can apply that for every routes.
I don't know, so I will read the documentation and we can specify the explicit like the path or
the methods we will get now.
I will introduce you, for example.
path will be post and I only want this work for the methods method will be let me see, will be request method
request method from the nestjs/common get okay.
If I open the terminal and I refresh it to the /posts, it is work, right?
But for example, if I change a method into the post, the post.
And if I refresh, it doesn't work right?
Very cool.
And let me scroll down.
We can use something like here the asterisk.
Let me see.
Or we can exclude.
You can try that.
Or apply it into the more global middleware.
Okay.
Now you can see in the header here we can use the forRoutes asterisk to access this within the app module.
To use anything
Asterisk.
And then we refresh this work right.
If I go to here this log and this is the global middleware.