In Nest.js, we usually apply the middleware inside the app module.
Okay, but what happens if I pass it in on another module, for example inside the post module?
I will copy into here and I will remove.
Okay.
And I will pass it inside the post module.
I will paste and et me import.
Now can you guess, this work or not?
You can see like here it's applied for every route with the logger middleware.
Now you can see, firstly if I request into the post it works right.
If I go to the home it will not let me see, if I click okay, it also works, right?
For that reason, with Nestjs we usually use the app module.
With the middleware.
Let me undo.
Okay.
This is it right here.
And now you may have a question how I can apply another middleware for the another route.
For example, the logger middleware I want is apply for every route.
But I want it have some middleware only apply for specific route.
In order to do that.
Let me see, for example I will name this will be uh, let me take the name.
Post
Okay.
I think post middleware.
Okay.
post middleware
And how I can apply that for here.
I can easily copy and paste this one more time And I can apply the post middleware.
Let me change the name.
Must we change the name of class?
Post middleware saved us right here.
Post middleware and I will apply it only for the post.
Sorry, I don't have a /post.
And if I refresh in the hello world, it already log the request for the logger middleware.
But if I go to /posts, you can see it will log that the request is logger middleware and it's
also run the post middleware.
Right?
Very cool.
Let me remove something right here.
Okay.
That's it, that's for the middleware.
Hope you understand this video.
I will see you in the next one.