1
00:00:00,430 --> 00:00:07,660
All right, I think we're moving along quite nicely, but as you can see the moment we're starting to

2
00:00:07,660 --> 00:00:13,810
have more routes and more functionality, we have another issue.

3
00:00:14,170 --> 00:00:21,010
And that issue, of course, is the fact that our objets is getting quite busy and technically, we

4
00:00:21,010 --> 00:00:22,630
don't even have that many routes.

5
00:00:23,520 --> 00:00:24,690
So what is the solution?

6
00:00:24,960 --> 00:00:33,360
Well, the solution is using expert router where we can group those routes together and as far as the

7
00:00:33,360 --> 00:00:38,000
functionality and actually set them up as separate controllers.

8
00:00:38,430 --> 00:00:45,240
Now, eventually, in the next example, when we work the database, I'll cover again the common setup,

9
00:00:45,960 --> 00:00:47,400
which is called MVC.

10
00:00:47,790 --> 00:00:49,580
Basically, it is a pattern.

11
00:00:49,590 --> 00:00:55,410
It's not a rule, but it is a nicely used pattern when you're setting up the API.

12
00:00:55,410 --> 00:01:00,920
But we are missing the model part in the MVC pattern because we haven't connected the database.

13
00:01:00,930 --> 00:01:03,030
So I'll just leave it at that.

14
00:01:03,360 --> 00:01:08,100
Just please keep in mind whatever I'm about to show you in this video and in the next.

15
00:01:08,460 --> 00:01:11,300
Yes, it is a pretty common convention and pattern.

16
00:01:11,610 --> 00:01:14,650
So I also suggest sticking to that.

17
00:01:15,010 --> 00:01:22,500
Now, also, I want to mention that in this video we will do a bit of copy and pasting, meaning we'll

18
00:01:22,500 --> 00:01:26,470
have to refactor the code because I think this is a perfect example for us.

19
00:01:26,700 --> 00:01:33,190
Set up the router where we already have some code so you can clearly see the benefits of setting up

20
00:01:33,220 --> 00:01:39,600
the router instead of me coming up with some empty routes and then saying that, yes, that is the best

21
00:01:39,600 --> 00:01:39,990
pattern.

22
00:01:40,200 --> 00:01:47,850
So with that said, first where we have the request, the post request for API, postmen, people,

23
00:01:48,240 --> 00:01:54,090
I want you to change this around a little bit so you can see I keep going back and forth in this case.

24
00:01:54,090 --> 00:01:59,150
I want to go with API people and then let's just say Posman again, it doesn't really matter.

25
00:01:59,160 --> 00:02:03,250
We're not going to make requests anymore from the postman in these examples anyway.

26
00:02:03,540 --> 00:02:11,009
This is just about our code structure and that once we change this around, I also want to move this

27
00:02:11,009 --> 00:02:13,960
log-in up just so it's a bit clearer what we're doing.

28
00:02:14,250 --> 00:02:19,710
So take this log in or out and keep moving, moving, moving, moving, and just place it on top of

29
00:02:19,710 --> 00:02:20,460
your roots.

30
00:02:21,000 --> 00:02:25,530
And there is of course, I have my husband and now noticed the pattern.

31
00:02:26,750 --> 00:02:34,700
Yes, I have one rule for the Log-in, but then the rest of them, I have API people, API people,

32
00:02:35,000 --> 00:02:40,210
API people, Posman API people, ID and hopefully get the gist.

33
00:02:40,220 --> 00:02:42,380
Going to have API people ID.

34
00:02:43,400 --> 00:02:52,510
So wouldn't it be nicer if I could somehow group all the API people and also add one for the logging,

35
00:02:53,300 --> 00:02:55,420
but basically in a separate file?

36
00:02:55,760 --> 00:02:59,430
And the way we can do that is by setting up the router.

37
00:02:59,750 --> 00:03:04,240
Now, again, Common Convention is just creating another folder.

38
00:03:05,000 --> 00:03:09,590
So I just want to make sure that I'm actually doing that in the express tutorial.

39
00:03:09,860 --> 00:03:16,970
Let's create a new folder and common convention is calling this route and then you just come up with

