WEBVTT

1
00:00.740 --> 00:02.120
Hi everyone.

2
00:02.120 --> 00:10.130
Let's continue with the core concept of the lectures will be the middleware.

3
00:10.730 --> 00:11.270
Okay.

4
00:11.480 --> 00:15.230
You can see, now what is a middleware?

5
00:15.740 --> 00:21.620
If you already know the express.js framework we will understand middleware.

6
00:21.980 --> 00:27.290
But anyway middleware is a function it will call before the route handler.

7
00:28.370 --> 00:28.850
Okay.

8
00:29.630 --> 00:32.390
Now for example we have a request.

9
00:35.120 --> 00:35.510
Okay.

10
00:35.540 --> 00:37.490
And we have a response right here.

11
00:39.050 --> 00:44.870
And in the middle it has a something called middleware okay.

12
00:44.990 --> 00:45.560
The middleware.

13
00:49.100 --> 00:51.530
That's the idea of the middleware.

14
00:55.520 --> 01:00.740
And by default the NestJS will use the express middleware by default.

15
01:00.770 --> 01:01.100
Right.

16
01:01.130 --> 01:06.490
So for that reason if you already understand what is the middleware inside the expressjs

17
01:06.550 --> 01:08.380
You will be familiar with this.

18
01:10.540 --> 01:12.220
And talk enough.

19
01:12.250 --> 01:15.820
I think I will go through the code this time.

20
01:16.090 --> 01:17.650
Now you can see.

21
01:20.140 --> 01:26.620
In this example it will create something called logger middleware and it will log the request.

22
01:27.850 --> 01:35.200
And now my advice is never remember the code, okay.

23
01:36.160 --> 01:39.280
Always go to the documentation and copy the code okay.

24
01:39.670 --> 01:50.500
Because you might be doesn't know implement methods or don't understand what is the use or

25
01:51.550 --> 01:52.690
something like this right.

26
01:53.350 --> 01:55.420
This is just the syntax of Nestjs.

27
01:55.960 --> 02:00.730
We as a developer don't worry about that.

28
02:03.940 --> 02:06.220
And let me copy everything okay.

29
02:06.310 --> 02:07.900
Never remember that code.

30
02:10.040 --> 02:13.340
For now in the src, I will create a middleware.

31
02:13.760 --> 02:14.300
Okay.

32
02:15.110 --> 02:23.390
Actually middlewares, and I will create a logger.middleware.ts and I will paste everything right here.

33
02:24.620 --> 02:29.060
And now how we can use this middleware.

34
02:30.320 --> 02:38.240
For example, if I open the terminal I want when I access into the route, for example go to the post.

35
02:38.480 --> 02:41.030
It will console.log something inside the terminal.

36
02:41.630 --> 02:42.560
How we can do that?

37
02:42.710 --> 02:45.800
And let's scroll down into apply middleware.

38
02:46.280 --> 02:49.040
This can be applied in the app module.

39
02:51.200 --> 02:52.700
And use something like this.

40
02:52.730 --> 02:53.810
Configure.

41
02:54.860 --> 02:58.250
Okay now let me show you how we can do that.

42
02:58.280 --> 02:59.570
I will open the app module.

43
03:00.500 --> 03:03.080
Open the app module inside the class module.

44
03:03.110 --> 03:04.580
Right here I will enter.

45
03:05.300 --> 03:09.590
And inside here we must implement the nest module.

46
03:09.950 --> 03:10.490
Okay.

47
03:11.150 --> 03:13.100
Never in my life I remember that.

48
03:13.780 --> 03:22.300
So I will implement nest module inside NestJS common right here, and for that reason it will suggest

49
03:22.300 --> 03:25.240
for me implement the interface called configure.

50
03:25.630 --> 03:26.110
Okay.

51
03:26.320 --> 03:30.040
And inside configure we will apply the middleware for the some routes

52
03:30.220 --> 03:30.550
Okay.

53
03:30.580 --> 03:31.210
If I copy.

54
03:31.630 --> 03:32.650
Okay I will copy.

55
03:32.890 --> 03:35.020
And you can always go to the documentation.

56
03:35.020 --> 03:38.410
And I will copy this and paste it here.

57
03:39.040 --> 03:43.990
And I will import the logger middleware, I will import it from the middleware right here.

58
03:45.550 --> 03:48.910
And if I run it not work.

59
03:48.940 --> 03:49.390
Right.

60
03:49.420 --> 03:55.180
The reason it not work because the forRoutes right here, it doesn't have any route call "cats".

61
03:55.570 --> 03:58.540
If I change the post let's see work or not.

62
03:59.530 --> 04:03.340
Now you can see it console.log from the request right here, right?

63
04:03.370 --> 04:04.180
Very cool.

64
04:05.050 --> 04:09.820
And if I go to the /home, you can see it work or not.

65
04:09.850 --> 04:11.080
It's not work, right?

66
04:11.380 --> 04:13.390
It's only have the previous one.

67
04:13.480 --> 04:16.570
Now how I can use the every row.

68
04:18.400 --> 04:19.140
Oh, sorry.

69
04:23.760 --> 04:24.180
Okay.

70
04:24.210 --> 04:27.030
How I can apply that for every routes.

71
04:28.680 --> 04:38.310
I don't know, so I will read the documentation and we can specify the explicit like the path or

72
04:38.310 --> 04:40.770
the methods we will get now.

73
04:41.010 --> 04:44.130
I will introduce you, for example.

74
04:44.160 --> 04:52.620
path will be post and I only want this work for the methods method will be let me see, will be request method

75
04:52.620 --> 05:00.690
request method from the nestjs/common get okay.

76
05:01.830 --> 05:10.650
If I open the terminal and I refresh it to the /posts, it is work, right?

77
05:11.730 --> 05:15.480
But for example, if I change a method into the post, the post.

78
05:18.390 --> 05:21.690
And if I refresh, it doesn't work right?

79
05:22.720 --> 05:23.500
Very cool.

80
05:25.210 --> 05:26.980
And let me scroll down.

81
05:29.020 --> 05:33.670
We can use something like here the asterisk.

82
05:38.830 --> 05:39.460
Let me see.

83
05:39.490 --> 05:40.720
Or we can exclude.

84
05:41.950 --> 05:42.970
You can try that.

85
05:46.930 --> 05:50.740
Or apply it into the more global middleware.

86
05:52.870 --> 05:53.200
Okay.

87
05:53.230 --> 06:00.490
Now you can see in the header here we can use the forRoutes asterisk to access this within the app module.

88
06:00.640 --> 06:02.320
To use anything

89
06:16.840 --> 06:17.380
Asterisk.

90
06:17.380 --> 06:22.450
And then we refresh this work right.

91
06:23.020 --> 06:28.180
If I go to here this log and this is the global middleware.