1
00:00:06,230 --> 00:00:07,400
In this video.

2
00:00:07,430 --> 00:00:15,650
Let's go ahead to refactor the code inside our server file to use MVC design pattern file.

3
00:00:15,650 --> 00:00:21,170
To understand this design pattern, we are going to break this video into two.

4
00:00:21,200 --> 00:00:25,940
The first one is we are going to work with the routing using Express.

5
00:00:25,940 --> 00:00:30,110
And then in this video we finish up the design party.

6
00:00:30,140 --> 00:00:32,780
So let's go inside the roots.

7
00:00:32,780 --> 00:00:37,190
We are going to create number of folders based on our route.

8
00:00:37,190 --> 00:00:43,040
And for this we have users and we also have the account.

9
00:00:46,000 --> 00:00:49,660
And we also have the transactions.

10
00:00:52,870 --> 00:00:53,480
All right.

11
00:00:53,500 --> 00:00:55,350
Let's start with the users.

12
00:00:55,360 --> 00:00:58,750
So let's go ahead and then create a file for this.

13
00:00:58,900 --> 00:01:02,820
This one I'm going to be using Route Dot.

14
00:01:04,640 --> 00:01:05,610
Geez.

15
00:01:05,810 --> 00:01:09,440
So we need to use the express routing.

16
00:01:09,440 --> 00:01:13,670
So let's go ahead and then require the package from Express.

17
00:01:19,320 --> 00:01:22,620
For this, we need the routing method from express.

18
00:01:22,630 --> 00:01:23,910
So here we go.

19
00:01:23,940 --> 00:01:26,280
Counts by convention.

20
00:01:26,420 --> 00:01:28,520
Use what is called router.

21
00:01:28,530 --> 00:01:33,390
But for this I just want to name my routing based on my endpoint.

22
00:01:33,390 --> 00:01:35,630
So it is how I like to name it.

23
00:01:35,640 --> 00:01:40,140
So anywhere you see in the code, you know that all this route is for users.

24
00:01:40,140 --> 00:01:42,060
So users route.

25
00:01:42,090 --> 00:01:51,450
Let me add as to this one to be as users route and equal to express on that we have the router.

26
00:01:51,750 --> 00:01:52,270
Okay.

27
00:01:52,380 --> 00:02:00,120
So next step is that we are going to pull out the business logic inside our route here.

28
00:02:00,210 --> 00:02:06,660
So let's cut this one and let's bring it here as our users route here.

29
00:02:06,900 --> 00:02:12,270
And the next step is that we want to move the business logic inside our route.

30
00:02:12,270 --> 00:02:16,810
And for this our logic lies in this handler.

31
00:02:17,250 --> 00:02:19,080
This is the callback function here.

32
00:02:19,080 --> 00:02:24,720
So we are going to cut this one from here and use it inside the easiest route.

33
00:02:24,720 --> 00:02:32,580
So let's let all of them here and let's cut it and let's get back here and let's paste it.

34
00:02:32,580 --> 00:02:35,640
So here is our anonymous function.

35
00:02:35,640 --> 00:02:37,920
So how are we going to get it done?

36
00:02:38,100 --> 00:02:43,830
Remember, guys on the easiest route, we have all the HTTP method.

37
00:02:43,830 --> 00:02:51,840
So here if you bring in the user route dot post, you can see we have the post route gets, we have

38
00:02:51,840 --> 00:02:56,670
it delete, we have it in PWD, we also have it.

39
00:02:56,670 --> 00:03:04,560
So for this going to be as post and for this we pass in the two, it means the first one going to be

40
00:03:04,560 --> 00:03:05,700
the point.

41
00:03:05,730 --> 00:03:09,840
The second one is going to be the callback function, which is this.

42
00:03:09,840 --> 00:03:11,910
So I'll paste it here.

43
00:03:12,480 --> 00:03:13,190
All right.

44
00:03:13,200 --> 00:03:14,340
There you go.

