WEBVTT

1
00:01.550 --> 00:09.200
In Nest.js, we usually apply the middleware inside the app module.

2
00:09.950 --> 00:18.860
Okay, but what happens if I pass it in on another module, for example inside the post module?

3
00:18.890 --> 00:23.090
I will copy into here and I will remove.

4
00:23.270 --> 00:23.900
Okay.

5
00:23.990 --> 00:26.300
And I will pass it inside the post module.

6
00:27.170 --> 00:31.370
I will paste and et me import.

7
00:33.950 --> 00:36.020
Now can you guess, this work or not?

8
00:37.550 --> 00:45.530
You can see like here it's applied for every route with the logger middleware.

9
00:46.040 --> 00:51.440
Now you can see, firstly if I request into the post it works right.

10
00:51.770 --> 00:58.850
If I go to the home it will not let me see, if I click okay, it also works, right?

11
00:59.030 --> 01:05.130
For that reason, with Nestjs we usually use the app module.

12
01:08.160 --> 01:09.330
With the middleware.

13
01:09.360 --> 01:11.070
Let me undo.

14
01:12.300 --> 01:12.750
Okay.

15
01:13.380 --> 01:14.490
This is it right here.

16
01:14.670 --> 01:22.680
And now you may have a question how I can apply another middleware for the another route.

17
01:23.520 --> 01:29.100
For example, the logger middleware I want is apply for every route.

18
01:29.100 --> 01:34.470
But I want it have some middleware only apply for specific route.

19
01:35.640 --> 01:36.480
In order to do that.

20
01:36.510 --> 01:43.950
Let me see, for example I will name this will be uh, let me take the name.

21
01:49.620 --> 01:50.160
Post

22
01:52.770 --> 01:53.130
Okay.

23
01:53.160 --> 01:56.160
I think post middleware.

24
01:56.760 --> 01:57.150
Okay.

25
01:58.080 --> 01:59.010
post middleware

26
02:01.050 --> 02:04.350
And how I can apply that for here.

27
02:04.470 --> 02:11.430
I can easily copy and paste this one more time And I can apply the post middleware.

28
02:13.200 --> 02:14.610
Let me change the name.

29
02:16.770 --> 02:18.810
Must we change the name of class?

30
02:21.570 --> 02:25.260
Post middleware saved us right here.

31
02:26.070 --> 02:30.600
Post middleware and I will apply it only for the post.

32
02:32.340 --> 02:35.400
Sorry, I don't have a /post.

33
02:36.480 --> 02:45.450
And if I refresh in the hello world, it already log the request for the logger middleware.

34
02:45.780 --> 02:56.910
But if I go to /posts, you can see it will log that the request is logger middleware and it's

35
02:56.910 --> 02:58.680
also run the post middleware.

36
02:59.490 --> 02:59.970
Right?

37
03:00.210 --> 03:00.840
Very cool.

38
03:02.550 --> 03:04.080
Let me remove something right here.

39
03:05.850 --> 03:06.270
Okay.

40
03:06.900 --> 03:08.580
That's it, that's for the middleware.

41
03:08.910 --> 03:10.800
Hope you understand this video.

42
03:10.830 --> 03:12.900
I will see you in the next one.