Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
00:01.630 --> 00:03.750
All right, On to steps number three and four.
00:03.760 --> 00:06.970
So in step three, we need to create a Nest module and a controller.
00:06.970 --> 00:09.760
I want to very quickly tell you what a module and a controller are.
00:09.790 --> 00:11.860
So we're going to take a look at a couple of different diagrams.
00:12.280 --> 00:12.580
All right.
00:12.580 --> 00:16.300
So this diagram right here is probably going to look somewhat familiar if you've ever, ever put together
00:16.300 --> 00:17.380
any kind of server.
00:17.380 --> 00:22.630
So in just about every Http server you're ever going to create, you're going to have what is referred
00:22.630 --> 00:25.030
to as a request response cycle.
00:25.730 --> 00:28.760
Users are going to make requests to your server inside of your server.
00:28.760 --> 00:31.730
You're going to have some amount of code that's going to process that request.
00:31.760 --> 00:34.130
You might validate some data inside the request.
00:34.160 --> 00:38.120
You might handle the request differently depending upon what route it is addressed to.
00:38.120 --> 00:42.380
And then eventually you're going to formulate a response and send it back to whoever made the request.
00:43.040 --> 00:47.660
Now, just about every server you ever put together, this code right here that's going to process that
00:47.660 --> 00:51.250
request is almost always going to look rather similar in nature.
00:51.260 --> 00:56.990
In other words, this kind of request response cycle almost always has the same steps inside of it,
00:56.990 --> 01:02.510
regardless of the language framework library or whatever else that you use to build your server.
01:02.870 --> 01:07.670
So this code or that process almost always looks something like this.
01:07.970 --> 01:12.470
So just about every server that you're ever going to put together is going to receive a request.
01:12.500 --> 01:15.980
It then might do some validation on data contained inside that request.
01:15.980 --> 01:20.180
So for example, we might validate some body or some data in the body of the request.
01:20.480 --> 01:25.280
We then might make sure that the user is authenticated or authorized to send us a request.
01:25.990 --> 01:31.060
We're then going to route the request off to a particular function to handle the request that might
01:31.060 --> 01:36.130
result in us running some particular business logic and eventually maybe access or store some information
01:36.130 --> 01:37.540
inside of a database.
01:39.140 --> 01:43.550
Then as a result of this entire process, we're going to formulate a response and send the response
01:43.550 --> 01:45.560
back to whoever made us the request.
01:46.010 --> 01:51.110
So, again, doesn't really matter what language framework, anything that you're using, just about
01:51.110 --> 01:53.990
every server is going to have the same series of steps.
01:54.170 --> 01:58.310
In some cases we might not be doing authentication, so maybe some steps will fall out.
01:58.310 --> 02:01.190
But at the end of the day, this is kind of a very general case.
02:01.580 --> 02:07.940
So in Nestjs we get special tools to help us address each of these very particular steps.
02:07.940 --> 02:12.530
So in Nest we have tools to help us create something called, say, a pipe.
02:12.650 --> 02:16.460
Pipes and nest help us validate data on incoming request.
02:16.610 --> 02:20.270
We also have tools that allow us to create something called guards.
02:20.300 --> 02:25.370
Guards make sure that incoming requests are coming from users who are authenticated or authorized to
02:25.370 --> 02:26.480
use our application.
02:26.690 --> 02:29.510
We get also tools to create controllers.
02:29.540 --> 02:32.030
Controllers contain routing logic.
02:32.060 --> 02:34.240
We get tools to create something called services.
02:34.250 --> 02:36.350
Those contain business logic and so on.
02:36.650 --> 02:41.340
So throughout this course, as we work with Nest, we're going to learn about each of these different
02:41.340 --> 02:42.420
sets of tools.
02:42.420 --> 02:47.910
Each of these different sets of tools included in Nest are meant to help us kind of put together some
02:47.910 --> 02:53.190
piece of handling an incoming request besides just the five I'm showing right here, there's a couple
02:53.190 --> 02:54.910
of other tools we're going to look at.
02:54.930 --> 03:00.150
So these are all tools inside of Nest that help us make things that handle these incoming requests.
03:01.560 --> 03:04.750
So we still have controllers and services which we saw in the last diagram.
03:04.770 --> 03:06.810
We also have something called modules.
03:06.840 --> 03:10.830
We have pipes, filters, guards, interceptors and repositories.
03:11.130 --> 03:14.130
And again, we're going to learn about all these things throughout this course.
03:14.720 --> 03:20.400
Now, the absolute bare minimum that is required to put together the most simple application possible
03:20.430 --> 03:23.130
is a module and a controller.
03:23.340 --> 03:28.110
Every application you ever create is going to have at least one module and one controller inside of
03:28.110 --> 03:28.560
it.
03:28.800 --> 03:32.730
So in the diagram we were just looking at a moment ago, this one right here where we said we were going
03:32.730 --> 03:35.250
to create a Nest module and a controller.
03:35.280 --> 03:39.900
These are the two things that are required to make the most simple, basic app possible.
03:40.140 --> 03:44.670
So with that in mind, let's go back over to our editor and we're going to create a module and a controller.
03:46.130 --> 03:50.060
So back over here to get started, I'm going to create a new directory called SRC.
03:51.000 --> 03:54.330
Then inside there, I'm going to make a new file called Main.ts.
03:55.530 --> 03:59.520
Main.ts is going to be the first file that gets executed in any project.
03:59.520 --> 04:04.020
So we'll usually have some code inside of here to start up our application and start listening for traffic
04:04.020 --> 04:05.340
on a particular port.
04:06.620 --> 04:10.820
Usually we will create a module and a controller in their own separate files.
04:10.820 --> 04:15.510
But for right now we're going to create the module and the controller directly inside the main.ts file.
04:15.530 --> 04:17.510
So for right now, all logic inside this file.
04:17.510 --> 04:21.950
But we will very, very quickly extract the stuff into separate files.
04:23.210 --> 04:24.860
To create a module in a controller.
04:24.860 --> 04:28.970
We're going to add in an import statement at the very top, and I'm going to import something called
04:28.970 --> 04:34.490
controller and module from at Nestjs slash Common.
04:36.070 --> 04:41.320
So like I mentioned, these are tools that Nest provides to us to help us make a controller and make
04:41.320 --> 04:42.100
a module.
04:42.830 --> 04:44.690
To use them to create a controller.
04:44.690 --> 04:46.400
We're going to focus on the controller first.
04:46.430 --> 04:51.530
I'm going to create a class called App Controller.
04:52.380 --> 04:59.490
And then right above it, I'm going to add an at symbol controller and then call that like a function.
05:00.020 --> 05:05.630
So this is referred to as a decorator In this case, this decorator is telling Nest that we are trying
05:05.630 --> 05:09.420
to create a class that is going to serve as a controller inside of our application.
05:09.440 --> 05:12.800
It is a class that is going to handle and route incoming requests.
05:13.950 --> 05:16.230
Nest makes use of decorators quite heavily.
05:16.230 --> 05:20.520
So throughout this course, we're going to have a big focus on decorators, how they work and so on.
05:20.520 --> 05:23.940
But for right now, we're just going to add these decorators in and we'll discuss what they're really
05:23.940 --> 05:25.290
doing for us later on.
05:26.250 --> 05:29.820
Then inside of our class, we're going to add in a variety of different methods.
05:29.850 --> 05:33.960
Each method is designed to handle one kind of incoming request.
05:33.990 --> 05:38.880
Specifically, we want to add in an additional method anytime we want to have another route inside of
05:38.880 --> 05:39.750
our application.
05:40.020 --> 05:46.050
So, for example, if we want to handle a get request to the root route of our application, we might
05:46.050 --> 05:48.870
add in a new method here called something like.
05:49.940 --> 05:51.070
Get root.
05:51.080 --> 05:51.800
Root.
05:52.840 --> 05:58.330
Then whenever someone makes a request to our application, we want to route that request to this method
05:58.330 --> 05:59.080
right here.
05:59.230 --> 06:03.280
To do so, we're going to import another helper from the Common library.
06:03.550 --> 06:06.040
This helper is called the Jit decorator.
06:06.070 --> 06:11.650
This allows us to create route handlers that respond to incoming requests that have an Http method of
06:11.650 --> 06:12.250
Jit.
06:12.490 --> 06:17.290
So right above my method, I'm going to put in at Jit like so.
06:18.490 --> 06:23.710
Then inside my method, anytime someone makes a request to our root route, that's going to run this
06:23.710 --> 06:28.930
method right here, and then all we have to do to respond to that request is return a value from this
06:28.930 --> 06:29.570
method.
06:29.590 --> 06:32.830
So in this case, I'm going to return simply a string of Hi there.
06:34.510 --> 06:38.280
Now, in theory, if someone makes a request to our application, it will get routed to this method
06:38.280 --> 06:38.910
right here.
06:38.940 --> 06:43.080
We're going to return that string, and nest is going to automatically take that string and send it
06:43.080 --> 06:44.610
back to whoever made the request.
06:45.570 --> 06:45.900
All right.
06:45.900 --> 06:48.030
So a lot of talking here, a lot of stuff going on.
06:48.030 --> 06:49.830
Let's take a pause very, very quickly.
06:49.830 --> 06:52.500
Come back in just a moment and start to create our module.
06:52.690 --> 06:56.940
Last thing I want to mention is if these decorators and things are really confusing right now, don't
06:56.940 --> 06:57.360
sweat it.
06:57.360 --> 07:02.130
As I mentioned, we're going to be going over decorators a tremendous amount inside this course.
10506
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.