45
00:03:14,460 --> 00:03:22,440
The next step is that I need to export this route from this file, going to be as module dock's exports

46
00:03:22,440 --> 00:03:24,720
equal to user route.

47
00:03:24,780 --> 00:03:30,060
So now back to the server file, let's go ahead and then require that route.

48
00:03:30,060 --> 00:03:32,550
So how are we going to use it?

49
00:03:32,550 --> 00:03:41,580
Well, we are not going to use this format, but instead we are going to pass this router as a middleware.

50
00:03:41,580 --> 00:03:51,900
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

51
00:03:51,900 --> 00:03:57,210
any type of HTTP method, whether it is gate put or delete.

52
00:03:57,210 --> 00:04:00,990
So here we are going to pass in two domains.

53
00:04:00,990 --> 00:04:04,500
The first one going to be the endpoint as that.

54
00:04:04,500 --> 00:04:09,150
And the second argument is callback function, which is this one.

55
00:04:09,150 --> 00:04:14,280
So we pass in here as uses route and that is it.

56
00:04:14,490 --> 00:04:16,769
But here comes another question.

57
00:04:16,769 --> 00:04:23,490
Are you going to pass this one inside the app to use or inside the router here?

58
00:04:23,520 --> 00:04:25,140
Here is the answer.

59
00:04:25,140 --> 00:04:29,010
I'm looking at the pattern of our routing.

60
00:04:29,040 --> 00:04:36,900
You can see that we have a common endpoint that is API reg one slash users, all of them start with

61
00:04:36,900 --> 00:04:37,560
that.

62
00:04:37,560 --> 00:04:38,010
Right.

63
00:04:38,010 --> 00:04:43,080
So we can put this one inside the app to use.

64
00:04:43,080 --> 00:04:49,440
So I'm going to cut this one from here and then put it inside app dot use.

65
00:04:49,470 --> 00:04:58,380
Now it has left with the register, so I'll cut this one and then bring it here as that or better so

66
00:04:58,380 --> 00:05:02,220
we can cut away from here and then bring it here.

67
00:05:02,340 --> 00:05:08,190
But let's say in future we may some update to our endpoint and then we change this one to version one

68
00:05:08,190 --> 00:05:14,730
to version two unless we go back to the routing and change from where you want Virgin to, which makes

69
00:05:14,730 --> 00:05:16,020
our life difficult.

70
00:05:16,050 --> 00:05:21,090
That is why we have a central place to have the common endpoint.

71
00:05:21,090 --> 00:05:25,440
So now let's remove this one, let's save this one.

72
00:05:25,440 --> 00:05:33,270
And again, let's go ahead and save the user route and let's test the endpoint uses and then register

73
00:05:33,270 --> 00:05:38,640
and let's hit send and you can see everything remains the same.

74
00:05:38,640 --> 00:05:46,140
So this is how we can refactor that using the routing, even though we haven't complete the MVC design

75
00:05:46,140 --> 00:05:46,950
pattern yet.

76
00:05:46,950 --> 00:05:49,260
But we will get there very soon.

77
00:05:49,260 --> 00:05:54,000
So let's go ahead and then refactor this one also for register.

78
00:05:54,000 --> 00:06:00,360
So here as we did, we are going to cut this one and back user route.

79
00:06:00,840 --> 00:06:09,570
Let's pass it here and let's start with the user route guard post and then the last endpoint is log

80
00:06:09,570 --> 00:06:12,960
in and then our callback function.

81
00:06:13,080 --> 00:06:15,030
Let me cut it from here and then.

82
00:06:15,170 --> 00:06:19,120
This it inside and everything going to be the same.

83
00:06:19,130 --> 00:06:22,260
So now I can remove this one from here.

84
00:06:22,280 --> 00:06:23,150
How is it?

85
00:06:23,150 --> 00:06:23,470
It is.

86
00:06:23,480 --> 00:06:27,290
You see how we are cleaning up our server file.

87
00:06:27,410 --> 00:06:28,250
It is awesome.

