In this video.
Let's go ahead to refactor the code inside our server file to use MVC design pattern file.
To understand this design pattern, we are going to break this video into two.
The first one is we are going to work with the routing using Express.
And then in this video we finish up the design party.
So let's go inside the roots.
We are going to create number of folders based on our route.
And for this we have users and we also have the account.
And we also have the transactions.
All right.
Let's start with the users.
So let's go ahead and then create a file for this.
This one I'm going to be using Route Dot.
Geez.
So we need to use the express routing.
So let's go ahead and then require the package from Express.
For this, we need the routing method from express.
So here we go.
Counts by convention.
Use what is called router.
But for this I just want to name my routing based on my endpoint.
So it is how I like to name it.
So anywhere you see in the code, you know that all this route is for users.
So users route.
Let me add as to this one to be as users route and equal to express on that we have the router.
Okay.
So next step is that we are going to pull out the business logic inside our route here.
So let's cut this one and let's bring it here as our users route here.
And the next step is that we want to move the business logic inside our route.
And for this our logic lies in this handler.
This is the callback function here.
So we are going to cut this one from here and use it inside the easiest route.
So let's let all of them here and let's cut it and let's get back here and let's paste it.
So here is our anonymous function.
So how are we going to get it done?
Remember, guys on the easiest route, we have all the HTTP method.
So here if you bring in the user route dot post, you can see we have the post route gets, we have
it delete, we have it in PWD, we also have it.
So for this going to be as post and for this we pass in the two, it means the first one going to be
the point.
The second one is going to be the callback function, which is this.
So I'll paste it here.
All right.
There you go.
The next step is that I need to export this route from this file, going to be as module dock's exports
equal to user route.
So now back to the server file, let's go ahead and then require that route.
So how are we going to use it?
Well, we are not going to use this format, but instead we are going to pass this router as a middleware.
And this how we can plug in to use media word by making use of app dot use for app to use it can accept
any type of HTTP method, whether it is gate put or delete.
So here we are going to pass in two domains.
The first one going to be the endpoint as that.
And the second argument is callback function, which is this one.
So we pass in here as uses route and that is it.
But here comes another question.
Are you going to pass this one inside the app to use or inside the router here?
Here is the answer.
I'm looking at the pattern of our routing.
You can see that we have a common endpoint that is API reg one slash users, all of them start with
that.
Right.
So we can put this one inside the app to use.
So I'm going to cut this one from here and then put it inside app dot use.
Now it has left with the register, so I'll cut this one and then bring it here as that or better so
we can cut away from here and then bring it here.
But let's say in future we may some update to our endpoint and then we change this one to version one
to version two unless we go back to the routing and change from where you want Virgin to, which makes
our life difficult.
That is why we have a central place to have the common endpoint.
So now let's remove this one, let's save this one.
And again, let's go ahead and save the user route and let's test the endpoint uses and then register
and let's hit send and you can see everything remains the same.
So this is how we can refactor that using the routing, even though we haven't complete the MVC design
pattern yet.
But we will get there very soon.
So let's go ahead and then refactor this one also for register.
So here as we did, we are going to cut this one and back user route.
Let's pass it here and let's start with the user route guard post and then the last endpoint is log
in and then our callback function.
Let me cut it from here and then.
This it inside and everything going to be the same.
So now I can remove this one from here.
How is it?
It is.
You see how we are cleaning up our server file.
It is awesome.
Right?
So let's go ahead and then test the log in also.
And this one, everything is also working.
The next point is going to be the file also.
So let's also cut this one from here and let's get to this roots.
And then down here, let's pass it and let's bring in the users root dirt gate.
And then for this, we're going to be forward slash and then some ID and let's cut the handler from
here and come on, separate it and paste it.
So let's bring in the comments as that I'm going to be forced to log in and here it's going to be it
and then use this profile and then some ID.
So here is going to be proof file and then forward slash the ID.
So let's also go ahead and move from here, save it and now let's test the end point for file.
And also everything is working perfectly.
The next one going to be the delete.
So as always, we can even come back here and then copy this one and let's change the endpoint.
So let's go last this one and let's work with the delete.
So let's copy this one down here.
Change this one to delete and here use and force lives some ID and here is going to be as delete and
here as delete user route.
So let's go ahead and then remove from here and let's make the request for delete and let's see is not
pull file or let's see a mine working with the profile.
Oh sorry.
I'm supposed to be delete.
Yeah, it is.
But why is it not working?
Let's see.
Let's check again.
So we have the delete profile and then ID.
Oh sorry you're going to be.
No profile is for delete endpoint.
This is a file.
Sorry guys.
Sometimes something goes wrong.
Okay, so I have it as that.
The next one is going to be the put.
So let's cut this one from here and now let's get back to the root here.
Let's copy this one and let's paste here and change this one to root.
And the difference here is going to be the HTTP method and going to be update.
All right.
So let's continue on and then let's hit st and we have the update.
So what has left is the transaction and then the account for this one.
I'm going to give it to you as a challenge, try to refactor that one to use the routing system.
So I would do my behind the scene and in the next video I'll show you.