Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,210 --> 00:00:03,910
Let's now refactor our code a little bit.
2
00:00:03,910 --> 00:00:06,800
So basically, reorganize some of our route
3
00:00:06,800 --> 00:00:08,693
to make the code a lot better.
4
00:00:09,760 --> 00:00:11,990
So right now, we have all these routes here.
5
00:00:11,990 --> 00:00:15,580
So the http method plus the url
6
00:00:15,580 --> 00:00:17,080
together with the route handler,
7
00:00:17,080 --> 00:00:19,020
which is this callback function.
8
00:00:19,020 --> 00:00:19,860
Right?
9
00:00:19,860 --> 00:00:22,020
And we have these routes and route handlers
10
00:00:22,020 --> 00:00:23,340
kind of all over the place.
11
00:00:23,340 --> 00:00:26,690
So we have this and then this and all after the other,
12
00:00:26,690 --> 00:00:28,240
but it's kind of difficult to see
13
00:00:28,240 --> 00:00:31,080
what route we actually have in our code.
14
00:00:31,080 --> 00:00:33,990
So all the routes should kind of be together,
15
00:00:33,990 --> 00:00:36,500
and then the handler functions also together.
16
00:00:36,500 --> 00:00:39,730
So separate from these routes here.
17
00:00:39,730 --> 00:00:40,563
Okay.
18
00:00:40,563 --> 00:00:43,300
So what I'm gonna do is to go ahead
19
00:00:43,300 --> 00:00:46,500
and basically export all of these handler functions
20
00:00:46,500 --> 00:00:48,510
into their own function.
21
00:00:48,510 --> 00:00:49,830
Okay?
22
00:00:49,830 --> 00:00:52,710
First of all, let's also get rid of this
23
00:00:52,710 --> 00:00:54,183
old code that we had here.
24
00:00:55,920 --> 00:00:57,950
And so, yeah.
25
00:00:57,950 --> 00:00:58,783
Take this.
26
00:00:58,783 --> 00:01:00,600
Or actually, not just this.
27
00:01:00,600 --> 00:01:02,033
So all of this.
28
00:01:03,700 --> 00:01:06,293
Take this, and create a new function out of it.
29
00:01:10,820 --> 00:01:13,077
So I'm calling this one getAllTours.
30
00:01:14,110 --> 00:01:17,170
And then I'm assigning it this function.
31
00:01:17,170 --> 00:01:18,430
Give it a save, and now here
32
00:01:18,430 --> 00:01:21,330
instead of passing the callback function directly,
33
00:01:21,330 --> 00:01:23,483
well, I just do getAllTours.
34
00:01:24,350 --> 00:01:25,760
And that's it.
35
00:01:25,760 --> 00:01:26,593
Okay?
36
00:01:26,593 --> 00:01:29,540
And so right now, if I'm gonna hit this route here.
37
00:01:29,540 --> 00:01:31,280
So this endpoint.
38
00:01:31,280 --> 00:01:34,750
It will then simply call this getAllTours function.
39
00:01:34,750 --> 00:01:37,220
So the functionality didn't change at all,
40
00:01:37,220 --> 00:01:39,303
but I did slightly refactor my code.
41
00:01:40,510 --> 00:01:41,343
Okay.
42
00:01:41,343 --> 00:01:43,220
And you will see how nice this is gonna be
43
00:01:43,220 --> 00:01:45,800
when all of the routes are together
44
00:01:45,800 --> 00:01:48,983
and all these route handlers are also together.
45
00:01:51,030 --> 00:01:52,510
So...
46
00:01:52,510 --> 00:01:57,093
This one is getTour because this is only to get one tour.
47
00:02:00,060 --> 00:02:00,893
Right?
48
00:02:04,350 --> 00:02:05,983
Add to here, getTour.
49
00:02:08,320 --> 00:02:09,152
So.
50
00:02:09,152 --> 00:02:11,220
And we're starting to see different routes
51
00:02:11,220 --> 00:02:14,343
that we have in our project at one glance.
52
00:02:15,410 --> 00:02:16,243
So.
53
00:02:16,243 --> 00:02:19,320
This is the next one, and it's gonna be createTour.
54
00:02:23,270 --> 00:02:26,270
Const createTour equals all of this.
55
00:02:30,490 --> 00:02:32,230
And here then createTour.
56
00:02:36,920 --> 00:02:39,400
Next up we have...
57
00:02:39,400 --> 00:02:40,533
Not like this.
58
00:02:42,020 --> 00:02:43,993
We have updateTour.
59
00:02:50,399 --> 00:02:51,330
All right.
60
00:02:51,330 --> 00:02:53,820
And so this also actually makes our code
61
00:02:53,820 --> 00:02:56,310
a little more clartive.
62
00:02:56,310 --> 00:02:57,143
Right?
63
00:02:57,143 --> 00:02:59,480
So we can see very easily what happens here
64
00:03:00,700 --> 00:03:02,420
when we're actually reading the code.
65
00:03:02,420 --> 00:03:03,670
So when we read this line of code,
66
00:03:03,670 --> 00:03:06,550
we immediately see, oh, this will update to tour
67
00:03:06,550 --> 00:03:08,950
because we actually have the function name here.
68
00:03:11,780 --> 00:03:12,613
Okay.
69
00:03:12,613 --> 00:03:15,200
And now finally, we have deleteTour.
70
00:03:22,200 --> 00:03:23,545
Okay.
71
00:03:23,545 --> 00:03:24,462
DeleteTour.
72
00:03:26,520 --> 00:03:27,353
So.
73
00:03:27,353 --> 00:03:28,690
This looks a lot nicer already,
74
00:03:28,690 --> 00:03:30,030
but it's still not perfect.
75
00:03:30,030 --> 00:03:31,240
We can do even better.
76
00:03:31,240 --> 00:03:34,280
Because, let's say, that we want to, for example,
77
00:03:34,280 --> 00:03:38,150
change the version or the resource name.
78
00:03:38,150 --> 00:03:39,430
We would then have to change it
79
00:03:39,430 --> 00:03:41,440
in all of these five places,
80
00:03:41,440 --> 00:03:42,980
and that is not ideal.
81
00:03:42,980 --> 00:03:44,600
And so instead of having all of this,
82
00:03:44,600 --> 00:03:46,750
we can do something better.
83
00:03:46,750 --> 00:03:49,280
So let me actually start it from scratch,
84
00:03:49,280 --> 00:03:53,870
and write app dot route, so that's a new one.
85
00:03:53,870 --> 00:03:56,570
And so here we specify the route that we want.
86
00:03:56,570 --> 00:03:59,893
So let's start actually with this one here.
87
00:04:01,630 --> 00:04:04,070
And then, on there is where we specify
88
00:04:04,070 --> 00:04:05,843
what we want to happen on get.
89
00:04:07,700 --> 00:04:08,533
Dot get.
90
00:04:09,440 --> 00:04:11,533
Well, we want getAllTours.
91
00:04:12,857 --> 00:04:13,690
So.
92
00:04:13,690 --> 00:04:14,750
What we have here is right now
93
00:04:14,750 --> 00:04:17,850
exactly the same as we have here.
94
00:04:17,850 --> 00:04:20,170
Okay, so that's coming out this part.
95
00:04:20,170 --> 00:04:21,670
So it's exactly the same.
96
00:04:21,670 --> 00:04:23,470
Now, the thing is, that here we can now
97
00:04:23,470 --> 00:04:25,593
actually chain the post method.
98
00:04:28,050 --> 00:04:32,373
And then we can say createTour just like this.
99
00:04:33,330 --> 00:04:34,163
Okay?
100
00:04:34,163 --> 00:04:35,640
And so just like this,
101
00:04:35,640 --> 00:04:38,360
we have now actually created an even better way
102
00:04:38,360 --> 00:04:40,750
of writing a route because right now,
103
00:04:40,750 --> 00:04:43,970
this is not repeated for the get and the post.
104
00:04:43,970 --> 00:04:44,803
Okay.
105
00:04:44,803 --> 00:04:46,210
Let's give it a save.
106
00:04:46,210 --> 00:04:49,513
It's nicely formatted, and even easier to read than before.
107
00:04:50,460 --> 00:04:51,293
Okay.
108
00:04:51,293 --> 00:04:52,126
So.
109
00:04:52,126 --> 00:04:53,920
The two route handlers that we use
110
00:04:53,920 --> 00:04:55,690
on this kind of route here.
111
00:04:55,690 --> 00:04:59,190
So without the id, is only for get and for post.
112
00:04:59,190 --> 00:05:02,323
So for getting all tours, and for creating a new tour.
113
00:05:03,760 --> 00:05:05,550
And now, as you can imagine,
114
00:05:05,550 --> 00:05:09,790
let's do the same for the other route.
115
00:05:09,790 --> 00:05:11,723
So basically at this url here.
116
00:05:14,740 --> 00:05:16,580
So we add this url, and now,
117
00:05:16,580 --> 00:05:18,733
we will have get, patch, and delete.
118
00:05:21,050 --> 00:05:22,200
So this one is getTour.
119
00:05:24,950 --> 00:05:28,270
This one is updateTour, ah it's already there.
120
00:05:28,270 --> 00:05:30,727
And finally, I have deleteTour.
121
00:05:30,727 --> 00:05:31,560
So deleteTour.
122
00:05:32,630 --> 00:05:33,780
Give it a save.
123
00:05:33,780 --> 00:05:36,050
And we can now go ahead and...
124
00:05:36,050 --> 00:05:38,040
Well, I'm not gonna delete it.
125
00:05:38,040 --> 00:05:41,840
So that we can keep the old code here as well.
126
00:05:41,840 --> 00:05:45,350
But, this is gonna work just the exact same way,
127
00:05:45,350 --> 00:05:47,380
but making the code a lot better,
128
00:05:47,380 --> 00:05:49,590
and also easier to read.
129
00:05:49,590 --> 00:05:51,230
Right?
130
00:05:51,230 --> 00:05:55,510
So again, just to recap, we separated our handler function
131
00:05:55,510 --> 00:05:58,890
of the route itself so that we can later actually,
132
00:05:58,890 --> 00:06:02,220
even export these into another file.
133
00:06:02,220 --> 00:06:05,220
Okay, so that will then make the code even better.
134
00:06:05,220 --> 00:06:07,603
But for now, let's keep it at this.
135
00:06:08,560 --> 00:06:12,160
So with the handlers nicely separated from the routes.
136
00:06:12,160 --> 00:06:14,570
And then, we specified all the actions
137
00:06:14,570 --> 00:06:15,900
for each of our two routes.
138
00:06:15,900 --> 00:06:17,420
Which are simply tours
139
00:06:17,420 --> 00:06:19,963
and then tours slash the id parameter.
140
00:06:20,920 --> 00:06:21,753
So.
141
00:06:21,753 --> 00:06:23,630
Just to make sure it still works,
142
00:06:23,630 --> 00:06:26,363
let's use this one here.
143
00:06:27,570 --> 00:06:29,890
And with a tour that actually is this.
144
00:06:29,890 --> 00:06:32,140
But you can see already it's not giving us an error
145
00:06:32,140 --> 00:06:34,083
and so it actually still works.
146
00:06:34,950 --> 00:06:38,720
Same four getAllTours because that's another url.
147
00:06:38,720 --> 00:06:40,930
And just to make sure it still works as well.
148
00:06:40,930 --> 00:06:42,563
Indeed, it does.
10360
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.