40
00:03:16,970 --> 00:03:18,050
whatever name you want.

41
00:03:18,320 --> 00:03:24,980
Now, in my case, I'm going to go off for a login because we're just going to imagine that maybe there's

42
00:03:24,980 --> 00:03:31,900
going to be one for register, one for logout or whatever, and then there's going to be one for the

43
00:03:31,910 --> 00:03:35,680
people that's going to be for my API and people.

44
00:03:35,960 --> 00:03:37,670
So let's set up these two files.

45
00:03:37,700 --> 00:03:41,200
I will zoom in massively just so you don't think that I'm cheating.

46
00:03:41,480 --> 00:03:49,340
So let's go here with all this is where I'll place my login one and also right away set up my people.

47
00:03:49,910 --> 00:03:51,250
People J.

48
00:03:51,310 --> 00:03:53,270
Yes, OK, awesome.

49
00:03:53,300 --> 00:03:57,710
I'll start working and people just because it's going to be a little bit interesting and then we'll

50
00:03:57,710 --> 00:04:00,380
do the same thing in the off as well.

51
00:04:00,890 --> 00:04:05,260
And the way we set this up, we simply start by requiring the express.

52
00:04:05,630 --> 00:04:12,650
So we go here, xpress require and of course the module name is Express and then instead of setting

53
00:04:12,650 --> 00:04:19,360
up the app, we go with router and we explicitly get the router from the express.

54
00:04:19,610 --> 00:04:26,270
So this is going to be a router instance that takes care of the routing instead of the app.

55
00:04:26,960 --> 00:04:30,320
So we're not going to do the routing anymore in our app.

56
00:04:30,320 --> 00:04:38,900
Just sort of we will we'll use the middleware part as far as the specific URLs or handle that in this

57
00:04:38,900 --> 00:04:45,230
route people file and the way we set up the router, we go with router and that one is equal to, like

58
00:04:45,230 --> 00:04:47,090
I said, express dot.

59
00:04:47,300 --> 00:04:51,920
And then we go, we add capital letter router and then we invoke it.

60
00:04:52,760 --> 00:05:00,710
Awesome, that's a great start, we can save this sucker and now I want you to go back to Abcess and

61
00:05:00,710 --> 00:05:01,680
don't grab the luggage.

62
00:05:01,680 --> 00:05:05,350
And that's why I set it up separately above all the rest.

63
00:05:05,630 --> 00:05:09,350
But grab all of the ones with apart and people.

64
00:05:09,710 --> 00:05:15,560
So take these suckers here all the way up to the plate.

65
00:05:16,100 --> 00:05:22,220
Basically, stop, listen, cut it out and then copy and paste in people like.

66
00:05:22,220 --> 00:05:24,440
So, OK, great.

67
00:05:24,770 --> 00:05:27,140
So now of course I have all my rootsier.

68
00:05:28,060 --> 00:05:37,120
Now, our next step is actually changing from AB to Aruda, because I want the router to handle drawing,

69
00:05:37,420 --> 00:05:44,380
so in my case, I'm going to select all of my apps like so so I'm just doing that with Kamandi in the

70
00:05:44,530 --> 00:05:45,570
virtual pseudocode.

71
00:05:45,850 --> 00:05:54,130
And at this point I should have multiple customers like so and I'll set up my router and all the way

72
00:05:54,160 --> 00:05:58,930
in the bottom I want to go with module, not export.

73
00:05:59,200 --> 00:06:03,340
And of course at the moment I'm exporting my router.

74
00:06:03,850 --> 00:06:11,920
And then once we export a router, of course in The Objets, I want to set up a abductor's where I'll

75
00:06:11,920 --> 00:06:21,160
say yes for the path that starts with API and people I want to use my people router and the way it's

76
00:06:21,160 --> 00:06:23,140
going to look like we're going to go back.

77
00:06:23,530 --> 00:06:25,960
And at this point we have people here.

78
00:06:25,960 --> 00:06:31,570
And as I said, not actually we need to cut it out, but we're not going to use it here as well.

