WEBVTT

1
00:00.440 --> 00:01.820
Hi everyone!

2
00:01.820 --> 00:09.050
In this video I will introduce for you the request Lifecycle in NestJS application.

3
00:09.980 --> 00:17.090
And in this video I will not write anything the code in this video, right?

4
00:18.590 --> 00:24.920
Okay, after we watch this video you can feel free to test in this.

5
00:27.080 --> 00:30.290
And what is the request life cycle.

6
00:30.590 --> 00:33.200
And actually the term life cycle right here.

7
00:33.200 --> 00:36.230
Exist in everywhere okay.

8
00:37.400 --> 00:40.520
Anything in the world will have a life cycle.

9
00:40.550 --> 00:41.210
Right.

10
00:41.540 --> 00:43.910
And the NestJS is not exception.

11
00:43.940 --> 00:45.800
We have a request lifecycle.

12
00:45.920 --> 00:54.050
If you want to look up the document you can go to the FAQ  right here and click on Request Life

13
00:54.080 --> 00:54.710
Cycle.

14
00:56.000 --> 00:57.830
Now let me explain for you.

15
00:58.550 --> 01:06.130
Let me reminder for you in my previous video. I think in the first section, we already know

16
01:06.340 --> 01:07.870
the life cycle.

17
01:08.050 --> 01:13.300
When we have a request, it will accept us using the middleware first.

18
01:13.330 --> 01:14.020
Right?

19
01:15.010 --> 01:15.730
Middleware.

20
01:16.390 --> 01:19.660
And after middleware, it will trigger the Guard

21
01:21.850 --> 01:23.320
Right after Guard.

22
01:23.350 --> 01:26.260
It will give the interceptor.

23
01:26.410 --> 01:27.100
Right.

24
01:27.670 --> 01:33.070
So this is the lifecycle of the request and response.

25
01:34.420 --> 01:40.690
But what is, for example, in the guard we have a global guard.

26
01:40.960 --> 01:47.380
We have a so many concept related to global guard okay or the interceptor.

27
01:47.380 --> 01:50.260
It also have a global interceptor right.

28
01:51.340 --> 01:59.410
And we have a something called controller level of the guard or the interceptor.

29
01:59.620 --> 02:02.170
And we have a method level.

30
02:04.980 --> 02:05.550
Okay.

31
02:05.550 --> 02:07.620
And also we have a global.

32
02:09.630 --> 02:12.090
So how it can be excuted.

33
02:12.120 --> 02:15.540
For example with guard we have a global guard.

34
02:15.780 --> 02:19.590
We have something called global controller.

35
02:20.160 --> 02:23.490
Sorry the guard controller.

36
02:23.580 --> 02:25.860
And we have a guard in the methods.

37
02:26.580 --> 02:31.710
Now let me show you what it means inside the overview right here.

38
02:32.490 --> 02:38.850
For example, if I go out into the interceptor now we will scroll down a little bit.

39
02:39.660 --> 02:43.200
We have an interceptor right and right here.

40
02:43.200 --> 02:51.180
It will use it into the class, it means this is a class level interceptor okay.

41
02:51.450 --> 02:54.600
And we can actually do that in the methods.

42
02:56.340 --> 02:59.100
Or we can use the global right here.

43
02:59.130 --> 03:02.340
Now you can see this is a global interceptor.

44
03:02.340 --> 03:10.280
And it is also available interceptor you can see and when we do the project, we will see this approach.

45
03:10.850 --> 03:13.070
Don't worry about what it means right now.

46
03:13.850 --> 03:15.440
You will understand later.

47
03:16.340 --> 03:16.970
Okay.

48
03:17.000 --> 03:23.960
And actually we can also use that in the in the method level right here.

49
03:23.990 --> 03:27.500
Let me show you for example right here.

50
03:29.420 --> 03:32.960
Let me open the the post controller here.

51
03:33.710 --> 03:34.070
Okay.

