WEBVTT

1
00:00.950 --> 00:02.630
Hi everyone.

2
00:03.050 --> 00:10.760
In this video let me tell you the what is the controller in the.

3
00:13.070 --> 00:14.030
Nest.js.

4
00:16.160 --> 00:23.810
Now I will use the official documentation to show you the core concept.

5
00:24.620 --> 00:35.750
Now firstly, inside the overview and first step I will briefly about the previous video.

6
00:35.780 --> 00:45.560
So in the previous video we have a app controller.spec.ts and controller and app.module

7
00:45.560 --> 00:48.590
and app.service and main.ts.

8
00:48.620 --> 00:55.310
Right, now let's deeply understand about what's all of that

9
00:55.460 --> 00:56.270
Firstly.

10
00:58.340 --> 01:00.320
The app.controller.ts.

11
01:00.680 --> 01:04.220
This is the a basic controller with single route.

12
01:05.510 --> 01:05.930
Okay.

13
01:07.160 --> 01:08.870
And that's for the video.

14
01:08.870 --> 01:19.760
We will understand what is the controller in nestjs and with the controller.spec.ts, it will

15
01:19.760 --> 01:23.210
be the unit test for the controller.

16
01:23.360 --> 01:26.570
And we already remove that.

17
01:26.600 --> 01:27.020
Right.

18
01:27.020 --> 01:31.640
Because I don't want to write the test in this course.

19
01:33.500 --> 01:37.700
And the next thing will be the app.module.ts.

20
01:38.060 --> 01:41.510
This is the root module of application.

21
01:41.720 --> 01:50.180
We will understand what is the module and what is the root module in this course later.

22
01:50.780 --> 01:57.680
And the app service right here it will be the basic service with single methods.

23
01:57.680 --> 02:00.200
We will understand that later.

24
02:00.350 --> 02:01.340
Don't worry.

25
02:01.700 --> 02:03.710
and main.ts

26
02:04.640 --> 02:07.520
This is the entry file application.

27
02:07.850 --> 02:08.330
Okay.

28
02:08.360 --> 02:16.070
In nest factory, we can see nest factory right here to run the application.

29
02:18.860 --> 02:22.370
And now let's understand what's in the controller.

30
02:22.400 --> 02:25.250
I will click into the controller right here.

31
02:26.030 --> 02:36.440
The controllers are responsible for handle incoming requests and return response to the client.

32
02:36.470 --> 02:38.600
Right here, right now.

33
02:38.630 --> 02:45.170
Let's see what you mean when the client sends a request into the controller.

34
02:45.800 --> 02:46.130
Okay.

35
02:46.160 --> 02:49.490
For example, I have a client a client.

36
02:49.730 --> 02:51.380
I send a request.

37
02:51.950 --> 02:54.080
For example, I send a get method.

38
02:56.240 --> 02:56.750
Okay.

39
02:56.780 --> 02:58.760
Into the /product.

40
02:58.790 --> 03:01.850
I want to get the product of application.

41
03:04.850 --> 03:13.760
The NestJS, it will handle the traffic into something called controller okay.

42
03:19.430 --> 03:20.960
It will routing.

43
03:21.080 --> 03:26.630
It will route in the URL right here into the specific controller.

44
03:28.880 --> 03:32.900
So that's the idea of the controller.

45
03:33.140 --> 03:43.640
And in order to create the controller inside the Nest.js we use something called class and decorator.

46
03:47.390 --> 03:55.310
If you already are familiar with TypeScript you may know decorator right?

47
03:56.060 --> 04:05.240
Actually, you can see this is just the decorator with asterisk (at) and controller.

48
04:05.300 --> 04:10.430
In order to create controller, we use something like this.

49
04:11.030 --> 04:11.720
Controller.

50
04:13.220 --> 04:15.440
export and export class.

51
04:15.470 --> 04:22.010
Okay, make sure we export the class and we name it for the controller.

52
04:22.010 --> 04:25.460
And we define the some method like this.