79
00:06:31,930 --> 00:06:34,300
Eventually, the setup is going to be different.

80
00:06:34,510 --> 00:06:39,820
So for now, yes, please cut it out and copy and paste in people.

81
00:06:40,090 --> 00:06:44,680
But since we'll be setting up, the controllers will have to do that one more time anyway.

82
00:06:44,890 --> 00:06:47,110
So I guess I'm going to go below the router.

83
00:06:47,350 --> 00:06:51,610
I'm going to look for my people, otherwise my functionality won't make sense.

84
00:06:51,820 --> 00:06:53,140
And in this case, remember.

85
00:06:54,090 --> 00:07:01,800
We are sitting already in route, so I need to go two levels up and I still need to look for my dear,

86
00:07:01,920 --> 00:07:02,390
correct.

87
00:07:02,790 --> 00:07:06,350
So now of course, our functionality should work.

88
00:07:06,720 --> 00:07:09,840
Hopefully we don't have some weird bugs.

89
00:07:10,170 --> 00:07:16,230
And then, like I said in the app just now, of course, I would want to use my app dot years.

90
00:07:16,500 --> 00:07:23,550
So I'm going to go below ExpressJet Sun and I'm going to say I'm not used and now I need to set up that

91
00:07:23,550 --> 00:07:24,830
base right now.

92
00:07:24,840 --> 00:07:27,040
What is my base around here now?

93
00:07:27,060 --> 00:07:29,010
It is API and people.

94
00:07:29,010 --> 00:07:32,160
Correct, because this is just my route.

95
00:07:32,370 --> 00:07:38,720
And then in some cases I have a postman or I have the ID, that's it.

96
00:07:38,910 --> 00:07:40,170
But the base stays the same.

97
00:07:40,440 --> 00:07:41,230
OK, awesome.

98
00:07:41,520 --> 00:07:49,710
So we provide this path, we say API and then people and now I want to set up that router and of course

99
00:07:49,710 --> 00:07:50,970
in order to set up the router.

100
00:07:51,830 --> 00:07:59,150
And it's important, and you can call it people Rueter, you can just call people again, that is really

101
00:07:59,150 --> 00:07:59,660
up to you.

102
00:08:00,020 --> 00:08:03,760
I'm just going to say people is equal and now we need to require it.

103
00:08:03,770 --> 00:08:10,960
And remember, we have a folder by the name of what routes and then we also have people file.

104
00:08:11,510 --> 00:08:16,040
OK, some we require our router here.

105
00:08:16,370 --> 00:08:16,870
Correct.

106
00:08:17,390 --> 00:08:18,530
So that's our router.

107
00:08:18,530 --> 00:08:20,420
And we're exporting Musim.

108
00:08:20,750 --> 00:08:26,480
So we get the people, like I said, with use, we can use the path if we want.

109
00:08:26,990 --> 00:08:28,820
So this is not going to be applied to all of them.

110
00:08:29,150 --> 00:08:36,559
This is only going to be applied to the ones that start with API people and then we pass in the people.

111
00:08:36,890 --> 00:08:37,730
We'll save it.

112
00:08:37,730 --> 00:08:46,010
But there is a problem is that once we set up this router with a base path in the people, actually

113
00:08:46,340 --> 00:08:54,020
we have a mess right now because we already have the base for our router and app address where we have

114
00:08:54,020 --> 00:08:57,410
API people and now people just check it out.

115
00:08:57,540 --> 00:09:01,310
We actually have API people forward slash API people.

116
00:09:01,670 --> 00:09:06,260
So I fully understand that this is going to look a bit drastic.

117
00:09:06,590 --> 00:09:10,850
But in order for this to work, we actually remove everything.

118
00:09:10,850 --> 00:09:12,140
We remove that base.

119
00:09:12,410 --> 00:09:15,010
So we go here with just the four slash y.

120
00:09:15,020 --> 00:09:18,980
Well, because we already have the base here, we have API people.

121
00:09:19,400 --> 00:09:22,490
So that's how the route is going to start.

122
00:09:23,330 --> 00:09:25,900
And then we just have to forget.