52
03:34.250 --> 03:36.710
Here will be the class level.

53
03:37.460 --> 03:38.450
Class level.

54
03:40.670 --> 03:48.440
And if I go in the @POST right here, it means this is the method level interceptor.

55
03:48.830 --> 03:49.400
Okay.

56
03:49.790 --> 03:54.620
And if we use something like this, I will call.

57
03:54.620 --> 03:55.640
This will be the.

58
04:01.640 --> 04:03.110
Something like this, I'll call

59
04:03.110 --> 04:05.330
This will be the global interceptor.

60
04:05.330 --> 04:07.340
And how it can be excuted.

61
04:07.880 --> 04:08.450
Okay.

62
04:08.570 --> 04:11.740
That's the concept of request lifecyle

63
04:11.740 --> 04:12.460
Read more about that

64
04:13.360 --> 04:20.890
If we scroll down a little bit right here, now, you can see, you can read a summary right here,

65
04:20.920 --> 04:23.140
or you can read details.

66
04:24.940 --> 04:29.380
You can read details for example like that, the middleware excute.

67
04:29.680 --> 04:32.200
Uh, right here.

68
04:32.230 --> 04:32.710
Right.

69
04:33.610 --> 04:38.590
You can see, and after that we will go to the guard.

70
04:39.040 --> 04:42.940
The guard will be start with global guard and then the controller guard.

71
04:42.940 --> 04:46.690
And finally the route guard. This is a "route guard"

72
04:48.160 --> 04:50.110
Or you can see the method level.

73
04:52.300 --> 05:03.040
Now you can see, if we use something like this if we if we use something like 2 guards here and also

74
05:03.040 --> 05:03.880
guard here.

75
05:03.880 --> 05:07.420
What it's mean, the guard one will be executed first.

76
05:10.240 --> 05:12.780
And after that it will run the guard 2

77
05:13.320 --> 05:18.060
And after that we will go to the useGuard in method level

78
05:21.900 --> 05:25.590
Okay, you can read more about them right here.

79
05:29.160 --> 05:35.790
And interceptor is it will be the same. Let meread more about that.

80
05:36.240 --> 05:41.130
When we have a incoming request it will accept by middleware okay.

81
05:41.130 --> 05:47.520
And it will back to the global middleware and also the module middleware.

82
05:48.660 --> 05:50.880
And the guard is similar.

83
05:50.910 --> 06:00.660
Firstly we will the global guard and the second will be controller guard and route guard

84
06:01.320 --> 06:01.920
Okay.

85
06:02.040 --> 06:04.200
Interceptor will be the same right.

86
06:04.230 --> 06:05.760
You can see it will be the same.

87
06:05.760 --> 06:07.050
And also the pipes.

88
06:08.370 --> 06:16.100
And we have so many things after each run, the some steps, it will come back to the controller

89
06:16.250 --> 06:25.820
and service and post interceptor because the interceptor it will have a two way, right?

90
06:26.030 --> 06:27.380
This is the interceptor.

91
06:27.740 --> 06:31.940
And when we respond, it also have interceptor, right?

92
06:32.660 --> 06:35.690
For that reason, it will trigger interceptor in this step.

93
06:36.860 --> 06:39.350
And we also have exception.

94
06:39.530 --> 06:43.130
And after that, the finally will be the response.

95
06:43.160 --> 06:43.670
Right.

96
06:44.240 --> 06:52.190
We can see the interceptor execute before the server response.

97
06:52.220 --> 06:52.520
Right.

98
06:52.550 --> 06:59.780
You can see okay, that's it. That's the concept of request life cycle.

99
06:59.810 --> 07:04.280
You can read more about them in the topics right here.

100
07:04.310 --> 07:07.760
But I think this very enough for you right now.

101
07:09.470 --> 07:13.190
So that's it for the video.

102
07:14.240 --> 07:16.100
I will see you in the next one.