53
04:27.080 --> 04:27.680
Okay.

54
04:28.400 --> 04:37.040
And for now I assume you are already familiar with get method or post.

55
04:37.070 --> 04:39.500
put patch and delete methods.

56
04:40.850 --> 04:48.020
And let me show you how we can create a controller.

57
04:48.050 --> 04:57.830
Now for example, right here I will create a new file called test.controller.ts and I will export

58
04:58.490 --> 05:01.370
class test controller.

59
05:02.470 --> 05:02.920
Okay.

60
05:03.820 --> 05:14.500
For example, right here we do the get method and we must import it from Nestjs/common.

61
05:15.130 --> 05:15.700
Okay.

62
05:17.200 --> 05:23.770
And right here I will specify the path to path right here.

63
05:24.970 --> 05:27.640
It can be undefined right.

64
05:29.080 --> 05:33.580
For example I will pass with products like this.

65
05:33.760 --> 05:43.570
And I will name it for method like find all products for example like this.

66
05:43.720 --> 05:49.150
And here I will return I will return the string.

67
05:49.300 --> 05:55.360
Maybe this a list of product.

68
05:56.740 --> 05:58.420
Now let's see.

69
05:58.510 --> 06:03.520
And the error right here it will come from prettierc & eslint

70
06:03.910 --> 06:05.710
I will fix that later.

71
06:05.740 --> 06:11.680
For now you can fix that using the right here.

72
06:12.160 --> 06:12.640
Okay.

73
06:12.670 --> 06:19.960
Inside the CRLF, we click on that and choose the LF option.

74
06:20.920 --> 06:21.370
Okay.

75
06:21.400 --> 06:23.560
I will fix that later.

76
06:24.130 --> 06:25.150
Don't worry about that.

77
06:30.700 --> 06:35.590
And I will go to the select product to see it.

78
06:35.620 --> 06:43.000
have or not localhost 3000 and /products right here.

79
06:43.030 --> 06:43.450
Right.

80
06:43.810 --> 06:51.070
If I go to now you can see it say the status will be four four.

81
06:51.280 --> 06:53.890
And the message cannot get product.

82
06:54.160 --> 06:56.950
And the error message will be not found.

83
06:58.030 --> 06:59.170
The reason it not found

84
06:59.200 --> 07:04.150
Because we have not yet setup

85
07:04.780 --> 07:05.350
Okay.

86
07:06.040 --> 07:13.660
We doesn't have set up successfully because we don't use the decorator controller right here, right.

87
07:13.990 --> 07:17.560
We can use the decorator called controller.

88
07:19.690 --> 07:27.880
And even that, if I refresh you can see it also 404

89
07:28.300 --> 07:31.060
Let's see what happened.

90
07:31.900 --> 07:41.050
The reason is 404 because we not apply that into the module.

91
07:41.620 --> 07:43.360
Then we apply that into the module.

92
07:44.050 --> 07:47.620
Don't worry, I will display the module in later.

93
07:48.130 --> 07:55.660
Firstly let's just import that into the controller right here.

94
07:56.140 --> 08:02.320
And if I refresh now you can see this is a list of products right.

95
08:02.740 --> 08:03.430
Very cool.

96
08:04.000 --> 08:05.230
And that's it.

97
08:05.380 --> 08:07.780
How to create a controller.

98
08:07.810 --> 08:12.490
Let me brief

99
08:13.000 --> 08:17.140
let me quickly show you.

100
08:18.490 --> 08:26.380
Firstly, we will use the decorator will be the controller.

101
08:26.410 --> 08:27.010
Right.

102
08:30.670 --> 08:38.560
And the second we must import it into the something called module.

103
08:40.360 --> 08:44.980
And with the module I will discuss.

104
08:46.990 --> 08:49.870
In maybe few next video.

105
08:50.890 --> 09:02.500
So that's the reason and how we can create a controller in Nest.js.

106
09:02.710 --> 09:04.660
I will see you in the next one.