123
00:09:26,090 --> 00:09:34,160
So this will match whatever I have in the past and sense in the get and in a post, both of them just

124
00:09:34,160 --> 00:09:37,810
have API people while I simply go with force again.

125
00:09:37,820 --> 00:09:42,410
I fully understand that this might look a bit confusing in the beginning.

126
00:09:42,680 --> 00:09:47,450
But again, the more hours you set up, the easier it's going to get.

127
00:09:47,750 --> 00:09:55,400
So this will just match whatever path I have here, whatever based path I set up in my app.

128
00:09:55,400 --> 00:10:01,790
Just so I'm just saying, yes, I'm using this people router and in the people since I already have

129
00:10:01,790 --> 00:10:06,470
baseband in place and simply reference it using the force.

130
00:10:06,800 --> 00:10:10,520
So this is going to match whatever I have in objects.

131
00:10:10,730 --> 00:10:18,020
And once I have spent what seems like an eternity on the base path, I can keep on scrolling and get

132
00:10:18,020 --> 00:10:20,720
noticed, have API people and then postma.

133
00:10:21,110 --> 00:10:27,430
So what's the only thing that I need here is just the postman because I already have API people so I

134
00:10:27,430 --> 00:10:28,640
remove the sucker here.

135
00:10:29,360 --> 00:10:31,280
Keep on scrolling then.

136
00:10:31,280 --> 00:10:32,480
The same with is right.

137
00:10:32,780 --> 00:10:34,580
We only care about this one.

138
00:10:34,610 --> 00:10:40,820
That's the whole point of setting up the router so we can start splitting up our functionality in multiple

139
00:10:40,820 --> 00:10:41,240
files.

140
00:10:41,450 --> 00:10:44,370
Same goes with this last study.

141
00:10:44,780 --> 00:10:49,910
And even though I think I said that we're not going to use the postman, I do want to quickly check

142
00:10:50,300 --> 00:10:52,970
and I'm going to go with get simple if the get.

143
00:10:53,270 --> 00:10:55,430
Trust me, the other ones work as well.

144
00:10:55,640 --> 00:10:58,550
So we go here with API people we send.

145
00:10:58,820 --> 00:11:01,260
But unfortunately I get the error.

146
00:11:01,610 --> 00:11:01,910
Hmm.

147
00:11:02,300 --> 00:11:03,210
That's interesting.

148
00:11:03,440 --> 00:11:04,270
So let me check.

149
00:11:04,280 --> 00:11:07,380
I think the problem here is in the objets.

150
00:11:07,700 --> 00:11:11,150
I don't have the fourth, so let me try this one.

151
00:11:11,580 --> 00:11:13,760
Let me send it one more time.

152
00:11:13,760 --> 00:11:16,660
And there is now, of course, everything works.

153
00:11:16,910 --> 00:11:17,840
So my apologies.

154
00:11:17,840 --> 00:11:20,360
I forgot to add here forward slash.

155
00:11:20,700 --> 00:11:23,330
That's why we were getting the error.

156
00:11:23,570 --> 00:11:25,430
And of course everything works.

157
00:11:25,760 --> 00:11:28,490
I can go with get I can get my list.

158
00:11:29,440 --> 00:11:36,220
If I want, I can also maybe delete a person, so I'm going to go delete and we're going to test three,

159
00:11:36,440 --> 00:11:41,780
so let's say we send as you can see, the person with idea of three is missing.

160
00:11:42,070 --> 00:11:44,440
So as you can see, our outworks.

161
00:11:44,800 --> 00:11:46,090
So that's the first step.

162
00:11:46,330 --> 00:11:47,880
So now here's the challenge.

163
00:11:48,310 --> 00:11:52,630
Try to repeat the same thing with logging and wood logging.

164
00:11:53,230 --> 00:11:58,090
It is going to be a bit simpler because we don't have that long of a path.

165
00:11:58,090 --> 00:12:04,090
But I can stop the video and try to set up the logging yourself and then once you're ready, resume

166
00:12:04,090 --> 00:12:06,510
it and we are going to do it together.

167
00:12:07,440 --> 00:12:08,020
How was it?

