Hi everyone.
In this video let me tell you the what is the controller in the.
Nest.js.
Now I will use the official documentation to show you the core concept.
Now firstly, inside the overview and first step I will briefly about the previous video.
So in the previous video we have a app controller.spec.ts and controller and app.module
and app.service and main.ts.
Right, now let's deeply understand about what's all of that
Firstly.
The app.controller.ts.
This is the a basic controller with single route.
Okay.
And that's for the video.
We will understand what is the controller in nestjs and with the controller.spec.ts, it will
be the unit test for the controller.
And we already remove that.
Right.
Because I don't want to write the test in this course.
And the next thing will be the app.module.ts.
This is the root module of application.
We will understand what is the module and what is the root module in this course later.
And the app service right here it will be the basic service with single methods.
We will understand that later.
Don't worry.
and main.ts
This is the entry file application.
Okay.
In nest factory, we can see nest factory right here to run the application.
And now let's understand what's in the controller.
I will click into the controller right here.
The controllers are responsible for handle incoming requests and return response to the client.
Right here, right now.
Let's see what you mean when the client sends a request into the controller.
Okay.
For example, I have a client a client.
I send a request.
For example, I send a get method.
Okay.
Into the /product.
I want to get the product of application.
The NestJS, it will handle the traffic into something called controller okay.
It will routing.
It will route in the URL right here into the specific controller.
So that's the idea of the controller.
And in order to create the controller inside the Nest.js we use something called class and decorator.
If you already are familiar with TypeScript you may know decorator right?
Actually, you can see this is just the decorator with asterisk (at) and controller.
In order to create controller, we use something like this.
Controller.
export and export class.
Okay, make sure we export the class and we name it for the controller.
And we define the some method like this.
Okay.
And for now I assume you are already familiar with get method or post.
put patch and delete methods.
And let me show you how we can create a controller.
Now for example, right here I will create a new file called test.controller.ts and I will export
class test controller.
Okay.
For example, right here we do the get method and we must import it from Nestjs/common.
Okay.
And right here I will specify the path to path right here.
It can be undefined right.
For example I will pass with products like this.
And I will name it for method like find all products for example like this.
And here I will return I will return the string.
Maybe this a list of product.
Now let's see.
And the error right here it will come from prettierc & eslint
I will fix that later.
For now you can fix that using the right here.
Okay.
Inside the CRLF, we click on that and choose the LF option.
Okay.
I will fix that later.
Don't worry about that.
And I will go to the select product to see it.
have or not localhost 3000 and /products right here.
Right.
If I go to now you can see it say the status will be four four.
And the message cannot get product.
And the error message will be not found.
The reason it not found
Because we have not yet setup
Okay.
We doesn't have set up successfully because we don't use the decorator controller right here, right.
We can use the decorator called controller.
And even that, if I refresh you can see it also 404
Let's see what happened.
The reason is 404 because we not apply that into the module.
Then we apply that into the module.
Don't worry, I will display the module in later.
Firstly let's just import that into the controller right here.
And if I refresh now you can see this is a list of products right.
Very cool.
And that's it.
How to create a controller.
Let me brief
let me quickly show you.
Firstly, we will use the decorator will be the controller.
Right.
And the second we must import it into the something called module.
And with the module I will discuss.
In maybe few next video.
So that's the reason and how we can create a controller in Nest.js.
I will see you in the next one.