Hey, did everyone had their share?
And in this video we're going to learn that how we can actually break our application into different
components, routers, controllers and a whole bunch of other things.
We have seen kind of a basics of it, but now I'm going to introduce you to the router, which is one
of the most easiest concept throughout the course itself.
So let me first walk you through now.
First, we are going to go into the controller itself and we're going to create a new file into controller.
I'm going to call this one as home controller.
DOT Geez, feel free to name it whatever you like.
Usually I tend to use the controller at the end of the name of it, not really compulsory and feel free
to just change however you like to that.
Now one thing that is pretty common in that is directly use the export statement.
You want to export all of the data that is coming up into the controller.
So every single method is being returned like export this, export that so that you can directly import
that.
Some people prefer to actually not export like this way.
Some people like to create an index file into controller and export all of that, all of these methods
from there, again, different strategies, but everything works here.
So I'm going to call this one as exports dot and I'm going to call this one as simply home.
This method can be named as anything, whatever you like.
And further down the road, it's really simple.
We're going to go ahead and say, hey, it accepts a request and response and further down the road
it just goes like that.
And what else do you want to do here, however you want to handle the response?
In my case, I'm going to go ahead and say, hey, resort JSON.
Let's also raise a status.
So resident status, there we go.
And I'm going to go ahead and say, hey, this is a 200 status and I'm going to go ahead and say JSON
just like this, let's pass on a success message of true and let's also pass on a greeting as well.
And that would be a simple greeting.
That is going to be.
Hello from API.
That's it.
Okay.
Can we add more of this stuff here?
Like, can we import models and all of that?
Yes, of course.
In fact, we will be doing it.
But usually the exports looks like this really compact, really easy, and usually a trend to keep them
short here so that I just know the name of it.
And when I need to see the internal functionality, I can go ahead and do that.
So that's basic your controller itself.
The next come up is the root.
So we're going to create a simple new route.
This one is going to be right for home.
So I'm going to go ahead and right click a new file into this one and I'm going to be calling this one
as home JS.
Now this home to us is going to be responsible for handling all of the routes that come at a home page,
which will be slash.
So again, it's up to you how you want to handle that.
This is my way of handling this.
What we are going to need this one we need actually express to have in this route.
So we're going to go ahead and say, hey, I need Express and that Express is going to come up from
require and express.
Now, the good news is for the router, you don't need to install any other package.
Router is actually a default package from the express itself, not package, but a functionality.
So we're going to go ahead and say, hey, I want to have a router and this router will be given to
me by express dot router.
There we go.
Just like that.
Now, once you have this router up and running, the very first thing that you should do always is export
that.
So we're going to go ahead and say module exports and we want to export this router.
Now, whatever you do in between, that's up to you.
Now, in my case, I want to go ahead and say, hey, router, you will be having a route being defined.
So what is this route?
The first parameter is that that will be slash and again, it can be many of these routes like slash,
log in and all of that.
We will be doing that.
But this is a route which will be handled on to just the slash itself.
Now, further down the road, I'm going to go ahead and put up a dot.
Now, in this dot, you can add whatever the method that you want to chain on.
We saw that earlier, get, post, delete.
All of them are available.
In our case, we'll be working on just the get.
But the question is what happens when somebody visits this home route?
Why are the get verb or get method itself?
Now this is where you bring in your controller.
So I'm going to go ahead and say, Hey, I want to bring some methods from the controller.
So I go ahead and say, Hey, I want to bring something that will be coming up from one directory back
and it's, it's not really looking like happy.
Yep.
Not from my bad.
We're going to go ahead and say require just like that one directory back I want to go into controllers
and in the controller there is just one file which is home controller.
What is the name of the method in the home controller?
We just call it as home.
So let's go ahead and bring the method which is home.
That's how we do it and that's how we will be doing all ways further down the road.
You just pass on this method up here.
So I hope you can understand a little bit sense of how we are breaking down the application.
The functionality part goes into home controller itself or anything that you name that controller.
This home JS is inside the root, which is responsible for that.
Okay, if somebody visits this slash, it will get on.
This will be executed on the home.
So this will be handled by the home method itself.
But this is not all this is not all since we have broken down the application.
Now the functionality says that if you want to use that, you need to use this as a middleware.
So let me walk you through with the documentation of Express as well.
So we're going to go ahead and say, hey, express, let's go to documentation and let me search for
that.
And we're going to go ahead and say, hey, I want to get information on the router itself.
And this is how we use it.
So once we have done all of this breakage part and all of that, they actually mentioned this quite
in detail, in depth.
But what they mentioned further down the road is now you have to use this router to use functionality
and the next part and router, dot get and all of that.
Again, this can be a little bit tricky, but let me actually make it easier for you.
This is the one that you should be reading up.
So come up here into router use and try to read that in case you want to use that.
Okay, let's come back on to app dogs because this is exactly where things are going to be really,
really in control for us.
So what we need to do is first we need to bring up the roots and only roots.
We don't bring in controllers here because controllers need to go into the roots.
So we bring in just the roots here.
So I'm going to go ahead and say import all routes here.
Okay.
Let's go ahead and say I want to bring in the roots.
So let's call this one as a home.
And that will be coming up from require.
Let's go ahead and say I want to go I directly want to go inside the directory, which is roots and
inside that we have home.
Okay.
So you can call this one whatever you like, but this is how we are going to be doing this.
Now, I'll add another comment for you.
Router middleware.
Yes.
This is how exactly router actually goes on work with you.
So for any middleware, we use app use majorly and we're going to go ahead and say that whatever the
route you are going in.
So I'm not notice this very carefully.
I'm going to go ahead and say that you should be going on to slash API, slash V one and then.
We go ahead and inject this home.
So what this means, listen very carefully.
When you go on to slash API, slash V one, then just move into this home root and he will take care
of whatever happens next.
So as soon as I save this, as soon as somebody reaches to this one, then the home will take the control.
Now, home will tell you that you have different routes to work on, that you have a slash, maybe you
can have a slash login, you can have a slash register.
And on these different routes this function will execute, which is home.
Now what will home say?
We need to go back into the home controller.
So home controller will say that success is true and greeting is hello from API.
So this supposed to work in the theory itself?
Let's go ahead and test it out as well because we might have made some mistake up here.
So this is API slash, v1 slash.
So this is the home root.
Let's go into local host 4000 remember slash API slash v one slash.
That's it.
That's all the root we have and we are able to see this success.
True greeting and hello from API very carefully.
Notice one thing why we are doing all of this.
First, let just say in this home root itself, I want to get more control of this.
So I want to say slash login so I can go ahead and say slash login.
So now my obviously my root will change up here instead of somebody saying slash API, slash V1, he
cannot actually now he will see nothing here actually.
Now he needs to go into slash log in and then he'll say this.
That is very understandable.
Let's go back onto the slash and also go back up here a little bit there.
Hit, reload and we are getting this now the reason why actually a lot of big application like to do
this because it gives you a lot of control.
Instead of doing all that thing and changing the entire application, every single place you can right
your controller by one team, you can write your routes as well.
And one guy who is integrating all these API or packaging them up together, he can just import all
the routes and then change that that this is now not V1, this is actually now version two of the API.
So you can immediately define and you have so much of control that you will see nothing on this version
one and just with at a single place you have changed the entirety of the application.
Now everything is available on the root, which is version two, and that is the reason.
Having more control is the idea that we want to break our application.
I'll switch it back to version one because that's exactly what we are doing and that's exactly what
we wanted to do.
So I hope you are able to do this.
I will create just one more root to so to see that, yeah, this is how it actually works and this is
how it's going to make sense.
Okay.
So I'm going to go into home controller again just one more time and we'll be doing this so many times.
You go ahead, copy this controller, paste it up here.
And this time you go ahead and say that home are dummy.
This is really a dummy root that we're going to create.
Success is true.
And this time we're going to say that this is another dummy root notice here.
First time it took us a lot of time.
Now this time we just changed this one.
Whatever we wanted to write, we did that.
After that we go into home and we say, Hey, we got another method up here.
We call this as home dummy.
You imported that.
You simply go ahead and duplicate this line and you go ahead and say, Hey, now this time, if somebody
says slash dummy, this will be handled by home dummy.
There we go.
That's how crazy fast this actually becomes.
Do we need to do anything here in the edges?
Probably.
We need to make all of this.
Here is something interesting.
Now, we don't need to change anything.
The reason why I'm saying you, because notice here, this is not a controller, this is a root.
So anybody who visits this API slash V one, then further down the road, some of these routes are being
controlled by the home itself.
And this will get much more clear as we move on and have a little bit more complexity.
But this is the basics of it.
You can break it as much as you like.
You can just keep it as it is here.
I don't have to change anything.
I just have to bring the entirety of the home and that's it.
Now let's go ahead and see this in action.
So we're going to go ahead and see this as the V one and we get hello.
And if I go ahead and hit a slash, the same thing.
If I go ahead and say that this root was I forgot my bad this throughout we were calling it slash dummy.
My bad.
So let's go here and we're going to go ahead and say dummy and notice here we get this is a dummy route.
Again, obviously, we will not be checking all of these routes directly from the web application or
the browser.
We will be checking them through the postman and we'll be creating all of that.
But before moving that, we need to provide some of the configuration here in the app.
Just remember, we will be having files cloud, ordinary body parser, cookie parser, even.
Morgan So we need to do a lot of configuration up here.
So let's go ahead and do that in the next video.