168
00:12:08,310 --> 00:12:11,950
Hopefully you were successful and I'm going to give it a shot.

169
00:12:12,300 --> 00:12:12,990
So first.

170
00:12:13,830 --> 00:12:22,230
Remember, we needed a file, so in this case, I do have Arth Jazz awesome, so I'm just going to cut

171
00:12:22,230 --> 00:12:27,740
this sucker out from my jazz and play it in off.

172
00:12:28,180 --> 00:12:32,150
Then, of course, I do need to have the boiler plate for the router.

173
00:12:32,430 --> 00:12:39,900
So again, let's go with Xpress require we require the module by the name Express, of course, and

174
00:12:39,900 --> 00:12:46,380
we set up our router router and that one is equal to express dot router.

175
00:12:46,650 --> 00:12:56,550
We invoke it, then we change this value around like so and then we're just going to go with module

176
00:12:57,000 --> 00:13:01,620
and exports and that one is equal to our Aurora.

177
00:13:02,010 --> 00:13:07,950
We do this, then we navigate and back to objets and we'll have to import it as well.

178
00:13:08,250 --> 00:13:14,070
So we go with oth I guess let's call it that and we're looking for auth file.

179
00:13:15,130 --> 00:13:20,920
That's where our recruiters coming in and then, of course, I just need to set up my middle where my

180
00:13:21,190 --> 00:13:26,130
dad used, where I provide a path of what mile of logging.

181
00:13:26,140 --> 00:13:29,470
And in this case, I'm going to be more careful and I will add the ports.

182
00:13:30,100 --> 00:13:32,910
And of course, I'll set it up as off.

183
00:13:33,280 --> 00:13:34,840
So let's save this one.

184
00:13:34,840 --> 00:13:42,640
And then the only thing we need to do in logging is simply change it around where I'm already handling

185
00:13:42,640 --> 00:13:43,570
the logging in.

186
00:13:43,570 --> 00:13:45,340
The objets supports logging.

187
00:13:45,580 --> 00:13:49,050
So again, my route now is forward slash logging.

188
00:13:49,390 --> 00:13:51,730
So I simply go with the forward slash.

189
00:13:52,000 --> 00:13:58,020
I already cover the route in the objets, so I don't need to do that anymore in here.

190
00:13:58,180 --> 00:14:03,830
And you know, for the sucker just to make things a bit more interesting, why don't we use the browser?

191
00:14:03,850 --> 00:14:09,460
Now, keep in mind, you can still use the postman if you want, but I'm going to go the browser route,

192
00:14:09,710 --> 00:14:11,590
so we're going to go with new window.

193
00:14:11,890 --> 00:14:13,330
I'm going to go with five.

194
00:14:14,190 --> 00:14:20,550
I was in here and of course, this is going to be the small screen, I have traditional form, so I'll

195
00:14:20,640 --> 00:14:25,050
go with some kind of name, Peter and I send and I have welcome, Peter.

196
00:14:25,440 --> 00:14:29,180
And of course, if that is the case, I know that my functionality works.

197
00:14:29,550 --> 00:14:31,440
So we are in good shape.

198
00:14:31,740 --> 00:14:40,440
Again, probably the most confusing thing is this one where we use an app that used to set up our route.

199
00:14:40,710 --> 00:14:48,720
So all the routes that will start with this API people are going to be located and the people just in

200
00:14:48,720 --> 00:14:53,670
the wrong folder and in there, instead of using app, we use the router.

201
00:14:53,940 --> 00:15:02,070
And then since we already have the base set up in the app, just when we have a route that exactly matches

202
00:15:02,070 --> 00:15:03,830
the base, we just go with Fort's.

203
00:15:04,290 --> 00:15:11,490
Now, if there is something else after the base route, so some additional info, of course, then we

204
00:15:11,490 --> 00:15:18,840
go forward slash, which just means that we match the base path and then we add whatever value we want.

205
00:15:19,200 --> 00:15:27,630
And hopefully you can see how much cleaner years our abcess where now we're splitting up the functionality

206
00:15:27,990 --> 00:15:30,940
in separate files for our.

