In this video, let's refactor our code to use what is called express routing.
As it stands right now, you can see how our server file is being getting bloated.
The purpose for the server file is to start our server.
All other business logic must be on a separate file.
So let's see how we are going to refactor this inside the root.
And for users, let's create one file and call it as users root or users dot JS.
And here we are going to use the routing from experts.
We need to require a IDs as press and what we want from the express is core router.
So I'll make use of router is equal to express dot router.
On the router we have all the http method talking about get posts put delete, but this is how I would
like to name my variable.
So I'm going to be as user route.
All right.
So the first one is going to be the register.
It's going to be as user route dot post and pass in the round as we did before version one and then
users and then the register then pass in the callback function as req response and has that.
And this one is the same as we did on the server file.
So let's fill out the logic so we can go ahead and then copy the logic inside that.
So I will select that and inside here I'll paste it.
So now how are you going to make use of this?
Well, we are going to export this one and then pass it as a mirror to experts, going to be as users
route or user route.
So inside the server file, let's go ahead and then require the route.
So here, make use of user routes and I have the auto import.
So how are we going to make use of it?
Well, let's arrange our import because we need to have all the environment variable before running
our route and even before our server run.
So this one will be up here and all other custom import must be low with the default modules or the
modules that we installed.
All right, so now how are we going to use it?
That's why I name here as middleware.
We are going to use it as a murderer.
So how are we going to use it on the app?
We have a method called use and this app dot use.
Also accept any HTTP method, whether it is get post, delete or update.
So for the meantime, let me pass in this endpoint like forward slash API and then forward slash something.
And I'll pass in the callback as req response and ask that.
So inside here I can go ahead and then send some text.
For the meantime, I would say that I will call always.
All right, so let's save it.
And now let's get back to the client here and let's try to make a request.
For the meantime, let's change this one to API and then something.
And it sent.
You can see that I will always call.
If I change this one to get.
You see, I go back to the same response.
If I change this one to any of these ones, it will be called.
So we are going to take that one as an advantage to be able to plug in the routing.
So now let's move this one for now, because we have this function inside the user's root here.
So I can go ahead and then use that one and let it for what?
We have imported it here as that.
So we are going to remove this function called here.
And I'm going to plug in the user's root.
All right.
So now it goes like this.
When I call this root, first of all, is going to match this.
And next step is is going to run our root here, which is this.
So behind the scene, our End Point looks something like this.
First of all, is going to be as API for a slash something.
And then first, last, when you got here, is going to look up the next endpoint here and it's going
to be as the say is going to append so I can pass it here.
So this is the entire new URL if I make use of this one.
So we need to modify the endpoints.
First of all, we need to have the base.
You are looking at the user's root.
We have the base you are starting from API version one users have a look, so now we can put this one
as base.
You are inside the app user so I can remove this one and outpaced the base.
You are error.
Remove this forward slash.
So we need to modify this one a little bit because we have used the base.
You are l inside the app dot you.
So I'm going to remove this one now.
Everything remains the same.
So when I save this one and I make a request, let me change this one back to register and this one
post and hit sent where we got an error.
I think we missed something wrong here.
Well, it's this one.
Let's remove this one from here.
Save it and let's make the request and we can see that it works fine.
So let's go ahead and then refactor the entire users here so now I can cut all the users root here.
Let's select from here down here, and I will cut it from here.
And let me get back to the user's root.
And here I'm going to remove this one and then paste that.
So the difference here is we are going to change instead of app, we are going to use user's root.
So I'll select one and press command D have multiple selection and I would change this one to use this
root.
So let's remove the API users from each of the endpoint.
All right.
So now we have refracted the easiest route.
So let me bring this one here and the users route.
I start.
So let's test the endpoints.
So this one is for register.
It works.
It worked.
Look in fine details, fine profile, fine work for the poor.
We got an error, so let's check it out.
What is wrong?
Profile.
Supposed to be profile.
Voice slash ID.
All right, so let's try again.
It worked.
And then.
Sorry, it's this one, rather.
And then upload photo.
It worked.
Cover image.
It worked.
Password Update.
Whet user update it worked and then look out.
It also worked.
Now I can see that our server file is becoming more cleaner than before.
The next one is about the post route.
So as we did for users, we can go ahead and do that one quickly.
So I'm going to create one file and call this one as post dot G's.
And inside the user is let me copy this one and paste it here and change this one to post route.
And next step is let's go ahead and then cat or the post route inside the server file.
So it starts from here and it's up to here.
Cut it and let's get back to post root and paste it here and change app to post root.
And let's remove the base u r l as we did before.
The last step is less export module.
Exports is equal to the post route.
Next step is let's require it here as post route.
The auto import is fine, which is this.
And let's plug in.
Let's remove this comment.
And he wouldn't it be apt to use the base you are L was API version one and then the post and our pass
in the post route.
Let's try to make sure that it also work.
Crates of posts.
It's fine.
Fetch.
All is fine.
Details is fine.
Delete is fine and update.
It's also fine.
The last one will be the comment.
So let's create one file.
Comments.
Dot.
Just.
And let's copy this import here and paste it here.
As we did.
Let's catch the comment from the server file.
It also starts from here and up to here.
So paste that one and change this one to the comments route.
And let's rename this one to comment.
Let's look at the base.
You are going to be API version one and then the comment.
And that is it.
Last step is less export as module exports are going to be coming throughout and less required each
year as comments route.
So let's remove these comments.
And make use of app to use and pass in the base.
You are API version one and then the comments and then the comment route.
All right, so let's test the comments as well.
Great.
And there we go.
Now, we have improved the application a little bit by having a few code on the server file.
The last help we need to implement to have complete MVC design pattern is to restructure the code inside
the root.
So let's get into that in the next video.