88
00:06:28,250 --> 00:06:28,760
Right?

89
00:06:28,760 --> 00:06:31,940
So let's go ahead and then test the log in also.

90
00:06:31,940 --> 00:06:35,810
And this one, everything is also working.

91
00:06:35,840 --> 00:06:38,780
The next point is going to be the file also.

92
00:06:39,000 --> 00:06:44,150
So let's also cut this one from here and let's get to this roots.

93
00:06:44,150 --> 00:06:51,110
And then down here, let's pass it and let's bring in the users root dirt gate.

94
00:06:51,500 --> 00:06:59,840
And then for this, we're going to be forward slash and then some ID and let's cut the handler from

95
00:06:59,840 --> 00:07:03,200
here and come on, separate it and paste it.

96
00:07:03,200 --> 00:07:13,670
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

97
00:07:14,030 --> 00:07:20,690
and then use this profile and then some ID.

98
00:07:20,810 --> 00:07:27,620
So here is going to be proof file and then forward slash the ID.

99
00:07:27,830 --> 00:07:37,700
So let's also go ahead and move from here, save it and now let's test the end point for file.

100
00:07:37,700 --> 00:07:41,570
And also everything is working perfectly.

101
00:07:41,810 --> 00:07:44,660
The next one going to be the delete.

102
00:07:44,660 --> 00:07:52,190
So as always, we can even come back here and then copy this one and let's change the endpoint.

103
00:07:52,190 --> 00:07:57,110
So let's go last this one and let's work with the delete.

104
00:07:57,110 --> 00:08:00,980
So let's copy this one down here.

105
00:08:01,010 --> 00:08:12,560
Change this one to delete and here use and force lives some ID and here is going to be as delete and

106
00:08:12,560 --> 00:08:16,790
here as delete user route.

107
00:08:16,790 --> 00:08:27,260
So let's go ahead and then remove from here and let's make the request for delete and let's see is not

108
00:08:27,260 --> 00:08:31,460
pull file or let's see a mine working with the profile.

109
00:08:32,090 --> 00:08:32,780
Oh sorry.

110
00:08:32,780 --> 00:08:34,700
I'm supposed to be delete.

111
00:08:34,700 --> 00:08:35,960
Yeah, it is.

112
00:08:36,470 --> 00:08:38,390
But why is it not working?

113
00:08:40,250 --> 00:08:41,090
Let's see.

114
00:08:42,140 --> 00:08:43,250
Let's check again.

115
00:08:44,550 --> 00:08:51,270
So we have the delete profile and then ID.

116
00:08:51,270 --> 00:08:53,970
Oh sorry you're going to be.

117
00:08:54,390 --> 00:08:56,910
No profile is for delete endpoint.

118
00:08:56,910 --> 00:08:57,900
This is a file.

119
00:08:57,900 --> 00:08:59,130
Sorry guys.

120
00:08:59,250 --> 00:09:01,680
Sometimes something goes wrong.

121
00:09:01,860 --> 00:09:04,920
Okay, so I have it as that.

122
00:09:04,950 --> 00:09:08,190
The next one is going to be the put.

123
00:09:08,190 --> 00:09:13,980
So let's cut this one from here and now let's get back to the root here.

124
00:09:14,010 --> 00:09:21,120
Let's copy this one and let's paste here and change this one to root.

125
00:09:21,510 --> 00:09:30,090
And the difference here is going to be the HTTP method and going to be update.

126
00:09:31,860 --> 00:09:32,670
All right.

127
00:09:32,670 --> 00:09:39,240
So let's continue on and then let's hit st and we have the update.

128
00:09:39,360 --> 00:09:46,380
So what has left is the transaction and then the account for this one.

129
00:09:46,380 --> 00:09:52,920
I'm going to give it to you as a challenge, try to refactor that one to use the routing system.

130
00:09:52,950 --> 00:09:57,630
So I would do my behind the scene and in the next video I'll show you.

