Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,160 --> 00:00:01,760
this eight hour course will teach you
2
00:00:01,760 --> 00:00:04,720
the fundamentals of node.js and express
3
00:00:04,720 --> 00:00:07,120
so you can start creating backend and
4
00:00:07,120 --> 00:00:10,400
full stack web apps using javascript
5
00:00:10,400 --> 00:00:13,280
this course was created by john smilga
6
00:00:13,280 --> 00:00:16,000
john has created a bunch of courses
7
00:00:16,000 --> 00:00:18,000
including many of the more popular
8
00:00:18,000 --> 00:00:19,680
courses on the freecodecamp youtube
9
00:00:19,680 --> 00:00:20,720
channel
10
00:00:20,720 --> 00:00:23,359
john's teaching is very hands-on so you
11
00:00:23,359 --> 00:00:25,279
will create a bunch of projects while
12
00:00:25,279 --> 00:00:27,840
you're learning what's up everybody this
13
00:00:27,840 --> 00:00:30,720
is john from coding addict and welcome
14
00:00:30,720 --> 00:00:34,880
to node and express tutorial video where
15
00:00:34,880 --> 00:00:36,880
we'll cover fundamental building blocks
16
00:00:36,880 --> 00:00:40,559
of node and express js framework now
17
00:00:40,559 --> 00:00:42,559
since i want everyone to be on the same
18
00:00:42,559 --> 00:00:45,120
page we'll start the series with node
19
00:00:45,120 --> 00:00:47,680
and express fundamentals and gradually
20
00:00:47,680 --> 00:00:50,480
move to more complex topics we will
21
00:00:50,480 --> 00:00:53,840
build complex rest api with express.js
22
00:00:53,840 --> 00:00:57,199
as well as full-blown mern application
23
00:00:57,199 --> 00:00:59,359
in this comprehensive node.js course
24
00:00:59,359 --> 00:01:01,600
you'll not only learn fundamentals of
25
00:01:01,600 --> 00:01:05,119
node but also many related technologies
26
00:01:05,119 --> 00:01:08,400
like express.js framework mongodb
27
00:01:08,400 --> 00:01:11,200
as well as mongoose the main goal of the
28
00:01:11,200 --> 00:01:13,200
course is to show you how to build
29
00:01:13,200 --> 00:01:16,799
modern fast and scalable server-side web
30
00:01:16,799 --> 00:01:18,960
applications with node
31
00:01:18,960 --> 00:01:21,680
like in all my other courses to solidify
32
00:01:21,680 --> 00:01:23,840
our knowledge we'll build a bunch of
33
00:01:23,840 --> 00:01:26,560
cool apps and go through the steps of
34
00:01:26,560 --> 00:01:28,400
deployment as well
35
00:01:28,400 --> 00:01:30,880
lastly let me just say this if you're
36
00:01:30,880 --> 00:01:33,360
like me and you enjoy learning new tech
37
00:01:33,360 --> 00:01:34,880
by building projects
38
00:01:34,880 --> 00:01:37,040
instead of going through the slides you
39
00:01:37,040 --> 00:01:38,880
will enjoy this course
40
00:01:38,880 --> 00:01:40,560
all right and let's start by answering
41
00:01:40,560 --> 00:01:42,960
the most obvious question first what is
42
00:01:42,960 --> 00:01:44,320
node.js
43
00:01:44,320 --> 00:01:45,840
and even though there are plenty of good
44
00:01:45,840 --> 00:01:47,520
answers out there the one that i like
45
00:01:47,520 --> 00:01:49,119
the most is this one
46
00:01:49,119 --> 00:01:51,840
node.js is an environment to run
47
00:01:51,840 --> 00:01:53,119
javascript
48
00:01:53,119 --> 00:01:54,880
outside of the browser
49
00:01:54,880 --> 00:01:58,240
node.js was created in 2009 and it's
50
00:01:58,240 --> 00:02:01,439
built on top of chrome's v8 javascript
51
00:02:01,439 --> 00:02:02,240
engine
52
00:02:02,240 --> 00:02:04,399
as you probably are aware of every
53
00:02:04,399 --> 00:02:07,439
browser has an engine a tool that
54
00:02:07,439 --> 00:02:10,959
compiles our code down to machine code
55
00:02:10,959 --> 00:02:14,000
and chrome uses one by the name of v8 in
56
00:02:14,000 --> 00:02:17,360
extremely simple terms the creator of
57
00:02:17,360 --> 00:02:21,200
node ryan dahl ripped out the v8 engine
58
00:02:21,200 --> 00:02:23,760
and build node on top of it since the
59
00:02:23,760 --> 00:02:26,720
moment it was created node has evolved
60
00:02:26,720 --> 00:02:27,920
tremendously
61
00:02:27,920 --> 00:02:29,920
and while there are many things to like
62
00:02:29,920 --> 00:02:32,720
about node i particularly enjoy large
63
00:02:32,720 --> 00:02:35,120
community around node since that
64
00:02:35,120 --> 00:02:37,680
tremendously saves time on feature
65
00:02:37,680 --> 00:02:39,840
development as well as the fact that
66
00:02:39,840 --> 00:02:42,000
with the help of node it's never been
67
00:02:42,000 --> 00:02:44,319
easier to build full stack apps since
68
00:02:44,319 --> 00:02:46,879
both front end and backend are
69
00:02:46,879 --> 00:02:49,680
essentially built in the same language
70
00:02:49,680 --> 00:02:52,160
and you guessed it that language is our
71
00:02:52,160 --> 00:02:55,040
beloved javascript as far as course
72
00:02:55,040 --> 00:02:56,640
requirements go
73
00:02:56,640 --> 00:02:58,400
there are not that many
74
00:02:58,400 --> 00:03:00,959
my expectation is that you are familiar
75
00:03:00,959 --> 00:03:05,040
with basic html elements simple css
76
00:03:05,040 --> 00:03:08,239
rules and have general understanding of
77
00:03:08,239 --> 00:03:12,080
javascript especially es6 callbacks
78
00:03:12,080 --> 00:03:15,360
promises and a sync await
79
00:03:15,360 --> 00:03:17,440
since this is a node course
80
00:03:17,440 --> 00:03:19,920
and i already have tremendous amount of
81
00:03:19,920 --> 00:03:22,239
node content to cover i'm not going to
82
00:03:22,239 --> 00:03:25,680
spend too much time on javascript
83
00:03:25,680 --> 00:03:27,680
language fundamentals
84
00:03:27,680 --> 00:03:29,040
while you can still follow along with
85
00:03:29,040 --> 00:03:31,760
the course without knowing basics
86
00:03:31,760 --> 00:03:34,000
it's crucial if you want to understand
87
00:03:34,000 --> 00:03:36,799
how stuff works behind the scenes
88
00:03:36,799 --> 00:03:39,120
as a side note if you need to brush up
89
00:03:39,120 --> 00:03:42,239
on the javascript fundamentals i have a
90
00:03:42,239 --> 00:03:44,720
youtube channel called coding addict
91
00:03:44,720 --> 00:03:47,120
where i post a bunch of cool javascript
92
00:03:47,120 --> 00:03:48,480
related content
93
00:03:48,480 --> 00:03:51,040
specifically you might be interested in
94
00:03:51,040 --> 00:03:53,599
js nuggets playlist where i cover a
95
00:03:53,599 --> 00:03:57,200
bunch of useful es6 features as well as
96
00:03:57,200 --> 00:04:00,000
asynchronous javascript topics so if you
97
00:04:00,000 --> 00:04:02,239
need to jog your memory on javascript
98
00:04:02,239 --> 00:04:05,680
fundamentals i suggest going to youtube
99
00:04:05,680 --> 00:04:07,599
look for the channel by the name of
100
00:04:07,599 --> 00:04:10,640
coding addict and then more specifically
101
00:04:10,640 --> 00:04:13,840
the playlist by the name of javascript
102
00:04:13,840 --> 00:04:15,920
nuggets when it comes to course
103
00:04:15,920 --> 00:04:18,720
structure it goes something like this
104
00:04:18,720 --> 00:04:21,279
in first section we'll get familiar with
105
00:04:21,279 --> 00:04:22,560
node.js
106
00:04:22,560 --> 00:04:24,960
as well as my dev setup
107
00:04:24,960 --> 00:04:26,720
in the second section we're going to
108
00:04:26,720 --> 00:04:29,360
install node.js and quickly cover some
109
00:04:29,360 --> 00:04:31,919
general differences between browser.js
110
00:04:31,919 --> 00:04:33,280
and node.js
111
00:04:33,280 --> 00:04:35,759
then we'll spend some time on node.js
112
00:04:35,759 --> 00:04:39,040
fundamentals in section 3 and after that
113
00:04:39,040 --> 00:04:42,160
we'll learn what is express.js in
114
00:04:42,160 --> 00:04:43,680
section number four
115
00:04:43,680 --> 00:04:46,240
and once we're done with node and
116
00:04:46,240 --> 00:04:48,639
express fundamentals so once we're done
117
00:04:48,639 --> 00:04:50,160
with pleasantries
118
00:04:50,160 --> 00:04:52,080
then we're after the races where
119
00:04:52,080 --> 00:04:54,560
effectively in every section after that
120
00:04:54,560 --> 00:04:57,759
we will build a bunch of cool node.js
121
00:04:57,759 --> 00:05:00,320
apps all right and before we install
122
00:05:00,320 --> 00:05:02,960
node let's quickly cover some major
123
00:05:02,960 --> 00:05:05,120
differences between browser javascript
124
00:05:05,120 --> 00:05:08,320
and node.js so essentially how node.js
125
00:05:08,320 --> 00:05:10,960
apps are different from the browser
126
00:05:10,960 --> 00:05:12,320
javascript apps
127
00:05:12,320 --> 00:05:14,160
and let's start with the biggest one
128
00:05:14,160 --> 00:05:17,440
when working on node.js app you have no
129
00:05:17,440 --> 00:05:20,400
access let me repeat no access to
130
00:05:20,400 --> 00:05:22,320
browser apis
131
00:05:22,320 --> 00:05:25,120
since well they don't simply exist in
132
00:05:25,120 --> 00:05:27,600
node there is no browser so there's no
133
00:05:27,600 --> 00:05:31,280
dom there's no geo location no fetch and
134
00:05:31,280 --> 00:05:34,160
all the other cool browser goodies you
135
00:05:34,160 --> 00:05:36,800
see unlike browser apps that are
136
00:05:36,800 --> 00:05:39,120
interactive for example user clicks the
137
00:05:39,120 --> 00:05:41,840
button or toggles the nav and that sort
138
00:05:41,840 --> 00:05:42,800
of thing
139
00:05:42,800 --> 00:05:46,800
with node we build server side apps
140
00:05:46,800 --> 00:05:48,479
and i know it's hard to picture that
141
00:05:48,479 --> 00:05:49,680
right now but
142
00:05:49,680 --> 00:05:53,199
our node apps will only consist of pure
143
00:05:53,199 --> 00:05:56,880
logic without the graphical interface on
144
00:05:56,880 --> 00:05:59,440
that note you know how in browser apps
145
00:05:59,440 --> 00:06:02,400
we use a bunch of cool things available
146
00:06:02,400 --> 00:06:04,479
on the window object
147
00:06:04,479 --> 00:06:06,800
yeah sorry to break it to you but that's
148
00:06:06,800 --> 00:06:09,360
also not available in node
149
00:06:09,360 --> 00:06:12,160
so at no point in the course we'll write
150
00:06:12,160 --> 00:06:14,560
document.queryselector
151
00:06:14,560 --> 00:06:15,440
all right
152
00:06:15,440 --> 00:06:17,759
and once we have covered things that
153
00:06:17,759 --> 00:06:20,880
node.js does not have let's switch gears
154
00:06:20,880 --> 00:06:22,800
and see some of the goodies that node
155
00:06:22,800 --> 00:06:26,160
has to offer for example unlike browser
156
00:06:26,160 --> 00:06:28,319
we can access file system
157
00:06:28,319 --> 00:06:31,360
info about our operating system respond
158
00:06:31,360 --> 00:06:34,639
to network quests just to name a few
159
00:06:34,639 --> 00:06:37,120
also another major benefit of node is
160
00:06:37,120 --> 00:06:40,000
the fact that it's based on versions
161
00:06:40,000 --> 00:06:42,960
so unlike browser apps which depend on
162
00:06:42,960 --> 00:06:45,039
the user's browser
163
00:06:45,039 --> 00:06:48,080
our node app depends only on node.js
164
00:06:48,080 --> 00:06:49,759
version it was built in
165
00:06:49,759 --> 00:06:53,039
so in other words if we build a browser
166
00:06:53,039 --> 00:06:53,840
app
167
00:06:53,840 --> 00:06:56,800
and if our user's browser does not
168
00:06:56,800 --> 00:06:59,039
support a specific feature
169
00:06:59,039 --> 00:07:01,919
it is our responsibility to fix it
170
00:07:01,919 --> 00:07:04,560
otherwise our app might have bugs
171
00:07:04,560 --> 00:07:07,199
correct but when we're done building our
172
00:07:07,199 --> 00:07:09,520
node app there's no rule that we have to
173
00:07:09,520 --> 00:07:12,880
upgrade to a newer node.js version
174
00:07:12,880 --> 00:07:15,120
and possibly break our app
175
00:07:15,120 --> 00:07:17,520
we simply can keep running our node app
176
00:07:17,520 --> 00:07:20,080
in the version it was built in and
177
00:07:20,080 --> 00:07:23,120
without any worries go on about our day
178
00:07:23,120 --> 00:07:25,599
lastly unlike browsers where modules are
179
00:07:25,599 --> 00:07:26,560
optional
180
00:07:26,560 --> 00:07:28,960
in node.js you have access to modules by
181
00:07:28,960 --> 00:07:32,319
default and it's hard to imagine a node
182
00:07:32,319 --> 00:07:35,199
app where you're not going to use them
183
00:07:35,199 --> 00:07:37,680
as a quick side note under the hood
184
00:07:37,680 --> 00:07:40,319
nodejs uses common js library for the
185
00:07:40,319 --> 00:07:42,479
modules and while the general idea is
186
00:07:42,479 --> 00:07:45,599
exactly the same as for the es6 modules
187
00:07:45,599 --> 00:07:49,039
that we use in the browser syntax is a
188
00:07:49,039 --> 00:07:50,240
bit different
189
00:07:50,240 --> 00:07:51,919
as far as the install it's as
190
00:07:51,919 --> 00:07:54,000
straightforward as it gets
191
00:07:54,000 --> 00:07:57,919
just navigate to nodejs.org pick a lts
192
00:07:57,919 --> 00:07:58,879
version
193
00:07:58,879 --> 00:08:01,440
answer yes to all the questions
194
00:08:01,440 --> 00:08:04,000
and if you don't get some weird bugs
195
00:08:04,000 --> 00:08:06,879
congrats you have installed node
196
00:08:06,879 --> 00:08:08,720
in order to check if everything went
197
00:08:08,720 --> 00:08:10,720
correct just type node
198
00:08:10,720 --> 00:08:12,479
version in your terminal
199
00:08:12,479 --> 00:08:14,960
and you should see the version number if
200
00:08:14,960 --> 00:08:17,440
you don't you'll have to troubleshoot
201
00:08:17,440 --> 00:08:19,919
the error now as much as i would like to
202
00:08:19,919 --> 00:08:20,720
help
203
00:08:20,720 --> 00:08:23,280
when it comes to install it's impossible
204
00:08:23,280 --> 00:08:26,240
for me to troubleshoot your local setup
205
00:08:26,240 --> 00:08:28,720
so when it comes to troubleshooting
206
00:08:28,720 --> 00:08:31,599
install errors i strongly suggest using
207
00:08:31,599 --> 00:08:34,320
your favorite search engine instead
208
00:08:34,320 --> 00:08:36,559
typically within a matter of minutes
209
00:08:36,559 --> 00:08:38,958
you'll see a bunch of useful solutions
210
00:08:38,958 --> 00:08:41,120
to your exact problem
211
00:08:41,120 --> 00:08:43,599
lastly let's also talk about versions as
212
00:08:43,599 --> 00:08:45,279
you can see at the time of the recording
213
00:08:45,279 --> 00:08:46,720
i have two options
214
00:08:46,720 --> 00:08:48,399
14 lts
215
00:08:48,399 --> 00:08:50,320
or 15 current one
216
00:08:50,320 --> 00:08:51,839
if you read the note docs you'll see
217
00:08:51,839 --> 00:08:54,720
that for production apps they always
218
00:08:54,720 --> 00:08:58,560
suggest using lts which stands for long
219
00:08:58,560 --> 00:09:01,440
term support and therefore i will also
220
00:09:01,440 --> 00:09:03,839
suggest the same but as far as tutorial
221
00:09:03,839 --> 00:09:06,000
i don't think you'll have any issues if
222
00:09:06,000 --> 00:09:08,560
you use the current version instead now
223
00:09:08,560 --> 00:09:10,320
also since you're watching this in the
224
00:09:10,320 --> 00:09:13,920
future your version number most likely
225
00:09:13,920 --> 00:09:16,720
is going to be greater than 14 basically
226
00:09:16,720 --> 00:09:19,279
greater than mine i definitely suggest
227
00:09:19,279 --> 00:09:21,600
installing the lts option
228
00:09:21,600 --> 00:09:23,200
that's offered to you instead of
229
00:09:23,200 --> 00:09:25,440
downgrading to my version and if you're
230
00:09:25,440 --> 00:09:27,200
stressing about content being out of
231
00:09:27,200 --> 00:09:28,000
date
232
00:09:28,000 --> 00:09:30,160
let me assure you that everything we
233
00:09:30,160 --> 00:09:32,880
cover in the course will work perfectly
234
00:09:32,880 --> 00:09:36,800
fine with your current note version
235
00:09:36,800 --> 00:09:40,560
and eventually if some bugs do creep in
236
00:09:40,560 --> 00:09:42,160
i will definitely update the course
237
00:09:42,160 --> 00:09:45,360
content so long story short just install
238
00:09:45,360 --> 00:09:48,880
your current lts version whatever it is
239
00:09:48,880 --> 00:09:51,760
and you'll be in good shape so in my
240
00:09:51,760 --> 00:09:53,440
case it is 14
241
00:09:53,440 --> 00:09:55,920
i have two big buttons and of course
242
00:09:55,920 --> 00:09:58,399
since i would want to go with lts i'll
243
00:09:58,399 --> 00:10:00,240
click on left one
244
00:10:00,240 --> 00:10:03,200
then notice we are getting the download
245
00:10:03,200 --> 00:10:05,519
package and now i just need to go to my
246
00:10:05,519 --> 00:10:07,279
downloads
247
00:10:07,279 --> 00:10:09,519
and then crack it open
248
00:10:09,519 --> 00:10:11,040
and then like i said we just need to
249
00:10:11,040 --> 00:10:14,079
answer yes to all the questions so we go
250
00:10:14,079 --> 00:10:15,440
with continue
251
00:10:15,440 --> 00:10:16,640
continue
252
00:10:16,640 --> 00:10:18,880
and yep i would agree
253
00:10:18,880 --> 00:10:21,920
and again we just install and then we'll
254
00:10:21,920 --> 00:10:24,720
be prompted for our passport
255
00:10:24,720 --> 00:10:28,000
and once we add that one we should be in
256
00:10:28,000 --> 00:10:30,560
good shape and then like i said once the
257
00:10:30,560 --> 00:10:32,959
install is complete
258
00:10:32,959 --> 00:10:34,959
and yep we'll move this sucker to the
259
00:10:34,959 --> 00:10:38,000
trash then if we go to our terminal and
260
00:10:38,000 --> 00:10:40,320
if i massively zoom in
261
00:10:40,320 --> 00:10:42,800
and if i type node
262
00:10:42,800 --> 00:10:44,000
version
263
00:10:44,000 --> 00:10:46,240
there it is i should have my version
264
00:10:46,240 --> 00:10:49,360
number which in my case of course is 14.
265
00:10:49,360 --> 00:10:52,079
if you also can see the node version in
266
00:10:52,079 --> 00:10:54,880
your terminal you are in good shape and
267
00:10:54,880 --> 00:10:56,240
you can continue
268
00:10:56,240 --> 00:10:57,760
with the videos
269
00:10:57,760 --> 00:10:58,800
if not
270
00:10:58,800 --> 00:11:01,360
please troubleshoot it because otherwise
271
00:11:01,360 --> 00:11:04,240
everything we're about to do next is not
272
00:11:04,240 --> 00:11:06,079
going to work since you haven't
273
00:11:06,079 --> 00:11:07,440
installed node
274
00:11:07,440 --> 00:11:09,120
on your computer
275
00:11:09,120 --> 00:11:11,200
install is complete hopefully we all
276
00:11:11,200 --> 00:11:14,720
have node on our machines now what
277
00:11:14,720 --> 00:11:16,079
what do we do
278
00:11:16,079 --> 00:11:18,720
for contrast with vanilla.js it was
279
00:11:18,720 --> 00:11:21,920
somewhat easy right we create index.html
280
00:11:21,920 --> 00:11:24,880
jam our javascript code whichever way we
281
00:11:24,880 --> 00:11:27,839
prefer and browser does the rest it
282
00:11:27,839 --> 00:11:30,800
evaluates our javascript code but
283
00:11:30,800 --> 00:11:33,600
what about node how do we get node to
284
00:11:33,600 --> 00:11:35,519
evaluate our code
285
00:11:35,519 --> 00:11:37,760
and effectively we have two options
286
00:11:37,760 --> 00:11:42,000
a repl which stands for read eval print
287
00:11:42,000 --> 00:11:45,440
loop and cli executable which just means
288
00:11:45,440 --> 00:11:49,040
running our app code in node if i have
289
00:11:49,040 --> 00:11:51,839
to be perfectly honest reply is for
290
00:11:51,839 --> 00:11:54,639
playing around and cli is essentially
291
00:11:54,639 --> 00:11:56,959
for everything else in fact this is
292
00:11:56,959 --> 00:11:58,800
going to be the first and most likely
293
00:11:58,800 --> 00:12:01,680
the only time we'll use a rebel in this
294
00:12:01,680 --> 00:12:05,600
course and after that cli is going to be
295
00:12:05,600 --> 00:12:07,839
our best friend how do we spin up the
296
00:12:07,839 --> 00:12:09,600
rebel well
297
00:12:09,600 --> 00:12:12,000
we go to terminal and in terminal we
298
00:12:12,000 --> 00:12:13,360
simply type
299
00:12:13,360 --> 00:12:16,480
node and then no arguments nothing we
300
00:12:16,480 --> 00:12:19,920
just press return or enter however you
301
00:12:19,920 --> 00:12:21,920
would like to call the button and that's
302
00:12:21,920 --> 00:12:24,880
it now notice we have this little angle
303
00:12:24,880 --> 00:12:27,279
bracket that just means that we are in
304
00:12:27,279 --> 00:12:30,000
the rebel and once we are in a rebel we
305
00:12:30,000 --> 00:12:32,880
can do reply things what would be a
306
00:12:32,880 --> 00:12:34,880
first ripley thing we can do well we can
307
00:12:34,880 --> 00:12:37,519
simply type our name so cons
308
00:12:37,519 --> 00:12:40,560
and the name is equal to john
309
00:12:40,560 --> 00:12:42,720
and there it is of course now i can
310
00:12:42,720 --> 00:12:46,800
access name and my name is john
311
00:12:46,800 --> 00:12:48,320
as you can see
312
00:12:48,320 --> 00:12:49,680
you're not going to write anything
313
00:12:49,680 --> 00:12:52,079
serious in here and if you have written
314
00:12:52,079 --> 00:12:54,160
any vanilla javascript apps you know
315
00:12:54,160 --> 00:12:56,480
that of course we can also do the same
316
00:12:56,480 --> 00:12:59,200
thing in a browser console
317
00:12:59,200 --> 00:13:01,519
so we can also evaluate some code there
318
00:13:01,519 --> 00:13:03,200
but of course we're not going to write
319
00:13:03,200 --> 00:13:06,000
anything serious in a browser's console
320
00:13:06,000 --> 00:13:08,800
so the same thing is for reple yes we
321
00:13:08,800 --> 00:13:10,800
can come here we can play around but
322
00:13:10,800 --> 00:13:12,959
when it comes to serious applications of
323
00:13:12,959 --> 00:13:16,240
course you'll pick a cli executable
324
00:13:16,240 --> 00:13:18,800
instead and in order to close the repo
325
00:13:18,800 --> 00:13:22,320
we simply go with control and c
326
00:13:22,320 --> 00:13:24,079
and as you can see we have suggestion
327
00:13:24,079 --> 00:13:25,839
that we need to press it again and now
328
00:13:25,839 --> 00:13:29,040
of course we're out of the rebel not bad
329
00:13:29,040 --> 00:13:31,360
that bad we now know how the ripple
330
00:13:31,360 --> 00:13:32,240
works
331
00:13:32,240 --> 00:13:36,000
but what about a more serious node
332
00:13:36,000 --> 00:13:37,120
application
333
00:13:37,120 --> 00:13:41,279
how do we get node to evaluate our code
334
00:13:41,279 --> 00:13:44,240
well we would need to start by creating
335
00:13:44,240 --> 00:13:48,000
a project right so back on my desktop
336
00:13:48,000 --> 00:13:50,079
i'm going to create a new folder
337
00:13:50,079 --> 00:13:52,240
and surprise surprise i will call this
338
00:13:52,240 --> 00:13:54,560
tutorial why well because we'll do
339
00:13:54,560 --> 00:13:56,560
tutorial things
340
00:13:56,560 --> 00:13:59,199
and i'm going to say tutorial now of
341
00:13:59,199 --> 00:14:01,440
course if you want you can add node but
342
00:14:01,440 --> 00:14:04,160
i'm just going to leave it as a tutorial
343
00:14:04,160 --> 00:14:06,800
and then of course we'll have to use our
344
00:14:06,800 --> 00:14:09,440
text error in my case like i already
345
00:14:09,440 --> 00:14:11,600
previously mentioned i'm using visual
346
00:14:11,600 --> 00:14:12,959
studio code
347
00:14:12,959 --> 00:14:15,680
and my preferred method is just drag and
348
00:14:15,680 --> 00:14:16,480
drop
349
00:14:16,480 --> 00:14:18,560
and then instead of the folder what i
350
00:14:18,560 --> 00:14:21,839
would want is to create a new file and
351
00:14:21,839 --> 00:14:24,079
this is the case where you can literally
352
00:14:24,079 --> 00:14:26,160
name it whatever you would want and in
353
00:14:26,160 --> 00:14:30,560
my case i'm going to go with app and js
354
00:14:30,560 --> 00:14:33,360
and in this app.js okay now we want to
355
00:14:33,360 --> 00:14:37,120
write some more serious node program and
356
00:14:37,120 --> 00:14:38,560
the program is going to be following
357
00:14:38,560 --> 00:14:41,279
where there is a const so there's a
358
00:14:41,279 --> 00:14:43,760
variable by the name of amount and as a
359
00:14:43,760 --> 00:14:46,320
side note i'll make this bigger and i'll
360
00:14:46,320 --> 00:14:48,800
probably bump up the font size just so
361
00:14:48,800 --> 00:14:51,199
we can see a bit clearer
362
00:14:51,199 --> 00:14:53,920
so we're gonna go with const and not
363
00:14:53,920 --> 00:14:56,720
also close the sidebar hopefully you see
364
00:14:56,720 --> 00:14:58,320
that i have the file by the name of
365
00:14:58,320 --> 00:14:59,680
app.js
366
00:14:59,680 --> 00:15:01,440
and like i previously mentioned we're
367
00:15:01,440 --> 00:15:02,959
going to have a variable by the name of
368
00:15:02,959 --> 00:15:06,240
amount and it's going to be equal to 12
369
00:15:06,240 --> 00:15:08,560
and now let's set up a if condition
370
00:15:08,560 --> 00:15:11,440
where if amount is less than 10 then we
371
00:15:11,440 --> 00:15:14,240
simply cancel log small number and
372
00:15:14,240 --> 00:15:16,560
otherwise we'll just say that the number
373
00:15:16,560 --> 00:15:19,920
e is large so we go with if
374
00:15:19,920 --> 00:15:22,240
and we're looking for amount
375
00:15:22,240 --> 00:15:25,519
is less than 10 and if that is the case
376
00:15:25,519 --> 00:15:27,760
we go with console log and we'll just
377
00:15:27,760 --> 00:15:28,720
say
378
00:15:28,720 --> 00:15:29,680
small
379
00:15:29,680 --> 00:15:30,720
number
380
00:15:30,720 --> 00:15:33,600
and otherwise if it is of course equal
381
00:15:33,600 --> 00:15:35,759
or bigger than 10 then we go with
382
00:15:35,759 --> 00:15:38,079
console log and we say
383
00:15:38,079 --> 00:15:39,519
large
384
00:15:39,519 --> 00:15:40,560
number
385
00:15:40,560 --> 00:15:43,199
and at the end of the day we just write
386
00:15:43,199 --> 00:15:46,959
console log hey it's my first note up as
387
00:15:46,959 --> 00:15:49,120
you can see an extremely complicated
388
00:15:49,120 --> 00:15:51,519
functionality and in this case i'm just
389
00:15:51,519 --> 00:15:53,680
going to set up the template strings so
390
00:15:53,680 --> 00:15:55,519
you understand that of course we can use
391
00:15:55,519 --> 00:15:59,920
them here as well i'll say hey it's my
392
00:15:59,920 --> 00:16:01,199
first
393
00:16:01,199 --> 00:16:03,199
node app and if you want of course you
394
00:16:03,199 --> 00:16:05,199
can jam the hello world there somewhere
395
00:16:05,199 --> 00:16:07,279
as well since that is the traditional
396
00:16:07,279 --> 00:16:09,680
approach with the first program
397
00:16:09,680 --> 00:16:12,959
and then in order to execute this code
398
00:16:12,959 --> 00:16:16,480
we effectively have two options i can
399
00:16:16,480 --> 00:16:19,120
navigate back to my terminal
400
00:16:19,120 --> 00:16:22,720
and i can zoom in like so and of course
401
00:16:22,720 --> 00:16:24,399
now i'm in a root so i would need to
402
00:16:24,399 --> 00:16:28,079
navigate to the desktop and probably the
403
00:16:28,079 --> 00:16:31,440
fastest way is just typing cd and drag
404
00:16:31,440 --> 00:16:32,639
and drop
405
00:16:32,639 --> 00:16:35,920
my folder here okay so now as you can
406
00:16:35,920 --> 00:16:40,320
see i'm in the tutorial and in order to
407
00:16:40,320 --> 00:16:44,320
execute my code i just need to type node
408
00:16:44,320 --> 00:16:48,079
and then app.js now keep in mind that
409
00:16:48,079 --> 00:16:51,120
technically i could have done that from
410
00:16:51,120 --> 00:16:52,240
the root
411
00:16:52,240 --> 00:16:54,320
we just need to provide the full path
412
00:16:54,320 --> 00:16:56,160
where i go to users blah blah blah and
413
00:16:56,160 --> 00:16:58,079
then tutorial and of course the file
414
00:16:58,079 --> 00:17:01,440
name is app.js and let me bump up the
415
00:17:01,440 --> 00:17:03,839
font size even more and the moment we
416
00:17:03,839 --> 00:17:07,760
run it check it out we have large number
417
00:17:07,760 --> 00:17:10,959
and hey it's my first note app now if
418
00:17:10,959 --> 00:17:12,480
you don't believe me
419
00:17:12,480 --> 00:17:14,799
we can go back and we for example can
420
00:17:14,799 --> 00:17:17,839
say that the number is nine
421
00:17:17,839 --> 00:17:19,839
and of course again we would need to run
422
00:17:19,839 --> 00:17:20,880
the code
423
00:17:20,880 --> 00:17:23,839
and we're gonna go with node and then
424
00:17:23,839 --> 00:17:27,839
app js beautiful we have small number
425
00:17:27,839 --> 00:17:30,880
and we still have our hey it's my first
426
00:17:30,880 --> 00:17:33,919
node up again the whole point of this
427
00:17:33,919 --> 00:17:36,960
line here is just to showcase that not
428
00:17:36,960 --> 00:17:39,520
only we can use quotation marks but also
429
00:17:39,520 --> 00:17:42,559
we can use the template string as well
430
00:17:42,559 --> 00:17:45,280
so that's one option but if you're
431
00:17:45,280 --> 00:17:48,559
familiar with vs code you know that it
432
00:17:48,559 --> 00:17:51,039
has this cool feature of integrated
433
00:17:51,039 --> 00:17:53,679
terminal where effectively we have our
434
00:17:53,679 --> 00:17:54,799
terminal
435
00:17:54,799 --> 00:17:55,760
but
436
00:17:55,760 --> 00:17:58,880
it is right away pointing to our folder
437
00:17:58,880 --> 00:18:00,960
so we don't need to do this whole dance
438
00:18:00,960 --> 00:18:03,200
where we navigate to the folder and then
439
00:18:03,200 --> 00:18:05,919
run the app we simply can open this up
440
00:18:05,919 --> 00:18:09,760
and the shortcut is control and tilde
441
00:18:09,760 --> 00:18:11,760
and also keep in mind that of course you
442
00:18:11,760 --> 00:18:14,320
can navigate through the tabs the
443
00:18:14,320 --> 00:18:17,039
problem is that i always forget where it
444
00:18:17,039 --> 00:18:20,559
is and effectively we need to go to view
445
00:18:20,559 --> 00:18:22,160
and then you're looking for this
446
00:18:22,160 --> 00:18:24,080
terminal command here
447
00:18:24,080 --> 00:18:27,440
as you can see this would be a shortcut
448
00:18:27,440 --> 00:18:29,840
and tilde and in here we have nice
449
00:18:29,840 --> 00:18:32,720
suggestion that we already located in
450
00:18:32,720 --> 00:18:35,440
tutorial folder and what do we need to
451
00:18:35,440 --> 00:18:37,919
do well again we just go with
452
00:18:37,919 --> 00:18:38,960
node
453
00:18:38,960 --> 00:18:42,480
app.js why well because that is my file
454
00:18:42,480 --> 00:18:44,400
name so for the remainder of the course
455
00:18:44,400 --> 00:18:47,200
i'll use the integrated terminal that
456
00:18:47,200 --> 00:18:49,039
comes with this code just understand
457
00:18:49,039 --> 00:18:51,440
that both of them effectively do the
458
00:18:51,440 --> 00:18:54,480
same thing and there it is now i run
459
00:18:54,480 --> 00:18:57,600
node app.js because that of course is
460
00:18:57,600 --> 00:18:59,760
the file name and i have small number
461
00:18:59,760 --> 00:19:03,760
and hey it's my first node app and
462
00:19:03,760 --> 00:19:06,720
that's how we can evaluate a more
463
00:19:06,720 --> 00:19:10,880
serious code using node as i note if at
464
00:19:10,880 --> 00:19:12,799
any point during the course you need to
465
00:19:12,799 --> 00:19:15,600
compare your code to mine just navigate
466
00:19:15,600 --> 00:19:17,000
to website
467
00:19:17,000 --> 00:19:20,200
johnsmilk.com and the website is
468
00:19:20,200 --> 00:19:23,440
johnsmega.com and then either in the
469
00:19:23,440 --> 00:19:26,240
project page or in the homepage you'll
470
00:19:26,240 --> 00:19:30,000
find node express tutorial project so in
471
00:19:30,000 --> 00:19:31,840
the homepage you'll find it in the
472
00:19:31,840 --> 00:19:34,240
latest project so just keep on scrolling
473
00:19:34,240 --> 00:19:36,080
but if it's not here it's definitely
474
00:19:36,080 --> 00:19:38,720
going to be in the projects page and you
475
00:19:38,720 --> 00:19:40,960
can even filter it in order to save some
476
00:19:40,960 --> 00:19:43,919
time so look for node in here like i
477
00:19:43,919 --> 00:19:46,640
said the project name is node express
478
00:19:46,640 --> 00:19:50,160
tutorial and not any of these links
479
00:19:50,160 --> 00:19:53,039
will get you to the repo and at this
480
00:19:53,039 --> 00:19:55,120
point you can either clone it 4k
481
00:19:55,120 --> 00:19:57,679
or just simply download and you're
482
00:19:57,679 --> 00:20:00,720
looking for this node tutorial and in
483
00:20:00,720 --> 00:20:02,400
here you'll find
484
00:20:02,400 --> 00:20:05,919
all the folders as well as the files now
485
00:20:05,919 --> 00:20:08,960
keep in mind that eventually when we
486
00:20:08,960 --> 00:20:11,440
start working on express
487
00:20:11,440 --> 00:20:14,480
we'll have to get this repo anyway
488
00:20:14,480 --> 00:20:17,039
because i'll use it for starters since i
489
00:20:17,039 --> 00:20:20,159
have prepared some assets for you some
490
00:20:20,159 --> 00:20:22,159
assets that we're going to use in our
491
00:20:22,159 --> 00:20:24,640
examples so this is really up to you if
492
00:20:24,640 --> 00:20:26,720
you want you can get it right now just
493
00:20:26,720 --> 00:20:28,880
keep in mind that you'll have to get it
494
00:20:28,880 --> 00:20:31,760
later anyway and once we have executed
495
00:20:31,760 --> 00:20:34,480
our first code in node
496
00:20:34,480 --> 00:20:37,039
let's quickly cover the concept of
497
00:20:37,039 --> 00:20:38,320
globals
498
00:20:38,320 --> 00:20:41,120
or global variables in node
499
00:20:41,120 --> 00:20:44,799
and the emphasis is on the word quickly
500
00:20:44,799 --> 00:20:47,120
yes there are more global variables
501
00:20:47,120 --> 00:20:49,280
available but the idea
502
00:20:49,280 --> 00:20:51,679
is not to spend half an hour in slide
503
00:20:51,679 --> 00:20:53,919
bill instead i would want to give you a
504
00:20:53,919 --> 00:20:55,280
general idea
505
00:20:55,280 --> 00:20:57,200
and as we progress with the course don't
506
00:20:57,200 --> 00:20:58,080
worry
507
00:20:58,080 --> 00:20:59,760
we will implement them
508
00:20:59,760 --> 00:21:01,679
and that way you can right away see them
509
00:21:01,679 --> 00:21:05,200
in action not just some random general
510
00:21:05,200 --> 00:21:06,320
description
511
00:21:06,320 --> 00:21:08,240
and before we continue let me just
512
00:21:08,240 --> 00:21:09,280
mention
513
00:21:09,280 --> 00:21:11,600
that throughout the tutorial
514
00:21:11,600 --> 00:21:14,320
the code from the previous video
515
00:21:14,320 --> 00:21:17,120
i'm going to save in a file so for
516
00:21:17,120 --> 00:21:19,520
example remember our first
517
00:21:19,520 --> 00:21:21,039
complex app
518
00:21:21,039 --> 00:21:23,039
with a mount and all that
519
00:21:23,039 --> 00:21:24,320
so what i did
520
00:21:24,320 --> 00:21:27,679
i created a file name by the one
521
00:21:27,679 --> 00:21:31,440
hyphen intro js and then i just copy and
522
00:21:31,440 --> 00:21:33,360
paste it this is what i'm gonna do
523
00:21:33,360 --> 00:21:36,159
throughout the no tutorial part of the
524
00:21:36,159 --> 00:21:39,039
course so that way you can always have
525
00:21:39,039 --> 00:21:42,000
access to the code and if you ever need
526
00:21:42,000 --> 00:21:43,919
to you can jog your memory now if you
527
00:21:43,919 --> 00:21:45,760
don't want to use my one if you're
528
00:21:45,760 --> 00:21:48,080
adding your own comments and all that
529
00:21:48,080 --> 00:21:50,240
i would also suggest doing that where
530
00:21:50,240 --> 00:21:52,640
essentially just create a file just dump
531
00:21:52,640 --> 00:21:55,039
it and then that way you are slowly
532
00:21:55,039 --> 00:21:57,120
progressing on the course and if you
533
00:21:57,120 --> 00:21:59,200
ever need to go back and refresh your
534
00:21:59,200 --> 00:22:02,640
memory you always have that option and
535
00:22:02,640 --> 00:22:04,320
keep in mind that
536
00:22:04,320 --> 00:22:06,720
if you would want to run this file you
537
00:22:06,720 --> 00:22:09,520
simply go with node and then whatever
538
00:22:09,520 --> 00:22:11,039
the file name right so in my case that
539
00:22:11,039 --> 00:22:12,720
would be one
540
00:22:12,720 --> 00:22:14,320
dash intro
541
00:22:14,320 --> 00:22:16,880
js again i'm going to keep on numbering
542
00:22:16,880 --> 00:22:19,919
them because i'm just annoying that way
543
00:22:19,919 --> 00:22:22,720
but you don't have to that is really up
544
00:22:22,720 --> 00:22:26,240
to you so back to the globals
545
00:22:26,240 --> 00:22:29,520
when we work in vanilla.js applications
546
00:22:29,520 --> 00:22:30,960
we know a
547
00:22:30,960 --> 00:22:33,120
very cool thing is the fact that we have
548
00:22:33,120 --> 00:22:35,840
access to the window object and the
549
00:22:35,840 --> 00:22:37,440
reason why it's so cool because in the
550
00:22:37,440 --> 00:22:40,400
window object we can get a bunch of
551
00:22:40,400 --> 00:22:43,440
useful things right so for example we
552
00:22:43,440 --> 00:22:46,000
can get a query selector so i can select
553
00:22:46,000 --> 00:22:46,960
a node
554
00:22:46,960 --> 00:22:49,520
i can get built-in fetch
555
00:22:49,520 --> 00:22:51,679
and hopefully you get the gist
556
00:22:51,679 --> 00:22:54,080
now i don't want to rain on your parade
557
00:22:54,080 --> 00:22:54,880
but
558
00:22:54,880 --> 00:22:57,200
you know there's no window
559
00:22:57,200 --> 00:22:59,280
why well because there's no browser so
560
00:22:59,280 --> 00:23:01,520
there's no window object so if you'll
561
00:23:01,520 --> 00:23:03,360
try to access window
562
00:23:03,360 --> 00:23:05,919
node will spit back the error and most
563
00:23:05,919 --> 00:23:08,799
likely your app will crash
564
00:23:08,799 --> 00:23:11,360
now there are some globals of course
565
00:23:11,360 --> 00:23:13,520
there is this concept of global
566
00:23:13,520 --> 00:23:16,159
variables and essentially what global
567
00:23:16,159 --> 00:23:17,840
variables means that
568
00:23:17,840 --> 00:23:19,600
anywhere in your application you can
569
00:23:19,600 --> 00:23:22,080
access them so no matter how complex my
570
00:23:22,080 --> 00:23:23,440
application gets
571
00:23:23,440 --> 00:23:26,240
how nested it gets or whatever
572
00:23:26,240 --> 00:23:29,120
i'll always have access to those
573
00:23:29,120 --> 00:23:32,080
variables and again let me repeat these
574
00:23:32,080 --> 00:23:34,080
are just some of the variables of course
575
00:23:34,080 --> 00:23:36,480
there's more and i'm not going to spend
576
00:23:36,480 --> 00:23:39,200
hours and hours talking about them i
577
00:23:39,200 --> 00:23:41,760
simply want to give you a general idea
578
00:23:41,760 --> 00:23:43,840
and then like i said we'll start using
579
00:23:43,840 --> 00:23:46,080
them and you'll see them in action and
580
00:23:46,080 --> 00:23:47,520
in my opinion
581
00:23:47,520 --> 00:23:50,880
that's just going to make it way clearer
582
00:23:50,880 --> 00:23:54,240
about what in fact they're doing and
583
00:23:54,240 --> 00:23:56,240
some of the useful ones we have their
584
00:23:56,240 --> 00:23:58,320
name so that would be underscore
585
00:23:58,320 --> 00:24:00,000
underscore their name
586
00:24:00,000 --> 00:24:01,679
just going to give us path to current
587
00:24:01,679 --> 00:24:04,080
directory then we have also underscore
588
00:24:04,080 --> 00:24:06,640
underscore filename which as you can see
589
00:24:06,640 --> 00:24:08,480
is going to give us a file name then we
590
00:24:08,480 --> 00:24:11,279
have require which is a function to use
591
00:24:11,279 --> 00:24:12,400
modules
592
00:24:12,400 --> 00:24:14,720
as well as the module that would be info
593
00:24:14,720 --> 00:24:17,520
about the current module now these two
594
00:24:17,520 --> 00:24:19,760
will literally cover them
595
00:24:19,760 --> 00:24:22,720
in the upcoming videos because our next
596
00:24:22,720 --> 00:24:25,120
big topic is going to be modules
597
00:24:25,120 --> 00:24:27,039
so for time being
598
00:24:27,039 --> 00:24:29,039
just remember that we have require and
599
00:24:29,039 --> 00:24:32,400
module and you'll see them in action in
600
00:24:32,400 --> 00:24:35,520
no time and then we have a very very
601
00:24:35,520 --> 00:24:38,799
very useful one and that one is a
602
00:24:38,799 --> 00:24:41,120
process and why it's so useful because
603
00:24:41,120 --> 00:24:45,440
it gives us a info about the environment
604
00:24:45,440 --> 00:24:46,960
where the program
605
00:24:46,960 --> 00:24:49,120
is executed because we need to keep in
606
00:24:49,120 --> 00:24:52,640
mind when it comes to a node app
607
00:24:52,640 --> 00:24:55,360
at the moment yeah we're executing that
608
00:24:55,360 --> 00:24:57,039
on our machines
609
00:24:57,039 --> 00:24:57,919
but
610
00:24:57,919 --> 00:25:00,799
when you deploy for example your api so
611
00:25:00,799 --> 00:25:02,960
for example your server right where is
612
00:25:02,960 --> 00:25:05,679
it sitting it is sitting on digital
613
00:25:05,679 --> 00:25:08,080
ocean or herald core or whatever so that
614
00:25:08,080 --> 00:25:09,679
of course is already a different
615
00:25:09,679 --> 00:25:11,039
environment
616
00:25:11,039 --> 00:25:13,760
and of course based on that environment
617
00:25:13,760 --> 00:25:14,960
you can
618
00:25:14,960 --> 00:25:17,200
make a bunch of decisions in your
619
00:25:17,200 --> 00:25:18,320
application
620
00:25:18,320 --> 00:25:19,279
again
621
00:25:19,279 --> 00:25:20,480
if you'll take a look at the process
622
00:25:20,480 --> 00:25:22,480
right now it's just going to be a
623
00:25:22,480 --> 00:25:24,320
giant object
624
00:25:24,320 --> 00:25:27,200
of gibberish data but trust me as we're
625
00:25:27,200 --> 00:25:29,760
progressing we the course
626
00:25:29,760 --> 00:25:32,320
will find a bunch of useful properties
627
00:25:32,320 --> 00:25:35,760
that we'll implement in our application
628
00:25:35,760 --> 00:25:38,000
now any of them you can simply console
629
00:25:38,000 --> 00:25:40,880
log and you'll get that useful data so
630
00:25:40,880 --> 00:25:43,679
you can just run console log and then
631
00:25:43,679 --> 00:25:45,600
for example their name again i'm not
632
00:25:45,600 --> 00:25:47,679
going to cancel log all of them probably
633
00:25:47,679 --> 00:25:48,799
the biggest one is going to be the
634
00:25:48,799 --> 00:25:50,880
process like i said you'll get a bunch
635
00:25:50,880 --> 00:25:53,200
of properties that at the moment
636
00:25:53,200 --> 00:25:56,000
will look like a gibberish and i simply
637
00:25:56,000 --> 00:25:58,240
need to save and of course then we go
638
00:25:58,240 --> 00:25:59,039
with
639
00:25:59,039 --> 00:26:02,880
node and app.js we run that and there it
640
00:26:02,880 --> 00:26:05,919
is now of course this gives me the path
641
00:26:05,919 --> 00:26:06,640
so
642
00:26:06,640 --> 00:26:08,320
if you're interested of course you can
643
00:26:08,320 --> 00:26:10,880
console log rest of them
644
00:26:10,880 --> 00:26:11,919
as well
645
00:26:11,919 --> 00:26:12,799
but
646
00:26:12,799 --> 00:26:15,039
in my case i'm going to stop
647
00:26:15,039 --> 00:26:19,039
with their name now also very quickly
648
00:26:19,039 --> 00:26:21,600
yes you have access to the console
649
00:26:21,600 --> 00:26:23,679
so as you can see in order to cancel log
650
00:26:23,679 --> 00:26:25,600
we have to use the console so that is
651
00:26:25,600 --> 00:26:27,039
also something that is available
652
00:26:27,039 --> 00:26:29,919
globally then just like
653
00:26:29,919 --> 00:26:33,679
in the event ljs we have set interval as
654
00:26:33,679 --> 00:26:36,799
well as set timeout so you can run these
655
00:26:36,799 --> 00:26:38,640
ones and i'm not going to run both of
656
00:26:38,640 --> 00:26:41,440
them but i'll show you set interval very
657
00:26:41,440 --> 00:26:43,840
quickly just so you don't think that i'm
658
00:26:43,840 --> 00:26:44,799
cheating
659
00:26:44,799 --> 00:26:45,919
and the way it's going to look like of
660
00:26:45,919 --> 00:26:47,919
course we have our callback function and
661
00:26:47,919 --> 00:26:50,159
then i'll say that my interval is going
662
00:26:50,159 --> 00:26:53,760
to run every second and i must type
663
00:26:53,760 --> 00:26:55,840
hello world otherwise
664
00:26:55,840 --> 00:26:58,880
well what kind of tutorial is this and
665
00:26:58,880 --> 00:27:02,640
again we simply go back to node and then
666
00:27:02,640 --> 00:27:06,480
app js and as you can see now
667
00:27:06,480 --> 00:27:09,520
after every second i have hello world in
668
00:27:09,520 --> 00:27:11,440
console and in order to stop it remember
669
00:27:11,440 --> 00:27:15,360
the command was ctrl and c
670
00:27:15,360 --> 00:27:17,520
and before we move on and start talking
671
00:27:17,520 --> 00:27:18,840
about the
672
00:27:18,840 --> 00:27:21,279
modules let me also mention something
673
00:27:21,279 --> 00:27:22,320
else
674
00:27:22,320 --> 00:27:25,760
where essentially we don't need to type
675
00:27:25,760 --> 00:27:29,360
node and then app.js you can simply
676
00:27:29,360 --> 00:27:30,159
write
677
00:27:30,159 --> 00:27:33,279
node and then app however throughout the
678
00:27:33,279 --> 00:27:36,559
course i'll probably keep on typing js
679
00:27:36,559 --> 00:27:38,880
just because i'm annoying that would be
680
00:27:38,880 --> 00:27:41,120
the first thing now the second thing and
681
00:27:41,120 --> 00:27:42,559
this is not a node thing this is
682
00:27:42,559 --> 00:27:44,320
actually a terminal
683
00:27:44,320 --> 00:27:46,240
if you don't want to type it you can
684
00:27:46,240 --> 00:27:48,559
simply go back to the previous command
685
00:27:48,559 --> 00:27:50,640
and in order to do that you need to
686
00:27:50,640 --> 00:27:53,919
press on arrow key up and check it out
687
00:27:53,919 --> 00:27:56,240
the moment you press notice that was my
688
00:27:56,240 --> 00:27:57,520
previous command
689
00:27:57,520 --> 00:27:59,760
and then i have one before that so in my
690
00:27:59,760 --> 00:28:03,919
case that is node and then app.js again
691
00:28:03,919 --> 00:28:05,520
you don't need to add that just
692
00:28:05,520 --> 00:28:08,880
extension but probably i will
693
00:28:08,880 --> 00:28:10,960
and then you just go back to the
694
00:28:10,960 --> 00:28:12,559
previous command and then press and
695
00:28:12,559 --> 00:28:16,320
again you just execute that command and
696
00:28:16,320 --> 00:28:17,600
of course if you want to clear the
697
00:28:17,600 --> 00:28:19,520
console again this is not a node thing
698
00:28:19,520 --> 00:28:22,080
this is a terminal i just type clear and
699
00:28:22,080 --> 00:28:23,440
lastly
700
00:28:23,440 --> 00:28:26,159
to answer the question that's probably
701
00:28:26,159 --> 00:28:28,000
bugging you the most we always have to
702
00:28:28,000 --> 00:28:30,720
just type node and then the file name
703
00:28:30,720 --> 00:28:32,320
because as we're going to be testing a
704
00:28:32,320 --> 00:28:34,799
special in tutorial it will probably get
705
00:28:34,799 --> 00:28:37,760
annoying and the answer is yes
706
00:28:37,760 --> 00:28:40,000
but only for now
707
00:28:40,000 --> 00:28:41,919
once we learn about npm
708
00:28:41,919 --> 00:28:43,520
the external packages which at the
709
00:28:43,520 --> 00:28:46,080
moment i fully understand doesn't make
710
00:28:46,080 --> 00:28:48,960
any sense at all then there's command
711
00:28:48,960 --> 00:28:52,159
that effectively will restart our
712
00:28:52,159 --> 00:28:54,640
application so we'll have to install
713
00:28:54,640 --> 00:28:55,600
some
714
00:28:55,600 --> 00:28:58,960
third-party module some external library
715
00:28:58,960 --> 00:29:01,039
and that will do the job of restarting
716
00:29:01,039 --> 00:29:03,760
the application for now yes all
717
00:29:03,760 --> 00:29:05,919
throughout tutorial up until the point
718
00:29:05,919 --> 00:29:08,399
where we cover that external library and
719
00:29:08,399 --> 00:29:11,039
external libraries in general yes we'll
720
00:29:11,039 --> 00:29:13,919
have to type node and then whatever is
721
00:29:13,919 --> 00:29:14,880
the file
722
00:29:14,880 --> 00:29:17,279
i know it's a little bit annoying but i
723
00:29:17,279 --> 00:29:19,679
just don't want to skip ahead i want to
724
00:29:19,679 --> 00:29:23,279
get us there with a full information of
725
00:29:23,279 --> 00:29:26,320
what is happening so with this in place
726
00:29:26,320 --> 00:29:29,520
with clear understanding of the
727
00:29:29,520 --> 00:29:33,360
global variables in node now let's move
728
00:29:33,360 --> 00:29:35,279
on to our next topic which is going to
729
00:29:35,279 --> 00:29:39,360
be modules in node beautiful
730
00:29:39,360 --> 00:29:41,679
and once we understand the globals in
731
00:29:41,679 --> 00:29:44,960
node now let's talk about the modules
732
00:29:44,960 --> 00:29:48,320
and first let's return to this command
733
00:29:48,320 --> 00:29:50,159
notice we have here
734
00:29:50,159 --> 00:29:54,600
node and then app.js
735
00:29:54,640 --> 00:29:56,399
probably the question
736
00:29:56,399 --> 00:29:57,760
that you have
737
00:29:57,760 --> 00:30:00,640
is well does that mean that all my
738
00:30:00,640 --> 00:30:01,760
application
739
00:30:01,760 --> 00:30:04,159
needs to be in this one file
740
00:30:04,159 --> 00:30:07,120
and the answer again is yes and no
741
00:30:07,120 --> 00:30:09,679
yes you'll execute one file
742
00:30:09,679 --> 00:30:13,120
but you'll split your code in modules
743
00:30:13,120 --> 00:30:15,840
otherwise that would be literally insane
744
00:30:15,840 --> 00:30:20,000
to jam all your code in one file
745
00:30:20,000 --> 00:30:22,000
and effectively i'll just show you with
746
00:30:22,000 --> 00:30:24,799
an extremely complex app and of course
747
00:30:24,799 --> 00:30:27,600
extremely complex is in the air quotes
748
00:30:27,600 --> 00:30:28,480
right now
749
00:30:28,480 --> 00:30:30,720
and you'll see why we'd want to use the
750
00:30:30,720 --> 00:30:32,880
modules in the first place so in the
751
00:30:32,880 --> 00:30:35,520
next video i'll give you official intro
752
00:30:35,520 --> 00:30:37,279
with a bunch of gibberish and all that
753
00:30:37,279 --> 00:30:39,760
but for time being let's just build
754
00:30:39,760 --> 00:30:41,279
super complex
755
00:30:41,279 --> 00:30:44,000
app and then see why i would want to use
756
00:30:44,000 --> 00:30:45,520
those modules
757
00:30:45,520 --> 00:30:48,480
in the first place and if you have any
758
00:30:48,480 --> 00:30:51,120
familiarity with es6 modules if you have
759
00:30:51,120 --> 00:30:54,240
worked with them in vanilla.js or in
760
00:30:54,240 --> 00:30:56,960
react.js you're already in good shape
761
00:30:56,960 --> 00:30:58,640
the syntax is going to be tiny bit
762
00:30:58,640 --> 00:31:00,720
different because you know it uses
763
00:31:00,720 --> 00:31:03,360
common js but the general idea is
764
00:31:03,360 --> 00:31:04,799
exactly the same
765
00:31:04,799 --> 00:31:07,600
and we start with a simple function by
766
00:31:07,600 --> 00:31:09,039
the name of
767
00:31:09,039 --> 00:31:11,519
say hi you guessed it and then this
768
00:31:11,519 --> 00:31:13,200
function is going to be looking for the
769
00:31:13,200 --> 00:31:15,679
name parameter and then inside of the
770
00:31:15,679 --> 00:31:17,440
function what do we do
771
00:31:17,440 --> 00:31:20,080
we simply cancel log and we say hello
772
00:31:20,080 --> 00:31:21,120
there
773
00:31:21,120 --> 00:31:23,919
and then whatever is the name of the
774
00:31:23,919 --> 00:31:26,960
dude or the gal that is being passed in
775
00:31:26,960 --> 00:31:28,799
as a parameter and of course we can
776
00:31:28,799 --> 00:31:31,279
invoke it and i can say say hi and then
777
00:31:31,279 --> 00:31:33,919
pass in susan for example
778
00:31:33,919 --> 00:31:35,840
and in order to make this interesting
779
00:31:35,840 --> 00:31:38,240
i'll set up some variables as well so
780
00:31:38,240 --> 00:31:40,480
i'm going to say const john is equal to
781
00:31:40,480 --> 00:31:41,760
john
782
00:31:41,760 --> 00:31:43,120
and you guessed it
783
00:31:43,120 --> 00:31:44,720
since there's a john there also has to
784
00:31:44,720 --> 00:31:47,600
be a peter and hopefully you get the
785
00:31:47,600 --> 00:31:49,919
gist so imagine we would have a bunch of
786
00:31:49,919 --> 00:31:52,960
names and then we have probably more
787
00:31:52,960 --> 00:31:54,880
functions than this one but for time
788
00:31:54,880 --> 00:31:56,960
being this is just going to be say hi we
789
00:31:56,960 --> 00:31:59,679
copy and paste we invoke this function i
790
00:31:59,679 --> 00:32:02,880
guess three times one for johnny boy
791
00:32:02,880 --> 00:32:05,200
one poor peter
792
00:32:05,200 --> 00:32:06,880
and then the first one we already have
793
00:32:06,880 --> 00:32:09,919
for susan and what do we do we run node
794
00:32:09,919 --> 00:32:13,440
app or app.js we run that and there it
795
00:32:13,440 --> 00:32:15,039
is now of course in our concert we can
796
00:32:15,039 --> 00:32:17,039
clearly see hello there susan blah blah
797
00:32:17,039 --> 00:32:20,480
blah now wouldn't it make sense if the
798
00:32:20,480 --> 00:32:23,360
names would be separate as well as this
799
00:32:23,360 --> 00:32:26,960
function or the future functions that
800
00:32:26,960 --> 00:32:28,799
we're going to create would be in
801
00:32:28,799 --> 00:32:30,000
separate files
802
00:32:30,000 --> 00:32:32,240
and then we can access them all
803
00:32:32,240 --> 00:32:34,240
throughout the application
804
00:32:34,240 --> 00:32:35,039
and
805
00:32:35,039 --> 00:32:38,000
that way of course we would have smaller
806
00:32:38,000 --> 00:32:40,640
files and more structure to our
807
00:32:40,640 --> 00:32:43,679
application and that is exactly what the
808
00:32:43,679 --> 00:32:45,760
modules allow us to do
809
00:32:45,760 --> 00:32:48,240
and that's why we'll move on and start
810
00:32:48,240 --> 00:32:49,919
talking about the modules in the next
811
00:32:49,919 --> 00:32:50,880
video
812
00:32:50,880 --> 00:32:51,919
beautiful
813
00:32:51,919 --> 00:32:54,240
and once we understand the main problem
814
00:32:54,240 --> 00:32:57,279
with this code where again the moment
815
00:32:57,279 --> 00:33:00,000
code is technically still tiny but as
816
00:33:00,000 --> 00:33:02,960
you can see it is getting bigger and
817
00:33:02,960 --> 00:33:05,840
more annoying with every line now let's
818
00:33:05,840 --> 00:33:08,880
talk about the modules and i guess let
819
00:33:08,880 --> 00:33:10,720
me just give you the
820
00:33:10,720 --> 00:33:13,120
main idea first and then we'll talk
821
00:33:13,120 --> 00:33:16,159
about the implementations and
822
00:33:16,159 --> 00:33:18,399
all that good stuff so first of all what
823
00:33:18,399 --> 00:33:20,159
are the modules again if you're familiar
824
00:33:20,159 --> 00:33:23,279
with es6 modules you already have the
825
00:33:23,279 --> 00:33:25,320
idea but essentially that is
826
00:33:25,320 --> 00:33:27,519
encapsulated code
827
00:33:27,519 --> 00:33:30,080
and the key here is that we're only
828
00:33:30,080 --> 00:33:32,559
sharing what we want
829
00:33:32,559 --> 00:33:35,440
so we're only sharing minimum how would
830
00:33:35,440 --> 00:33:37,919
that look like in real life well i can
831
00:33:37,919 --> 00:33:40,320
go here with secret
832
00:33:40,320 --> 00:33:43,039
and then i can type here super
833
00:33:43,039 --> 00:33:46,000
secret and then since this variable
834
00:33:46,000 --> 00:33:48,320
is in this file
835
00:33:48,320 --> 00:33:51,200
as we can see you can access anywhere
836
00:33:51,200 --> 00:33:53,519
okay so by mistake i can just access it
837
00:33:53,519 --> 00:33:55,600
i can override it now of course you can
838
00:33:55,600 --> 00:33:58,080
make an argument that i mean it is made
839
00:33:58,080 --> 00:34:00,640
with cons but hopefully you get the gist
840
00:34:00,640 --> 00:34:03,360
this is available to me everywhere in
841
00:34:03,360 --> 00:34:06,880
this file so once we set up the module
842
00:34:06,880 --> 00:34:10,480
you'll see what are the differences now
843
00:34:10,480 --> 00:34:13,918
node uses common js library under the
844
00:34:13,918 --> 00:34:17,040
hood that's what makes it happen
845
00:34:17,040 --> 00:34:20,960
and then every file in node is a module
846
00:34:20,960 --> 00:34:24,079
again let me repeat every file in node
847
00:34:24,079 --> 00:34:27,440
is a module so those super powers
848
00:34:27,440 --> 00:34:30,320
that we will cover in just a few seconds
849
00:34:30,320 --> 00:34:32,000
we have them by default we don't need to
850
00:34:32,000 --> 00:34:33,679
do anything we don't need to go to the
851
00:34:33,679 --> 00:34:36,560
store and buy some javascript code no we
852
00:34:36,560 --> 00:34:38,800
just get them by default
853
00:34:38,800 --> 00:34:41,760
and the way the modules work well we
854
00:34:41,760 --> 00:34:44,000
simply need to create a new file right
855
00:34:44,000 --> 00:34:45,839
and i'll right away
856
00:34:45,839 --> 00:34:48,320
set up the names here because of course
857
00:34:48,320 --> 00:34:51,599
eventually this code will be sitting in
858
00:34:51,599 --> 00:34:54,159
the file name by the three hyphen and
859
00:34:54,159 --> 00:34:57,599
then the module basics or whatever but
860
00:34:57,599 --> 00:35:00,480
the names as well as the function well
861
00:35:00,480 --> 00:35:02,480
it's going to be in a separate file so
862
00:35:02,480 --> 00:35:05,359
what i'm trying to say is that in a root
863
00:35:05,359 --> 00:35:07,520
of course there's no folders or nothing
864
00:35:07,520 --> 00:35:09,760
so everything is rude i'm going to go
865
00:35:09,760 --> 00:35:11,920
with four again i'm doing that because
866
00:35:11,920 --> 00:35:13,760
eventually this is going to be three
867
00:35:13,760 --> 00:35:16,400
hyphen modules something i'm gonna go
868
00:35:16,400 --> 00:35:18,720
with maybe names or you can type first
869
00:35:18,720 --> 00:35:21,359
module whatever you would want and i'm
870
00:35:21,359 --> 00:35:24,079
just gonna go with names.js
871
00:35:24,079 --> 00:35:27,599
okay awesome and then i'll take these
872
00:35:27,599 --> 00:35:28,480
three
873
00:35:28,480 --> 00:35:29,599
variables
874
00:35:29,599 --> 00:35:32,640
from my app.js and i'll copy and paste
875
00:35:32,640 --> 00:35:34,800
in the names and then also what i would
876
00:35:34,800 --> 00:35:38,000
want is to set up one more file and of
877
00:35:38,000 --> 00:35:40,320
course this one will be
878
00:35:40,320 --> 00:35:41,359
five
879
00:35:41,359 --> 00:35:42,240
and
880
00:35:42,240 --> 00:35:45,520
i don't know we can say second module
881
00:35:45,520 --> 00:35:49,599
or so i'll say here utils
882
00:35:49,599 --> 00:35:52,560
something generic so we set that up
883
00:35:52,560 --> 00:35:55,119
and now of course what i would want is
884
00:35:55,119 --> 00:35:57,520
to grab this function
885
00:35:57,520 --> 00:36:01,280
from the mgs and copy and paste in the
886
00:36:01,280 --> 00:36:04,160
utils and now sixty four thousand dollar
887
00:36:04,160 --> 00:36:06,000
question
888
00:36:06,000 --> 00:36:07,680
is this app gonna work
889
00:36:07,680 --> 00:36:09,760
or is it gonna break and of course the
890
00:36:09,760 --> 00:36:11,680
answer is it's gonna break because we
891
00:36:11,680 --> 00:36:14,720
have no access right now to say hi
892
00:36:14,720 --> 00:36:18,079
or to john or peter so
893
00:36:18,079 --> 00:36:19,359
why don't we
894
00:36:19,359 --> 00:36:21,680
go to the names and console log
895
00:36:21,680 --> 00:36:23,920
something and as i said yes you can
896
00:36:23,920 --> 00:36:26,640
console log this in the app.js it is a
897
00:36:26,640 --> 00:36:29,760
file so by default it is a module but
898
00:36:29,760 --> 00:36:32,400
since we'll do most of our work in the
899
00:36:32,400 --> 00:36:34,640
names and new tools first why don't we
900
00:36:34,640 --> 00:36:36,400
go to names and that way we can also see
901
00:36:36,400 --> 00:36:40,640
how we can run the file just by using
902
00:36:40,640 --> 00:36:42,880
node and what i would want to console
903
00:36:42,880 --> 00:36:46,560
log is the global by the name of module
904
00:36:46,560 --> 00:36:48,240
so in the names
905
00:36:48,240 --> 00:36:50,640
go below or above the variables doesn't
906
00:36:50,640 --> 00:36:51,839
really matter
907
00:36:51,839 --> 00:36:54,880
and go with console.log and then module
908
00:36:54,880 --> 00:36:57,280
and remember to run node and then
909
00:36:57,280 --> 00:36:59,280
whatever is the file name so in my case
910
00:36:59,280 --> 00:37:02,400
it is four dash and then names
911
00:37:02,400 --> 00:37:04,640
js you run this
912
00:37:04,640 --> 00:37:07,280
and you'll notice something pretty
913
00:37:07,280 --> 00:37:08,640
pretty cool
914
00:37:08,640 --> 00:37:10,560
where we have the object
915
00:37:10,560 --> 00:37:12,880
and in that object we have quite a few
916
00:37:12,880 --> 00:37:14,560
properties now the one that i'm
917
00:37:14,560 --> 00:37:16,000
interested in the most
918
00:37:16,000 --> 00:37:18,240
is this exports one
919
00:37:18,240 --> 00:37:21,599
and it tells me that this export is an
920
00:37:21,599 --> 00:37:22,880
object
921
00:37:22,880 --> 00:37:26,640
and the way the common js syntax works
922
00:37:26,640 --> 00:37:28,480
whatever i'm going to dump
923
00:37:28,480 --> 00:37:31,680
in that export i'm going to be able to
924
00:37:31,680 --> 00:37:34,800
access anywhere in the application
925
00:37:34,800 --> 00:37:36,480
so that's the key
926
00:37:36,480 --> 00:37:39,839
remember we're only sharing the minimum
927
00:37:39,839 --> 00:37:42,079
so it's up to us
928
00:37:42,079 --> 00:37:43,119
to
929
00:37:43,119 --> 00:37:47,040
set those values in that export object
930
00:37:47,040 --> 00:37:49,520
we decide what goes in there previously
931
00:37:49,520 --> 00:37:50,480
remember
932
00:37:50,480 --> 00:37:52,400
secret was accessible
933
00:37:52,400 --> 00:37:54,640
everywhere in my app.js
934
00:37:54,640 --> 00:37:57,119
now in the names i can be hey you know
935
00:37:57,119 --> 00:37:59,520
what john and peter yeah
936
00:37:59,520 --> 00:38:01,680
i'm sharing them anywhere in application
937
00:38:01,680 --> 00:38:03,200
we can access that but the secret
938
00:38:03,200 --> 00:38:05,359
variable no this is just going to be
939
00:38:05,359 --> 00:38:09,200
local so i'm going to add here i guess
940
00:38:09,200 --> 00:38:10,320
the
941
00:38:10,320 --> 00:38:12,800
comment by the name of local and that
942
00:38:12,800 --> 00:38:14,560
way at least we understand so this is
943
00:38:14,560 --> 00:38:16,480
going to be i mean i'm not going to use
944
00:38:16,480 --> 00:38:17,520
global
945
00:38:17,520 --> 00:38:19,760
but i'm just going to say that
946
00:38:19,760 --> 00:38:23,359
share so we're sharing this with rest of
947
00:38:23,359 --> 00:38:25,680
our application and then if i know that
948
00:38:25,680 --> 00:38:27,839
this is a object
949
00:38:27,839 --> 00:38:30,560
and it is sitting in the module how does
950
00:38:30,560 --> 00:38:34,560
that work well we go with module
951
00:38:34,560 --> 00:38:37,200
then it is an object so we go with
952
00:38:37,200 --> 00:38:38,400
export
953
00:38:38,400 --> 00:38:41,119
and we have multiple ways how we can set
954
00:38:41,119 --> 00:38:43,760
this up but the most basic one will
955
00:38:43,760 --> 00:38:46,720
simply say that yep it is an object and
956
00:38:46,720 --> 00:38:48,480
inside of that object there's going to
957
00:38:48,480 --> 00:38:50,640
be two properties john
958
00:38:50,640 --> 00:38:53,119
and peter now i can probably already
959
00:38:53,119 --> 00:38:56,400
guess that we're using a es6 syntax
960
00:38:56,400 --> 00:38:58,800
since the key name is exactly the same
961
00:38:58,800 --> 00:39:01,200
as the value well we can simply pass it
962
00:39:01,200 --> 00:39:03,760
right so we have john and peter so now
963
00:39:03,760 --> 00:39:07,119
what happens in the app.js we can access
964
00:39:07,119 --> 00:39:09,920
them so i can go back and this is the
965
00:39:09,920 --> 00:39:11,599
case where we'll need to run that
966
00:39:11,599 --> 00:39:14,000
require remember in the globals we
967
00:39:14,000 --> 00:39:15,599
talked about this function
968
00:39:15,599 --> 00:39:18,640
so in order to share them we need to
969
00:39:18,640 --> 00:39:19,640
use
970
00:39:19,640 --> 00:39:22,240
module.exports and essentially i'm
971
00:39:22,240 --> 00:39:25,599
setting up the object like so where i go
972
00:39:25,599 --> 00:39:27,920
with whatever values i would like to
973
00:39:27,920 --> 00:39:30,640
share now in order to access them
974
00:39:30,640 --> 00:39:33,200
i need to go with require
975
00:39:33,200 --> 00:39:34,079
and
976
00:39:34,079 --> 00:39:36,400
i can assign it to a variable or i can
977
00:39:36,400 --> 00:39:38,720
simply require now in our case we will
978
00:39:38,720 --> 00:39:41,280
assign it to the variable and i'll use
979
00:39:41,280 --> 00:39:43,920
the same type of syntax where i'm going
980
00:39:43,920 --> 00:39:45,599
to go with names just so we can
981
00:39:45,599 --> 00:39:46,960
console.log that
982
00:39:46,960 --> 00:39:50,320
and this is going to be equal to require
983
00:39:50,320 --> 00:39:53,680
so that's my function and as a parameter
984
00:39:53,680 --> 00:39:57,280
we need to pass in where the module is
985
00:39:57,280 --> 00:39:58,560
located
986
00:39:58,560 --> 00:40:01,599
so where is this data coming from
987
00:40:01,599 --> 00:40:04,480
and always always always always
988
00:40:04,480 --> 00:40:06,880
you will be setting up your module you
989
00:40:06,880 --> 00:40:09,119
have to start with dot now sometimes
990
00:40:09,119 --> 00:40:11,920
your modules will be two levels up i
991
00:40:11,920 --> 00:40:13,920
don't know three levels up whatever then
992
00:40:13,920 --> 00:40:16,400
of course you'll start with dot dot but
993
00:40:16,400 --> 00:40:18,960
you'll always start with this one dot
994
00:40:18,960 --> 00:40:21,200
because there's also going to be a third
995
00:40:21,200 --> 00:40:22,640
party modules and there's going to be
996
00:40:22,640 --> 00:40:25,359
built-in modules in node and then we'll
997
00:40:25,359 --> 00:40:28,240
use just these quotation marks again
998
00:40:28,240 --> 00:40:30,319
super super important every time you
999
00:40:30,319 --> 00:40:32,079
create your own module you'll have to
1000
00:40:32,079 --> 00:40:34,240
start with dot and then forward slash so
1001
00:40:34,240 --> 00:40:36,560
essentially you're giving a path now in
1002
00:40:36,560 --> 00:40:38,560
our case what is the path well i'm
1003
00:40:38,560 --> 00:40:41,200
looking for the names right so i set up
1004
00:40:41,200 --> 00:40:44,720
here the path and then if i cancel out
1005
00:40:44,720 --> 00:40:45,760
the names
1006
00:40:45,760 --> 00:40:47,920
i should see something pretty pretty
1007
00:40:47,920 --> 00:40:50,560
cool where i go with names
1008
00:40:50,560 --> 00:40:52,960
and there it is now i'm going to run
1009
00:40:52,960 --> 00:40:56,400
node and then app js now keep in mind
1010
00:40:56,400 --> 00:40:58,319
we're not running names anymore we go
1011
00:40:58,319 --> 00:41:02,400
with node and app.js now of course still
1012
00:41:02,400 --> 00:41:05,599
complains that we have no access to hi
1013
00:41:05,599 --> 00:41:07,760
let me comment this one out so let's say
1014
00:41:07,760 --> 00:41:10,240
node and app.js
1015
00:41:10,240 --> 00:41:12,800
and check it out now of course we have
1016
00:41:12,800 --> 00:41:14,480
the object
1017
00:41:14,480 --> 00:41:17,599
with john john and peter and peter
1018
00:41:17,599 --> 00:41:20,240
and we explicitly said what we would
1019
00:41:20,240 --> 00:41:23,760
want to export so we go with exports and
1020
00:41:23,760 --> 00:41:24,800
that way
1021
00:41:24,800 --> 00:41:26,960
no one has access to the secret one so
1022
00:41:26,960 --> 00:41:30,000
this secret one is only for this file
1023
00:41:30,000 --> 00:41:32,880
and of course we can do the same thing
1024
00:41:32,880 --> 00:41:35,520
in the utils as well right so we have
1025
00:41:35,520 --> 00:41:37,040
the say hi
1026
00:41:37,040 --> 00:41:40,079
and we go with module and export
1027
00:41:40,079 --> 00:41:42,960
and i can just simply set it equal to
1028
00:41:42,960 --> 00:41:44,640
now the reason why i was setting this up
1029
00:41:44,640 --> 00:41:46,640
as a object because i had multiple
1030
00:41:46,640 --> 00:41:49,040
things at john and peter and don't worry
1031
00:41:49,040 --> 00:41:50,880
in the next video i'll show you
1032
00:41:50,880 --> 00:41:53,040
alternative syntax how you can also set
1033
00:41:53,040 --> 00:41:56,240
this up but in this case i have object
1034
00:41:56,240 --> 00:41:57,920
and then of course i'm setting these
1035
00:41:57,920 --> 00:42:00,720
properties and i said note this is just
1036
00:42:00,720 --> 00:42:02,560
my preferred way of
1037
00:42:02,560 --> 00:42:05,040
actually using the exports again there's
1038
00:42:05,040 --> 00:42:07,280
multiple ways how we can do that and i
1039
00:42:07,280 --> 00:42:10,960
will cover the other ways in next video
1040
00:42:10,960 --> 00:42:12,560
but this is just my preferred method
1041
00:42:12,560 --> 00:42:15,280
that's why i right away set my exports
1042
00:42:15,280 --> 00:42:17,680
equal to an object and then pass these
1043
00:42:17,680 --> 00:42:20,800
two properties and in utils since i only
1044
00:42:20,800 --> 00:42:23,280
have one thing i'm just gonna go with
1045
00:42:23,280 --> 00:42:25,520
say hi that's it i'm saying
1046
00:42:25,520 --> 00:42:28,480
that i'm gonna be exporting my function
1047
00:42:28,480 --> 00:42:32,400
so say say hi like so and once i have
1048
00:42:32,400 --> 00:42:34,319
exported my function
1049
00:42:34,319 --> 00:42:36,560
now of course i need to go back to the
1050
00:42:36,560 --> 00:42:37,760
app.js
1051
00:42:37,760 --> 00:42:41,200
import my function the same name
1052
00:42:41,200 --> 00:42:43,760
and i'm going to go with say
1053
00:42:43,760 --> 00:42:45,359
say hi
1054
00:42:45,359 --> 00:42:47,760
and that one is going to be coming from
1055
00:42:47,760 --> 00:42:49,440
the different file of course so we go
1056
00:42:49,440 --> 00:42:50,800
with five
1057
00:42:50,800 --> 00:42:52,720
and then utils
1058
00:42:52,720 --> 00:42:54,400
and notice here how we don't need to
1059
00:42:54,400 --> 00:42:58,000
pass in the js if it is ajs file we just
1060
00:42:58,000 --> 00:43:00,560
simply omit the extension as long as the
1061
00:43:00,560 --> 00:43:02,319
file name of course matches
1062
00:43:02,319 --> 00:43:03,440
and then
1063
00:43:03,440 --> 00:43:05,520
i can remove now if you want you can
1064
00:43:05,520 --> 00:43:08,000
cancel log but i already know that i'm
1065
00:43:08,000 --> 00:43:10,319
gonna have the function over there i'll
1066
00:43:10,319 --> 00:43:12,400
uncomment my say hi
1067
00:43:12,400 --> 00:43:14,000
so now of course there's going to be no
1068
00:43:14,000 --> 00:43:16,240
error and let's make a clear console and
1069
00:43:16,240 --> 00:43:17,760
again we run
1070
00:43:17,760 --> 00:43:21,920
the node and then app js and there it is
1071
00:43:21,920 --> 00:43:24,079
now of course we have a different error
1072
00:43:24,079 --> 00:43:26,960
where it says well john is not defined
1073
00:43:26,960 --> 00:43:29,440
all right but i know that
1074
00:43:29,440 --> 00:43:31,920
john is sitting in the names right so
1075
00:43:31,920 --> 00:43:35,520
here we have multiple flavors either
1076
00:43:35,520 --> 00:43:37,760
you can destructure it
1077
00:43:37,760 --> 00:43:41,440
so instead of going with names i can go
1078
00:43:41,440 --> 00:43:44,079
john and peter that is definitely one
1079
00:43:44,079 --> 00:43:45,920
option or
1080
00:43:45,920 --> 00:43:47,839
i can go with property names right so i
1081
00:43:47,839 --> 00:43:48,880
can say
1082
00:43:48,880 --> 00:43:51,520
names and then john
1083
00:43:51,520 --> 00:43:55,440
and then the second one is names and
1084
00:43:55,440 --> 00:43:58,960
peter names and peter and now hopefully
1085
00:43:58,960 --> 00:44:01,119
everything works otherwise you're
1086
00:44:01,119 --> 00:44:03,520
probably thinking that i'm just messing
1087
00:44:03,520 --> 00:44:05,599
with you there it is now we have hello
1088
00:44:05,599 --> 00:44:08,960
there susan john and peter
1089
00:44:08,960 --> 00:44:11,760
and effectively what we did we separated
1090
00:44:11,760 --> 00:44:14,720
our concerns again the main idea is that
1091
00:44:14,720 --> 00:44:17,520
every file by default is a module and
1092
00:44:17,520 --> 00:44:20,560
then when it comes to a modules we are
1093
00:44:20,560 --> 00:44:23,839
in charge what we are sharing and in
1094
00:44:23,839 --> 00:44:25,200
order to
1095
00:44:25,200 --> 00:44:28,000
export or make it something available
1096
00:44:28,000 --> 00:44:30,319
from this module we go with module
1097
00:44:30,319 --> 00:44:31,680
exports
1098
00:44:31,680 --> 00:44:34,240
and then we just set up the
1099
00:44:34,240 --> 00:44:36,240
functionality whether this is going to
1100
00:44:36,240 --> 00:44:39,359
be a object that we're exporting or
1101
00:44:39,359 --> 00:44:42,720
we're simply exporting as a function
1102
00:44:42,720 --> 00:44:44,800
so we set this equal to
1103
00:44:44,800 --> 00:44:47,440
to the exports and if you're familiar
1104
00:44:47,440 --> 00:44:49,520
with es6 modules
1105
00:44:49,520 --> 00:44:52,480
as you can see this is very familiar to
1106
00:44:52,480 --> 00:44:55,119
the export
1107
00:44:55,119 --> 00:44:57,520
default and then whatever you're
1108
00:44:57,520 --> 00:44:59,359
exporting as default whether that is a
1109
00:44:59,359 --> 00:45:00,960
function whether that is a react
1110
00:45:00,960 --> 00:45:03,520
component or whatever notice i'm
1111
00:45:03,520 --> 00:45:05,839
exporting as default so of course
1112
00:45:05,839 --> 00:45:08,000
when i'm importing this
1113
00:45:08,000 --> 00:45:11,280
in this case app.js well i already know
1114
00:45:11,280 --> 00:45:13,280
that this is a function so i can name
1115
00:45:13,280 --> 00:45:15,440
this whatever i would want and the same
1116
00:45:15,440 --> 00:45:16,480
goes here
1117
00:45:16,480 --> 00:45:19,599
since i'm exporting this as object i can
1118
00:45:19,599 --> 00:45:22,000
call this shaken bait as long as i
1119
00:45:22,000 --> 00:45:24,400
access it later of course i'll be able
1120
00:45:24,400 --> 00:45:26,960
to access the property as long as the
1121
00:45:26,960 --> 00:45:27,920
name
1122
00:45:27,920 --> 00:45:32,079
matches to whatever i set up over here
1123
00:45:32,079 --> 00:45:35,119
so let me save this and now of course we
1124
00:45:35,119 --> 00:45:38,160
can talk about our next topic
1125
00:45:38,160 --> 00:45:41,119
all right so far we know two flavors
1126
00:45:41,119 --> 00:45:42,079
one
1127
00:45:42,079 --> 00:45:44,640
if we want to export one value
1128
00:45:44,640 --> 00:45:47,359
we just set module export
1129
00:45:47,359 --> 00:45:50,160
equal to that value and then of course
1130
00:45:50,160 --> 00:45:52,640
wherever we want to use it we just
1131
00:45:52,640 --> 00:45:53,680
import it
1132
00:45:53,680 --> 00:45:55,839
set it equal to some kind of variable
1133
00:45:55,839 --> 00:45:57,280
and of course
1134
00:45:57,280 --> 00:46:00,560
once we use that variable we have access
1135
00:46:00,560 --> 00:46:02,240
to that value
1136
00:46:02,240 --> 00:46:04,319
and the second flavor is if we have
1137
00:46:04,319 --> 00:46:05,760
multiple values
1138
00:46:05,760 --> 00:46:09,280
then we simply set module exports equal
1139
00:46:09,280 --> 00:46:12,400
to an object and then in that object we
1140
00:46:12,400 --> 00:46:15,119
set up the properties however there's
1141
00:46:15,119 --> 00:46:17,680
also another way how we can do that
1142
00:46:17,680 --> 00:46:20,319
and essentially it is called
1143
00:46:20,319 --> 00:46:22,400
export as you go
1144
00:46:22,400 --> 00:46:24,319
and the way it looks like
1145
00:46:24,319 --> 00:46:28,160
first i'm gonna set up a new file and
1146
00:46:28,160 --> 00:46:30,480
i'll call this six and i'll call this
1147
00:46:30,480 --> 00:46:32,720
alternative
1148
00:46:32,720 --> 00:46:35,920
flavor now again naming is really up to
1149
00:46:35,920 --> 00:46:38,800
you since that is the hardest thing in
1150
00:46:38,800 --> 00:46:39,920
programming
1151
00:46:39,920 --> 00:46:43,040
and then i'm going to come up with one
1152
00:46:43,040 --> 00:46:45,200
array and i'm going to call this items
1153
00:46:45,200 --> 00:46:47,599
and that will be equal to item one
1154
00:46:47,599 --> 00:46:50,480
and item number two
1155
00:46:50,480 --> 00:46:52,880
and then i'll set up a object and i'll
1156
00:46:52,880 --> 00:46:56,400
say const person is equal
1157
00:46:56,400 --> 00:46:59,200
to a object and there surprise surprise
1158
00:46:59,200 --> 00:47:01,359
we'll go with name property and set it
1159
00:47:01,359 --> 00:47:03,119
equal to bob
1160
00:47:03,119 --> 00:47:05,040
and i'm purposely adding multiple data
1161
00:47:05,040 --> 00:47:05,920
types
1162
00:47:05,920 --> 00:47:08,800
just so you don't think that we can only
1163
00:47:08,800 --> 00:47:12,000
export variables or functions
1164
00:47:12,000 --> 00:47:15,040
and in here we have two options
1165
00:47:15,040 --> 00:47:16,079
either
1166
00:47:16,079 --> 00:47:18,400
where we are setting up the
1167
00:47:18,400 --> 00:47:21,119
value in this case items i simply go
1168
00:47:21,119 --> 00:47:21,920
with
1169
00:47:21,920 --> 00:47:25,040
module exports and then whatever is the
1170
00:47:25,040 --> 00:47:26,880
name and of course in this case i'm
1171
00:47:26,880 --> 00:47:28,640
going to keep the items but notice the
1172
00:47:28,640 --> 00:47:31,440
syntax where instead of going with const
1173
00:47:31,440 --> 00:47:34,079
items i right away go with
1174
00:47:34,079 --> 00:47:35,760
module.exports
1175
00:47:35,760 --> 00:47:37,920
or if you are a true rebel you can do
1176
00:47:37,920 --> 00:47:41,200
something like this module exports set
1177
00:47:41,200 --> 00:47:43,520
up the property for example
1178
00:47:43,520 --> 00:47:44,480
single
1179
00:47:44,480 --> 00:47:48,400
person and set it equal to your person
1180
00:47:48,400 --> 00:47:50,480
object why we are able to do that
1181
00:47:50,480 --> 00:47:52,400
because if you remember in the previous
1182
00:47:52,400 --> 00:47:54,559
video when we console logged
1183
00:47:54,559 --> 00:47:57,920
module exports it was an object right so
1184
00:47:57,920 --> 00:47:59,839
what are we doing here we're setting up
1185
00:47:59,839 --> 00:48:02,880
a property on that object and set it
1186
00:48:02,880 --> 00:48:04,960
equal to an array
1187
00:48:04,960 --> 00:48:07,599
and here i set up another property and
1188
00:48:07,599 --> 00:48:09,520
just set it equal to a person keep in
1189
00:48:09,520 --> 00:48:12,400
mind of course i can still write simply
1190
00:48:12,400 --> 00:48:15,520
module exports and then the person
1191
00:48:15,520 --> 00:48:17,599
but since you might
1192
00:48:17,599 --> 00:48:19,520
sometimes see this type of syntax as
1193
00:48:19,520 --> 00:48:22,559
well i wanted to cover that and now of
1194
00:48:22,559 --> 00:48:26,000
course we save alternative flavor.js
1195
00:48:26,000 --> 00:48:27,680
and you're probably not going to be
1196
00:48:27,680 --> 00:48:30,880
surprised that if we navigate to app.js
1197
00:48:30,880 --> 00:48:33,200
and in this case i will call this data
1198
00:48:33,200 --> 00:48:35,440
because i'm getting a object and on that
1199
00:48:35,440 --> 00:48:37,280
object i have two properties and i need
1200
00:48:37,280 --> 00:48:39,440
to come up with some kind of name now as
1201
00:48:39,440 --> 00:48:43,359
always this name is really up to you
1202
00:48:43,359 --> 00:48:46,319
you can call it shake and bake you can
1203
00:48:46,319 --> 00:48:49,040
call it uncle bobby whatever you want in
1204
00:48:49,040 --> 00:48:52,079
my case i'm gonna go with generic data
1205
00:48:52,079 --> 00:48:54,400
we go with require again we're looking
1206
00:48:54,400 --> 00:48:57,760
for the file by the name of alternative
1207
00:48:57,760 --> 00:48:58,800
flavor
1208
00:48:58,800 --> 00:48:59,680
and
1209
00:48:59,680 --> 00:49:01,359
in this case i'm not gonna do anything
1210
00:49:01,359 --> 00:49:04,319
with it apart from console logging and
1211
00:49:04,319 --> 00:49:06,800
if i navigate back to my integrated
1212
00:49:06,800 --> 00:49:08,880
terminal and run
1213
00:49:08,880 --> 00:49:11,200
the node app.js
1214
00:49:11,200 --> 00:49:13,359
there it is of course now i have the
1215
00:49:13,359 --> 00:49:16,400
items as well as the single person
1216
00:49:16,400 --> 00:49:18,880
now please keep in mind one thing
1217
00:49:18,880 --> 00:49:21,119
where this syntax
1218
00:49:21,119 --> 00:49:24,240
whether the first line or line number
1219
00:49:24,240 --> 00:49:25,200
six
1220
00:49:25,200 --> 00:49:27,680
at the end of the day is exactly the
1221
00:49:27,680 --> 00:49:30,559
same like we're doing over here yes the
1222
00:49:30,559 --> 00:49:32,800
syntax is a bit different but at the end
1223
00:49:32,800 --> 00:49:35,920
of the day you just make these values
1224
00:49:35,920 --> 00:49:39,440
available somewhere else in your code
1225
00:49:39,440 --> 00:49:41,680
now just because i prefer this type of
1226
00:49:41,680 --> 00:49:44,240
syntax most likely throughout the course
1227
00:49:44,240 --> 00:49:47,280
you'll see this approach but
1228
00:49:47,280 --> 00:49:49,920
if you prefer this one instead
1229
00:49:49,920 --> 00:49:52,000
be my guest at the end of the day it is
1230
00:49:52,000 --> 00:49:54,880
your application and you are the one who
1231
00:49:54,880 --> 00:49:56,800
is going to be maintaining it and before
1232
00:49:56,800 --> 00:50:00,800
we move on to built in modules as well
1233
00:50:00,800 --> 00:50:03,520
as third-party modules let me throw you
1234
00:50:03,520 --> 00:50:05,200
a mind grenade
1235
00:50:05,200 --> 00:50:07,280
and i'm going to start by removing this
1236
00:50:07,280 --> 00:50:08,640
console log
1237
00:50:08,640 --> 00:50:12,480
in the app js like so and i'm going to
1238
00:50:12,480 --> 00:50:15,440
create a new file with the same exact
1239
00:50:15,440 --> 00:50:18,480
name a mine grenade as always
1240
00:50:18,480 --> 00:50:19,680
if you would like to call this
1241
00:50:19,680 --> 00:50:20,880
differently
1242
00:50:20,880 --> 00:50:23,839
that's totally your option
1243
00:50:23,839 --> 00:50:26,880
and i'm going to go with mine grenade
1244
00:50:26,880 --> 00:50:30,160
like so and js and in this file i'll
1245
00:50:30,160 --> 00:50:32,400
create some basic functionality and by
1246
00:50:32,400 --> 00:50:34,960
the way let me save app.js so let's go
1247
00:50:34,960 --> 00:50:36,319
with const
1248
00:50:36,319 --> 00:50:37,920
num1
1249
00:50:37,920 --> 00:50:42,000
is equal to 5 and then const num 2
1250
00:50:42,000 --> 00:50:44,720
is equal to 10. of course there has to
1251
00:50:44,720 --> 00:50:47,359
be a function that adds these values as
1252
00:50:47,359 --> 00:50:50,240
always with every tutorial so go with
1253
00:50:50,240 --> 00:50:52,400
add values just for the sake of it i'm
1254
00:50:52,400 --> 00:50:54,720
just going to go with good old function
1255
00:50:54,720 --> 00:50:57,920
keyword and the old setup meaning
1256
00:50:57,920 --> 00:51:01,280
not the arrow function syntax and simply
1257
00:51:01,280 --> 00:51:04,319
here we'll go with template string the
1258
00:51:04,319 --> 00:51:06,640
sum is colon
1259
00:51:06,640 --> 00:51:09,119
and then of course let's go for template
1260
00:51:09,119 --> 00:51:12,400
literal and we'll say num1 plus
1261
00:51:12,400 --> 00:51:13,839
num2
1262
00:51:13,839 --> 00:51:14,880
and
1263
00:51:14,880 --> 00:51:17,680
instead of setting up the exports i'll
1264
00:51:17,680 --> 00:51:19,359
simply invoke
1265
00:51:19,359 --> 00:51:21,119
add values
1266
00:51:21,119 --> 00:51:24,240
and now i'll throw you that migrate that
1267
00:51:24,240 --> 00:51:28,559
i was talking about where in the app.js
1268
00:51:28,559 --> 00:51:30,960
i'm not going to assign it to anything
1269
00:51:30,960 --> 00:51:33,200
i'll simply write require
1270
00:51:33,200 --> 00:51:36,559
and then we'll go with my grenade and
1271
00:51:36,559 --> 00:51:39,599
now let me comment out these three
1272
00:51:39,599 --> 00:51:41,599
since at the moment they will just over
1273
00:51:41,599 --> 00:51:44,720
populate the console and again let's run
1274
00:51:44,720 --> 00:51:47,760
node and app.js and once we're on our
1275
00:51:47,760 --> 00:51:49,760
code we can clearly see in the console
1276
00:51:49,760 --> 00:51:51,760
something pretty interesting where we
1277
00:51:51,760 --> 00:51:55,760
have a log of the sum is 15.
1278
00:51:55,760 --> 00:51:57,920
so what's happening over here
1279
00:51:57,920 --> 00:51:58,720
well
1280
00:51:58,720 --> 00:52:01,359
when we are setting up everything if we
1281
00:52:01,359 --> 00:52:04,240
have a function inside of the module
1282
00:52:04,240 --> 00:52:07,520
that we invoke yes that code will run
1283
00:52:07,520 --> 00:52:09,599
even though we didn't assign it to a
1284
00:52:09,599 --> 00:52:12,559
variable as you can see clearly here and
1285
00:52:12,559 --> 00:52:15,200
we didn't invoke that variable
1286
00:52:15,200 --> 00:52:17,520
and before we go any further let me
1287
00:52:17,520 --> 00:52:18,960
mention three things
1288
00:52:18,960 --> 00:52:20,000
first
1289
00:52:20,000 --> 00:52:22,079
it's not unique you can also do that in
1290
00:52:22,079 --> 00:52:23,599
es6 modules
1291
00:52:23,599 --> 00:52:25,280
and if you took my vanilla javascript
1292
00:52:25,280 --> 00:52:26,960
course you know that we use that
1293
00:52:26,960 --> 00:52:29,680
functionality number two yes throughout
1294
00:52:29,680 --> 00:52:31,520
the course here and there we will use
1295
00:52:31,520 --> 00:52:33,680
this particular setup now it's going to
1296
00:52:33,680 --> 00:52:35,920
be with third party modules and of
1297
00:52:35,920 --> 00:52:38,880
course once we get there i will remind
1298
00:52:38,880 --> 00:52:41,359
you about this setup so that's how it
1299
00:52:41,359 --> 00:52:43,280
works and third
1300
00:52:43,280 --> 00:52:46,000
i wouldn't suggest losing your sleep
1301
00:52:46,000 --> 00:52:47,280
over it
1302
00:52:47,280 --> 00:52:49,200
just please remember that when you
1303
00:52:49,200 --> 00:52:52,720
import a module you actually invoke it
1304
00:52:52,720 --> 00:52:55,280
and this is really going into weeds but
1305
00:52:55,280 --> 00:52:58,319
the reason why that happens is because
1306
00:52:58,319 --> 00:53:01,680
this code is not just exported on its
1307
00:53:01,680 --> 00:53:02,480
own
1308
00:53:02,480 --> 00:53:05,440
when the node exports this it actually
1309
00:53:05,440 --> 00:53:08,319
wraps it in the function so that's why
1310
00:53:08,319 --> 00:53:10,480
when you're required yeah you do invoke
1311
00:53:10,480 --> 00:53:11,839
the code if
1312
00:53:11,839 --> 00:53:14,400
you have some kind of function that
1313
00:53:14,400 --> 00:53:16,960
actually is executed here keep that in
1314
00:53:16,960 --> 00:53:18,960
mind not talking about exporting
1315
00:53:18,960 --> 00:53:21,280
something i'm talking about executing
1316
00:53:21,280 --> 00:53:23,440
function here and there and of course in
1317
00:53:23,440 --> 00:53:24,880
this case you need to use the
1318
00:53:24,880 --> 00:53:27,119
imagination that we'll do something way
1319
00:53:27,119 --> 00:53:29,040
more useful than this at this point
1320
00:53:29,040 --> 00:53:30,880
you're looking at it okay i could have
1321
00:53:30,880 --> 00:53:33,359
just set up that function in app.js and
1322
00:53:33,359 --> 00:53:36,240
you're 100 correct this is the place
1323
00:53:36,240 --> 00:53:38,240
where you definitely need to use your
1324
00:53:38,240 --> 00:53:40,079
imagination where of course the
1325
00:53:40,079 --> 00:53:41,839
functionality we'll use it for is going
1326
00:53:41,839 --> 00:53:44,720
to be way more complex but the idea is
1327
00:53:44,720 --> 00:53:47,119
going to be similar where we'll just
1328
00:53:47,119 --> 00:53:49,839
require some kind of module and behind
1329
00:53:49,839 --> 00:53:52,000
the scenes that module will do a bunch
1330
00:53:52,000 --> 00:53:55,119
of cool stuff okay that was fun we
1331
00:53:55,119 --> 00:53:57,440
created our first module and in the
1332
00:53:57,440 --> 00:53:59,520
process covered the general module
1333
00:53:59,520 --> 00:54:02,880
syntax and use cases as well
1334
00:54:02,880 --> 00:54:03,839
now what
1335
00:54:03,839 --> 00:54:07,680
well as helpful as our say hi function
1336
00:54:07,680 --> 00:54:08,480
is
1337
00:54:08,480 --> 00:54:11,680
some very smart people created way more
1338
00:54:11,680 --> 00:54:14,079
useful modules than that
1339
00:54:14,079 --> 00:54:15,920
and the good news is that we can start
1340
00:54:15,920 --> 00:54:17,839
using them right away
1341
00:54:17,839 --> 00:54:20,480
so what i'm trying to say here is that
1342
00:54:20,480 --> 00:54:23,839
node has bunch of built-in modules
1343
00:54:23,839 --> 00:54:26,880
that we can use freely in our apps
1344
00:54:26,880 --> 00:54:28,480
in the following videos we're going to
1345
00:54:28,480 --> 00:54:31,599
cover following modules os module so
1346
00:54:31,599 --> 00:54:34,480
operating system module path module file
1347
00:54:34,480 --> 00:54:38,079
system module as well as http module to
1348
00:54:38,079 --> 00:54:41,280
set up our http server and before we
1349
00:54:41,280 --> 00:54:43,280
continue let me just say this
1350
00:54:43,280 --> 00:54:45,359
i purposely cover only a few major
1351
00:54:45,359 --> 00:54:46,559
modules
1352
00:54:46,559 --> 00:54:48,319
and more specifically only a few
1353
00:54:48,319 --> 00:54:51,440
properties and methods provided by those
1354
00:54:51,440 --> 00:54:52,480
modules
1355
00:54:52,480 --> 00:54:55,200
you see the list of built-in modules in
1356
00:54:55,200 --> 00:54:57,520
node is quite big
1357
00:54:57,520 --> 00:55:00,160
and the amount of properties and methods
1358
00:55:00,160 --> 00:55:02,319
each module has
1359
00:55:02,319 --> 00:55:05,119
combined is of course even bigger
1360
00:55:05,119 --> 00:55:07,040
and as a result we could easily spend
1361
00:55:07,040 --> 00:55:10,319
the remainder of the course on just that
1362
00:55:10,319 --> 00:55:12,559
but is that really beneficial and of
1363
00:55:12,559 --> 00:55:14,480
course the answer is no
1364
00:55:14,480 --> 00:55:16,880
instead let's cover the general setup
1365
00:55:16,880 --> 00:55:17,760
first
1366
00:55:17,760 --> 00:55:20,640
and later as we progress with the course
1367
00:55:20,640 --> 00:55:24,079
implement some modules as we go
1368
00:55:24,079 --> 00:55:26,240
now if you're interested in more
1369
00:55:26,240 --> 00:55:27,760
properties and
1370
00:55:27,760 --> 00:55:30,640
methods built and modules have just
1371
00:55:30,640 --> 00:55:34,160
navigate to the docs and go nuts so if
1372
00:55:34,160 --> 00:55:36,480
at any point you would want to find out
1373
00:55:36,480 --> 00:55:39,680
more about specific property or method
1374
00:55:39,680 --> 00:55:43,040
or the built-in module in general just
1375
00:55:43,040 --> 00:55:46,319
navigate to nodejs.org
1376
00:55:46,319 --> 00:55:49,119
and then look for the docs
1377
00:55:49,119 --> 00:55:51,760
and again in my case
1378
00:55:51,760 --> 00:55:54,880
the version the long term support
1379
00:55:54,880 --> 00:55:57,119
version is 14
1380
00:55:57,119 --> 00:55:59,040
so i'm gonna click here but as you can
1381
00:55:59,040 --> 00:56:02,799
see we also have an option of going to
1382
00:56:02,799 --> 00:56:05,599
the older versions as well so as you can
1383
00:56:05,599 --> 00:56:08,079
see i can get the docs for the older
1384
00:56:08,079 --> 00:56:09,680
versions as well
1385
00:56:09,680 --> 00:56:11,920
but i'm just going to stick with my
1386
00:56:11,920 --> 00:56:12,799
version
1387
00:56:12,799 --> 00:56:15,520
and then be prepared to go nuts
1388
00:56:15,520 --> 00:56:17,520
if you enjoy docs
1389
00:56:17,520 --> 00:56:20,720
then you have yourself a big old party
1390
00:56:20,720 --> 00:56:22,559
where of course you can look for modules
1391
00:56:22,559 --> 00:56:26,000
and then each module has the properties
1392
00:56:26,000 --> 00:56:29,040
as well as the methods so of course you
1393
00:56:29,040 --> 00:56:30,480
can get a
1394
00:56:30,480 --> 00:56:34,160
extremely detailed information on the
1395
00:56:34,160 --> 00:56:36,640
properties as well as the methods here
1396
00:56:36,640 --> 00:56:38,160
in the docs
1397
00:56:38,160 --> 00:56:40,400
all right and once we have covered the
1398
00:56:40,400 --> 00:56:43,440
general existence of built-in modules
1399
00:56:43,440 --> 00:56:45,760
now let's take a look at few of them
1400
00:56:45,760 --> 00:56:47,839
and i'm going to start by creating that
1401
00:56:47,839 --> 00:56:50,160
file that i promised a few videos ago
1402
00:56:50,160 --> 00:56:52,240
remember when i was setting up the
1403
00:56:52,240 --> 00:56:54,240
modules i said that eventually there is
1404
00:56:54,240 --> 00:56:56,000
going to be a file by the name of 3n
1405
00:56:56,000 --> 00:56:58,480
modules and this one i would want to do
1406
00:56:58,480 --> 00:56:59,599
it together
1407
00:56:59,599 --> 00:57:02,559
just so you don't think that i'm not
1408
00:57:02,559 --> 00:57:04,720
fulfilling my promises so we're going to
1409
00:57:04,720 --> 00:57:07,839
go with module and then i guess
1410
00:57:07,839 --> 00:57:10,000
modules.js
1411
00:57:10,000 --> 00:57:12,960
i'll grab that i currently have in the
1412
00:57:12,960 --> 00:57:15,440
address it's pretty much everything
1413
00:57:15,440 --> 00:57:17,680
and we copy and paste and in this case i
1414
00:57:17,680 --> 00:57:21,520
also uncommented these say hi again this
1415
00:57:21,520 --> 00:57:23,599
all stays for your reference and you'll
1416
00:57:23,599 --> 00:57:26,720
have access to it if you navigate to the
1417
00:57:26,720 --> 00:57:28,079
github repo
1418
00:57:28,079 --> 00:57:30,079
so grab everything in app.js i'll
1419
00:57:30,079 --> 00:57:31,440
actually cut it out since we'll start
1420
00:57:31,440 --> 00:57:33,520
from scratch and copy and paste now of
1421
00:57:33,520 --> 00:57:35,599
course all this code is sitting in the
1422
00:57:35,599 --> 00:57:37,280
modules and just keep in mind that of
1423
00:57:37,280 --> 00:57:39,359
course we're still referencing the names
1424
00:57:39,359 --> 00:57:42,720
utils alternative flavor as well as my
1425
00:57:42,720 --> 00:57:44,799
favorite the mind grenade
1426
00:57:44,799 --> 00:57:47,119
and back in app.js
1427
00:57:47,119 --> 00:57:49,760
now let's start by taking a look at the
1428
00:57:49,760 --> 00:57:51,200
os module
1429
00:57:51,200 --> 00:57:54,079
and os module we built in module
1430
00:57:54,079 --> 00:57:55,839
provides many useful properties and
1431
00:57:55,839 --> 00:57:59,359
methods for interacting with operating
1432
00:57:59,359 --> 00:58:01,599
system as well as the server
1433
00:58:01,599 --> 00:58:03,520
and the general setup
1434
00:58:03,520 --> 00:58:06,559
for the built-in modules or even the
1435
00:58:06,559 --> 00:58:08,079
external ones that we're going to cover
1436
00:58:08,079 --> 00:58:09,280
a bit later
1437
00:58:09,280 --> 00:58:11,280
is going to be similar
1438
00:58:11,280 --> 00:58:12,880
where we're going to go with some kind
1439
00:58:12,880 --> 00:58:14,000
of variable
1440
00:58:14,000 --> 00:58:15,520
and in this case i'm going to call this
1441
00:58:15,520 --> 00:58:18,400
os for operating system then we're going
1442
00:58:18,400 --> 00:58:20,720
to go with require
1443
00:58:20,720 --> 00:58:22,960
and then in the require we'll have to
1444
00:58:22,960 --> 00:58:24,720
pass in the name
1445
00:58:24,720 --> 00:58:26,720
so since this is a built-in module of
1446
00:58:26,720 --> 00:58:28,480
course we don't need to install anything
1447
00:58:28,480 --> 00:58:30,559
just remember it later when we work with
1448
00:58:30,559 --> 00:58:33,280
external ones yes we'll have to install
1449
00:58:33,280 --> 00:58:35,680
that module first in this case it is a
1450
00:58:35,680 --> 00:58:38,720
built-in so we just go with os but again
1451
00:58:38,720 --> 00:58:41,760
let me stress something we're not doing
1452
00:58:41,760 --> 00:58:44,240
dot and then forward slash no we just go
1453
00:58:44,240 --> 00:58:46,960
with os so that way node knows all right
1454
00:58:46,960 --> 00:58:48,960
so you're looking for the built-in
1455
00:58:48,960 --> 00:58:50,960
module and
1456
00:58:50,960 --> 00:58:53,200
once we set this up
1457
00:58:53,200 --> 00:58:55,200
then we have multiple options
1458
00:58:55,200 --> 00:58:58,000
either you can access those methods by
1459
00:58:58,000 --> 00:59:00,640
just running this variable
1460
00:59:00,640 --> 00:59:02,480
so it would look something like this
1461
00:59:02,480 --> 00:59:04,400
where i go with os and notice
1462
00:59:04,400 --> 00:59:06,640
the moment i press dot
1463
00:59:06,640 --> 00:59:09,040
i have all these options so these are
1464
00:59:09,040 --> 00:59:09,760
the
1465
00:59:09,760 --> 00:59:13,119
methods and properties that i can use or
1466
00:59:13,119 --> 00:59:15,680
if we know that we want a specific one
1467
00:59:15,680 --> 00:59:17,920
you can simply destructure it here and
1468
00:59:17,920 --> 00:59:20,400
of course throughout the course i'll use
1469
00:59:20,400 --> 00:59:22,559
multiple setups just so you understand
1470
00:59:22,559 --> 00:59:24,319
that both of them effectively do the
1471
00:59:24,319 --> 00:59:27,680
same thing so here i have access to all
1472
00:59:27,680 --> 00:59:30,000
the methods and properties that this
1473
00:59:30,000 --> 00:59:32,240
module provides and of course now we can
1474
00:59:32,240 --> 00:59:34,640
go crazy for example i could get a
1475
00:59:34,640 --> 00:59:37,040
useful info about the current user which
1476
00:59:37,040 --> 00:59:41,599
of course is me so say info about
1477
00:59:41,599 --> 00:59:43,839
current and
1478
00:59:43,839 --> 00:59:46,640
user and you know what as a side note
1479
00:59:46,640 --> 00:59:48,559
i'll save here and i'll close all the
1480
00:59:48,559 --> 00:59:50,480
files it's just a bit annoying that i
1481
00:59:50,480 --> 00:59:52,319
have all those tabs open
1482
00:59:52,319 --> 00:59:54,480
back in the app.js
1483
00:59:54,480 --> 00:59:56,480
once i have a comment of course now i
1484
00:59:56,480 --> 00:59:58,839
would want to assign it to a
1485
00:59:58,839 --> 01:00:01,760
user and we'll simply go with os since
1486
01:00:01,760 --> 01:00:04,559
that is the name of my variable and then
1487
01:00:04,559 --> 01:00:07,040
method that i'm looking for is userinfo
1488
01:00:07,040 --> 01:00:09,280
i don't need to pass anything i simply
1489
01:00:09,280 --> 01:00:11,359
invoke it and of course since i would
1490
01:00:11,359 --> 01:00:12,960
want to see the result i simply go to
1491
01:00:12,960 --> 01:00:15,599
console log and then i'm looking for the
1492
01:00:15,599 --> 01:00:17,520
user and you can probably already guess
1493
01:00:17,520 --> 01:00:19,520
that we'll just run
1494
01:00:19,520 --> 01:00:22,400
node app.js and there it is now of
1495
01:00:22,400 --> 01:00:25,920
course i have a info about the user
1496
01:00:25,920 --> 01:00:28,160
which is me so this would be a home
1497
01:00:28,160 --> 01:00:30,000
directory this is going to be the
1498
01:00:30,000 --> 01:00:32,880
default shell and all kinds of useful
1499
01:00:32,880 --> 01:00:33,839
stuff
1500
01:00:33,839 --> 01:00:36,480
then i want to show you how we can get
1501
01:00:36,480 --> 01:00:40,319
the uptime of the system so effectively
1502
01:00:40,319 --> 01:00:42,720
how long the computer has been running
1503
01:00:42,720 --> 01:00:46,319
in seconds and i'll just add a comment
1504
01:00:46,319 --> 01:00:47,520
method
1505
01:00:47,520 --> 01:00:49,040
returns
1506
01:00:49,040 --> 01:00:50,880
the system
1507
01:00:50,880 --> 01:00:52,079
system
1508
01:00:52,079 --> 01:00:54,319
up time up time
1509
01:00:54,319 --> 01:00:56,400
in seconds
1510
01:00:56,400 --> 01:00:57,440
and
1511
01:00:57,440 --> 01:00:59,040
the way i'm going to run that i'll right
1512
01:00:59,040 --> 01:01:01,280
away set up the console log
1513
01:01:01,280 --> 01:01:03,599
just so you understand that of course we
1514
01:01:03,599 --> 01:01:06,319
have multiple options how we can invoke
1515
01:01:06,319 --> 01:01:07,520
those methods
1516
01:01:07,520 --> 01:01:09,920
you don't always have to assign to some
1517
01:01:09,920 --> 01:01:12,000
kind of variable now i'll leave this one
1518
01:01:12,000 --> 01:01:12,960
in here
1519
01:01:12,960 --> 01:01:15,520
so i'll leave the console log and then
1520
01:01:15,520 --> 01:01:16,559
user
1521
01:01:16,559 --> 01:01:18,880
and let's say right above or you know
1522
01:01:18,880 --> 01:01:20,880
what let's do it this way so leave the
1523
01:01:20,880 --> 01:01:23,280
console log where the user is and then
1524
01:01:23,280 --> 01:01:25,119
let's go with another one we'll go with
1525
01:01:25,119 --> 01:01:26,960
console.log in this case i'm going to go
1526
01:01:26,960 --> 01:01:29,599
with template string and we'll say the
1527
01:01:29,599 --> 01:01:30,799
system
1528
01:01:30,799 --> 01:01:35,040
system uptime yes and then let's use a
1529
01:01:35,040 --> 01:01:37,200
interpolation of course and the method
1530
01:01:37,200 --> 01:01:38,480
that i'm looking for
1531
01:01:38,480 --> 01:01:41,200
is uptime so again i go with my variable
1532
01:01:41,200 --> 01:01:42,720
name and i know i've said this before
1533
01:01:42,720 --> 01:01:44,799
but i just want to stress this point
1534
01:01:44,799 --> 01:01:47,440
just so we all are on the same page and
1535
01:01:47,440 --> 01:01:49,359
in this case i'm going to go with os and
1536
01:01:49,359 --> 01:01:52,480
then the method name is uptime
1537
01:01:52,480 --> 01:01:55,280
and then let's just add seconds and
1538
01:01:55,280 --> 01:01:58,400
again same deal back in the integrated
1539
01:01:58,400 --> 01:02:01,839
terminal run node and then app.js and
1540
01:02:01,839 --> 01:02:03,760
there it is now of course not only i
1541
01:02:03,760 --> 01:02:07,200
have the info i also have the system
1542
01:02:07,200 --> 01:02:09,599
uptime and what's interesting this
1543
01:02:09,599 --> 01:02:13,440
actually is 100 seconds meaning if i'll
1544
01:02:13,440 --> 01:02:16,559
run this notice that you'd expect that
1545
01:02:16,559 --> 01:02:19,119
this would probably stop at 60 right
1546
01:02:19,119 --> 01:02:21,520
since we have 60 seconds in a minute but
1547
01:02:21,520 --> 01:02:25,039
no actually this one goes to a hundred
1548
01:02:25,039 --> 01:02:26,960
and then i'm just gonna set up a object
1549
01:02:26,960 --> 01:02:29,760
where i'll show you a few more methods
1550
01:02:29,760 --> 01:02:32,000
and then we'll move on to a next module
1551
01:02:32,000 --> 01:02:34,240
because again the point is not to cover
1552
01:02:34,240 --> 01:02:36,720
each and every method the point is just
1553
01:02:36,720 --> 01:02:39,200
to give you a general idea how the
1554
01:02:39,200 --> 01:02:42,400
modules the built-in modules would work
1555
01:02:42,400 --> 01:02:46,000
in node so clear my terminal and then
1556
01:02:46,000 --> 01:02:48,400
i'll create a object i'll call this
1557
01:02:48,400 --> 01:02:49,359
current
1558
01:02:49,359 --> 01:02:51,200
and then os
1559
01:02:51,200 --> 01:02:53,119
and then in this object i'll set up
1560
01:02:53,119 --> 01:02:55,599
properties and i'll set them equal to
1561
01:02:55,599 --> 01:02:56,880
the method
1562
01:02:56,880 --> 01:02:59,839
that actually returns that value from my
1563
01:02:59,839 --> 01:03:03,119
os module so go with name and os and
1564
01:03:03,119 --> 01:03:04,960
this is going to be equal to a type
1565
01:03:04,960 --> 01:03:07,920
method then i can get the release
1566
01:03:07,920 --> 01:03:10,559
and this one will be equal to os and
1567
01:03:10,559 --> 01:03:11,440
then
1568
01:03:11,440 --> 01:03:13,920
release again we invoke it then we'll
1569
01:03:13,920 --> 01:03:17,119
look for total memory and free memory
1570
01:03:17,119 --> 01:03:18,240
total
1571
01:03:18,240 --> 01:03:19,599
and mem
1572
01:03:19,599 --> 01:03:22,559
and of course i'll go with os and then
1573
01:03:22,559 --> 01:03:25,039
total mam as you can see somewhat
1574
01:03:25,039 --> 01:03:27,359
predictable and then we're gonna go with
1575
01:03:27,359 --> 01:03:28,839
free memory
1576
01:03:28,839 --> 01:03:30,799
and it's not going to be surprised if
1577
01:03:30,799 --> 01:03:33,200
we're just going to go with free memory
1578
01:03:33,200 --> 01:03:35,839
here let's save that one and lastly
1579
01:03:35,839 --> 01:03:37,599
let's just cancel log it
1580
01:03:37,599 --> 01:03:38,960
and we should
1581
01:03:38,960 --> 01:03:42,000
get a bunch of info about our current
1582
01:03:42,000 --> 01:03:44,400
operating system again we're going to go
1583
01:03:44,400 --> 01:03:47,760
with node and then app.js
1584
01:03:47,760 --> 01:03:49,839
and not only have the user information
1585
01:03:49,839 --> 01:03:51,359
not only i know
1586
01:03:51,359 --> 01:03:54,160
how long the system has been up
1587
01:03:54,160 --> 01:03:56,720
in seconds i can also get some useful
1588
01:03:56,720 --> 01:03:59,680
data about my operating system and again
1589
01:03:59,680 --> 01:04:01,760
we're just scratching the surface of
1590
01:04:01,760 --> 01:04:03,680
course there's more methods and
1591
01:04:03,680 --> 01:04:05,680
properties but hopefully you have a
1592
01:04:05,680 --> 01:04:08,960
clear understanding how powerful these
1593
01:04:08,960 --> 01:04:12,400
built-in modules are in node.js since
1594
01:04:12,400 --> 01:04:14,240
the node we can interact with the file
1595
01:04:14,240 --> 01:04:17,680
system something we couldn't do when we
1596
01:04:17,680 --> 01:04:20,480
used our javascript in a browser another
1597
01:04:20,480 --> 01:04:23,760
useful module is a path module which
1598
01:04:23,760 --> 01:04:26,480
allows us to interact with file paths
1599
01:04:26,480 --> 01:04:27,599
easily
1600
01:04:27,599 --> 01:04:30,640
and i'm going to remove all my code in
1601
01:04:30,640 --> 01:04:33,039
the app.js because behind the scenes i
1602
01:04:33,039 --> 01:04:35,440
created a file by the name of
1603
01:04:35,440 --> 01:04:37,200
8 os
1604
01:04:37,200 --> 01:04:40,880
and module and of course copy and pasted
1605
01:04:40,880 --> 01:04:43,119
the contents from the previous video
1606
01:04:43,119 --> 01:04:44,880
that's why in app.js i'll remove
1607
01:04:44,880 --> 01:04:47,119
everything and the setup is going to be
1608
01:04:47,119 --> 01:04:48,880
exactly the same we come up with some
1609
01:04:48,880 --> 01:04:50,880
kind of name now most likely it's going
1610
01:04:50,880 --> 01:04:52,000
to be path
1611
01:04:52,000 --> 01:04:54,640
and then we pass in the module we would
1612
01:04:54,640 --> 01:04:56,720
want to get and in this case of course
1613
01:04:56,720 --> 01:04:58,880
it is a path module
1614
01:04:58,880 --> 01:05:01,200
and then let's just start with a very
1615
01:05:01,200 --> 01:05:02,960
useful property
1616
01:05:02,960 --> 01:05:06,000
a separator property that returns a
1617
01:05:06,000 --> 01:05:10,240
platform specific separator so if i go
1618
01:05:10,240 --> 01:05:12,720
straight up with console.log and then
1619
01:05:12,720 --> 01:05:13,599
path
1620
01:05:13,599 --> 01:05:16,960
sep this is going to return my platform
1621
01:05:16,960 --> 01:05:20,000
specific separator so if i write node
1622
01:05:20,000 --> 01:05:21,039
mjs
1623
01:05:21,039 --> 01:05:23,039
there it is and of course i have forward
1624
01:05:23,039 --> 01:05:24,319
slash now if you're using different
1625
01:05:24,319 --> 01:05:27,200
platform for you the value might be
1626
01:05:27,200 --> 01:05:29,039
different after that let's take a look
1627
01:05:29,039 --> 01:05:32,960
at the path dot join so the join method
1628
01:05:32,960 --> 01:05:36,720
which joins a sequence of path segments
1629
01:05:36,720 --> 01:05:39,520
using that platform specific separator
1630
01:05:39,520 --> 01:05:42,240
as the limiter and second thing it does
1631
01:05:42,240 --> 01:05:46,400
it returns a normalized resulting path
1632
01:05:46,400 --> 01:05:48,480
and in order to show you an example i'll
1633
01:05:48,480 --> 01:05:50,559
create a new folder so not a new file
1634
01:05:50,559 --> 01:05:53,680
but a new folder i'll zoom in and i'll
1635
01:05:53,680 --> 01:05:56,559
say content and inside of this content
1636
01:05:56,559 --> 01:05:59,200
folder i'll create another one and i'll
1637
01:05:59,200 --> 01:06:01,920
simply call this generic subfolder and
1638
01:06:01,920 --> 01:06:05,599
in this subfolder i'll say a test
1639
01:06:05,599 --> 01:06:08,319
and text file and instead of the text
1640
01:06:08,319 --> 01:06:09,839
file i mean
1641
01:06:09,839 --> 01:06:12,160
you can place something you can omit it
1642
01:06:12,160 --> 01:06:14,960
doesn't really matter i'll just say text
1643
01:06:14,960 --> 01:06:19,039
and then txt and once we save it back in
1644
01:06:19,039 --> 01:06:22,000
the app js now i want to use that path
1645
01:06:22,000 --> 01:06:25,280
dot join method so i'll say const and
1646
01:06:25,280 --> 01:06:29,680
then file path is equal to path dot join
1647
01:06:29,680 --> 01:06:32,480
and then we need to pass in those values
1648
01:06:32,480 --> 01:06:35,280
as strings so for example since i'm
1649
01:06:35,280 --> 01:06:37,839
looking for the content folder
1650
01:06:37,839 --> 01:06:40,640
i'll simply say forward slash and then
1651
01:06:40,640 --> 01:06:43,440
content then i know that inside of the
1652
01:06:43,440 --> 01:06:46,240
content folder i have the subfolder
1653
01:06:46,240 --> 01:06:48,160
that's another folder and then
1654
01:06:48,160 --> 01:06:51,440
eventually there's going to be a test
1655
01:06:51,440 --> 01:06:55,680
txt so we go with test and then dot txt
1656
01:06:55,680 --> 01:06:58,240
we save the sucker and then if we'll
1657
01:06:58,240 --> 01:07:01,760
cancel log the file path we should
1658
01:07:01,760 --> 01:07:03,839
see the whole path and there it is now
1659
01:07:03,839 --> 01:07:06,640
of course it is normalized and we get it
1660
01:07:06,640 --> 01:07:09,680
as a value so we have content subfolder
1661
01:07:09,680 --> 01:07:10,559
test
1662
01:07:10,559 --> 01:07:14,480
and txt so if i start adding some type
1663
01:07:14,480 --> 01:07:16,079
of forward slashes meaning trailing
1664
01:07:16,079 --> 01:07:18,640
slashes then of course they will be
1665
01:07:18,640 --> 01:07:22,559
removed we still get the normalized path
1666
01:07:22,559 --> 01:07:25,520
and we can also get the base name so for
1667
01:07:25,520 --> 01:07:28,880
example i create this file path but i
1668
01:07:28,880 --> 01:07:32,319
only would want the last portion of it
1669
01:07:32,319 --> 01:07:34,079
how can i access it well
1670
01:07:34,079 --> 01:07:36,720
the method name is base name
1671
01:07:36,720 --> 01:07:40,400
so we're gonna go with const and base
1672
01:07:40,400 --> 01:07:44,160
and path dot base name and we just pass
1673
01:07:44,160 --> 01:07:47,039
in that file path and now if we cancel
1674
01:07:47,039 --> 01:07:48,400
log the base
1675
01:07:48,400 --> 01:07:51,280
we should see the text again these are
1676
01:07:51,280 --> 01:07:52,799
just summaries if you want just a
1677
01:07:52,799 --> 01:07:55,039
directory of course you can imagine that
1678
01:07:55,039 --> 01:07:58,559
there is a method that returns that as
1679
01:07:58,559 --> 01:08:00,000
well but since i don't want to cover
1680
01:08:00,000 --> 01:08:02,319
each and every method that's why i will
1681
01:08:02,319 --> 01:08:04,640
skip that one and there it is now of
1682
01:08:04,640 --> 01:08:07,920
course not only i see my normalized path
1683
01:08:07,920 --> 01:08:11,520
but i also see the test txt which is my
1684
01:08:11,520 --> 01:08:13,359
base and the last method that i would
1685
01:08:13,359 --> 01:08:16,000
want to showcase is path.resolve
1686
01:08:16,000 --> 01:08:18,080
and why it's so cool because it returns
1687
01:08:18,080 --> 01:08:19,839
an absolute path
1688
01:08:19,839 --> 01:08:21,759
and once in a while in our applications
1689
01:08:21,759 --> 01:08:23,839
we need to provide that absolute path
1690
01:08:23,839 --> 01:08:25,839
that's why of course we'll have to use
1691
01:08:25,839 --> 01:08:27,040
that method
1692
01:08:27,040 --> 01:08:29,679
and when it comes to path.resolve it
1693
01:08:29,679 --> 01:08:32,719
accepts a sequence of paths or path
1694
01:08:32,719 --> 01:08:35,759
segments and resolves it into an
1695
01:08:35,759 --> 01:08:38,000
absolute path so below the base i'm
1696
01:08:38,000 --> 01:08:40,000
going to go with cons and then
1697
01:08:40,000 --> 01:08:43,040
absolute and that one is equal to path
1698
01:08:43,040 --> 01:08:46,080
and we're looking for dot resolve
1699
01:08:46,080 --> 01:08:48,238
and here remember when we talked about
1700
01:08:48,238 --> 01:08:50,399
the globals one of the first globals i
1701
01:08:50,399 --> 01:08:53,040
covered was underscore underscored their
1702
01:08:53,040 --> 01:08:56,080
name so this is going to point to this
1703
01:08:56,080 --> 01:08:59,600
directory where the app.js is located
1704
01:08:59,600 --> 01:09:01,759
and once we're here then of course we
1705
01:09:01,759 --> 01:09:04,799
just need to provide rest of the values
1706
01:09:04,799 --> 01:09:06,880
in my case that is content
1707
01:09:06,880 --> 01:09:08,399
then i'm looking for
1708
01:09:08,399 --> 01:09:09,359
sub
1709
01:09:09,359 --> 01:09:12,719
folder and then eventually i would want
1710
01:09:12,719 --> 01:09:15,920
to get to the test dxd i would want to
1711
01:09:15,920 --> 01:09:18,880
get this absolute path and then once i
1712
01:09:18,880 --> 01:09:20,640
set up the value of course
1713
01:09:20,640 --> 01:09:23,839
if i go with my console.log and if i
1714
01:09:23,839 --> 01:09:24,799
provide
1715
01:09:24,799 --> 01:09:26,238
absolute
1716
01:09:26,238 --> 01:09:29,600
once i run node and objects
1717
01:09:29,600 --> 01:09:32,000
you can probably guess that in the
1718
01:09:32,000 --> 01:09:34,960
console i'll have my absolute path
1719
01:09:34,960 --> 01:09:37,759
to the test txt and if you're wondering
1720
01:09:37,759 --> 01:09:39,839
why we would ever want that well
1721
01:09:39,839 --> 01:09:42,158
remember our application
1722
01:09:42,158 --> 01:09:45,120
is going to run in different
1723
01:09:45,120 --> 01:09:46,640
environments right
1724
01:09:46,640 --> 01:09:49,439
so of course the path to some kind of
1725
01:09:49,439 --> 01:09:51,920
resource is going to be different in my
1726
01:09:51,920 --> 01:09:55,040
computer in my local machine than for
1727
01:09:55,040 --> 01:09:57,760
example in heroku
1728
01:09:57,760 --> 01:09:59,920
hopefully it is clear and now of course
1729
01:09:59,920 --> 01:10:02,640
we can take a look at the next useful
1730
01:10:02,640 --> 01:10:04,560
built-in module
1731
01:10:04,560 --> 01:10:07,120
nice now we know how we can work with
1732
01:10:07,120 --> 01:10:09,440
path module and like i mentioned in the
1733
01:10:09,440 --> 01:10:12,239
previous video we also have an option of
1734
01:10:12,239 --> 01:10:15,679
interacting with a file system
1735
01:10:15,679 --> 01:10:16,400
so
1736
01:10:16,400 --> 01:10:18,960
what i would want right now is first
1737
01:10:18,960 --> 01:10:21,679
create a new file i'll call this nine
1738
01:10:21,679 --> 01:10:23,760
and we'll say path
1739
01:10:23,760 --> 01:10:24,719
and
1740
01:10:24,719 --> 01:10:27,920
module like so js i'll grab everything
1741
01:10:27,920 --> 01:10:30,400
that i have in the address
1742
01:10:30,400 --> 01:10:32,560
copy and paste
1743
01:10:32,560 --> 01:10:34,880
and once we have this one out of the way
1744
01:10:34,880 --> 01:10:36,400
i'll just remove
1745
01:10:36,400 --> 01:10:39,600
and keep the app.js clean for now
1746
01:10:39,600 --> 01:10:42,080
as well as clear the console
1747
01:10:42,080 --> 01:10:44,800
and yes we have an option of interacting
1748
01:10:44,800 --> 01:10:47,120
with the file system and the module the
1749
01:10:47,120 --> 01:10:50,480
name of the module is fs
1750
01:10:50,480 --> 01:10:53,199
now essentially there are two flavors
1751
01:10:53,199 --> 01:10:55,920
when it comes to file module we can do
1752
01:10:55,920 --> 01:10:58,640
it asynchronously non-blocking or we can
1753
01:10:58,640 --> 01:11:00,960
do it synchronously which is going to be
1754
01:11:00,960 --> 01:11:02,640
blocking but
1755
01:11:02,640 --> 01:11:05,600
for time being we'll just cover both of
1756
01:11:05,600 --> 01:11:06,960
the setups
1757
01:11:06,960 --> 01:11:09,280
and then later in the course
1758
01:11:09,280 --> 01:11:12,320
we'll cover what are the differences and
1759
01:11:12,320 --> 01:11:14,719
why you should prefer one over the other
1760
01:11:14,719 --> 01:11:17,920
one for now just think of them as
1761
01:11:17,920 --> 01:11:20,159
different methods but eventually yes
1762
01:11:20,159 --> 01:11:22,320
we'll talk about it the whole structure
1763
01:11:22,320 --> 01:11:23,840
of the node
1764
01:11:23,840 --> 01:11:25,840
and why would you want to use the
1765
01:11:25,840 --> 01:11:28,400
non-blocking code instead
1766
01:11:28,400 --> 01:11:30,400
as well as different approaches when it
1767
01:11:30,400 --> 01:11:32,560
comes to callback functions different
1768
01:11:32,560 --> 01:11:33,679
solutions
1769
01:11:33,679 --> 01:11:36,000
and for now i just want to show you both
1770
01:11:36,000 --> 01:11:38,480
methods essentially four methods
1771
01:11:38,480 --> 01:11:40,560
and then we'll just move on to the next
1772
01:11:40,560 --> 01:11:41,440
topic
1773
01:11:41,440 --> 01:11:44,000
and in order to set this up in this case
1774
01:11:44,000 --> 01:11:45,760
i'm just going to structure right away
1775
01:11:45,760 --> 01:11:47,760
the methods that i'm looking for and in
1776
01:11:47,760 --> 01:11:49,520
the first video we'll take a look at the
1777
01:11:49,520 --> 01:11:51,600
synchronous ones so that's why i'll
1778
01:11:51,600 --> 01:11:52,800
destructure
1779
01:11:52,800 --> 01:11:55,760
read file and sync and that is coming
1780
01:11:55,760 --> 01:11:59,440
from require and then the module name is
1781
01:11:59,440 --> 01:12:00,560
fs
1782
01:12:00,560 --> 01:12:02,560
again please keep in mind it would be
1783
01:12:02,560 --> 01:12:04,080
the same thing
1784
01:12:04,080 --> 01:12:06,400
if i were to
1785
01:12:06,400 --> 01:12:10,159
go like this fs and then fs and whatever
1786
01:12:10,159 --> 01:12:11,440
the name
1787
01:12:11,440 --> 01:12:13,920
i was going with read file sync and of
1788
01:12:13,920 --> 01:12:15,840
course i have access to it i'm just
1789
01:12:15,840 --> 01:12:17,520
showing you a different way how we can
1790
01:12:17,520 --> 01:12:20,320
access those methods we just destructure
1791
01:12:20,320 --> 01:12:23,280
them out of the fs module and then the
1792
01:12:23,280 --> 01:12:27,120
second one will be write file sync
1793
01:12:27,120 --> 01:12:28,719
write file sync
1794
01:12:28,719 --> 01:12:30,400
and then once i have the structured both
1795
01:12:30,400 --> 01:12:31,360
of them
1796
01:12:31,360 --> 01:12:34,080
then i'm going to set up two variables
1797
01:12:34,080 --> 01:12:36,960
so first we'll read two files
1798
01:12:36,960 --> 01:12:38,960
we haven't created those files yet but
1799
01:12:38,960 --> 01:12:41,360
in a second we will and then eventually
1800
01:12:41,360 --> 01:12:44,400
we'll create a new file with write file
1801
01:12:44,400 --> 01:12:46,640
sync so i would want you to go to the
1802
01:12:46,640 --> 01:12:48,640
content folder if you haven't created
1803
01:12:48,640 --> 01:12:50,880
then i suggest creating one
1804
01:12:50,880 --> 01:12:51,760
and
1805
01:12:51,760 --> 01:12:53,679
i mean i'll keep the subfolder just
1806
01:12:53,679 --> 01:12:54,560
because
1807
01:12:54,560 --> 01:12:57,040
i believe the path module is still using
1808
01:12:57,040 --> 01:13:00,400
it but in a content i'll create a new
1809
01:13:00,400 --> 01:13:02,560
file so not in the subfolder but
1810
01:13:02,560 --> 01:13:06,560
actually in the content i'll say
1811
01:13:06,560 --> 01:13:09,679
first first and then txt
1812
01:13:09,679 --> 01:13:11,440
so this by the way sets it up in the
1813
01:13:11,440 --> 01:13:13,360
content so let me move it up or i'm
1814
01:13:13,360 --> 01:13:15,760
sorry this sets it up in a subfolder i
1815
01:13:15,760 --> 01:13:16,640
believe
1816
01:13:16,640 --> 01:13:18,880
and it tells me do i want to move yes i
1817
01:13:18,880 --> 01:13:21,280
would like to move and now i have the
1818
01:13:21,280 --> 01:13:22,480
content
1819
01:13:22,480 --> 01:13:24,320
that has the first txt
1820
01:13:24,320 --> 01:13:25,840
and then i have the subfolder with
1821
01:13:25,840 --> 01:13:27,920
test.dxt of course if you want you can
1822
01:13:27,920 --> 01:13:29,760
place it in a subfolder that's really up
1823
01:13:29,760 --> 01:13:32,239
to you but in my case i would want them
1824
01:13:32,239 --> 01:13:35,040
simply in a content so i'll go the
1825
01:13:35,040 --> 01:13:38,880
second one txt and as far as the values
1826
01:13:38,880 --> 01:13:40,480
i'll say hello
1827
01:13:40,480 --> 01:13:42,960
this is first
1828
01:13:42,960 --> 01:13:45,600
first text file
1829
01:13:45,600 --> 01:13:47,920
and then of course we can copy and paste
1830
01:13:47,920 --> 01:13:49,760
the value and just change it around to
1831
01:13:49,760 --> 01:13:53,040
the second file so in the second txt
1832
01:13:53,040 --> 01:13:56,239
we'll say second text file
1833
01:13:56,239 --> 01:13:58,880
and once we have both of the text files
1834
01:13:58,880 --> 01:14:00,960
now let's take a look at how we can read
1835
01:14:00,960 --> 01:14:02,640
from the file system
1836
01:14:02,640 --> 01:14:06,159
and since we have read file sync we know
1837
01:14:06,159 --> 01:14:08,480
it is a method and in that method we
1838
01:14:08,480 --> 01:14:11,120
need to provide two parameters we need
1839
01:14:11,120 --> 01:14:14,960
to provide a path to that specific file
1840
01:14:14,960 --> 01:14:18,159
as well as well what is the encoding so
1841
01:14:18,159 --> 01:14:20,560
the node knows how to decode the file
1842
01:14:20,560 --> 01:14:24,239
and generally you'll go with utf-8
1843
01:14:24,239 --> 01:14:27,440
and we'll first go with first that's my
1844
01:14:27,440 --> 01:14:29,360
variable we'll go with
1845
01:14:29,360 --> 01:14:31,360
read file sync
1846
01:14:31,360 --> 01:14:33,199
my method name and like i said we
1847
01:14:33,199 --> 01:14:36,159
provide a path we go to the content
1848
01:14:36,159 --> 01:14:37,840
and then inside of the content there's
1849
01:14:37,840 --> 01:14:40,480
going to be a first
1850
01:14:40,480 --> 01:14:43,920
txt and then as far as the encoding the
1851
01:14:43,920 --> 01:14:47,280
default one will be utf-8 but if you
1852
01:14:47,280 --> 01:14:49,840
want to be a bit specific just set up
1853
01:14:49,840 --> 01:14:52,159
the second parameter and notice these
1854
01:14:52,159 --> 01:14:55,120
are all your options and just go with ut
1855
01:14:55,120 --> 01:14:57,040
effect okay
1856
01:14:57,040 --> 01:14:59,120
and then once we have everything in
1857
01:14:59,120 --> 01:15:00,960
place of course we can copy and paste
1858
01:15:00,960 --> 01:15:02,480
this is going to be the value for the
1859
01:15:02,480 --> 01:15:03,600
second one
1860
01:15:03,600 --> 01:15:05,760
and we just need to change the file name
1861
01:15:05,760 --> 01:15:06,800
of course
1862
01:15:06,800 --> 01:15:09,360
so we have the second one and now in a
1863
01:15:09,360 --> 01:15:10,640
console log
1864
01:15:10,640 --> 01:15:13,920
we should see both of the text so if i
1865
01:15:13,920 --> 01:15:16,560
go with console.log first
1866
01:15:16,560 --> 01:15:19,120
and then second
1867
01:15:19,120 --> 01:15:22,080
i should have one long text and let's go
1868
01:15:22,080 --> 01:15:24,400
with node and app.js and there it is i
1869
01:15:24,400 --> 01:15:28,159
have hello this is a first text file and
1870
01:15:28,159 --> 01:15:30,560
i also have hello this is the second
1871
01:15:30,560 --> 01:15:31,679
text file
1872
01:15:31,679 --> 01:15:33,360
and once i have both of them why don't
1873
01:15:33,360 --> 01:15:35,600
we create a new file
1874
01:15:35,600 --> 01:15:38,880
and the method for that is write file
1875
01:15:38,880 --> 01:15:39,760
sync
1876
01:15:39,760 --> 01:15:42,400
and the way it works again we provide
1877
01:15:42,400 --> 01:15:45,040
two values two arguments one is gonna be
1878
01:15:45,040 --> 01:15:47,679
the file name and if the file is not
1879
01:15:47,679 --> 01:15:48,560
there
1880
01:15:48,560 --> 01:15:50,880
then of course node will create that
1881
01:15:50,880 --> 01:15:52,960
file and you'll see that in a second and
1882
01:15:52,960 --> 01:15:55,040
the second one is the value that we
1883
01:15:55,040 --> 01:15:56,960
would want to pass and i'm going to go
1884
01:15:56,960 --> 01:15:59,360
with write file sync
1885
01:15:59,360 --> 01:16:01,840
write file sync and like i said first is
1886
01:16:01,840 --> 01:16:04,080
going to be a path so you can call this
1887
01:16:04,080 --> 01:16:06,000
third you can call this result whatever
1888
01:16:06,000 --> 01:16:08,000
you would want in my case i'm going to
1889
01:16:08,000 --> 01:16:10,400
go with forward slash content
1890
01:16:10,400 --> 01:16:13,760
and then we're looking for result
1891
01:16:13,760 --> 01:16:16,400
and then sync and this is going to be
1892
01:16:16,400 --> 01:16:18,800
txt and then as far as the value if you
1893
01:16:18,800 --> 01:16:20,880
want you can simply pass hello world and
1894
01:16:20,880 --> 01:16:22,880
test it in my case i'm going to make it
1895
01:16:22,880 --> 01:16:24,880
a bit more interesting where we'll set
1896
01:16:24,880 --> 01:16:27,440
up a template string we'll say here is
1897
01:16:27,440 --> 01:16:29,199
the result
1898
01:16:29,199 --> 01:16:31,840
and why don't we use the interpolation
1899
01:16:31,840 --> 01:16:34,640
we'll say first and second
1900
01:16:34,640 --> 01:16:37,120
so we'll have first value and then the
1901
01:16:37,120 --> 01:16:39,440
second value and you know what when we
1902
01:16:39,440 --> 01:16:42,560
add here colon as well so let's say
1903
01:16:42,560 --> 01:16:43,600
first
1904
01:16:43,600 --> 01:16:45,600
and then let's place a comma
1905
01:16:45,600 --> 01:16:48,400
and then we're gonna go with second
1906
01:16:48,400 --> 01:16:49,440
again
1907
01:16:49,440 --> 01:16:51,440
if the file is not there
1908
01:16:51,440 --> 01:16:52,800
then of course
1909
01:16:52,800 --> 01:16:55,600
node will create one and if the file is
1910
01:16:55,600 --> 01:16:58,239
already there node by default
1911
01:16:58,239 --> 01:17:01,199
will overwrite all the values that are
1912
01:17:01,199 --> 01:17:03,199
in the file and i'll show you that in a
1913
01:17:03,199 --> 01:17:05,920
second so i'm going to go here with node
1914
01:17:05,920 --> 01:17:09,679
and app.js and check it out now i have
1915
01:17:09,679 --> 01:17:11,040
result
1916
01:17:11,040 --> 01:17:12,719
sync txt
1917
01:17:12,719 --> 01:17:14,400
and then inside there i have here is the
1918
01:17:14,400 --> 01:17:18,239
result hello this is first text file and
1919
01:17:18,239 --> 01:17:21,040
also have hello this is second text file
1920
01:17:21,040 --> 01:17:22,960
and just to showcase that if there are
1921
01:17:22,960 --> 01:17:25,840
some values they will be overwritten by
1922
01:17:25,840 --> 01:17:26,800
node
1923
01:17:26,800 --> 01:17:29,920
well if i go with simple hello world and
1924
01:17:29,920 --> 01:17:34,080
then i run node.js again i have my new
1925
01:17:34,080 --> 01:17:37,120
result now if you just want to upend to
1926
01:17:37,120 --> 01:17:38,239
the file
1927
01:17:38,239 --> 01:17:40,239
well we need to pass in one more
1928
01:17:40,239 --> 01:17:42,719
argument notice i have the first one i
1929
01:17:42,719 --> 01:17:45,040
have the value that i would like to set
1930
01:17:45,040 --> 01:17:47,840
up in a file and the third one is an
1931
01:17:47,840 --> 01:17:50,480
object basically an options object and
1932
01:17:50,480 --> 01:17:53,760
here the property name is flag and if
1933
01:17:53,760 --> 01:17:56,719
you'll set this flag equal to a
1934
01:17:56,719 --> 01:17:59,360
then node will append this and the
1935
01:17:59,360 --> 01:18:02,239
easiest way for me to show you that is
1936
01:18:02,239 --> 01:18:05,679
run node.js and now we should have two
1937
01:18:05,679 --> 01:18:08,000
results so we have here is the result
1938
01:18:08,000 --> 01:18:10,960
and notice how we're starting a new line
1939
01:18:10,960 --> 01:18:13,120
why well because by default we are
1940
01:18:13,120 --> 01:18:16,159
overriding everything and then when we
1941
01:18:16,159 --> 01:18:18,480
add that flag then of course we create a
1942
01:18:18,480 --> 01:18:21,040
new value so these are two very useful
1943
01:18:21,040 --> 01:18:23,840
methods that we can use with node to
1944
01:18:23,840 --> 01:18:26,400
read files as well as create ones from
1945
01:18:26,400 --> 01:18:27,520
the scratch
1946
01:18:27,520 --> 01:18:30,159
beautiful and once we have looked at the
1947
01:18:30,159 --> 01:18:32,560
synchronous approach like promised i
1948
01:18:32,560 --> 01:18:34,000
also would want to show you the
1949
01:18:34,000 --> 01:18:37,040
asynchronous and as always let's start
1950
01:18:37,040 --> 01:18:39,520
by creating a new file again in my case
1951
01:18:39,520 --> 01:18:42,400
that is going to be 10 hyphen and then
1952
01:18:42,400 --> 01:18:44,400
i'm going to go with fa
1953
01:18:44,400 --> 01:18:46,480
sync and js
1954
01:18:46,480 --> 01:18:48,960
and of course i'll grab everything in
1955
01:18:48,960 --> 01:18:51,920
the app.js and then just copy and paste
1956
01:18:51,920 --> 01:18:54,080
and then back in the app.js i would want
1957
01:18:54,080 --> 01:18:56,800
to change these names so i'm not looking
1958
01:18:56,800 --> 01:18:59,600
anymore for the read file sync i'm
1959
01:18:59,600 --> 01:19:01,840
actually looking for a method by the
1960
01:19:01,840 --> 01:19:03,920
name of read file so this is going to be
1961
01:19:03,920 --> 01:19:06,719
the asynchronous one and the same goes
1962
01:19:06,719 --> 01:19:08,719
with writefile now of course we can
1963
01:19:08,719 --> 01:19:12,239
remove all this code as well
1964
01:19:12,239 --> 01:19:15,280
and the way the synchronous method works
1965
01:19:15,280 --> 01:19:17,760
we need to provide a callback and if
1966
01:19:17,760 --> 01:19:18,880
you're familiar with callback
1967
01:19:18,880 --> 01:19:22,719
essentially what it means is that we run
1968
01:19:22,719 --> 01:19:25,679
that callback when we're done
1969
01:19:25,679 --> 01:19:26,480
so
1970
01:19:26,480 --> 01:19:29,360
when whatever functionality we're doing
1971
01:19:29,360 --> 01:19:30,640
is complete
1972
01:19:30,640 --> 01:19:33,040
then we run that callback just like we
1973
01:19:33,040 --> 01:19:35,440
normally would do with add event
1974
01:19:35,440 --> 01:19:37,840
listener on a button remember we go with
1975
01:19:37,840 --> 01:19:40,000
add event listener we add whatever the
1976
01:19:40,000 --> 01:19:40,880
event
1977
01:19:40,880 --> 01:19:42,159
of course with the button that would be
1978
01:19:42,159 --> 01:19:44,640
a click event and then pass in the
1979
01:19:44,640 --> 01:19:46,239
callback function and that callback
1980
01:19:46,239 --> 01:19:48,880
function will run every time we click on
1981
01:19:48,880 --> 01:19:52,000
a button so this is the same thing where
1982
01:19:52,000 --> 01:19:55,040
we have the read file function
1983
01:19:55,040 --> 01:19:57,440
and we can simply use it since of course
1984
01:19:57,440 --> 01:19:59,520
we have the structured then we need to
1985
01:19:59,520 --> 01:20:02,239
provide a path so again we go with
1986
01:20:02,239 --> 01:20:05,199
forward slash content and then the file
1987
01:20:05,199 --> 01:20:08,560
name is first txt and this is where we
1988
01:20:08,560 --> 01:20:12,560
provide that second argument and that is
1989
01:20:12,560 --> 01:20:14,800
going to be a callback mission now in
1990
01:20:14,800 --> 01:20:16,960
that callback function we pass into more
1991
01:20:16,960 --> 01:20:19,120
arguments first one is going to be error
1992
01:20:19,120 --> 01:20:22,239
if there is one as well as the result
1993
01:20:22,239 --> 01:20:24,560
and then inside of the body first i
1994
01:20:24,560 --> 01:20:26,719
would want to check if there is an error
1995
01:20:26,719 --> 01:20:29,679
and then i can just return null as well
1996
01:20:29,679 --> 01:20:32,480
as maybe console.log so we go with if
1997
01:20:32,480 --> 01:20:35,520
error and then we'll say return
1998
01:20:35,520 --> 01:20:37,600
null or simply return whatever you'd
1999
01:20:37,600 --> 01:20:39,600
want and if you want you of course can
2000
01:20:39,600 --> 01:20:43,120
cancel log the error as well like so and
2001
01:20:43,120 --> 01:20:46,639
then if everything is correct well then
2002
01:20:46,639 --> 01:20:49,280
of course i'll have access to my data
2003
01:20:49,280 --> 01:20:51,440
which of course has the name the
2004
01:20:51,440 --> 01:20:54,400
parameter name of result and since i
2005
01:20:54,400 --> 01:20:56,159
would simply want to console log the
2006
01:20:56,159 --> 01:20:58,719
result i'm going to go with console.log
2007
01:20:58,719 --> 01:21:00,880
and the result but of course keep in
2008
01:21:00,880 --> 01:21:04,000
mind that this is where you would set up
2009
01:21:04,000 --> 01:21:06,719
the functionality so let's go back and
2010
01:21:06,719 --> 01:21:09,920
we're going to go with node app.js
2011
01:21:09,920 --> 01:21:11,920
and in this case something really
2012
01:21:11,920 --> 01:21:15,679
interesting where we don't get the text
2013
01:21:15,679 --> 01:21:18,000
right so we're getting this buffer so
2014
01:21:18,000 --> 01:21:20,159
this is what's going to happen where if
2015
01:21:20,159 --> 01:21:23,440
we don't provide that utf coding then of
2016
01:21:23,440 --> 01:21:25,920
course we'll get the buffer now where do
2017
01:21:25,920 --> 01:21:28,080
we add this encoding value well right
2018
01:21:28,080 --> 01:21:31,280
here in between in between the content
2019
01:21:31,280 --> 01:21:34,080
as well as the path so let's just go
2020
01:21:34,080 --> 01:21:37,120
with string and we're looking for the
2021
01:21:37,120 --> 01:21:40,639
utf so let's say utf-8
2022
01:21:40,639 --> 01:21:42,960
and we'll save that one and let's run it
2023
01:21:42,960 --> 01:21:45,280
one more time and of course i have node
2024
01:21:45,280 --> 01:21:49,040
app.js and then hello this is the first
2025
01:21:49,040 --> 01:21:50,239
text file
2026
01:21:50,239 --> 01:21:52,960
and now the million dollar question
2027
01:21:52,960 --> 01:21:54,480
well how we can set up the same
2028
01:21:54,480 --> 01:21:56,000
functionality
2029
01:21:56,000 --> 01:21:59,360
check it out we have read file read file
2030
01:21:59,360 --> 01:22:02,400
and then we simply create a new file
2031
01:22:02,400 --> 01:22:04,719
with both of these values so if you're
2032
01:22:04,719 --> 01:22:07,120
familiar with callbacks
2033
01:22:07,120 --> 01:22:09,199
you know that of course we would need to
2034
01:22:09,199 --> 01:22:10,960
set up that functionality
2035
01:22:10,960 --> 01:22:13,280
inside of this callback where i'm
2036
01:22:13,280 --> 01:22:14,960
getting this result
2037
01:22:14,960 --> 01:22:16,719
otherwise there's going to be an error
2038
01:22:16,719 --> 01:22:18,239
because the only way for me to access
2039
01:22:18,239 --> 01:22:19,679
this result
2040
01:22:19,679 --> 01:22:22,560
if i'm setting this as a callback is of
2041
01:22:22,560 --> 01:22:25,199
course inside of this function and this
2042
01:22:25,199 --> 01:22:28,080
is what i was saying before where yes
2043
01:22:28,080 --> 01:22:30,320
eventually i'll show you a different
2044
01:22:30,320 --> 01:22:33,520
approach because inevitably we'll set up
2045
01:22:33,520 --> 01:22:36,080
a callback help and you'll see that in a
2046
01:22:36,080 --> 01:22:38,239
second so what i would want to do right
2047
01:22:38,239 --> 01:22:40,480
now when i get back
2048
01:22:40,480 --> 01:22:43,280
my first text file well now of course i
2049
01:22:43,280 --> 01:22:44,719
would want to assign it to some kind of
2050
01:22:44,719 --> 01:22:45,840
variable
2051
01:22:45,840 --> 01:22:47,520
and then i would want to read that
2052
01:22:47,520 --> 01:22:50,000
second file right and then eventually
2053
01:22:50,000 --> 01:22:52,000
once i read the second file then of
2054
01:22:52,000 --> 01:22:54,000
course i would want to create that new
2055
01:22:54,000 --> 01:22:54,880
file
2056
01:22:54,880 --> 01:22:57,040
and the way it's going to look like
2057
01:22:57,040 --> 01:22:59,679
i'm going to go with const and i'll say
2058
01:22:59,679 --> 01:23:00,560
first
2059
01:23:00,560 --> 01:23:02,719
and you can name it first text i'm just
2060
01:23:02,719 --> 01:23:04,880
going to know it first and this will be
2061
01:23:04,880 --> 01:23:07,840
equal to my result okay awesome and then
2062
01:23:07,840 --> 01:23:09,920
right after that result again i'm going
2063
01:23:09,920 --> 01:23:12,480
to go with read file
2064
01:23:12,480 --> 01:23:14,239
and then the same thing
2065
01:23:14,239 --> 01:23:17,280
we provide a path so we go with content
2066
01:23:17,280 --> 01:23:18,800
and then in this case of course we're
2067
01:23:18,800 --> 01:23:22,800
looking for second and then txt then we
2068
01:23:22,800 --> 01:23:25,600
need to provide the encoding so node
2069
01:23:25,600 --> 01:23:28,080
knows how to decode that and we go with
2070
01:23:28,080 --> 01:23:30,239
utf-8 and then of course we need to
2071
01:23:30,239 --> 01:23:33,440
provide our callback again same deal we
2072
01:23:33,440 --> 01:23:34,960
go with error
2073
01:23:34,960 --> 01:23:37,840
and result and in order to speed this up
2074
01:23:37,840 --> 01:23:40,000
i will copy and paste so just grab
2075
01:23:40,000 --> 01:23:42,239
everything starting with if and ending
2076
01:23:42,239 --> 01:23:45,760
with the result copy and paste again
2077
01:23:45,760 --> 01:23:47,760
this is the case where we're checking
2078
01:23:47,760 --> 01:23:50,000
for the error first if there is an error
2079
01:23:50,000 --> 01:23:52,159
we cancel log it and if everything is
2080
01:23:52,159 --> 01:23:54,639
successful we assign this one to the
2081
01:23:54,639 --> 01:23:57,120
second one and then eventually now i
2082
01:23:57,120 --> 01:23:59,760
would want to set up my right file so
2083
01:23:59,760 --> 01:24:01,600
again it is a function
2084
01:24:01,600 --> 01:24:04,000
we need to provide that value and we
2085
01:24:04,000 --> 01:24:07,040
already have result sync txt that's why
2086
01:24:07,040 --> 01:24:09,600
i'll create a new one and i'll call this
2087
01:24:09,600 --> 01:24:12,560
result and async so again we're looking
2088
01:24:12,560 --> 01:24:14,560
for the content then we go with the
2089
01:24:14,560 --> 01:24:17,520
filename which is going to be result and
2090
01:24:17,520 --> 01:24:19,440
a sync
2091
01:24:19,440 --> 01:24:23,040
and txt of course since there is no file
2092
01:24:23,040 --> 01:24:25,360
node will create that for us i'll right
2093
01:24:25,360 --> 01:24:28,000
away set up my data now if you want of
2094
01:24:28,000 --> 01:24:30,000
course you can copy and paste this
2095
01:24:30,000 --> 01:24:32,480
functionality this is totally up to you
2096
01:24:32,480 --> 01:24:34,960
where basically we use two of them and
2097
01:24:34,960 --> 01:24:36,560
in my case this is exactly what i'm
2098
01:24:36,560 --> 01:24:38,320
going to do in order to speed this up if
2099
01:24:38,320 --> 01:24:39,760
you want to create something from
2100
01:24:39,760 --> 01:24:42,320
scratch of course that is also an option
2101
01:24:42,320 --> 01:24:44,000
and i'm just going to copy and paste
2102
01:24:44,000 --> 01:24:46,800
that's my template string and again if
2103
01:24:46,800 --> 01:24:48,880
you want to add this flag
2104
01:24:48,880 --> 01:24:51,760
you can definitely do so in my case i'm
2105
01:24:51,760 --> 01:24:53,600
just going to omit that so here's the
2106
01:24:53,600 --> 01:24:55,040
result i have
2107
01:24:55,040 --> 01:24:56,560
first and second
2108
01:24:56,560 --> 01:24:58,960
and the reason why i have access to them
2109
01:24:58,960 --> 01:25:01,600
because of course i'm looking
2110
01:25:01,600 --> 01:25:04,800
in the outside scope of this callback
2111
01:25:04,800 --> 01:25:07,920
function and then again we have a
2112
01:25:07,920 --> 01:25:10,320
callback function in the right file i
2113
01:25:10,320 --> 01:25:12,159
know there's tons of callback functions
2114
01:25:12,159 --> 01:25:14,880
that's why we'll have to set up a better
2115
01:25:14,880 --> 01:25:16,880
setup when we're handling these
2116
01:25:16,880 --> 01:25:18,880
callbacks but we'll do that a little bit
2117
01:25:18,880 --> 01:25:21,679
later so for now let's just add a
2118
01:25:21,679 --> 01:25:23,520
comment here where we have first
2119
01:25:23,520 --> 01:25:25,679
parameter second parameter and then
2120
01:25:25,679 --> 01:25:27,840
third parameter and of course this is
2121
01:25:27,840 --> 01:25:29,760
going to be again our callback where we
2122
01:25:29,760 --> 01:25:30,880
have error
2123
01:25:30,880 --> 01:25:32,960
and result
2124
01:25:32,960 --> 01:25:36,000
and then inside of this callback finally
2125
01:25:36,000 --> 01:25:38,239
we can take a look at the value and of
2126
01:25:38,239 --> 01:25:40,880
course the value will be in this result
2127
01:25:40,880 --> 01:25:42,320
now of course we're creating the file
2128
01:25:42,320 --> 01:25:44,400
and all that but hopefully you get the
2129
01:25:44,400 --> 01:25:46,719
gist that if you'd be doing something
2130
01:25:46,719 --> 01:25:48,400
where you're looking for this result of
2131
01:25:48,400 --> 01:25:51,040
course it would be over here so again
2132
01:25:51,040 --> 01:25:53,280
let's check for error quickly if there
2133
01:25:53,280 --> 01:25:55,760
is an error then of course i would want
2134
01:25:55,760 --> 01:25:56,960
to return
2135
01:25:56,960 --> 01:25:59,600
as well as console log the error
2136
01:25:59,600 --> 01:26:01,920
just in case and if everything is
2137
01:26:01,920 --> 01:26:04,320
correct let me just console log the
2138
01:26:04,320 --> 01:26:06,560
result so save it here
2139
01:26:06,560 --> 01:26:07,520
and
2140
01:26:07,520 --> 01:26:09,679
two things we need to watch out for
2141
01:26:09,679 --> 01:26:12,080
maybe there's an error and second one we
2142
01:26:12,080 --> 01:26:14,239
should create a new file by the name
2143
01:26:14,239 --> 01:26:17,920
result and then async txt let me clear
2144
01:26:17,920 --> 01:26:21,120
the console and we're going to run node
2145
01:26:21,120 --> 01:26:24,880
app.js and i do get an error and error
2146
01:26:24,880 --> 01:26:27,600
simply tells me that i did not add the
2147
01:26:27,600 --> 01:26:30,480
dot in front of the content so i would
2148
01:26:30,480 --> 01:26:33,679
need to go back to write file and just
2149
01:26:33,679 --> 01:26:36,560
add that and once we add the dot we are
2150
01:26:36,560 --> 01:26:39,199
going to be in good place where i'll
2151
01:26:39,199 --> 01:26:41,840
clear the console and i'll run one more
2152
01:26:41,840 --> 01:26:45,040
time node app.js and then the result
2153
01:26:45,040 --> 01:26:48,000
will be undefined okay that's fine like
2154
01:26:48,000 --> 01:26:50,320
i said in this case we're not expecting
2155
01:26:50,320 --> 01:26:53,120
anything back but what's really cool
2156
01:26:53,120 --> 01:26:56,800
that in the result async we should have
2157
01:26:56,800 --> 01:26:58,639
our text here is the result hello this
2158
01:26:58,639 --> 01:27:01,600
is first text and then the second text
2159
01:27:01,600 --> 01:27:03,280
so hopefully it is clear
2160
01:27:03,280 --> 01:27:06,000
this is how we would use the async
2161
01:27:06,000 --> 01:27:08,400
pattern so let me zoom out right now
2162
01:27:08,400 --> 01:27:10,719
here and you'll see this
2163
01:27:10,719 --> 01:27:13,920
famous callback hell and in the next
2164
01:27:13,920 --> 01:27:16,880
video i would want to give you a
2165
01:27:16,880 --> 01:27:19,120
brief description of what are the
2166
01:27:19,120 --> 01:27:21,600
differences between the asynchronous
2167
01:27:21,600 --> 01:27:24,960
approach or non-blocking approach and
2168
01:27:24,960 --> 01:27:27,120
the blocking one the synchronous one as
2169
01:27:27,120 --> 01:27:30,239
well as what are the alternatives to
2170
01:27:30,239 --> 01:27:32,960
this callback l all right and just to
2171
01:27:32,960 --> 01:27:34,880
give you an early taste of the
2172
01:27:34,880 --> 01:27:38,320
differences between the asynchronous or
2173
01:27:38,320 --> 01:27:41,280
non-blocking and synchronous which is a
2174
01:27:41,280 --> 01:27:44,000
blocking code why don't we set up few
2175
01:27:44,000 --> 01:27:46,560
console logs now before we do that i
2176
01:27:46,560 --> 01:27:48,560
will create a new file
2177
01:27:48,560 --> 01:27:51,040
and i'll say 11 here and by the way i
2178
01:27:51,040 --> 01:27:52,880
definitely need to zoom in
2179
01:27:52,880 --> 01:27:54,080
just so you don't think that i'm
2180
01:27:54,080 --> 01:27:55,840
cheating fs
2181
01:27:55,840 --> 01:27:58,800
and async js
2182
01:27:58,800 --> 01:28:01,679
and let's go to the address we'll select
2183
01:28:01,679 --> 01:28:05,120
everything and copy and paste and of
2184
01:28:05,120 --> 01:28:07,679
course in next video we'll set up
2185
01:28:07,679 --> 01:28:10,639
different module in this appdress so for
2186
01:28:10,639 --> 01:28:13,120
time being i can just remove it
2187
01:28:13,120 --> 01:28:15,040
and then like i promised we'll set up
2188
01:28:15,040 --> 01:28:16,800
some console logs so why don't we go
2189
01:28:16,800 --> 01:28:20,000
back to fs and by the way during this
2190
01:28:20,000 --> 01:28:22,880
video i'll zoom out because i definitely
2191
01:28:22,880 --> 01:28:25,199
wanna go back to this
2192
01:28:25,199 --> 01:28:28,560
evil evil callback hell that we're
2193
01:28:28,560 --> 01:28:31,440
getting from nesting callback within the
2194
01:28:31,440 --> 01:28:34,960
callback and as far as fa sync
2195
01:28:34,960 --> 01:28:36,320
let me zoom in
2196
01:28:36,320 --> 01:28:39,840
and why don't we set up a console log in
2197
01:28:39,840 --> 01:28:41,040
few places
2198
01:28:41,040 --> 01:28:43,360
let's say before we're accessing the
2199
01:28:43,360 --> 01:28:44,320
file
2200
01:28:44,320 --> 01:28:46,880
so i'm gonna go with log and we'll say
2201
01:28:46,880 --> 01:28:48,080
start
2202
01:28:48,080 --> 01:28:51,600
then we're gonna go with write file sync
2203
01:28:51,600 --> 01:28:53,440
and then right after that we're gonna
2204
01:28:53,440 --> 01:28:55,520
say i'm done with the task and then
2205
01:28:55,520 --> 01:28:58,800
inside there let's write done with this
2206
01:28:58,800 --> 01:29:01,520
task copy and paste
2207
01:29:01,520 --> 01:29:04,000
and we're gonna go with starting the
2208
01:29:04,000 --> 01:29:05,120
next one
2209
01:29:05,120 --> 01:29:06,880
starting
2210
01:29:06,880 --> 01:29:08,560
the next one
2211
01:29:08,560 --> 01:29:10,639
meaning the next task
2212
01:29:10,639 --> 01:29:12,560
so that should do it and of course i
2213
01:29:12,560 --> 01:29:16,000
need to go with clear so clear my
2214
01:29:16,000 --> 01:29:19,040
console and i'm gonna go to node and
2215
01:29:19,040 --> 01:29:21,760
then the file name is 10
2216
01:29:21,760 --> 01:29:25,280
iphone and then fs and we're looking for
2217
01:29:25,280 --> 01:29:26,159
sync
2218
01:29:26,159 --> 01:29:28,719
i run it and notice something
2219
01:29:28,719 --> 01:29:31,199
interesting where i have the start
2220
01:29:31,199 --> 01:29:33,520
so that is going to be before i read the
2221
01:29:33,520 --> 01:29:37,040
files and only then once i'm done
2222
01:29:37,040 --> 01:29:38,400
writing that file
2223
01:29:38,400 --> 01:29:41,600
i have done with this task and starting
2224
01:29:41,600 --> 01:29:42,880
a next one
2225
01:29:42,880 --> 01:29:45,199
and what we need to imagine that of
2226
01:29:45,199 --> 01:29:47,280
course this task
2227
01:29:47,280 --> 01:29:49,920
of getting those files as well as the
2228
01:29:49,920 --> 01:29:53,040
second one of setting up that file well
2229
01:29:53,040 --> 01:29:55,520
it might be very time consuming right
2230
01:29:55,520 --> 01:29:59,040
and imagine that if we have even 10
2231
01:29:59,040 --> 01:30:00,880
users and they're all the time using our
2232
01:30:00,880 --> 01:30:01,920
application
2233
01:30:01,920 --> 01:30:06,000
and if for example this one user does
2234
01:30:06,000 --> 01:30:09,040
this one task or both of them
2235
01:30:09,040 --> 01:30:10,960
and they take really long time what
2236
01:30:10,960 --> 01:30:12,000
means that
2237
01:30:12,000 --> 01:30:14,159
node is not going to be able to serve
2238
01:30:14,159 --> 01:30:16,480
other users and trust me it is at the
2239
01:30:16,480 --> 01:30:19,120
end of the really really big deal and
2240
01:30:19,120 --> 01:30:21,440
notice how javascript is reading this
2241
01:30:21,440 --> 01:30:23,440
code synchronously
2242
01:30:23,440 --> 01:30:25,520
it just goes line by line so we start
2243
01:30:25,520 --> 01:30:28,239
with this task then we read these files
2244
01:30:28,239 --> 01:30:31,040
and then we write the result and again
2245
01:30:31,040 --> 01:30:33,120
if this is something that takes really
2246
01:30:33,120 --> 01:30:35,520
long time then pretty much your
2247
01:30:35,520 --> 01:30:37,120
application is down
2248
01:30:37,120 --> 01:30:39,040
so no other user can do anything with
2249
01:30:39,040 --> 01:30:40,880
that application because one of the
2250
01:30:40,880 --> 01:30:43,280
users is for example reading these two
2251
01:30:43,280 --> 01:30:46,639
files and then writing one again this is
2252
01:30:46,639 --> 01:30:49,360
a very simple example but you need to
2253
01:30:49,360 --> 01:30:51,040
picture that of course there's going to
2254
01:30:51,040 --> 01:30:54,400
be tasks that take longer time
2255
01:30:54,400 --> 01:30:56,800
and as an alternative let's take a look
2256
01:30:56,800 --> 01:30:58,719
at the async approach
2257
01:30:58,719 --> 01:31:01,040
so here again we'll set up some simple
2258
01:31:01,040 --> 01:31:02,960
console logs and again we'll cover this
2259
01:31:02,960 --> 01:31:05,600
in a bigger detail a bit later for time
2260
01:31:05,600 --> 01:31:07,679
being let's just set up console logs and
2261
01:31:07,679 --> 01:31:09,600
you'll see the biggest difference
2262
01:31:09,600 --> 01:31:12,320
where i'm gonna go with log
2263
01:31:12,320 --> 01:31:14,560
and let's say start so i'm gonna do that
2264
01:31:14,560 --> 01:31:16,800
before i read this file
2265
01:31:16,800 --> 01:31:19,840
and then i would want to go inside of
2266
01:31:19,840 --> 01:31:21,760
the right file
2267
01:31:21,760 --> 01:31:23,840
the last one and instead of console
2268
01:31:23,840 --> 01:31:25,760
logging result which of course is
2269
01:31:25,760 --> 01:31:28,080
undefined since in this case we are not
2270
01:31:28,080 --> 01:31:30,560
getting anything back now i want to go
2271
01:31:30,560 --> 01:31:32,400
with done
2272
01:31:32,400 --> 01:31:34,159
with this
2273
01:31:34,159 --> 01:31:38,239
task and then right after my first read
2274
01:31:38,239 --> 01:31:41,040
file i'm going to go with log
2275
01:31:41,040 --> 01:31:42,639
and starting
2276
01:31:42,639 --> 01:31:44,400
next task
2277
01:31:44,400 --> 01:31:46,080
hopefully you're able to follow along
2278
01:31:46,080 --> 01:31:48,960
again before the read file inside of the
2279
01:31:48,960 --> 01:31:51,760
right file and then right after the
2280
01:31:51,760 --> 01:31:54,320
first read file because of course the
2281
01:31:54,320 --> 01:31:56,480
other ones are nested inside of this
2282
01:31:56,480 --> 01:31:59,760
callback and then if we go to node and
2283
01:31:59,760 --> 01:32:03,280
if we just change it around to 11
2284
01:32:03,280 --> 01:32:07,040
and then fa and i believe it was async
2285
01:32:07,040 --> 01:32:08,800
and if we run it
2286
01:32:08,800 --> 01:32:10,320
notice something cool
2287
01:32:10,320 --> 01:32:13,199
where i have my start
2288
01:32:13,199 --> 01:32:15,679
and then i right away have this starting
2289
01:32:15,679 --> 01:32:16,960
next task
2290
01:32:16,960 --> 01:32:19,679
and eventually i have this done with
2291
01:32:19,679 --> 01:32:22,320
this task so what this means
2292
01:32:22,320 --> 01:32:25,040
is that since we have this asynchronous
2293
01:32:25,040 --> 01:32:28,320
approach the moment we start this task
2294
01:32:28,320 --> 01:32:29,920
we just offload
2295
01:32:29,920 --> 01:32:32,080
we just offload this task and we
2296
01:32:32,080 --> 01:32:35,520
continue with our code so that way when
2297
01:32:35,520 --> 01:32:38,239
user number one for example comes here
2298
01:32:38,239 --> 01:32:41,600
and wants to get this functionality
2299
01:32:41,600 --> 01:32:44,639
yeah sure here node just offload this
2300
01:32:44,639 --> 01:32:47,520
task and then your application can keep
2301
01:32:47,520 --> 01:32:50,480
on serving other users again
2302
01:32:50,480 --> 01:32:52,320
it seems a bit trivial at the moment
2303
01:32:52,320 --> 01:32:54,800
because we have a basic approach but
2304
01:32:54,800 --> 01:32:57,440
hopefully you get an idea where in one
2305
01:32:57,440 --> 01:32:59,440
case we have everything happening line
2306
01:32:59,440 --> 01:33:01,760
by line so only when we're done with the
2307
01:33:01,760 --> 01:33:04,159
task we're starting a next one
2308
01:33:04,159 --> 01:33:07,440
and with this approach we start a task
2309
01:33:07,440 --> 01:33:09,679
and we just offload that task and then
2310
01:33:09,679 --> 01:33:12,800
we start a next task right away and
2311
01:33:12,800 --> 01:33:15,679
eventually yes we have done with this
2312
01:33:15,679 --> 01:33:18,080
task in the console as well
2313
01:33:18,080 --> 01:33:20,400
now as far as the implementation as far
2314
01:33:20,400 --> 01:33:22,320
as the callbacks
2315
01:33:22,320 --> 01:33:24,880
yes this is a bit messy right notice how
2316
01:33:24,880 --> 01:33:28,239
we only want to accomplish few tasks and
2317
01:33:28,239 --> 01:33:31,199
it's already somewhat off a pain now
2318
01:33:31,199 --> 01:33:34,480
alternatives are using promises
2319
01:33:34,480 --> 01:33:37,840
as well as async await now again
2320
01:33:37,840 --> 01:33:39,840
hopefully you understand that when we
2321
01:33:39,840 --> 01:33:42,400
talk about reading the file as well as
2322
01:33:42,400 --> 01:33:43,920
the writing or file
2323
01:33:43,920 --> 01:33:47,360
i use this as an example of some kind of
2324
01:33:47,360 --> 01:33:50,639
task that would take a long time now for
2325
01:33:50,639 --> 01:33:52,239
the remainder of the course mostly it's
2326
01:33:52,239 --> 01:33:54,639
going to be reading from the database
2327
01:33:54,639 --> 01:33:57,440
writing to the database and that sort of
2328
01:33:57,440 --> 01:34:00,080
thing but this is a very good
2329
01:34:00,080 --> 01:34:02,880
illustration of how some tasks would
2330
01:34:02,880 --> 01:34:04,880
take some time so of course we would
2331
01:34:04,880 --> 01:34:07,120
need to use a callback and as far as the
2332
01:34:07,120 --> 01:34:09,600
callbacks yes you can use them but the
2333
01:34:09,600 --> 01:34:12,159
code is going to get messy somewhat
2334
01:34:12,159 --> 01:34:14,480
quickly that's why a bit later in the
2335
01:34:14,480 --> 01:34:15,920
course we'll take a look at the
2336
01:34:15,920 --> 01:34:18,400
alternatives which are promises
2337
01:34:18,400 --> 01:34:21,520
and a sync await and i have to tell you
2338
01:34:21,520 --> 01:34:23,360
honestly for the remainder of the course
2339
01:34:23,360 --> 01:34:26,719
we'll use async await because in my
2340
01:34:26,719 --> 01:34:30,000
opinion it is just easier to work with
2341
01:34:30,000 --> 01:34:32,639
excellent and up next we have http
2342
01:34:32,639 --> 01:34:33,679
module
2343
01:34:33,679 --> 01:34:35,679
but before we continue let me just
2344
01:34:35,679 --> 01:34:38,880
mention that i purposely won't spend too
2345
01:34:38,880 --> 01:34:41,760
much time on explaining every single
2346
01:34:41,760 --> 01:34:45,119
http method we're going to use in great
2347
01:34:45,119 --> 01:34:47,600
detail and here's why
2348
01:34:47,600 --> 01:34:50,159
you see starting from next section
2349
01:34:50,159 --> 01:34:53,199
all of our work meaning code in some
2350
01:34:53,199 --> 01:34:54,639
shape or form
2351
01:34:54,639 --> 01:34:57,920
will revolve around server setup
2352
01:34:57,920 --> 01:35:02,400
aka http module so that point of course
2353
01:35:02,400 --> 01:35:05,360
i will cover every last bit of syntax in
2354
01:35:05,360 --> 01:35:06,480
great detail
2355
01:35:06,480 --> 01:35:08,719
at this point though i think it's just a
2356
01:35:08,719 --> 01:35:11,760
waste of our time so if you're confused
2357
01:35:11,760 --> 01:35:14,639
about something http related
2358
01:35:14,639 --> 01:35:16,800
don't sweat it we will return to that
2359
01:35:16,800 --> 01:35:19,520
subject shortly and cover it in far
2360
01:35:19,520 --> 01:35:21,199
greater detail
2361
01:35:21,199 --> 01:35:22,880
now if you're wondering
2362
01:35:22,880 --> 01:35:25,920
why we bother with http module at this
2363
01:35:25,920 --> 01:35:27,280
point at all
2364
01:35:27,280 --> 01:35:30,000
since we're going to use it later anyway
2365
01:35:30,000 --> 01:35:33,440
you see with the help of http module
2366
01:35:33,440 --> 01:35:35,520
i believe i can show you some core
2367
01:35:35,520 --> 01:35:37,760
features of node in action
2368
01:35:37,760 --> 01:35:39,840
and give you somewhat of a
2369
01:35:39,840 --> 01:35:42,560
visual representation of them otherwise
2370
01:35:42,560 --> 01:35:44,480
i would have to base everything on
2371
01:35:44,480 --> 01:35:46,639
slides and i don't think it's the best
2372
01:35:46,639 --> 01:35:47,760
approach
2373
01:35:47,760 --> 01:35:49,600
so long story short
2374
01:35:49,600 --> 01:35:52,639
if you're iffy about something http
2375
01:35:52,639 --> 01:35:53,760
related
2376
01:35:53,760 --> 01:35:56,719
just please be patient and i'll answer
2377
01:35:56,719 --> 01:35:58,080
all your questions
2378
01:35:58,080 --> 01:36:00,719
in the next course section
2379
01:36:00,719 --> 01:36:03,440
nice i think we've got a good initial
2380
01:36:03,440 --> 01:36:05,360
understanding of how the
2381
01:36:05,360 --> 01:36:08,480
file system module works in node and
2382
01:36:08,480 --> 01:36:11,520
hopefully i did not lose too many of you
2383
01:36:11,520 --> 01:36:12,960
so hopefully there are still some
2384
01:36:12,960 --> 01:36:14,560
students left in the course because i
2385
01:36:14,560 --> 01:36:16,080
fully understand that seeing this for
2386
01:36:16,080 --> 01:36:17,440
the first time
2387
01:36:17,440 --> 01:36:21,040
is probably a bit overwhelming
2388
01:36:21,040 --> 01:36:23,520
and what i would want to do next is
2389
01:36:23,520 --> 01:36:26,320
cover the last initial module the
2390
01:36:26,320 --> 01:36:28,639
built-in module and that is going to be
2391
01:36:28,639 --> 01:36:31,840
the http module which is going to allow
2392
01:36:31,840 --> 01:36:33,280
us to
2393
01:36:33,280 --> 01:36:36,639
set up a web server now please keep in
2394
01:36:36,639 --> 01:36:38,320
mind one thing we'll just cover the
2395
01:36:38,320 --> 01:36:40,639
basics because the large part of this
2396
01:36:40,639 --> 01:36:43,199
course is going to be creating your own
2397
01:36:43,199 --> 01:36:46,239
server and setting up the api now
2398
01:36:46,239 --> 01:36:49,920
granted we will use an abstraction on
2399
01:36:49,920 --> 01:36:52,480
top of the http module and that is going
2400
01:36:52,480 --> 01:36:55,520
to be the package by the name of express
2401
01:36:55,520 --> 01:36:56,880
but the whole point is going to be
2402
01:36:56,880 --> 01:36:58,960
exactly the same that's why again we're
2403
01:36:58,960 --> 01:37:00,880
not going to cover everything
2404
01:37:00,880 --> 01:37:03,600
in greatest detail at the moment because
2405
01:37:03,600 --> 01:37:05,600
i don't see the point of repeating
2406
01:37:05,600 --> 01:37:06,719
myself
2407
01:37:06,719 --> 01:37:09,119
and now i want to go back to objects
2408
01:37:09,119 --> 01:37:11,360
i'll zoom back in
2409
01:37:11,360 --> 01:37:13,440
just so you can see better what is
2410
01:37:13,440 --> 01:37:15,840
happening by the way let me check my
2411
01:37:15,840 --> 01:37:18,960
zoom level so i'm going with settings
2412
01:37:18,960 --> 01:37:21,119
and i think that should do it my zoom
2413
01:37:21,119 --> 01:37:24,080
level is two at the moment and then back
2414
01:37:24,080 --> 01:37:25,679
in the app.js
2415
01:37:25,679 --> 01:37:28,639
let's grab the http module and we'll
2416
01:37:28,639 --> 01:37:30,159
assign it to some kind of variable as
2417
01:37:30,159 --> 01:37:31,119
always
2418
01:37:31,119 --> 01:37:33,920
let's go with require and we're looking
2419
01:37:33,920 --> 01:37:37,119
for http that's the module name and the
2420
01:37:37,119 --> 01:37:39,440
method we would want to use is create
2421
01:37:39,440 --> 01:37:41,360
server again we'll assign this to some
2422
01:37:41,360 --> 01:37:42,960
kind of variable and in this case the
2423
01:37:42,960 --> 01:37:45,360
variable name will be server then we go
2424
01:37:45,360 --> 01:37:46,800
with http
2425
01:37:46,800 --> 01:37:49,840
create server and this create server
2426
01:37:49,840 --> 01:37:52,880
method is looking for the callback and
2427
01:37:52,880 --> 01:37:54,960
in that callback function
2428
01:37:54,960 --> 01:37:57,280
we have two parameters and both of those
2429
01:37:57,280 --> 01:38:00,719
parameters are objects and since they're
2430
01:38:00,719 --> 01:38:02,400
parameters you can call it whatever you
2431
01:38:02,400 --> 01:38:05,040
would want but a common practice is
2432
01:38:05,040 --> 01:38:08,480
calling them rec and res now this first
2433
01:38:08,480 --> 01:38:11,119
parameter represents the incoming
2434
01:38:11,119 --> 01:38:14,080
request so imagine a client is
2435
01:38:14,080 --> 01:38:16,719
requesting from the web browser your
2436
01:38:16,719 --> 01:38:20,400
webpage so you'll have information about
2437
01:38:20,400 --> 01:38:23,520
the method and all kinds of useful stuff
2438
01:38:23,520 --> 01:38:26,560
in that request object and the response
2439
01:38:26,560 --> 01:38:28,320
is what we're sending back
2440
01:38:28,320 --> 01:38:30,239
now we're not done of course we'll set
2441
01:38:30,239 --> 01:38:32,320
up more code here but before we do
2442
01:38:32,320 --> 01:38:34,560
anything we also would need to
2443
01:38:34,560 --> 01:38:37,119
specifically say what port our server is
2444
01:38:37,119 --> 01:38:38,880
going to be listening to and that's why
2445
01:38:38,880 --> 01:38:40,800
we'll go with server
2446
01:38:40,800 --> 01:38:44,000
and listen so that's another method and
2447
01:38:44,000 --> 01:38:46,880
again this is arbitrary but i'm gonna go
2448
01:38:46,880 --> 01:38:49,760
with 5000. again
2449
01:38:49,760 --> 01:38:51,679
if this looks like a lot of gibberish
2450
01:38:51,679 --> 01:38:54,080
where we have the request response as
2451
01:38:54,080 --> 01:38:56,560
well as the ports don't worry
2452
01:38:56,560 --> 01:38:58,719
later in the course i'll cover that in
2453
01:38:58,719 --> 01:39:01,600
more detail as well as show you some
2454
01:39:01,600 --> 01:39:04,800
useful slides and all that good stuff
2455
01:39:04,800 --> 01:39:07,600
and as far as the response well
2456
01:39:07,600 --> 01:39:10,719
we have few methods we have write so we
2457
01:39:10,719 --> 01:39:12,880
go with arrest and again this is the
2458
01:39:12,880 --> 01:39:15,040
object and notice here how i'm not using
2459
01:39:15,040 --> 01:39:17,119
the rack yet so i'm not checking for
2460
01:39:17,119 --> 01:39:19,119
anything i'm just concerned about my
2461
01:39:19,119 --> 01:39:22,400
response and the method name is right
2462
01:39:22,400 --> 01:39:24,480
and inside the right we just come up
2463
01:39:24,480 --> 01:39:26,880
with some kind of text and i'll say
2464
01:39:26,880 --> 01:39:28,320
welcome
2465
01:39:28,320 --> 01:39:29,920
to
2466
01:39:29,920 --> 01:39:32,080
our homepage
2467
01:39:32,080 --> 01:39:34,400
homepage and once we have sent back the
2468
01:39:34,400 --> 01:39:37,440
response i also would wanna end my
2469
01:39:37,440 --> 01:39:41,119
request so i'm gonna go with res.end
2470
01:39:41,119 --> 01:39:42,960
and once we're done setting up the code
2471
01:39:42,960 --> 01:39:45,040
inside of the callback function now of
2472
01:39:45,040 --> 01:39:46,719
course i would want to open up my
2473
01:39:46,719 --> 01:39:47,920
terminal
2474
01:39:47,920 --> 01:39:50,639
clear everything and we would need to
2475
01:39:50,639 --> 01:39:52,960
spin the sucker up and of course the way
2476
01:39:52,960 --> 01:39:55,920
we do that is node and then objects
2477
01:39:55,920 --> 01:39:57,360
since that is the file name make sure
2478
01:39:57,360 --> 01:40:00,159
that you save it though so before i run
2479
01:40:00,159 --> 01:40:02,560
i'll save it and then notice something
2480
01:40:02,560 --> 01:40:05,600
interesting notice how we're not exiting
2481
01:40:05,600 --> 01:40:09,360
so previously every time we ran the file
2482
01:40:09,360 --> 01:40:11,760
every time we ran node and then whatever
2483
01:40:11,760 --> 01:40:14,000
the file name we exited from the file
2484
01:40:14,000 --> 01:40:16,320
right so node was done executing the
2485
01:40:16,320 --> 01:40:19,360
code and we just moved on now in this
2486
01:40:19,360 --> 01:40:22,560
case since we have a web server what do
2487
01:40:22,560 --> 01:40:24,320
web servers do
2488
01:40:24,320 --> 01:40:26,960
they keep on listening for requests and
2489
01:40:26,960 --> 01:40:29,600
actually you want your server to always
2490
01:40:29,600 --> 01:40:32,639
be up and of course if i go to my web
2491
01:40:32,639 --> 01:40:33,679
browser
2492
01:40:33,679 --> 01:40:35,040
and then
2493
01:40:35,040 --> 01:40:36,400
i'll make this bigger
2494
01:40:36,400 --> 01:40:38,480
and if we go with localhost
2495
01:40:38,480 --> 01:40:42,239
and of course in this case we have 5000
2496
01:40:42,239 --> 01:40:45,360
there it is we have welcome to our
2497
01:40:45,360 --> 01:40:48,239
homepage so now of course we have
2498
01:40:48,239 --> 01:40:51,119
effectively created our first website
2499
01:40:51,119 --> 01:40:54,480
now granted it's very basic but at least
2500
01:40:54,480 --> 01:40:57,119
we have something on screen
2501
01:40:57,119 --> 01:40:59,440
and before we discuss the external
2502
01:40:59,440 --> 01:41:00,560
modules
2503
01:41:00,560 --> 01:41:03,600
and actually set up a module that nicely
2504
01:41:03,600 --> 01:41:06,719
watches our code and does the restarts
2505
01:41:06,719 --> 01:41:07,760
for us
2506
01:41:07,760 --> 01:41:10,719
i also want to showcase what we have in
2507
01:41:10,719 --> 01:41:12,880
the request object now this is going to
2508
01:41:12,880 --> 01:41:14,800
be a giant object
2509
01:41:14,800 --> 01:41:16,400
so i'm not going to cover
2510
01:41:16,400 --> 01:41:19,199
each and every property however i would
2511
01:41:19,199 --> 01:41:22,159
want to showcase a property that gets us
2512
01:41:22,159 --> 01:41:24,639
the url and what would be the url well
2513
01:41:24,639 --> 01:41:27,119
that would be an address and if you're
2514
01:41:27,119 --> 01:41:30,080
worked with reactor or dom you know that
2515
01:41:30,080 --> 01:41:32,159
for example for the homepage we used
2516
01:41:32,159 --> 01:41:35,199
forward slash and then if the client
2517
01:41:35,199 --> 01:41:38,320
wants to go to for example about page he
2518
01:41:38,320 --> 01:41:40,320
or she would enter forward slash and
2519
01:41:40,320 --> 01:41:42,639
then the about now in our case notice
2520
01:41:42,639 --> 01:41:45,760
how we're still serving the same content
2521
01:41:45,760 --> 01:41:49,280
so just to give you a quick introduction
2522
01:41:49,280 --> 01:41:52,320
of the request object as well as set up
2523
01:41:52,320 --> 01:41:54,880
a bit more realistic application we're
2524
01:41:54,880 --> 01:41:56,800
going to go with the request now again
2525
01:41:56,800 --> 01:41:59,600
if you want you can log it so we're
2526
01:41:59,600 --> 01:42:01,760
going to go here with request and you'll
2527
01:42:01,760 --> 01:42:04,480
see that it is a giant object now since
2528
01:42:04,480 --> 01:42:06,320
we made some changes we do need to stop
2529
01:42:06,320 --> 01:42:08,880
it and then we'll go with node object
2530
01:42:08,880 --> 01:42:10,480
but notice how we're not right away
2531
01:42:10,480 --> 01:42:13,040
console logging the request object why
2532
01:42:13,040 --> 01:42:15,199
well because server
2533
01:42:15,199 --> 01:42:18,560
is waiting for those requests so we need
2534
01:42:18,560 --> 01:42:20,880
to go back to our browser and then
2535
01:42:20,880 --> 01:42:22,800
refresh and then of course once we
2536
01:42:22,800 --> 01:42:24,719
navigate back like i said this is going
2537
01:42:24,719 --> 01:42:25,840
to be a
2538
01:42:25,840 --> 01:42:29,760
giant object which bunch of useful info
2539
01:42:29,760 --> 01:42:31,760
now what we're looking for
2540
01:42:31,760 --> 01:42:34,320
is the url property because that url
2541
01:42:34,320 --> 01:42:36,560
property is going to tell us well what
2542
01:42:36,560 --> 01:42:40,000
address client is requesting so what
2543
01:42:40,000 --> 01:42:42,639
endpoint the client is requesting and
2544
01:42:42,639 --> 01:42:45,119
like i said for the home page the common
2545
01:42:45,119 --> 01:42:48,239
practice is using simple forward slash
2546
01:42:48,239 --> 01:42:50,000
so in here we can set up the if
2547
01:42:50,000 --> 01:42:53,760
statement where i say if request url is
2548
01:42:53,760 --> 01:42:56,960
equal to the home page then of course
2549
01:42:56,960 --> 01:42:58,960
this is what i would like to send back
2550
01:42:58,960 --> 01:43:01,199
and just to speed this up i can tell you
2551
01:43:01,199 --> 01:43:03,040
that we can cheat a little bit where we
2552
01:43:03,040 --> 01:43:06,000
can just place everything in the end
2553
01:43:06,000 --> 01:43:08,239
and in my case i'll just cut out my
2554
01:43:08,239 --> 01:43:11,600
string so we have res dot end so now we
2555
01:43:11,600 --> 01:43:13,520
simply send the text
2556
01:43:13,520 --> 01:43:15,360
and now of course i also would want to
2557
01:43:15,360 --> 01:43:18,320
check for the about or contact or
2558
01:43:18,320 --> 01:43:21,199
whatever so in here let's go with if
2559
01:43:21,199 --> 01:43:23,199
and then request
2560
01:43:23,199 --> 01:43:26,400
and url and if that one is equal to
2561
01:43:26,400 --> 01:43:27,760
about
2562
01:43:27,760 --> 01:43:29,679
about then of course i can send
2563
01:43:29,679 --> 01:43:31,440
different text and as far as the text
2564
01:43:31,440 --> 01:43:33,920
i'm just going to say here is
2565
01:43:33,920 --> 01:43:35,280
our short
2566
01:43:35,280 --> 01:43:37,760
history and then lastly well what
2567
01:43:37,760 --> 01:43:40,719
happens if the user is trying to access
2568
01:43:40,719 --> 01:43:43,280
a resource that doesn't exist
2569
01:43:43,280 --> 01:43:44,639
on our server
2570
01:43:44,639 --> 01:43:46,719
so we'll have the about page we'll have
2571
01:43:46,719 --> 01:43:48,960
the home page what about if the user is
2572
01:43:48,960 --> 01:43:51,119
asking for a contact page that we don't
2573
01:43:51,119 --> 01:43:53,920
have well here we'll simply set up a
2574
01:43:53,920 --> 01:43:57,440
default response we'll say res that end
2575
01:43:57,440 --> 01:43:59,199
and just to make it interesting i'll set
2576
01:43:59,199 --> 01:44:01,360
up the template string and i'll show you
2577
01:44:01,360 --> 01:44:03,920
that if we really want to we can also
2578
01:44:03,920 --> 01:44:08,719
pass indirectly html and i'll say oops
2579
01:44:08,719 --> 01:44:12,080
an explanation point i'll close my html
2580
01:44:12,080 --> 01:44:13,840
again please understand this is just a
2581
01:44:13,840 --> 01:44:16,080
basic scenario of course that's not how
2582
01:44:16,080 --> 01:44:18,639
we'll be setting up our apis or nothing
2583
01:44:18,639 --> 01:44:21,280
like that but just to give you a quick
2584
01:44:21,280 --> 01:44:22,159
intro
2585
01:44:22,159 --> 01:44:24,080
we'll do it quick and dirty where
2586
01:44:24,080 --> 01:44:26,000
there's going to be a paragraph and
2587
01:44:26,000 --> 01:44:28,159
we'll say we can't
2588
01:44:28,159 --> 01:44:31,040
seem to find the page
2589
01:44:31,040 --> 01:44:33,600
that you're looking for page
2590
01:44:33,600 --> 01:44:35,600
you are looking for
2591
01:44:35,600 --> 01:44:37,920
looking for and at the very end i would
2592
01:44:37,920 --> 01:44:39,679
want to set up a link
2593
01:44:39,679 --> 01:44:43,360
that allows the user to navigate back so
2594
01:44:43,360 --> 01:44:45,679
go with href and i'll say forward slash
2595
01:44:45,679 --> 01:44:48,080
which just means home page and we're
2596
01:44:48,080 --> 01:44:49,679
gonna go back home
2597
01:44:49,679 --> 01:44:53,360
let's close our link let's save that one
2598
01:44:53,360 --> 01:44:55,760
so at the moment of course we have the
2599
01:44:55,760 --> 01:44:58,080
about and by the way
2600
01:44:58,080 --> 01:44:59,360
almost forgot
2601
01:44:59,360 --> 01:45:02,080
yes we do need to stop the server and we
2602
01:45:02,080 --> 01:45:05,600
need to go with node and then app.js
2603
01:45:05,600 --> 01:45:08,480
refresh one more time there it is notice
2604
01:45:08,480 --> 01:45:10,400
how we don't have the about we can't
2605
01:45:10,400 --> 01:45:12,400
seem to have the page you're looking for
2606
01:45:12,400 --> 01:45:14,000
and that is actually a bit interesting
2607
01:45:14,000 --> 01:45:16,239
because it should be available right and
2608
01:45:16,239 --> 01:45:18,239
of course the reason is because i messed
2609
01:45:18,239 --> 01:45:21,600
up on the url property my bad let me do
2610
01:45:21,600 --> 01:45:23,199
this one more time
2611
01:45:23,199 --> 01:45:25,760
and we'll stop the server here
2612
01:45:25,760 --> 01:45:29,119
again let's run node app.js and now we
2613
01:45:29,119 --> 01:45:31,760
should have here's our short history
2614
01:45:31,760 --> 01:45:34,000
that's our about page then we have the
2615
01:45:34,000 --> 01:45:35,760
homepage and of course you already
2616
01:45:35,760 --> 01:45:38,480
familiar with our error page so if i'll
2617
01:45:38,480 --> 01:45:41,119
go with error or any kind of resource
2618
01:45:41,119 --> 01:45:42,480
that doesn't exist
2619
01:45:42,480 --> 01:45:44,560
then of course we'll get the error page
2620
01:45:44,560 --> 01:45:46,560
and of course we can bravely navigate
2621
01:45:46,560 --> 01:45:49,520
back home and we go back to our home
2622
01:45:49,520 --> 01:45:51,600
page that should do it for the quick
2623
01:45:51,600 --> 01:45:53,920
intro of base modules
2624
01:45:53,920 --> 01:45:55,840
and now of course we are ready to move
2625
01:45:55,840 --> 01:45:59,280
on to our next topic all right at this
2626
01:45:59,280 --> 01:46:01,040
point we're familiar with two types of
2627
01:46:01,040 --> 01:46:02,159
modules
2628
01:46:02,159 --> 01:46:03,280
our own
2629
01:46:03,280 --> 01:46:06,719
as well as the nodes built-in ones
2630
01:46:06,719 --> 01:46:07,600
but
2631
01:46:07,600 --> 01:46:10,880
just like in any good infomercial
2632
01:46:10,880 --> 01:46:14,320
i'll say but wait there's more
2633
01:46:14,320 --> 01:46:16,560
we also have access to the world's
2634
01:46:16,560 --> 01:46:18,719
biggest code store
2635
01:46:18,719 --> 01:46:20,400
and before you seriously start
2636
01:46:20,400 --> 01:46:23,119
questioning my mental state
2637
01:46:23,119 --> 01:46:24,800
imagine this scenario
2638
01:46:24,800 --> 01:46:26,480
you're building an app
2639
01:46:26,480 --> 01:46:29,119
and of course as any good app it needs
2640
01:46:29,119 --> 01:46:30,960
to have a slider
2641
01:46:30,960 --> 01:46:32,480
just for funsies
2642
01:46:32,480 --> 01:46:34,800
now at this point you generally have two
2643
01:46:34,800 --> 01:46:36,159
options
2644
01:46:36,159 --> 01:46:38,560
build it from the scratch yourself
2645
01:46:38,560 --> 01:46:41,920
or search on google and copy paste
2646
01:46:41,920 --> 01:46:44,080
someone else's solution
2647
01:46:44,080 --> 01:46:46,800
well have no issues with either of these
2648
01:46:46,800 --> 01:46:49,600
options keep in mind that someone
2649
01:46:49,600 --> 01:46:50,719
somewhere
2650
01:46:50,719 --> 01:46:54,159
has struggled with the same issue the
2651
01:46:54,159 --> 01:46:56,080
need for the slider
2652
01:46:56,080 --> 01:46:57,920
and there's a good chance that that
2653
01:46:57,920 --> 01:46:59,040
person
2654
01:46:59,040 --> 01:47:01,600
was kind enough to build it
2655
01:47:01,600 --> 01:47:02,800
and share
2656
01:47:02,800 --> 01:47:05,119
the whole project with us
2657
01:47:05,119 --> 01:47:07,440
so our only job
2658
01:47:07,440 --> 01:47:09,520
is to go through the docs
2659
01:47:09,520 --> 01:47:11,920
and with the help of one command
2660
01:47:11,920 --> 01:47:14,320
just add it to our project
2661
01:47:14,320 --> 01:47:16,960
we can do that because when we install
2662
01:47:16,960 --> 01:47:17,920
node
2663
01:47:17,920 --> 01:47:21,199
we automatically also install npm
2664
01:47:21,199 --> 01:47:24,639
or node package manager
2665
01:47:24,639 --> 01:47:28,320
and npm enables us to do three things
2666
01:47:28,320 --> 01:47:30,480
reuse our own code
2667
01:47:30,480 --> 01:47:32,560
in other projects
2668
01:47:32,560 --> 01:47:35,600
use code written by other developers
2669
01:47:35,600 --> 01:47:38,480
and lastly share our own solutions
2670
01:47:38,480 --> 01:47:40,960
with other developers as well
2671
01:47:40,960 --> 01:47:45,440
the npm project is hosted at
2672
01:47:45,560 --> 01:47:48,840
npmjs.com again the site is
2673
01:47:48,840 --> 01:47:50,800
npm.js
2674
01:47:50,800 --> 01:47:53,520
and here you can find everything
2675
01:47:53,520 --> 01:47:56,560
starting with useful utility functions
2676
01:47:56,560 --> 01:47:59,600
to full-blown frameworks and libraries
2677
01:47:59,600 --> 01:48:01,520
and as an example if you're familiar
2678
01:48:01,520 --> 01:48:04,400
with react you know that react has the
2679
01:48:04,400 --> 01:48:06,800
create react app package
2680
01:48:06,800 --> 01:48:11,280
and of course it is hosted on the npm
2681
01:48:11,280 --> 01:48:14,239
a typical node project
2682
01:48:14,239 --> 01:48:16,840
will have more than few npm packages
2683
01:48:16,840 --> 01:48:19,520
installed as dependencies
2684
01:48:19,520 --> 01:48:22,320
and before we install some cool packages
2685
01:48:22,320 --> 01:48:24,320
let's talk about naming
2686
01:48:24,320 --> 01:48:28,400
npm calls the reusable code a package
2687
01:48:28,400 --> 01:48:30,560
and a package essentially is a folder
2688
01:48:30,560 --> 01:48:33,280
that contains a javascript code
2689
01:48:33,280 --> 01:48:35,760
now another names you'll hear
2690
01:48:35,760 --> 01:48:37,199
are modules
2691
01:48:37,199 --> 01:48:40,080
and dependencies and honestly at this
2692
01:48:40,080 --> 01:48:40,960
point
2693
01:48:40,960 --> 01:48:44,000
all three are used interchangeably when
2694
01:48:44,000 --> 01:48:46,560
talking about shareable javascript code
2695
01:48:46,560 --> 01:48:48,239
so don't be surprised if during the
2696
01:48:48,239 --> 01:48:50,560
course i call them any of these names
2697
01:48:50,560 --> 01:48:53,600
package dependency or module at the end
2698
01:48:53,600 --> 01:48:55,760
of the day they all mean the same thing
2699
01:48:55,760 --> 01:48:57,920
lastly let me just mention two things
2700
01:48:57,920 --> 01:48:58,880
first
2701
01:48:58,880 --> 01:49:01,520
there is no quality control in npm
2702
01:49:01,520 --> 01:49:02,960
registry
2703
01:49:02,960 --> 01:49:05,520
anyone can publish anything
2704
01:49:05,520 --> 01:49:08,080
so it's up to you to sniff out the empty
2705
01:49:08,080 --> 01:49:10,800
and useless packages and yes there are
2706
01:49:10,800 --> 01:49:13,040
quite a few of those ones out there as
2707
01:49:13,040 --> 01:49:16,560
well a good indication of the security
2708
01:49:16,560 --> 01:49:19,679
and usefulness of package is the amount
2709
01:49:19,679 --> 01:49:23,119
of weekly download if the number is high
2710
01:49:23,119 --> 01:49:25,119
meaning if it's popular it's a good
2711
01:49:25,119 --> 01:49:27,599
chance that it's a battle tested and
2712
01:49:27,599 --> 01:49:28,800
ready to go
2713
01:49:28,800 --> 01:49:31,520
and that brings me to my second point
2714
01:49:31,520 --> 01:49:33,679
remember the slider example we discussed
2715
01:49:33,679 --> 01:49:35,360
in the beginning of the video
2716
01:49:35,360 --> 01:49:37,920
when it comes to npm packages there's a
2717
01:49:37,920 --> 01:49:41,199
good chance that if there is a bug
2718
01:49:41,199 --> 01:49:43,760
someone else has already fixed it
2719
01:49:43,760 --> 01:49:46,480
and as a result it's already fixed in
2720
01:49:46,480 --> 01:49:47,679
the package
2721
01:49:47,679 --> 01:49:48,560
or
2722
01:49:48,560 --> 01:49:50,560
there's a working solution
2723
01:49:50,560 --> 01:49:51,440
all right
2724
01:49:51,440 --> 01:49:54,159
so that you did for intro let's start
2725
01:49:54,159 --> 01:49:56,719
using node package manager
2726
01:49:56,719 --> 01:49:59,520
in our own project and as a side note if
2727
01:49:59,520 --> 01:50:01,760
you want to search for some packages
2728
01:50:01,760 --> 01:50:03,119
just visit
2729
01:50:03,119 --> 01:50:06,119
npmjs.com
2730
01:50:06,159 --> 01:50:08,239
and then for example if you're looking
2731
01:50:08,239 --> 01:50:10,719
for the bootstrap you'll find the
2732
01:50:10,719 --> 01:50:11,840
package
2733
01:50:11,840 --> 01:50:13,840
and of course you can click on any of
2734
01:50:13,840 --> 01:50:14,719
them
2735
01:50:14,719 --> 01:50:17,520
this will bring you to the docs as well
2736
01:50:17,520 --> 01:50:20,400
as weekly downloads and rest of the
2737
01:50:20,400 --> 01:50:22,480
stuff once we're familiar with node
2738
01:50:22,480 --> 01:50:24,080
package manager let's see how we can
2739
01:50:24,080 --> 01:50:26,719
start using it in our own project
2740
01:50:26,719 --> 01:50:28,960
and the good news is that it's much
2741
01:50:28,960 --> 01:50:31,440
simpler than you would expect you see
2742
01:50:31,440 --> 01:50:34,880
when we install node we also install npm
2743
01:50:34,880 --> 01:50:37,119
and because of that we have access to
2744
01:50:37,119 --> 01:50:39,199
npm global command
2745
01:50:39,199 --> 01:50:41,599
and you can check the npm version by
2746
01:50:41,599 --> 01:50:43,840
running npm
2747
01:50:43,840 --> 01:50:46,320
version in your terminal just keep in
2748
01:50:46,320 --> 01:50:48,320
mind that the version most likely won't
2749
01:50:48,320 --> 01:50:51,199
be the same as your node version and
2750
01:50:51,199 --> 01:50:54,239
that is totally okay so you can either
2751
01:50:54,239 --> 01:50:56,480
do that in the terminal
2752
01:50:56,480 --> 01:50:59,119
or of course in the integrated terminal
2753
01:50:59,119 --> 01:51:01,760
and you can simply type npm and then
2754
01:51:01,760 --> 01:51:03,920
hyphen hyphen version i believe you can
2755
01:51:03,920 --> 01:51:06,639
also check by hyphen hyphen v and there
2756
01:51:06,639 --> 01:51:10,159
it is of course now we have the version
2757
01:51:10,159 --> 01:51:13,920
for our node package manager and then we
2758
01:51:13,920 --> 01:51:16,480
have two flavors we can install package
2759
01:51:16,480 --> 01:51:19,119
as a local dependency and that just
2760
01:51:19,119 --> 01:51:22,320
means that we'll only use that package
2761
01:51:22,320 --> 01:51:24,800
in this particular project that we're
2762
01:51:24,800 --> 01:51:27,119
working on and the command for that one
2763
01:51:27,119 --> 01:51:29,119
would be npm
2764
01:51:29,119 --> 01:51:32,320
install or i for short and then whatever
2765
01:51:32,320 --> 01:51:34,800
is the package name so whether that is
2766
01:51:34,800 --> 01:51:37,679
bootstrap low dash express or whatever
2767
01:51:37,679 --> 01:51:41,520
or we can install dependency as a global
2768
01:51:41,520 --> 01:51:43,520
dependency and that just means that we
2769
01:51:43,520 --> 01:51:46,159
can use it in any project and the
2770
01:51:46,159 --> 01:51:48,880
command for that one would be again npm
2771
01:51:48,880 --> 01:51:52,639
i or install whichever method you prefer
2772
01:51:52,639 --> 01:51:55,599
and then hyphen g so this is going to be
2773
01:51:55,599 --> 01:51:58,719
the flag and then again the package name
2774
01:51:58,719 --> 01:52:00,560
now when you install something globally
2775
01:52:00,560 --> 01:52:03,520
most likely on a mac they will ask you
2776
01:52:03,520 --> 01:52:05,840
for the sudo so you'll have to provide
2777
01:52:05,840 --> 01:52:08,400
the credentials that's why you'll run
2778
01:52:08,400 --> 01:52:09,520
sudo
2779
01:52:09,520 --> 01:52:10,639
npm
2780
01:52:10,639 --> 01:52:13,280
install and then hyphen g and again
2781
01:52:13,280 --> 01:52:14,800
package name
2782
01:52:14,800 --> 01:52:16,400
as far as which one you'll use more
2783
01:52:16,400 --> 01:52:19,119
often that definitely will be a local
2784
01:52:19,119 --> 01:52:20,719
dependency flavor
2785
01:52:20,719 --> 01:52:23,040
because even though yes you can install
2786
01:52:23,040 --> 01:52:27,840
packages globally with arrival of npx
2787
01:52:27,840 --> 01:52:31,520
there's actually less and less need for
2788
01:52:31,520 --> 01:52:34,080
setting up something globally that's why
2789
01:52:34,080 --> 01:52:36,960
we'll focus on local dependencies first
2790
01:52:36,960 --> 01:52:39,920
how to set it up in our project and then
2791
01:52:39,920 --> 01:52:42,719
in a few videos when we talk about npx
2792
01:52:42,719 --> 01:52:46,400
i'll cover why there's less need for
2793
01:52:46,400 --> 01:52:48,880
setting up something globally now
2794
01:52:48,880 --> 01:52:50,560
there's one more thing that we would
2795
01:52:50,560 --> 01:52:52,719
need to set up in our project as far as
2796
01:52:52,719 --> 01:52:54,560
dependencies so i know i know you're
2797
01:52:54,560 --> 01:52:58,560
eager to start installing the packages
2798
01:52:58,560 --> 01:52:59,440
but
2799
01:52:59,440 --> 01:53:01,119
let's just wait a little bit and next
2800
01:53:01,119 --> 01:53:04,480
video we'll add that extra thing that
2801
01:53:04,480 --> 01:53:06,480
we're missing right now and then we'll
2802
01:53:06,480 --> 01:53:08,159
be in good shape and then we'll start
2803
01:53:08,159 --> 01:53:11,280
installing every package under the sun
2804
01:53:11,280 --> 01:53:14,080
excellent we now know that we have
2805
01:53:14,080 --> 01:53:16,960
access to the npm global command we now
2806
01:53:16,960 --> 01:53:19,599
know that in order to install the local
2807
01:53:19,599 --> 01:53:22,560
package we will need to run npm i
2808
01:53:22,560 --> 01:53:24,960
and the package name so what are we
2809
01:53:24,960 --> 01:53:27,760
missing well we're missing file by the
2810
01:53:27,760 --> 01:53:30,320
name of package.json
2811
01:53:30,320 --> 01:53:32,639
and essentially you can think of it as a
2812
01:53:32,639 --> 01:53:35,119
manifest file that stores important
2813
01:53:35,119 --> 01:53:38,239
information about our project
2814
01:53:38,239 --> 01:53:40,000
and there are three ways how you can
2815
01:53:40,000 --> 01:53:43,119
create package.json first is the manual
2816
01:53:43,119 --> 01:53:45,520
approach where you just create a
2817
01:53:45,520 --> 01:53:48,320
package.json in the root and please do
2818
01:53:48,320 --> 01:53:50,800
that in the root if that's something
2819
01:53:50,800 --> 01:53:53,360
that eventually you decide doing
2820
01:53:53,360 --> 01:53:54,880
and then of course you would need to
2821
01:53:54,880 --> 01:53:58,080
create each property or
2822
01:53:58,080 --> 01:53:59,199
there are two
2823
01:53:59,199 --> 01:54:01,599
ways how we can automate this and the
2824
01:54:01,599 --> 01:54:04,560
first one is running npm init
2825
01:54:04,560 --> 01:54:07,360
and in there they'll just step by step
2826
01:54:07,360 --> 01:54:09,360
ask you the questions and if you want to
2827
01:54:09,360 --> 01:54:11,920
skip it you can just press enter
2828
01:54:11,920 --> 01:54:14,239
and the another way the third way is
2829
01:54:14,239 --> 01:54:18,400
running npm init with a y flag and then
2830
01:54:18,400 --> 01:54:21,199
everything is set up as default so i'm
2831
01:54:21,199 --> 01:54:22,560
not going to show you the manual
2832
01:54:22,560 --> 01:54:25,679
approach it's just too time consuming
2833
01:54:25,679 --> 01:54:28,000
and we'll right away go with npm in it
2834
01:54:28,000 --> 01:54:30,080
first i'll show you the step by step
2835
01:54:30,080 --> 01:54:32,400
approach and then of course i'll show
2836
01:54:32,400 --> 01:54:35,040
you how everything is set up by default
2837
01:54:35,040 --> 01:54:37,920
using the y flag so go to your terminal
2838
01:54:37,920 --> 01:54:41,199
and just type npm in it and there it is
2839
01:54:41,199 --> 01:54:43,920
of course now they tell you that there's
2840
01:54:43,920 --> 01:54:46,639
going to be a walkthrough of creating a
2841
01:54:46,639 --> 01:54:49,040
package.json file and the first one is
2842
01:54:49,040 --> 01:54:50,719
the package name and by default of
2843
01:54:50,719 --> 01:54:53,679
course it is going to use the folder
2844
01:54:53,679 --> 01:54:55,920
name now keep in mind that if you
2845
01:54:55,920 --> 01:54:58,400
eventually want to publish this package
2846
01:54:58,400 --> 01:55:01,040
then the name has to be unique so you
2847
01:55:01,040 --> 01:55:02,639
need to make sure that
2848
01:55:02,639 --> 01:55:05,199
nowhere in the npm
2849
01:55:05,199 --> 01:55:08,000
you can see the package with the same
2850
01:55:08,000 --> 01:55:10,159
name and i'm just gonna go with tutorial
2851
01:55:10,159 --> 01:55:12,480
as far as version we'll talk about
2852
01:55:12,480 --> 01:55:15,760
versions shortly so i'll just skip this
2853
01:55:15,760 --> 01:55:18,320
one as far as description whatever again
2854
01:55:18,320 --> 01:55:20,400
is just the default now as far as the
2855
01:55:20,400 --> 01:55:22,239
entry point eventually will change it
2856
01:55:22,239 --> 01:55:24,159
but for now it's good enough we're going
2857
01:55:24,159 --> 01:55:27,280
to go with one intro js and then the
2858
01:55:27,280 --> 01:55:30,080
test command as well as git repository
2859
01:55:30,080 --> 01:55:32,880
and keywords and author and license and
2860
01:55:32,880 --> 01:55:35,760
we simply say yes and we are in good
2861
01:55:35,760 --> 01:55:37,679
shape so now of course we have the
2862
01:55:37,679 --> 01:55:40,239
package json file with name version
2863
01:55:40,239 --> 01:55:42,880
description as well as the main property
2864
01:55:42,880 --> 01:55:45,760
and of course the scripts author and the
2865
01:55:45,760 --> 01:55:48,480
license now i'll remove it
2866
01:55:48,480 --> 01:55:50,639
i'll say delete and i'll show you that
2867
01:55:50,639 --> 01:55:52,800
of course you can skip all those
2868
01:55:52,800 --> 01:55:55,760
questions by simply typing npm
2869
01:55:55,760 --> 01:55:57,760
in it and why
2870
01:55:57,760 --> 01:55:59,599
and as you can guess
2871
01:55:59,599 --> 01:56:02,560
yes of course you can come here later
2872
01:56:02,560 --> 01:56:04,880
and then just change these values that's
2873
01:56:04,880 --> 01:56:07,920
why it's definitely faster to just type
2874
01:56:07,920 --> 01:56:09,040
npm
2875
01:56:09,040 --> 01:56:11,679
in it and then the y flag
2876
01:56:11,679 --> 01:56:15,199
so why do we need this package.json well
2877
01:56:15,199 --> 01:56:17,440
because if i would want to install the
2878
01:56:17,440 --> 01:56:19,840
package the local package
2879
01:56:19,840 --> 01:56:22,320
now of course this package is going to
2880
01:56:22,320 --> 01:56:25,440
be stored as a dependency and to show
2881
01:56:25,440 --> 01:56:27,520
you that let's just go with npm and you
2882
01:56:27,520 --> 01:56:28,639
know what
2883
01:56:28,639 --> 01:56:30,639
let me clear the console first so we're
2884
01:56:30,639 --> 01:56:32,239
going to go with npm
2885
01:56:32,239 --> 01:56:33,280
i
2886
01:56:33,280 --> 01:56:35,520
and then we're going to go with the
2887
01:56:35,520 --> 01:56:37,520
package name lowdash and if you're not
2888
01:56:37,520 --> 01:56:40,400
familiar with lodash it's just a utility
2889
01:56:40,400 --> 01:56:43,119
library and i'm just specifically using
2890
01:56:43,119 --> 01:56:45,920
for installation purposes there's no
2891
01:56:45,920 --> 01:56:49,119
real reason to have low dash for our own
2892
01:56:49,119 --> 01:56:51,440
project but it's just something pretty
2893
01:56:51,440 --> 01:56:52,400
interesting that i would want to
2894
01:56:52,400 --> 01:56:55,119
showcase that's why we're using lodash
2895
01:56:55,119 --> 01:56:57,199
and we run it and notice how we
2896
01:56:57,199 --> 01:57:00,000
installed the package and check it out
2897
01:57:00,000 --> 01:57:02,800
now in the package.json we have
2898
01:57:02,800 --> 01:57:05,920
dependencies property and it's an object
2899
01:57:05,920 --> 01:57:08,159
and inside of that object we have a
2900
01:57:08,159 --> 01:57:12,080
package by the name of lowdash now i'll
2901
01:57:12,080 --> 01:57:13,440
show you
2902
01:57:13,440 --> 01:57:16,000
in this video or maybe in the next one
2903
01:57:16,000 --> 01:57:18,080
why it's so important to have this
2904
01:57:18,080 --> 01:57:19,920
package.json
2905
01:57:19,920 --> 01:57:21,280
but another thing that i would want to
2906
01:57:21,280 --> 01:57:24,320
showcase once we can see that we have
2907
01:57:24,320 --> 01:57:27,440
dependencies property if you go in your
2908
01:57:27,440 --> 01:57:29,040
visual studio code
2909
01:57:29,040 --> 01:57:32,080
and if you click on refresh explorer
2910
01:57:32,080 --> 01:57:34,080
what you'll also notice
2911
01:57:34,080 --> 01:57:37,760
is this node modules folder and in that
2912
01:57:37,760 --> 01:57:40,560
node modules folder this is where all
2913
01:57:40,560 --> 01:57:42,080
the dependencies
2914
01:57:42,080 --> 01:57:43,679
are stored
2915
01:57:43,679 --> 01:57:46,719
so if you don't have that folder already
2916
01:57:46,719 --> 01:57:49,199
and pm creates it so in our case of
2917
01:57:49,199 --> 01:57:51,280
course we didn't have any dependencies
2918
01:57:51,280 --> 01:57:53,840
so when we install that first dependency
2919
01:57:53,840 --> 01:57:56,239
npm creates that folder and this is
2920
01:57:56,239 --> 01:57:59,199
where you'll find the dependencies now
2921
01:57:59,199 --> 01:58:01,040
notice something interesting where for
2922
01:58:01,040 --> 01:58:02,400
the low dash
2923
01:58:02,400 --> 01:58:05,440
we only have one folder right so we have
2924
01:58:05,440 --> 01:58:07,040
only one dependency
2925
01:58:07,040 --> 01:58:09,520
but when we need to keep in mind that of
2926
01:58:09,520 --> 01:58:12,480
course there's going to be some packages
2927
01:58:12,480 --> 01:58:15,280
that have other dependencies
2928
01:58:15,280 --> 01:58:17,840
and that's why i first installed lodash
2929
01:58:17,840 --> 01:58:19,520
just to showcase that yes once you
2930
01:58:19,520 --> 01:58:21,599
install dependency of course it is going
2931
01:58:21,599 --> 01:58:24,880
to be in node modules but if you'll try
2932
01:58:24,880 --> 01:58:27,599
to install a package that is bigger that
2933
01:58:27,599 --> 01:58:30,320
uses other dependencies you'll notice
2934
01:58:30,320 --> 01:58:33,040
something pretty cool where if i clear
2935
01:58:33,040 --> 01:58:36,800
my terminal and if i'm gonna run npm i
2936
01:58:36,800 --> 01:58:39,679
and then bootstrap again you can use any
2937
01:58:39,679 --> 01:58:41,840
other package you'd want this is just to
2938
01:58:41,840 --> 01:58:45,280
showcase how the packages work we won't
2939
01:58:45,280 --> 01:58:47,679
use the bootstrap in this project so
2940
01:58:47,679 --> 01:58:51,520
once i install and once i refresh
2941
01:58:51,520 --> 01:58:54,159
check it out not only i have low dash
2942
01:58:54,159 --> 01:58:55,840
not only i have bootstrap which i
2943
01:58:55,840 --> 01:58:58,960
installed but since bootstrap is using
2944
01:58:58,960 --> 01:59:01,760
jquery as well as the proper js now
2945
01:59:01,760 --> 01:59:04,560
these are installed as dependencies and
2946
01:59:04,560 --> 01:59:06,480
if you take a look at the dependencies
2947
01:59:06,480 --> 01:59:08,480
property now of course we have the
2948
01:59:08,480 --> 01:59:11,599
bootstrap as well and again
2949
01:59:11,599 --> 01:59:13,840
the whole point of this video is to
2950
01:59:13,840 --> 01:59:16,480
showcase why we need package.json
2951
01:59:16,480 --> 01:59:18,800
so we need it because we need to provide
2952
01:59:18,800 --> 01:59:22,800
information about our project and inside
2953
01:59:22,800 --> 01:59:23,599
there
2954
01:59:23,599 --> 01:59:25,440
a very important property is the
2955
01:59:25,440 --> 01:59:28,400
dependencies one because in there we'll
2956
01:59:28,400 --> 01:59:31,280
just store the dependencies which our
2957
01:59:31,280 --> 01:59:34,480
project is using and then some packages
2958
01:59:34,480 --> 01:59:36,320
are actually going to use
2959
01:59:36,320 --> 01:59:38,239
more dependencies and they will be
2960
01:59:38,239 --> 01:59:41,040
automatically installed as well in our
2961
01:59:41,040 --> 01:59:44,400
case that was bootstrap and
2962
01:59:44,400 --> 01:59:48,719
just to give you a taste of the package
2963
01:59:48,719 --> 01:59:51,520
why don't we just navigate to app
2964
01:59:51,520 --> 01:59:53,040
and i'm going to do that below the
2965
01:59:53,040 --> 01:59:55,280
comments of course you probably would
2966
01:59:55,280 --> 01:59:57,119
need to clean the file and as a side
2967
01:59:57,119 --> 02:00:00,960
note the last module the http i saved in
2968
02:00:00,960 --> 02:00:03,760
file by the name of 12 http
2969
02:00:03,760 --> 02:00:06,880
and back in the app.js in order to start
2970
02:00:06,880 --> 02:00:08,400
using
2971
02:00:08,400 --> 02:00:09,840
the module
2972
02:00:09,840 --> 02:00:11,520
in our case i guess i'm gonna go with
2973
02:00:11,520 --> 02:00:12,480
low dash
2974
02:00:12,480 --> 02:00:14,639
first i would need to set up the
2975
02:00:14,639 --> 02:00:17,040
variable and common convention is
2976
02:00:17,040 --> 02:00:19,599
calling the variable like so and we'll
2977
02:00:19,599 --> 02:00:22,159
set it equal to require and now of
2978
02:00:22,159 --> 02:00:25,280
course we have access to the low dash
2979
02:00:25,280 --> 02:00:27,520
now this is the difference between
2980
02:00:27,520 --> 02:00:28,800
node packages
2981
02:00:28,800 --> 02:00:31,920
like for example the http
2982
02:00:31,920 --> 02:00:35,040
or the file module or whatever and the
2983
02:00:35,040 --> 02:00:38,080
ones that you install the external ones
2984
02:00:38,080 --> 02:00:40,320
the external ones you always have to
2985
02:00:40,320 --> 02:00:41,840
install first
2986
02:00:41,840 --> 02:00:44,480
if you won't install the dependency well
2987
02:00:44,480 --> 02:00:46,800
node won't be able to find it so in our
2988
02:00:46,800 --> 02:00:49,199
case we did install the dependency the
2989
02:00:49,199 --> 02:00:51,199
lowdash so now of course i can just
2990
02:00:51,199 --> 02:00:53,280
assign it to the variable and since
2991
02:00:53,280 --> 02:00:56,320
lowdash is utility library why don't we
2992
02:00:56,320 --> 02:00:59,440
test out one of their methods and i'm
2993
02:00:59,440 --> 02:01:01,119
just going to go with items and i'll
2994
02:01:01,119 --> 02:01:04,480
create an array of arrays so items is
2995
02:01:04,480 --> 02:01:06,400
going to be an array however
2996
02:01:06,400 --> 02:01:09,599
the items are going to be arrays itself
2997
02:01:09,599 --> 02:01:12,000
so two and three
2998
02:01:12,000 --> 02:01:14,639
and then we're gonna go with four
2999
02:01:14,639 --> 02:01:17,599
and low dash has this method the flat
3000
02:01:17,599 --> 02:01:20,159
and deep method that effectively will
3001
02:01:20,159 --> 02:01:24,159
just spit this back as a flat array
3002
02:01:24,159 --> 02:01:27,360
and the way we set it up we just go with
3003
02:01:27,360 --> 02:01:29,360
const and i'll call this
3004
02:01:29,360 --> 02:01:32,639
new items and then since we have access
3005
02:01:32,639 --> 02:01:34,239
to everything
3006
02:01:34,239 --> 02:01:37,199
in this variable now i can simply go
3007
02:01:37,199 --> 02:01:42,080
with underscore here dot and then flat
3008
02:01:42,080 --> 02:01:44,800
and deep so flatten deep and then we'll
3009
02:01:44,800 --> 02:01:47,760
pass in the items and now if we console
3010
02:01:47,760 --> 02:01:48,800
log
3011
02:01:48,800 --> 02:01:52,480
new items and if i go to my terminal and
3012
02:01:52,480 --> 02:01:55,760
just type node app.js and by the way i
3013
02:01:55,760 --> 02:01:56,960
need to save it
3014
02:01:56,960 --> 02:02:00,639
so let me go back i'll say node app.js
3015
02:02:00,639 --> 02:02:02,639
and once we run check it out in a
3016
02:02:02,639 --> 02:02:05,599
console i can see of course the one two
3017
02:02:05,599 --> 02:02:08,880
three four so lowdash has the method by
3018
02:02:08,880 --> 02:02:11,040
the name of flattendeep we have access
3019
02:02:11,040 --> 02:02:12,800
to it because we install it as
3020
02:02:12,800 --> 02:02:15,520
dependency and now of course i can start
3021
02:02:15,520 --> 02:02:18,080
using it now we're not gonna use
3022
02:02:18,080 --> 02:02:20,880
bootstrap in this project because that
3023
02:02:20,880 --> 02:02:23,119
would be too time-consuming bootstrap
3024
02:02:23,119 --> 02:02:26,400
was just used so you can see that some
3025
02:02:26,400 --> 02:02:28,639
dependencies will have
3026
02:02:28,639 --> 02:02:31,199
more packages so once we install one
3027
02:02:31,199 --> 02:02:32,880
dependency there's actually going to be
3028
02:02:32,880 --> 02:02:34,560
more dependencies and hopefully you have
3029
02:02:34,560 --> 02:02:36,880
a clear understanding of how you would
3030
02:02:36,880 --> 02:02:39,440
set up package.json how we would install
3031
02:02:39,440 --> 02:02:41,440
the package and next video i would want
3032
02:02:41,440 --> 02:02:44,400
to showcase why having package.json
3033
02:02:44,400 --> 02:02:48,080
is so crucial and so beneficial
3034
02:02:48,080 --> 02:02:51,760
when we are starting to share our
3035
02:02:51,760 --> 02:02:54,560
project with other developers
3036
02:02:54,560 --> 02:02:57,199
awesome we have package.json we
3037
02:02:57,199 --> 02:02:59,440
installed few dependencies
3038
02:02:59,440 --> 02:03:01,280
and now i want to cover
3039
02:03:01,280 --> 02:03:04,400
why having package.json is so crucial
3040
02:03:04,400 --> 02:03:07,440
when we start sharing our project with
3041
02:03:07,440 --> 02:03:10,080
other developers and for this example
3042
02:03:10,080 --> 02:03:13,440
i'm going to use github as well as few
3043
02:03:13,440 --> 02:03:16,080
basic git commands and my assumption is
3044
02:03:16,080 --> 02:03:18,320
that since this is somewhat of a
3045
02:03:18,320 --> 02:03:20,800
advanced course you're already familiar
3046
02:03:20,800 --> 02:03:23,440
with git and github and you have the
3047
02:03:23,440 --> 02:03:25,119
account if you don't please stop the
3048
02:03:25,119 --> 02:03:26,960
video create the account
3049
02:03:26,960 --> 02:03:29,520
and just use your favorite search engine
3050
02:03:29,520 --> 02:03:32,080
to learn about the basics of setting up
3051
02:03:32,080 --> 02:03:35,520
the github repository as well as basic
3052
02:03:35,520 --> 02:03:37,760
git commands so
3053
02:03:37,760 --> 02:03:41,679
our task is to push this up to the up
3054
02:03:41,679 --> 02:03:44,159
and now million dollar question are we
3055
02:03:44,159 --> 02:03:45,440
just pushing
3056
02:03:45,440 --> 02:03:48,639
everything up including the mode modules
3057
02:03:48,639 --> 02:03:49,599
which
3058
02:03:49,599 --> 02:03:51,520
for the most projects is going to be
3059
02:03:51,520 --> 02:03:52,800
somewhat big
3060
02:03:52,800 --> 02:03:55,360
i believe i just checked in our case it
3061
02:03:55,360 --> 02:03:58,320
was like nine megabytes but trust me it
3062
02:03:58,320 --> 02:04:01,599
always gets way bigger than that
3063
02:04:01,599 --> 02:04:02,560
or
3064
02:04:02,560 --> 02:04:04,960
we would just want to send the code and
3065
02:04:04,960 --> 02:04:06,719
you can probably already guess that
3066
02:04:06,719 --> 02:04:09,199
since i'm implying that the size is too
3067
02:04:09,199 --> 02:04:12,719
big that we'll have to push this up to
3068
02:04:12,719 --> 02:04:16,239
the github without the node modules and
3069
02:04:16,239 --> 02:04:19,040
we'll start by creating a dummy repo on
3070
02:04:19,040 --> 02:04:20,079
a github
3071
02:04:20,079 --> 02:04:22,560
so let me open up a new window
3072
02:04:22,560 --> 02:04:24,639
i'm going to go with github
3073
02:04:24,639 --> 02:04:27,440
and i'll just create a temp repo
3074
02:04:27,440 --> 02:04:29,679
and then i'm just going to create a repo
3075
02:04:29,679 --> 02:04:32,719
and i'm looking for these three commands
3076
02:04:32,719 --> 02:04:36,159
essentially i would want to get that url
3077
02:04:36,159 --> 02:04:39,599
the remote url so i know where to push
3078
02:04:39,599 --> 02:04:42,960
it and then inside of the repo before we
3079
02:04:42,960 --> 02:04:45,679
set it up as github repo first i would
3080
02:04:45,679 --> 02:04:48,000
want to go with new file and we'll
3081
02:04:48,000 --> 02:04:50,480
create a git ignored because of course
3082
02:04:50,480 --> 02:04:52,960
what i would want is to place
3083
02:04:52,960 --> 02:04:54,639
the node modules
3084
02:04:54,639 --> 02:04:56,800
in a git ignore file because that way
3085
02:04:56,800 --> 02:04:59,040
they will be ignored by my source
3086
02:04:59,040 --> 02:05:02,320
control and i won't be pushing up this
3087
02:05:02,320 --> 02:05:05,199
giant folder to the github because
3088
02:05:05,199 --> 02:05:07,440
you'll see in a second that there is no
3089
02:05:07,440 --> 02:05:09,199
need for it so we're going to go with
3090
02:05:09,199 --> 02:05:11,679
forward slash and then node modules and
3091
02:05:11,679 --> 02:05:13,599
if you're not familiar with git ignored
3092
02:05:13,599 --> 02:05:15,119
it's just a file
3093
02:05:15,119 --> 02:05:17,920
that specifies which files are going to
3094
02:05:17,920 --> 02:05:20,400
be ignored by the source control and in
3095
02:05:20,400 --> 02:05:23,280
our case as you can see i'm placing node
3096
02:05:23,280 --> 02:05:25,840
modules inside there then i'll clear my
3097
02:05:25,840 --> 02:05:29,040
console and i'll just initialize this as
3098
02:05:29,040 --> 02:05:32,400
an empty git repo and then we'll add
3099
02:05:32,400 --> 02:05:35,599
everything so git add git commit
3100
02:05:35,599 --> 02:05:39,119
we'll just say first commit
3101
02:05:39,119 --> 02:05:41,280
first commit and then i'll copy and
3102
02:05:41,280 --> 02:05:43,679
paste those three commands in order to
3103
02:05:43,679 --> 02:05:46,000
set up that url
3104
02:05:46,000 --> 02:05:50,000
the remote url and once i have this in
3105
02:05:50,000 --> 02:05:50,880
place
3106
02:05:50,880 --> 02:05:53,920
should be able to go back to my github
3107
02:05:53,920 --> 02:05:56,960
and there it is this is my repo
3108
02:05:56,960 --> 02:05:59,920
now probably your next question is okay
3109
02:05:59,920 --> 02:06:02,960
so we pushed it up to the github
3110
02:06:02,960 --> 02:06:05,840
but i can clearly see that in my app.js
3111
02:06:05,840 --> 02:06:08,800
well technically i'm using the load as
3112
02:06:08,800 --> 02:06:10,239
dependency right
3113
02:06:10,239 --> 02:06:12,400
but we didn't push up the node modules
3114
02:06:12,400 --> 02:06:15,440
folder so how the person who is going to
3115
02:06:15,440 --> 02:06:18,079
get this repo will be able to run the
3116
02:06:18,079 --> 02:06:19,920
code in the app.js
3117
02:06:19,920 --> 02:06:22,880
since there's no node modules and you
3118
02:06:22,880 --> 02:06:25,040
told us previously that we can only run
3119
02:06:25,040 --> 02:06:28,079
it if the dependency is there well let's
3120
02:06:28,079 --> 02:06:30,000
check it out i'm going to go with clone
3121
02:06:30,000 --> 02:06:32,719
option so i'll just get the
3122
02:06:32,719 --> 02:06:36,079
url i'll navigate back here
3123
02:06:36,079 --> 02:06:38,480
and i'll do that in messed up again it
3124
02:06:38,480 --> 02:06:40,239
doesn't really matter
3125
02:06:40,239 --> 02:06:41,920
we're just going to go with desktop and
3126
02:06:41,920 --> 02:06:43,840
then git clone
3127
02:06:43,840 --> 02:06:47,040
now i copy and paste the url i'll open
3128
02:06:47,040 --> 02:06:48,320
up a
3129
02:06:48,320 --> 02:06:52,320
new instance of my text error copy and
3130
02:06:52,320 --> 02:06:54,320
paste and this is where the magic
3131
02:06:54,320 --> 02:06:55,599
happens
3132
02:06:55,599 --> 02:06:59,119
if we have package json we simply need
3133
02:06:59,119 --> 02:07:00,079
to run
3134
02:07:00,079 --> 02:07:01,440
npm
3135
02:07:01,440 --> 02:07:02,560
install
3136
02:07:02,560 --> 02:07:04,800
and what is going to happen
3137
02:07:04,800 --> 02:07:06,320
the npm
3138
02:07:06,320 --> 02:07:07,520
will check
3139
02:07:07,520 --> 02:07:09,280
for dependencies that we have in the
3140
02:07:09,280 --> 02:07:13,280
package.json and automatically set up
3141
02:07:13,280 --> 02:07:17,520
that node modules folder so if i go here
3142
02:07:17,520 --> 02:07:19,040
first of all i can see that there has
3143
02:07:19,040 --> 02:07:21,199
been some changes that is already good
3144
02:07:21,199 --> 02:07:23,520
news that just means that
3145
02:07:23,520 --> 02:07:26,480
i have my node modules and if i refresh
3146
02:07:26,480 --> 02:07:29,920
check it out so now of course i have my
3147
02:07:29,920 --> 02:07:33,360
node modules folder and if you have been
3148
02:07:33,360 --> 02:07:35,599
using react applications you're probably
3149
02:07:35,599 --> 02:07:37,520
already familiar with this one where
3150
02:07:37,520 --> 02:07:38,480
again
3151
02:07:38,480 --> 02:07:41,360
when you are setting up the code
3152
02:07:41,360 --> 02:07:43,360
you're not sharing the node modules
3153
02:07:43,360 --> 02:07:46,320
folder and i can showcase that by taking
3154
02:07:46,320 --> 02:07:48,639
a look at my react projects and that's
3155
02:07:48,639 --> 02:07:50,320
not what i wanted i didn't want to look
3156
02:07:50,320 --> 02:07:53,040
at my awesome picture what i wanted is
3157
02:07:53,040 --> 02:07:54,560
react
3158
02:07:54,560 --> 02:07:57,199
and then let's go with project
3159
02:07:57,199 --> 02:08:00,000
and you'll see that in that repo
3160
02:08:00,000 --> 02:08:03,360
we have bunch and bunch of folders and
3161
02:08:03,360 --> 02:08:06,239
each folder has two more folders final
3162
02:08:06,239 --> 02:08:08,480
and setup and now check it out
3163
02:08:08,480 --> 02:08:10,159
none of them have the node models
3164
02:08:10,159 --> 02:08:12,000
because imagine the size
3165
02:08:12,000 --> 02:08:14,800
of this sucker if i would push
3166
02:08:14,800 --> 02:08:18,880
for every project for every folder the
3167
02:08:18,880 --> 02:08:22,400
node modules instead i have package json
3168
02:08:22,400 --> 02:08:25,119
so when you get the repo when you clone
3169
02:08:25,119 --> 02:08:27,840
the repo or download or whatever
3170
02:08:27,840 --> 02:08:30,960
then of course you get the package json
3171
02:08:30,960 --> 02:08:32,400
and in here
3172
02:08:32,400 --> 02:08:34,159
these are the dependencies and you just
3173
02:08:34,159 --> 02:08:37,040
run npm install and they are being
3174
02:08:37,040 --> 02:08:39,520
installed okay hopefully it is clear
3175
02:08:39,520 --> 02:08:41,599
that why it is so crucial to have the
3176
02:08:41,599 --> 02:08:43,920
package.json because we can just share
3177
02:08:43,920 --> 02:08:46,800
the code without dragging the massive
3178
02:08:46,800 --> 02:08:49,599
node modules folder with us
3179
02:08:49,599 --> 02:08:52,079
we can simply just provide what packages
3180
02:08:52,079 --> 02:08:54,880
our project is using
3181
02:08:54,880 --> 02:08:58,079
and then once we get the repo then we
3182
02:08:58,079 --> 02:09:01,360
just run npm install that is the command
3183
02:09:01,360 --> 02:09:03,760
and then npm will install all the
3184
02:09:03,760 --> 02:09:06,639
packages that are in the dependencies
3185
02:09:06,639 --> 02:09:09,119
awesome we're familiar with
3186
02:09:09,119 --> 02:09:12,079
npm we're familiar with package.json
3187
02:09:12,079 --> 02:09:15,119
now let's finally install the dependency
3188
02:09:15,119 --> 02:09:17,199
that we'll use for the remainder of the
3189
02:09:17,199 --> 02:09:20,719
course and that is no other than the
3190
02:09:20,719 --> 02:09:23,119
node one and of course if you want to
3191
02:09:23,119 --> 02:09:26,480
get more info you can visit the npm and
3192
02:09:26,480 --> 02:09:28,560
you can search for the package but as
3193
02:09:28,560 --> 02:09:30,400
far as the general setup
3194
02:09:30,400 --> 02:09:33,040
is just to watch our files
3195
02:09:33,040 --> 02:09:36,480
and then restart our app for us so that
3196
02:09:36,480 --> 02:09:39,760
way we don't have to each and every time
3197
02:09:39,760 --> 02:09:42,880
type node and then whatever the file
3198
02:09:42,880 --> 02:09:43,679
name
3199
02:09:43,679 --> 02:09:46,400
and we can install it as a simple
3200
02:09:46,400 --> 02:09:48,800
dependency that is definitely an option
3201
02:09:48,800 --> 02:09:51,040
but since i also would want to cover
3202
02:09:51,040 --> 02:09:53,520
depth dependencies we'll install that as
3203
02:09:53,520 --> 02:09:56,400
a depth dependency and the command for
3204
02:09:56,400 --> 02:09:58,880
dev dependencies is following where we
3205
02:09:58,880 --> 02:10:02,079
go with npm and then i or install
3206
02:10:02,079 --> 02:10:05,440
whatever and then we go with nodemon so
3207
02:10:05,440 --> 02:10:07,199
that is the package name and then you
3208
02:10:07,199 --> 02:10:10,159
can either do hyphen d
3209
02:10:10,159 --> 02:10:12,800
so that just signals that that is a that
3210
02:10:12,800 --> 02:10:16,880
dependency or you'll see this save and
3211
02:10:16,880 --> 02:10:20,400
dev so again whichever method you choose
3212
02:10:20,400 --> 02:10:23,119
that is really up to you but just
3213
02:10:23,119 --> 02:10:25,040
remember that both of them will save it
3214
02:10:25,040 --> 02:10:27,840
as a dev dependency so let's install it
3215
02:10:27,840 --> 02:10:30,000
and then i'll talk about it why we would
3216
02:10:30,000 --> 02:10:32,639
want to set up as a div dependency and
3217
02:10:32,639 --> 02:10:34,320
the reason for that is because if we
3218
02:10:34,320 --> 02:10:37,520
think about it do we really need nodemon
3219
02:10:37,520 --> 02:10:39,040
in production
3220
02:10:39,040 --> 02:10:40,960
and the answer is no when we push it up
3221
02:10:40,960 --> 02:10:44,159
to digitalocean or heroku or whatever
3222
02:10:44,159 --> 02:10:45,920
we'll of course use something more
3223
02:10:45,920 --> 02:10:48,320
serious than nodemon to restart our
3224
02:10:48,320 --> 02:10:49,440
application
3225
02:10:49,440 --> 02:10:52,320
pm2 comes to mind but while we're
3226
02:10:52,320 --> 02:10:54,800
developing yeah that is an option
3227
02:10:54,800 --> 02:10:57,440
so in this case we'll add right away
3228
02:10:57,440 --> 02:11:00,400
nodemon to the dev dependencies now what
3229
02:11:00,400 --> 02:11:02,560
else what kind of packages we would add
3230
02:11:02,560 --> 02:11:04,239
to dev dependencies
3231
02:11:04,239 --> 02:11:07,280
for example testing packages for example
3232
02:11:07,280 --> 02:11:10,239
linting for example formatting and that
3233
02:11:10,239 --> 02:11:11,920
sort of thing again nothing stops you
3234
02:11:11,920 --> 02:11:14,880
from jamming this in dependencies
3235
02:11:14,880 --> 02:11:15,760
but
3236
02:11:15,760 --> 02:11:18,079
if you think about it it makes way more
3237
02:11:18,079 --> 02:11:20,960
sense if we add this as dev dependency
3238
02:11:20,960 --> 02:11:23,520
so we use it while we are creating the
3239
02:11:23,520 --> 02:11:26,159
app but then once it's in production
3240
02:11:26,159 --> 02:11:28,480
then we just share the dependencies that
3241
02:11:28,480 --> 02:11:31,360
actually the app is using
3242
02:11:31,360 --> 02:11:33,520
not the ones that we used while we
3243
02:11:33,520 --> 02:11:35,440
developed the app hopefully that is
3244
02:11:35,440 --> 02:11:36,320
clear
3245
02:11:36,320 --> 02:11:38,560
and once we have the package we're
3246
02:11:38,560 --> 02:11:40,400
almost there now we just need to
3247
02:11:40,400 --> 02:11:42,880
understand how the scripts
3248
02:11:42,880 --> 02:11:45,679
work in a package.json and at the moment
3249
02:11:45,679 --> 02:11:47,360
as you can see we have the test one we
3250
02:11:47,360 --> 02:11:49,760
won't use that one and inside of the
3251
02:11:49,760 --> 02:11:52,159
script object we just set up the
3252
02:11:52,159 --> 02:11:55,040
commands and as a side note
3253
02:11:55,040 --> 02:11:56,800
we can set up the command right now even
3254
02:11:56,800 --> 02:11:59,440
without the node mod for example i could
3255
02:11:59,440 --> 02:12:02,560
go with start and that command will be
3256
02:12:02,560 --> 02:12:05,199
equal to npm
3257
02:12:05,199 --> 02:12:08,159
and or i'm sorry node and then for
3258
02:12:08,159 --> 02:12:11,679
example app.js so that is my command and
3259
02:12:11,679 --> 02:12:14,480
once i save my package.json instead of
3260
02:12:14,480 --> 02:12:17,840
running this node app.js i can simply
3261
02:12:17,840 --> 02:12:21,280
run npm and start and there it is as you
3262
02:12:21,280 --> 02:12:22,400
can see
3263
02:12:22,400 --> 02:12:26,079
same functionality i run app.js and i
3264
02:12:26,079 --> 02:12:28,560
get back the array as well as the hello
3265
02:12:28,560 --> 02:12:30,560
world again we still exit the
3266
02:12:30,560 --> 02:12:32,880
application so it's not like we're out
3267
02:12:32,880 --> 02:12:34,400
of the woods but i'm just showcasing
3268
02:12:34,400 --> 02:12:37,040
that yes we can set up the commands and
3269
02:12:37,040 --> 02:12:39,280
then in the command we just provide a
3270
02:12:39,280 --> 02:12:41,679
value and in this case of course it is
3271
02:12:41,679 --> 02:12:44,320
node and app.js
3272
02:12:44,320 --> 02:12:46,480
now for some commands you can simply
3273
02:12:46,480 --> 02:12:49,920
type npm start as you can see but for
3274
02:12:49,920 --> 02:12:52,719
some of them you'll have to provide the
3275
02:12:52,719 --> 02:12:55,119
full value and that is going to be npm
3276
02:12:55,119 --> 02:12:57,280
run and nor to showcase that i'm going
3277
02:12:57,280 --> 02:12:59,760
to go with dev and that one will be
3278
02:12:59,760 --> 02:13:02,239
equal and here we're gonna go with a
3279
02:13:02,239 --> 02:13:03,360
node
3280
02:13:03,360 --> 02:13:06,880
mod and then app.js so instead of
3281
02:13:06,880 --> 02:13:08,079
running node
3282
02:13:08,079 --> 02:13:10,320
like we're doing in the start now i'm
3283
02:13:10,320 --> 02:13:13,119
setting up a dev command and in order to
3284
02:13:13,119 --> 02:13:15,840
run this one i'll have to go with
3285
02:13:15,840 --> 02:13:17,679
run and then whatever here's the command
3286
02:13:17,679 --> 02:13:19,840
name again some of them you'll be able
3287
02:13:19,840 --> 02:13:21,520
to use the shortcut where you go with
3288
02:13:21,520 --> 02:13:23,440
start and some of them you'll have to go
3289
02:13:23,440 --> 02:13:26,560
the full route with npm run and then
3290
02:13:26,560 --> 02:13:28,239
whatever is the command and as a side
3291
02:13:28,239 --> 02:13:31,760
note yes you can still run npm run start
3292
02:13:31,760 --> 02:13:33,760
and this is also going to invoke the
3293
02:13:33,760 --> 02:13:35,360
command but
3294
02:13:35,360 --> 02:13:38,000
in our case we have dev and in here i
3295
02:13:38,000 --> 02:13:38,800
have
3296
02:13:38,800 --> 02:13:40,000
nodemon
3297
02:13:40,000 --> 02:13:42,560
and then app js
3298
02:13:42,560 --> 02:13:45,199
so let's try it out let's say first the
3299
02:13:45,199 --> 02:13:47,599
package json and you should notice
3300
02:13:47,599 --> 02:13:50,159
something pretty cool where if i go with
3301
02:13:50,159 --> 02:13:51,440
npm
3302
02:13:51,440 --> 02:13:53,599
run and dev
3303
02:13:53,599 --> 02:13:56,000
now i'm spinning up the node one
3304
02:13:56,000 --> 02:13:59,520
and it tells me that nodemon is watching
3305
02:13:59,520 --> 02:14:01,360
my application
3306
02:14:01,360 --> 02:14:03,119
and since i know that of course we can
3307
02:14:03,119 --> 02:14:06,000
test it out where i'm gonna go with
3308
02:14:06,000 --> 02:14:07,199
console.log
3309
02:14:07,199 --> 02:14:09,599
and then instead of the hello world
3310
02:14:09,599 --> 02:14:12,000
which i have after new items i'm gonna
3311
02:14:12,000 --> 02:14:14,960
go with hello people and you should see
3312
02:14:14,960 --> 02:14:17,520
something interesting where once i
3313
02:14:17,520 --> 02:14:21,840
change the text yep nodemon restarts
3314
02:14:21,840 --> 02:14:23,920
my app and now of course my value is
3315
02:14:23,920 --> 02:14:27,440
hello people awesome so now i don't have
3316
02:14:27,440 --> 02:14:30,960
to type every time the node and then
3317
02:14:30,960 --> 02:14:34,159
whatever file name nodemon automatically
3318
02:14:34,159 --> 02:14:37,360
just restarts my app and i simply set it
3319
02:14:37,360 --> 02:14:41,040
up as a script now if you want to change
3320
02:14:41,040 --> 02:14:43,360
this around if you don't want to run npm
3321
02:14:43,360 --> 02:14:46,960
around dev you can simply say nodemon
3322
02:14:46,960 --> 02:14:49,599
and then app.js and just remove the dev1
3323
02:14:49,599 --> 02:14:52,000
completely and a side note again if you
3324
02:14:52,000 --> 02:14:55,119
have worked with react project you are
3325
02:14:55,119 --> 02:14:57,040
familiar with the setup because for
3326
02:14:57,040 --> 02:14:59,679
react again we run npm start and then
3327
02:14:59,679 --> 02:15:03,040
there's a value that spins up the whole
3328
02:15:03,040 --> 02:15:05,760
react dev server where we can see our
3329
02:15:05,760 --> 02:15:08,159
application not bad not bad well we're
3330
02:15:08,159 --> 02:15:10,800
still on a roll let's cover how we can
3331
02:15:10,800 --> 02:15:13,920
uninstall the package as well and as a
3332
02:15:13,920 --> 02:15:16,159
side note if you want to stop the
3333
02:15:16,159 --> 02:15:19,199
nodemon just press again ctrl and c
3334
02:15:19,199 --> 02:15:22,159
and notice how we're of course stopping
3335
02:15:22,159 --> 02:15:25,119
the nodemon and again if we want to spin
3336
02:15:25,119 --> 02:15:29,679
up we'll just go with npm start and as
3337
02:15:29,679 --> 02:15:30,639
far as
3338
02:15:30,639 --> 02:15:32,480
uninstalling the package we have the
3339
02:15:32,480 --> 02:15:34,480
command for that and the name is
3340
02:15:34,480 --> 02:15:38,079
uninstall so i'll stop the nodemon and
3341
02:15:38,079 --> 02:15:41,000
in the terminal i'll type npm
3342
02:15:41,000 --> 02:15:43,599
uninstall and then of course the package
3343
02:15:43,599 --> 02:15:46,719
name which in my case is going to be a
3344
02:15:46,719 --> 02:15:49,040
bootstrap now this is one of the
3345
02:15:49,040 --> 02:15:50,719
approaches how we can do that and
3346
02:15:50,719 --> 02:15:53,599
there's also a nuclear approach now why
3347
02:15:53,599 --> 02:15:55,119
i'm calling this a nuclear approach
3348
02:15:55,119 --> 02:15:57,679
because that way we remove the whole
3349
02:15:57,679 --> 02:16:00,400
node modules folder and don't worry
3350
02:16:00,400 --> 02:16:02,960
once you run the npm install then of
3351
02:16:02,960 --> 02:16:05,040
course you set everything back up from
3352
02:16:05,040 --> 02:16:06,960
the scratch as well as you would remove
3353
02:16:06,960 --> 02:16:09,520
this package like json and don't worry i
3354
02:16:09,520 --> 02:16:13,199
will cover the package.json file a bit
3355
02:16:13,199 --> 02:16:14,079
later
3356
02:16:14,079 --> 02:16:16,400
and the way that is going to look like
3357
02:16:16,400 --> 02:16:18,560
well we can first clear the terminal
3358
02:16:18,560 --> 02:16:20,560
i'll install the bootstrap from the
3359
02:16:20,560 --> 02:16:23,679
scratch again so say bootstrap here and
3360
02:16:23,679 --> 02:16:24,639
by the way
3361
02:16:24,639 --> 02:16:27,440
probably need to add npm i
3362
02:16:27,440 --> 02:16:29,679
and now again as you can see i have it
3363
02:16:29,679 --> 02:16:32,240
as my dependency and then like i said
3364
02:16:32,240 --> 02:16:34,959
the nuclear approach is removing the
3365
02:16:34,959 --> 02:16:36,799
node modules yep
3366
02:16:36,799 --> 02:16:40,000
and then removing the package log
3367
02:16:40,000 --> 02:16:44,558
and then just running npm install now of
3368
02:16:44,558 --> 02:16:46,799
course in the package.json if i want to
3369
02:16:46,799 --> 02:16:48,799
remove the bootstrap for example in this
3370
02:16:48,799 --> 02:16:50,799
case i can simply remove it from my
3371
02:16:50,799 --> 02:16:53,120
dependencies so i just remove it in the
3372
02:16:53,120 --> 02:16:56,000
package.json then clear everything
3373
02:16:56,000 --> 02:16:59,760
and then we go with npm and then install
3374
02:16:59,760 --> 02:17:02,558
and now we'll get from the scratch the
3375
02:17:02,558 --> 02:17:04,160
node modules
3376
02:17:04,160 --> 02:17:06,318
since we're the ones who removed it
3377
02:17:06,318 --> 02:17:09,359
and also we'll get that package hyphen
3378
02:17:09,359 --> 02:17:12,240
lock json now i'm showing you that
3379
02:17:12,240 --> 02:17:14,638
nuclear approach because if you have
3380
02:17:14,638 --> 02:17:16,558
used gatsby you know that cats be
3381
02:17:16,558 --> 02:17:19,920
somewhat notorious for sometimes just
3382
02:17:19,920 --> 02:17:22,799
being a little bit annoying where you do
3383
02:17:22,799 --> 02:17:24,718
need to remove the cache folder you do
3384
02:17:24,718 --> 02:17:27,040
need to remove the node modules as well
3385
02:17:27,040 --> 02:17:29,679
as package lock and then once you start
3386
02:17:29,679 --> 02:17:31,599
up everything from scratch
3387
02:17:31,599 --> 02:17:32,398
then
3388
02:17:32,398 --> 02:17:35,599
as a magic the gatsby app starts working
3389
02:17:35,599 --> 02:17:37,439
and check it out if we take a look at
3390
02:17:37,439 --> 02:17:39,920
the node modules now yes there's a bunch
3391
02:17:39,920 --> 02:17:43,439
of modules that nodemon is using right
3392
02:17:43,439 --> 02:17:45,840
but there's no bootstrap and i can
3393
02:17:45,840 --> 02:17:48,080
clearly see that because there's nothing
3394
02:17:48,080 --> 02:17:50,879
under the letter of b so we can clearly
3395
02:17:50,879 --> 02:17:53,760
see that we removed bootstrap from our
3396
02:17:53,760 --> 02:17:55,200
dependencies
3397
02:17:55,200 --> 02:17:56,799
all right and now let's take a look at
3398
02:17:56,799 --> 02:18:00,318
how we can install package globally and
3399
02:18:00,318 --> 02:18:03,120
what would be some of the use cases and
3400
02:18:03,120 --> 02:18:06,080
first let's start with command let's jog
3401
02:18:06,080 --> 02:18:08,799
our memory command was npm install and
3402
02:18:08,799 --> 02:18:10,718
then dash g
3403
02:18:10,718 --> 02:18:13,359
and the package name and in mac they
3404
02:18:13,359 --> 02:18:15,840
might ask you for permissions so you'll
3405
02:18:15,840 --> 02:18:17,760
have to start with sudo and then again
3406
02:18:17,760 --> 02:18:20,799
same spiel npm install blah blah blah
3407
02:18:20,799 --> 02:18:23,040
and first i want to showcase that i
3408
02:18:23,040 --> 02:18:24,558
haven't installed
3409
02:18:24,558 --> 02:18:26,799
nodemon globally
3410
02:18:26,799 --> 02:18:29,040
and the easiest way for me to showcase
3411
02:18:29,040 --> 02:18:34,240
that is by running nodemon and app js so
3412
02:18:34,240 --> 02:18:36,558
we'll try to install
3413
02:18:36,558 --> 02:18:38,638
nodeman package globally why well
3414
02:18:38,638 --> 02:18:41,599
because then i can use nodemon in any of
3415
02:18:41,599 --> 02:18:44,080
my projects at the moment i have it as a
3416
02:18:44,080 --> 02:18:46,638
local package and of course i can spin
3417
02:18:46,638 --> 02:18:49,439
it up by running npm start but let's
3418
02:18:49,439 --> 02:18:51,439
imagine scenario where i have i don't
3419
02:18:51,439 --> 02:18:53,760
know 20 nodal applications and i'm
3420
02:18:53,760 --> 02:18:56,160
constantly working on node applications
3421
02:18:56,160 --> 02:18:59,760
so to make my life easier i will install
3422
02:18:59,760 --> 02:19:02,318
node 1 globally and then i won't get
3423
02:19:02,318 --> 02:19:04,000
this error because at the moment you can
3424
02:19:04,000 --> 02:19:06,959
see command not found normal all right
3425
02:19:06,959 --> 02:19:09,120
so how do we do that well we can install
3426
02:19:09,120 --> 02:19:10,080
it
3427
02:19:10,080 --> 02:19:13,120
in our terminal or we can do it in the
3428
02:19:13,120 --> 02:19:14,959
integrated thermal it doesn't really
3429
02:19:14,959 --> 02:19:16,799
matter when it comes to global packages
3430
02:19:16,799 --> 02:19:19,519
you can install it from anywhere so in
3431
02:19:19,519 --> 02:19:20,959
my case i'm just going to navigate back
3432
02:19:20,959 --> 02:19:22,959
to my terminal just so you don't think
3433
02:19:22,959 --> 02:19:24,318
that i'm cheating
3434
02:19:24,318 --> 02:19:27,200
and i'll zoom in and we'll simply run
3435
02:19:27,200 --> 02:19:29,599
and by the way i need to start with sudo
3436
02:19:29,599 --> 02:19:32,080
and then npm install
3437
02:19:32,080 --> 02:19:34,318
and hyphen g and of course we're looking
3438
02:19:34,318 --> 02:19:37,120
for nodemon so now of course
3439
02:19:37,120 --> 02:19:38,558
i'm prompted
3440
02:19:38,558 --> 02:19:41,599
to enter my password and once i enter my
3441
02:19:41,599 --> 02:19:42,718
password
3442
02:19:42,718 --> 02:19:45,760
of course i'm going to install nodemon
3443
02:19:45,760 --> 02:19:47,760
globally and now
3444
02:19:47,760 --> 02:19:51,280
in any of my projects i can simply go
3445
02:19:51,280 --> 02:19:53,920
with command of nodemon and then
3446
02:19:53,920 --> 02:19:54,960
whatever
3447
02:19:54,960 --> 02:19:57,520
is the file name now
3448
02:19:57,520 --> 02:20:00,080
to show you some of the use cases if you
3449
02:20:00,080 --> 02:20:02,479
work with gatsby you know that they have
3450
02:20:02,479 --> 02:20:06,319
the global gatsby cli tool and that was
3451
02:20:06,319 --> 02:20:08,319
one of the reasons why you installed
3452
02:20:08,319 --> 02:20:10,479
something globally is because well you
3453
02:20:10,479 --> 02:20:13,040
used one of the frameworks for example
3454
02:20:13,040 --> 02:20:16,240
react or gatsby in this case and then of
3455
02:20:16,240 --> 02:20:17,920
course in order to set up the gatsby
3456
02:20:17,920 --> 02:20:19,359
project or
3457
02:20:19,359 --> 02:20:21,520
react project you needed to install this
3458
02:20:21,520 --> 02:20:24,080
globally now things have changed though
3459
02:20:24,080 --> 02:20:27,680
with arrival of npx and i'll talk about
3460
02:20:27,680 --> 02:20:30,000
the npx at the end of the video but
3461
02:20:30,000 --> 02:20:32,160
notice if you navigate to your react
3462
02:20:32,160 --> 02:20:35,359
docs and if you look for create react
3463
02:20:35,359 --> 02:20:37,280
app which again
3464
02:20:37,280 --> 02:20:39,680
was something that you needed to install
3465
02:20:39,680 --> 02:20:40,960
globally
3466
02:20:40,960 --> 02:20:44,160
now they suggest this npx route where
3467
02:20:44,160 --> 02:20:46,479
essentially you go with npx and then
3468
02:20:46,479 --> 02:20:48,640
again whatever is the command name for
3469
02:20:48,640 --> 02:20:51,040
example for create react app that would
3470
02:20:51,040 --> 02:20:54,319
be create react app for strappy the
3471
02:20:54,319 --> 02:20:56,880
headless cms that would be create
3472
02:20:56,880 --> 02:20:58,800
strappy app and hopefully you get the
3473
02:20:58,800 --> 02:21:01,520
gist where for every resource that of
3474
02:21:01,520 --> 02:21:04,080
course would be different and with that
3475
02:21:04,080 --> 02:21:06,160
command you don't have to install that
3476
02:21:06,160 --> 02:21:08,080
tool globally
3477
02:21:08,080 --> 02:21:09,600
for example again in our case we're
3478
02:21:09,600 --> 02:21:12,640
installing nodemon but normally prior to
3479
02:21:12,640 --> 02:21:15,760
npx you would install this create react
3480
02:21:15,760 --> 02:21:18,160
app globally and then you can spin up
3481
02:21:18,160 --> 02:21:20,399
those react projects now i'm going to go
3482
02:21:20,399 --> 02:21:22,560
back to my terminal as you can see i was
3483
02:21:22,560 --> 02:21:25,280
successful so what's going to happen if
3484
02:21:25,280 --> 02:21:28,080
i go back to my project and if i run
3485
02:21:28,080 --> 02:21:30,720
nodemon and app.js since i have
3486
02:21:30,720 --> 02:21:33,520
installed that package globally
3487
02:21:33,520 --> 02:21:36,560
there it is i spin up my app.js and if
3488
02:21:36,560 --> 02:21:38,560
you don't believe me i can change it
3489
02:21:38,560 --> 02:21:41,920
back to hello world and i have no issues
3490
02:21:41,920 --> 02:21:44,399
hello world and there it is now of
3491
02:21:44,399 --> 02:21:47,200
course i have hello world e in the
3492
02:21:47,200 --> 02:21:49,760
console so hopefully it is clear that
3493
02:21:49,760 --> 02:21:53,200
yes we can install packages globally and
3494
02:21:53,200 --> 02:21:56,080
yes one of the biggest use cases was
3495
02:21:56,080 --> 02:21:58,399
working with some type of front-end
3496
02:21:58,399 --> 02:21:59,439
library
3497
02:21:59,439 --> 02:22:02,399
but with an arrival of npx
3498
02:22:02,399 --> 02:22:04,960
things have shifted where now those
3499
02:22:04,960 --> 02:22:08,479
libraries suggest using the npx route
3500
02:22:08,479 --> 02:22:12,000
instead as far as my personal preference
3501
02:22:12,000 --> 02:22:14,720
since i'm recording a lot of courses and
3502
02:22:14,720 --> 02:22:16,800
since some of the global packages
3503
02:22:16,800 --> 02:22:19,439
usually introduce some kind of bugs
3504
02:22:19,439 --> 02:22:21,359
to the students now not for all the
3505
02:22:21,359 --> 02:22:23,920
students but there's always this one
3506
02:22:23,920 --> 02:22:26,800
student who just has a lot of issues
3507
02:22:26,800 --> 02:22:28,319
with the global package
3508
02:22:28,319 --> 02:22:30,640
personally i avoid them as much as
3509
02:22:30,640 --> 02:22:33,680
possible so i always either use the mpx
3510
02:22:33,680 --> 02:22:36,000
or i just set it up as a local
3511
02:22:36,000 --> 02:22:38,560
dependency again it's not a rule you can
3512
02:22:38,560 --> 02:22:40,800
do whatever you would want but i'm just
3513
02:22:40,800 --> 02:22:43,760
telling you what is my preferred option
3514
02:22:43,760 --> 02:22:46,720
as far as what is npx it stands for
3515
02:22:46,720 --> 02:22:50,399
execute and official name is the package
3516
02:22:50,399 --> 02:22:52,720
runner it is a feature that was
3517
02:22:52,720 --> 02:22:55,479
introduced in npm
3518
02:22:55,479 --> 02:22:57,200
5.2
3519
02:22:57,200 --> 02:22:59,680
and again the main idea is following
3520
02:22:59,680 --> 02:23:02,640
where you can run that cli tool for
3521
02:23:02,640 --> 02:23:05,359
example in this case create react app
3522
02:23:05,359 --> 02:23:08,640
without installing these globally so as
3523
02:23:08,640 --> 02:23:10,800
long as you have npm with a version of
3524
02:23:10,800 --> 02:23:14,160
5.2 or greater instead of doing this
3525
02:23:14,160 --> 02:23:17,200
whole spiel of setting up the cli tool
3526
02:23:17,200 --> 02:23:20,000
globally you just go with npx and then
3527
02:23:20,000 --> 02:23:21,920
whatever here is the tool name and of
3528
02:23:21,920 --> 02:23:25,040
course the last one is the folder name
3529
02:23:25,040 --> 02:23:27,760
as far as the package log json
3530
02:23:27,760 --> 02:23:30,399
why do we need it well if we take a look
3531
02:23:30,399 --> 02:23:33,040
at our dependencies we can see that they
3532
02:23:33,040 --> 02:23:34,800
have versions
3533
02:23:34,800 --> 02:23:36,720
and if you remember
3534
02:23:36,720 --> 02:23:38,560
some of the dependencies have
3535
02:23:38,560 --> 02:23:41,359
dependencies on their own and of course
3536
02:23:41,359 --> 02:23:43,200
we need to keep in mind that those
3537
02:23:43,200 --> 02:23:47,040
dependencies have versions as well
3538
02:23:47,040 --> 02:23:50,000
and for example the person who gets your
3539
02:23:50,000 --> 02:23:53,200
project you probably want them to have
3540
02:23:53,200 --> 02:23:55,439
the same exact setup because keep in
3541
02:23:55,439 --> 02:23:56,319
mind
3542
02:23:56,319 --> 02:23:58,960
as these versions change
3543
02:23:58,960 --> 02:24:01,920
well so does some of the functionality
3544
02:24:01,920 --> 02:24:04,319
correct so for example you set up to
3545
02:24:04,319 --> 02:24:06,240
your whole project you use some kind of
3546
02:24:06,240 --> 02:24:10,080
dependency that uses some other package
3547
02:24:10,080 --> 02:24:12,800
and that version changes not for example
3548
02:24:12,800 --> 02:24:15,920
for the bootstrap but for the jquery and
3549
02:24:15,920 --> 02:24:18,560
then pretty much your project
3550
02:24:18,560 --> 02:24:20,319
is obsolete meaning
3551
02:24:20,319 --> 02:24:23,200
it might get some bugs because the
3552
02:24:23,200 --> 02:24:26,160
version of the jquery changed so that's
3553
02:24:26,160 --> 02:24:28,720
why you have this package.json
3554
02:24:28,720 --> 02:24:31,439
and in there you have those specific
3555
02:24:31,439 --> 02:24:34,160
versions for all the packages
3556
02:24:34,160 --> 02:24:37,680
not only for dependencies but also for
3557
02:24:37,680 --> 02:24:38,960
the packages
3558
02:24:38,960 --> 02:24:43,040
that the dependency is using now as far
3559
02:24:43,040 --> 02:24:45,840
as this version we have three values and
3560
02:24:45,840 --> 02:24:48,640
you can think of it as a contract
3561
02:24:48,640 --> 02:24:50,080
between us
3562
02:24:50,080 --> 02:24:52,319
the people who are using the package and
3563
02:24:52,319 --> 02:24:55,920
the person who is creating and first
3564
02:24:55,920 --> 02:24:58,640
number is a major change so when this
3565
02:24:58,640 --> 02:25:00,560
changes that means that there are some
3566
02:25:00,560 --> 02:25:03,600
breaking changes now the second one is a
3567
02:25:03,600 --> 02:25:06,000
minor one so that means that it is
3568
02:25:06,000 --> 02:25:08,399
backward compatible so for example if
3569
02:25:08,399 --> 02:25:10,399
this changes to 18
3570
02:25:10,399 --> 02:25:12,880
i shouldn't expect any breaking changes
3571
02:25:12,880 --> 02:25:15,439
and the last one is just a patch for the
3572
02:25:15,439 --> 02:25:17,520
bug fix so that's also something to keep
3573
02:25:17,520 --> 02:25:20,560
in mind when you decide to publish your
3574
02:25:20,560 --> 02:25:23,439
own package that of course that's why we
3575
02:25:23,439 --> 02:25:26,640
have here this version now lastly i
3576
02:25:26,640 --> 02:25:28,160
would just want to mention that if
3577
02:25:28,160 --> 02:25:31,120
you're interested in more info about the
3578
02:25:31,120 --> 02:25:33,439
package.json i would suggest this
3579
02:25:33,439 --> 02:25:34,720
resource
3580
02:25:34,720 --> 02:25:37,200
so i simply went with package.json then
3581
02:25:37,200 --> 02:25:39,439
keep on scrolling keep on scrolling and
3582
02:25:39,439 --> 02:25:41,920
you're looking for i guess this blog
3583
02:25:41,920 --> 02:25:45,080
post right so the name is the basics of
3584
02:25:45,080 --> 02:25:48,960
package.json and in here you literally
3585
02:25:48,960 --> 02:25:51,439
find everything explained to the
3586
02:25:51,439 --> 02:25:54,240
smallest detail whether that is a name
3587
02:25:54,240 --> 02:25:56,960
version and rest of the fields that you
3588
02:25:56,960 --> 02:26:00,399
can find in a package.json beautiful we
3589
02:26:00,399 --> 02:26:03,040
now know how to utilize npm and for
3590
02:26:03,040 --> 02:26:05,920
starters as a result for the remainder
3591
02:26:05,920 --> 02:26:07,760
of the course instead of frantically
3592
02:26:07,760 --> 02:26:08,840
typing
3593
02:26:08,840 --> 02:26:12,000
node.js in a terminal we'll simply spin
3594
02:26:12,000 --> 02:26:14,319
up nodemon and it will watch for the
3595
02:26:14,319 --> 02:26:17,040
changes and restart the app for us
3596
02:26:17,040 --> 02:26:19,359
now what before we move on to creating
3597
02:26:19,359 --> 02:26:21,920
servers with express.js there are a few
3598
02:26:21,920 --> 02:26:23,920
important node topics i would like to
3599
02:26:23,920 --> 02:26:26,240
cover first and those topics are
3600
02:26:26,240 --> 02:26:28,720
following event loop async patterns in
3601
02:26:28,720 --> 02:26:32,000
node.js events emitter and streams in
3602
02:26:32,000 --> 02:26:34,720
node.js while these topics are extremely
3603
02:26:34,720 --> 02:26:36,800
important please keep in mind that our
3604
02:26:36,800 --> 02:26:39,520
goal is to only understand the general
3605
02:26:39,520 --> 02:26:41,520
ideas behind these concepts
3606
02:26:41,520 --> 02:26:44,160
and i only introduce them so you have an
3607
02:26:44,160 --> 02:26:46,399
overall understanding of how things work
3608
02:26:46,399 --> 02:26:49,600
in node.js before we build our first
3609
02:26:49,600 --> 02:26:51,040
node.js app
3610
02:26:51,040 --> 02:26:52,399
if you're not satisfied with my
3611
02:26:52,399 --> 02:26:54,399
explanations or simply want to do more
3612
02:26:54,399 --> 02:26:56,720
research by yourself just type any of
3613
02:26:56,720 --> 02:26:58,479
these terms in your favorite search
3614
02:26:58,479 --> 02:27:00,560
engine and i can guarantee you'll find
3615
02:27:00,560 --> 02:27:02,720
plenty of useful resources within a
3616
02:27:02,720 --> 02:27:04,479
matter of seconds
3617
02:27:04,479 --> 02:27:06,640
like blog posts youtube videos and
3618
02:27:06,640 --> 02:27:09,359
conference talks with that said it's my
3619
02:27:09,359 --> 02:27:11,840
strong opinion that it might be easier
3620
02:27:11,840 --> 02:27:14,399
to understand those concepts more deeply
3621
02:27:14,399 --> 02:27:16,560
so not just a general understanding but
3622
02:27:16,560 --> 02:27:18,800
understanding them more deeply once you
3623
02:27:18,800 --> 02:27:22,080
have one or few working node apps under
3624
02:27:22,080 --> 02:27:24,560
your developer's belt let me also
3625
02:27:24,560 --> 02:27:26,319
mention that in order not to waste your
3626
02:27:26,319 --> 02:27:28,800
time with time consuming setup
3627
02:27:28,800 --> 02:27:31,840
in few upcoming videos i will run some
3628
02:27:31,840 --> 02:27:33,280
pre-built code
3629
02:27:33,280 --> 02:27:35,520
let me be very clear though i'm only
3630
02:27:35,520 --> 02:27:37,600
going to do that in a few videos
3631
02:27:37,600 --> 02:27:39,840
and for the remainder of the course we
3632
02:27:39,840 --> 02:27:42,319
will type out everything together
3633
02:27:42,319 --> 02:27:43,200
all right
3634
02:27:43,200 --> 02:27:45,520
and let's kick things off by discussing
3635
02:27:45,520 --> 02:27:47,120
the event loop
3636
02:27:47,120 --> 02:27:49,680
now event loop in node.js is one of
3637
02:27:49,680 --> 02:27:51,920
those topics where i could spend the
3638
02:27:51,920 --> 02:27:54,960
entire course discussing it and it still
3639
02:27:54,960 --> 02:27:57,200
wouldn't be enough so let's try to avoid
3640
02:27:57,200 --> 02:28:00,000
that and instead just understand the
3641
02:28:00,000 --> 02:28:01,520
journal concepts
3642
02:28:01,520 --> 02:28:03,680
while there are tons of useful event
3643
02:28:03,680 --> 02:28:05,680
loop explanations out there the one that
3644
02:28:05,680 --> 02:28:07,600
i probably like the most
3645
02:28:07,600 --> 02:28:08,880
is this one
3646
02:28:08,880 --> 02:28:12,640
the event loop is what allows node.js
3647
02:28:12,640 --> 02:28:16,800
to perform non-blocking i o so input and
3648
02:28:16,800 --> 02:28:19,280
output operations despite the fact that
3649
02:28:19,280 --> 02:28:22,560
javascript is single threaded by
3650
02:28:22,560 --> 02:28:24,880
offloading operations to the system
3651
02:28:24,880 --> 02:28:27,760
kernel whenever possible
3652
02:28:27,760 --> 02:28:30,160
and as you can see i'm reading straight
3653
02:28:30,160 --> 02:28:32,560
from the node docs
3654
02:28:32,560 --> 02:28:35,359
now don't beat yourself up if this
3655
02:28:35,359 --> 02:28:37,359
sounds like a lot of gibberish there's a
3656
02:28:37,359 --> 02:28:40,000
reason why one can dedicate the entire
3657
02:28:40,000 --> 02:28:43,520
course just for event loop it is a
3658
02:28:43,520 --> 02:28:46,160
pretty complex topic but
3659
02:28:46,160 --> 02:28:48,640
one word i do want you to remember
3660
02:28:48,640 --> 02:28:51,439
is this offloading and you'll see why in
3661
02:28:51,439 --> 02:28:53,920
a second also don't worry i have
3662
02:28:53,920 --> 02:28:56,479
prepared more examples as well as some
3663
02:28:56,479 --> 02:28:59,840
slides to get my point across but just
3664
02:28:59,840 --> 02:29:01,439
in case you're not happy with my
3665
02:29:01,439 --> 02:29:02,560
examples
3666
02:29:02,560 --> 02:29:05,200
or you just want to explore the note
3667
02:29:05,200 --> 02:29:07,520
event loop in greater detail here are a
3668
02:29:07,520 --> 02:29:10,479
few external resources i find
3669
02:29:10,479 --> 02:29:12,560
particularly useful
3670
02:29:12,560 --> 02:29:14,479
when it comes to blog posts
3671
02:29:14,479 --> 02:29:16,479
just go to your favorite search engine
3672
02:29:16,479 --> 02:29:18,240
and type node.js
3673
02:29:18,240 --> 02:29:19,760
event loop
3674
02:29:19,760 --> 02:29:22,240
and the one that i find really useful is
3675
02:29:22,240 --> 02:29:23,120
this one
3676
02:29:23,120 --> 02:29:27,280
and the resource is nodejs and not.org
3677
02:29:27,280 --> 02:29:29,920
but it is dot dev
3678
02:29:29,920 --> 02:29:33,200
and then follow the link and here they
3679
02:29:33,200 --> 02:29:35,359
cover event loop
3680
02:29:35,359 --> 02:29:38,640
in great detail with a lot of cool
3681
02:29:38,640 --> 02:29:41,760
examples and pictures and slides and all
3682
02:29:41,760 --> 02:29:44,160
that good stuff and when it comes to
3683
02:29:44,160 --> 02:29:47,359
videos i would suggest going to youtube
3684
02:29:47,359 --> 02:29:49,840
and then just type event loop and the
3685
02:29:49,840 --> 02:29:52,000
first video that's going to pop up is
3686
02:29:52,000 --> 02:29:54,800
going to be the event loop bot in
3687
02:29:54,800 --> 02:29:56,399
browser javascript
3688
02:29:56,399 --> 02:29:58,560
and i'll talk about it in a second
3689
02:29:58,560 --> 02:30:02,880
and the nodejs specific event loop video
3690
02:30:02,880 --> 02:30:05,120
is this one i believe it's 15 minutes
3691
02:30:05,120 --> 02:30:08,560
long and you can see the name over here
3692
02:30:08,560 --> 02:30:11,520
so this is a very very useful video
3693
02:30:11,520 --> 02:30:15,040
where the speaker covers a lot of useful
3694
02:30:15,040 --> 02:30:19,040
details about node.js loop in great
3695
02:30:19,040 --> 02:30:21,520
detail and the reason why i'm suggesting
3696
02:30:21,520 --> 02:30:23,200
the first one as well
3697
02:30:23,200 --> 02:30:24,080
well
3698
02:30:24,080 --> 02:30:26,399
what is the language that we use
3699
02:30:26,399 --> 02:30:28,080
in node
3700
02:30:28,080 --> 02:30:30,160
that of course is javascript right and
3701
02:30:30,160 --> 02:30:31,520
even though there's some differences
3702
02:30:31,520 --> 02:30:33,600
between the event loop
3703
02:30:33,600 --> 02:30:35,680
the one that we use in the browser and
3704
02:30:35,680 --> 02:30:38,720
the one that is in the node.js
3705
02:30:38,720 --> 02:30:41,359
if you understand the concepts behind
3706
02:30:41,359 --> 02:30:43,760
the event loop that we use in a browser
3707
02:30:43,760 --> 02:30:47,200
trust me you're already halfway there to
3708
02:30:47,200 --> 02:30:51,040
understanding how the no js event loop
3709
02:30:51,040 --> 02:30:53,760
works since it's such an important topic
3710
02:30:53,760 --> 02:30:56,160
like promised i have prepared some of my
3711
02:30:56,160 --> 02:30:59,040
own resources as well we'll start with
3712
02:30:59,040 --> 02:31:01,280
the slides and move on to the closed
3713
02:31:01,280 --> 02:31:03,840
examples in next video
3714
02:31:03,840 --> 02:31:06,000
as a side note i made all the course
3715
02:31:06,000 --> 02:31:10,240
slides available on course api.com again
3716
02:31:10,240 --> 02:31:12,840
their website name is coors
3717
02:31:12,840 --> 02:31:15,760
api.com and once you click on the slides
3718
02:31:15,760 --> 02:31:16,640
link
3719
02:31:16,640 --> 02:31:19,120
you'll see all the slides
3720
02:31:19,120 --> 02:31:21,040
and i would want to start our discussion
3721
02:31:21,040 --> 02:31:24,319
by taking a look at what it means that
3722
02:31:24,319 --> 02:31:27,359
javascript is synchronous and single
3723
02:31:27,359 --> 02:31:29,680
threaded and effectively it's just a
3724
02:31:29,680 --> 02:31:32,640
fancy way of saying that just repeat
3725
02:31:32,640 --> 02:31:34,720
everything line by line
3726
02:31:34,720 --> 02:31:36,560
so for example if i have console log
3727
02:31:36,560 --> 02:31:38,080
with first task
3728
02:31:38,080 --> 02:31:40,960
then i have a for loop that takes some
3729
02:31:40,960 --> 02:31:43,040
time in this case two seconds but that
3730
02:31:43,040 --> 02:31:45,040
could be 10 seconds that could be 20 or
3731
02:31:45,040 --> 02:31:47,680
whatever and then i have another console
3732
02:31:47,680 --> 02:31:49,760
log of next task
3733
02:31:49,760 --> 02:31:52,080
javascript will just start reading
3734
02:31:52,080 --> 02:31:54,840
everything and it will read it line by
3735
02:31:54,840 --> 02:31:58,479
line and if this takes a long time it
3736
02:31:58,479 --> 02:32:02,000
will only run the next test once it's
3737
02:32:02,000 --> 02:32:05,520
done performing this time consuming one
3738
02:32:05,520 --> 02:32:07,520
so hopefully that is clear that
3739
02:32:07,520 --> 02:32:10,399
javascript just reads everything line by
3740
02:32:10,399 --> 02:32:11,280
line
3741
02:32:11,280 --> 02:32:13,600
and now let's take a look at our second
3742
02:32:13,600 --> 02:32:16,960
slide and in here we can see the
3743
02:32:16,960 --> 02:32:18,160
solution
3744
02:32:18,160 --> 02:32:21,520
if we would want to offload something to
3745
02:32:21,520 --> 02:32:24,000
the browser so when we're building
3746
02:32:24,000 --> 02:32:26,880
browser javascript apps we have this
3747
02:32:26,880 --> 02:32:27,840
option
3748
02:32:27,840 --> 02:32:29,840
of offloading to the browser now of
3749
02:32:29,840 --> 02:32:31,439
course it doesn't mean that we can
3750
02:32:31,439 --> 02:32:33,840
offload the for loop that's not going to
3751
02:32:33,840 --> 02:32:36,080
work this effectively is still going to
3752
02:32:36,080 --> 02:32:39,600
be the blocking code but browser nicely
3753
02:32:39,600 --> 02:32:41,920
provides the api
3754
02:32:41,920 --> 02:32:44,960
where we can offload those tasks to the
3755
02:32:44,960 --> 02:32:48,160
browser and only when the task is done
3756
02:32:48,160 --> 02:32:51,120
then we execute the callback and
3757
02:32:51,120 --> 02:32:52,800
probably the example we have done the
3758
02:32:52,800 --> 02:32:55,840
most is the fetch essentially when we
3759
02:32:55,840 --> 02:32:58,640
make the network request but we can also
3760
02:32:58,640 --> 02:33:01,920
do that for example with a set timeout
3761
02:33:01,920 --> 02:33:04,080
so i still have console.log with first
3762
02:33:04,080 --> 02:33:07,760
task but then even though my set timeout
3763
02:33:07,760 --> 02:33:08,800
function
3764
02:33:08,800 --> 02:33:11,600
has the second argument of zero so
3765
02:33:11,600 --> 02:33:14,160
essentially i have set timeout function
3766
02:33:14,160 --> 02:33:16,000
i provide the callback function that's
3767
02:33:16,000 --> 02:33:18,000
going to be executed in certain amount
3768
02:33:18,000 --> 02:33:18,960
of time
3769
02:33:18,960 --> 02:33:21,840
even if this is zero meaning you would
3770
02:33:21,840 --> 02:33:25,200
expect this one to run right away it
3771
02:33:25,200 --> 02:33:30,080
only runs after the next task so once
3772
02:33:30,080 --> 02:33:32,960
javascript is done executing the
3773
02:33:32,960 --> 02:33:34,399
immediate code
3774
02:33:34,399 --> 02:33:37,920
only then it executes the callback so in
3775
02:33:37,920 --> 02:33:39,840
this case we have the set timeout the
3776
02:33:39,840 --> 02:33:42,960
api that is provided from the browser
3777
02:33:42,960 --> 02:33:44,560
and we just said that we would want to
3778
02:33:44,560 --> 02:33:47,600
execute that e in zero seconds so
3779
02:33:47,600 --> 02:33:49,280
effectively there is no wait time
3780
02:33:49,280 --> 02:33:52,240
however javascript will first execute
3781
02:33:52,240 --> 02:33:55,439
this code and only then will execute the
3782
02:33:55,439 --> 02:33:58,880
callback so that way we can offload
3783
02:33:58,880 --> 02:34:02,479
those time consuming operations to the
3784
02:34:02,479 --> 02:34:04,319
browser again it doesn't mean that we
3785
02:34:04,319 --> 02:34:06,640
can offload for loops it means that
3786
02:34:06,640 --> 02:34:09,120
browser does provide some apis
3787
02:34:09,120 --> 02:34:11,520
where we don't have to write the
3788
02:34:11,520 --> 02:34:13,840
blocking code now let me be very clear
3789
02:34:13,840 --> 02:34:16,800
though when i say we cannot offload for
3790
02:34:16,800 --> 02:34:17,680
loops
3791
02:34:17,680 --> 02:34:20,479
what i mean is that we can still
3792
02:34:20,479 --> 02:34:23,760
write blocking code in javascript
3793
02:34:23,760 --> 02:34:26,800
but the browser does provide some nice
3794
02:34:26,800 --> 02:34:30,399
apis where we can offload those time
3795
02:34:30,399 --> 02:34:32,479
consuming tasks
3796
02:34:32,479 --> 02:34:35,359
and that brings us to our
3797
02:34:35,359 --> 02:34:39,680
main friend the nodejs event loop again
3798
02:34:39,680 --> 02:34:41,439
before i go over the example let me
3799
02:34:41,439 --> 02:34:44,160
stress something event loop is somewhat
3800
02:34:44,160 --> 02:34:47,520
complex and this is just to give you a
3801
02:34:47,520 --> 02:34:49,200
general understanding
3802
02:34:49,200 --> 02:34:51,600
so let's imagine this scenario i have an
3803
02:34:51,600 --> 02:34:54,479
app and just like any cool app
3804
02:34:54,479 --> 02:34:57,760
i have subscribers or users or however
3805
02:34:57,760 --> 02:34:59,600
you would want to call them and in this
3806
02:34:59,600 --> 02:35:02,560
case since my app is so so popular i
3807
02:35:02,560 --> 02:35:04,399
have eight of them
3808
02:35:04,399 --> 02:35:07,920
and what do the users do well they're
3809
02:35:07,920 --> 02:35:09,280
probably being annoying and they're
3810
02:35:09,280 --> 02:35:11,439
requesting something from the
3811
02:35:11,439 --> 02:35:14,319
application and as the requests are
3812
02:35:14,319 --> 02:35:15,760
coming in
3813
02:35:15,760 --> 02:35:19,120
the event loop is responsible for
3814
02:35:19,120 --> 02:35:22,560
avoiding this type of scenario
3815
02:35:22,560 --> 02:35:24,880
so let's imagine this i have all these
3816
02:35:24,880 --> 02:35:27,120
users the requests are coming in
3817
02:35:27,120 --> 02:35:29,760
but larry the little
3818
02:35:29,760 --> 02:35:31,040
decides that
3819
02:35:31,040 --> 02:35:33,680
in his request there's going to be
3820
02:35:33,680 --> 02:35:36,319
some kind of time consuming i don't know
3821
02:35:36,319 --> 02:35:38,640
database crawl or something like that so
3822
02:35:38,640 --> 02:35:40,800
effectively he's requesting something
3823
02:35:40,800 --> 02:35:42,560
and behind the scenes in my code that
3824
02:35:42,560 --> 02:35:44,800
means that i need to perform something
3825
02:35:44,800 --> 02:35:47,520
that takes a long time
3826
02:35:47,520 --> 02:35:48,319
so
3827
02:35:48,319 --> 02:35:49,760
in this case
3828
02:35:49,760 --> 02:35:52,840
the event loop just registers the
3829
02:35:52,840 --> 02:35:55,280
callback so it registers what needs to
3830
02:35:55,280 --> 02:35:58,080
be done when the task is complete
3831
02:35:58,080 --> 02:36:00,319
because if the event loop wouldn't do
3832
02:36:00,319 --> 02:36:03,040
that then we would have this scenario
3833
02:36:03,040 --> 02:36:05,359
where the requests are coming in
3834
02:36:05,359 --> 02:36:08,479
and because larry is requesting
3835
02:36:08,479 --> 02:36:10,720
something that takes a long time the
3836
02:36:10,720 --> 02:36:13,600
rest of the users would have to wait and
3837
02:36:13,600 --> 02:36:15,600
essentially it's not that the actual
3838
02:36:15,600 --> 02:36:18,000
operation takes a long time it's just
3839
02:36:18,000 --> 02:36:20,319
the fact that we're wasting our time
3840
02:36:20,319 --> 02:36:24,800
on waiting for that operation to be done
3841
02:36:24,800 --> 02:36:27,520
and only then we can serve the other
3842
02:36:27,520 --> 02:36:31,040
users but what the event loop does it
3843
02:36:31,040 --> 02:36:33,680
registers the callback and only when the
3844
02:36:33,680 --> 02:36:37,760
operation is complete it executes it now
3845
02:36:37,760 --> 02:36:39,520
keep in mind that again we're not
3846
02:36:39,520 --> 02:36:42,160
executing this right away
3847
02:36:42,160 --> 02:36:44,160
when we can effectively it's the same
3848
02:36:44,160 --> 02:36:47,920
scenario where we run our immediate code
3849
02:36:47,920 --> 02:36:49,120
first
3850
02:36:49,120 --> 02:36:52,240
and only then when we have the time we
3851
02:36:52,240 --> 02:36:54,000
execute the callback so for example in
3852
02:36:54,000 --> 02:36:56,240
this scenario if i would have hundred
3853
02:36:56,240 --> 02:36:57,840
console logs
3854
02:36:57,840 --> 02:36:59,680
after the next task
3855
02:36:59,680 --> 02:37:02,960
i would run them first and only then the
3856
02:37:02,960 --> 02:37:05,840
second task would appear here regardless
3857
02:37:05,840 --> 02:37:09,120
of what is the time set in here because
3858
02:37:09,120 --> 02:37:11,359
again we're running our immediate tasks
3859
02:37:11,359 --> 02:37:14,080
first and only then we run the callbacks
3860
02:37:14,080 --> 02:37:16,240
so the same thing happens here
3861
02:37:16,240 --> 02:37:18,560
where the requests are coming in let's
3862
02:37:18,560 --> 02:37:21,280
say that the operation is complete we
3863
02:37:21,280 --> 02:37:23,359
first registered the callback operation
3864
02:37:23,359 --> 02:37:25,840
is complete and instead of executing
3865
02:37:25,840 --> 02:37:28,479
that callback right away
3866
02:37:28,479 --> 02:37:31,040
it effectively gets put at the end of
3867
02:37:31,040 --> 02:37:34,000
the line and then when there is no
3868
02:37:34,000 --> 02:37:36,960
immediate code to run then we execute
3869
02:37:36,960 --> 02:37:39,920
the callback hopefully that is clear
3870
02:37:39,920 --> 02:37:42,399
event loop is our best friend because
3871
02:37:42,399 --> 02:37:44,880
with the help of event loop we can
3872
02:37:44,880 --> 02:37:47,840
offload some time consuming operations
3873
02:37:47,840 --> 02:37:50,319
and effectively just keep all our users
3874
02:37:50,319 --> 02:37:52,640
happy all right and once we have looked
3875
02:37:52,640 --> 02:37:55,200
at the event loop in theory
3876
02:37:55,200 --> 02:37:57,840
to hammer this home let's also take a
3877
02:37:57,840 --> 02:38:00,800
look at some code examples where we can
3878
02:38:00,800 --> 02:38:03,280
see event loop in action
3879
02:38:03,280 --> 02:38:06,240
as a side note all code examples are
3880
02:38:06,240 --> 02:38:08,720
located in the event loop example
3881
02:38:08,720 --> 02:38:11,280
directory so if you need to take a look
3882
02:38:11,280 --> 02:38:13,920
just grab the repo and you can find it
3883
02:38:13,920 --> 02:38:16,640
there and you should be familiar with
3884
02:38:16,640 --> 02:38:18,160
our first example
3885
02:38:18,160 --> 02:38:23,040
since it's a async version of read file
3886
02:38:23,040 --> 02:38:24,000
method
3887
02:38:24,000 --> 02:38:26,960
so we import read file from the
3888
02:38:26,960 --> 02:38:29,920
fs module and then we have console.log
3889
02:38:29,920 --> 02:38:32,240
started our task first
3890
02:38:32,240 --> 02:38:33,840
then we have
3891
02:38:33,840 --> 02:38:36,960
read file method where we pass the path
3892
02:38:36,960 --> 02:38:39,280
we pass the encoding and then of course
3893
02:38:39,280 --> 02:38:41,120
we have the callback
3894
02:38:41,120 --> 02:38:43,359
and then in the callback i cancel logged
3895
02:38:43,359 --> 02:38:45,280
first result and then of course i have
3896
02:38:45,280 --> 02:38:47,040
completed the first task
3897
02:38:47,040 --> 02:38:50,640
and then right after the read file
3898
02:38:50,640 --> 02:38:53,280
i have starting next task
3899
02:38:53,280 --> 02:38:55,200
and something really interesting in a
3900
02:38:55,200 --> 02:38:56,960
console i can see that
3901
02:38:56,960 --> 02:38:59,520
we first cancel logged started the first
3902
02:38:59,520 --> 02:39:00,399
task
3903
02:39:00,399 --> 02:39:02,160
then i right away have
3904
02:39:02,160 --> 02:39:04,319
starting the next task and then of
3905
02:39:04,319 --> 02:39:07,439
course once i'm done once i get back my
3906
02:39:07,439 --> 02:39:10,160
result then of course i have hello this
3907
02:39:10,160 --> 02:39:12,319
is first text file and of course
3908
02:39:12,319 --> 02:39:15,680
completed first task and again the
3909
02:39:15,680 --> 02:39:17,600
reason why is this happening because
3910
02:39:17,600 --> 02:39:18,800
read file
3911
02:39:18,800 --> 02:39:20,960
is a synchronous
3912
02:39:20,960 --> 02:39:22,880
and we already know that
3913
02:39:22,880 --> 02:39:25,120
event loop will offload this in this
3914
02:39:25,120 --> 02:39:27,200
case to a file system
3915
02:39:27,200 --> 02:39:29,600
so we start reading the file
3916
02:39:29,600 --> 02:39:32,319
notice like okay run this line of code
3917
02:39:32,319 --> 02:39:35,760
then offload this one and only when i
3918
02:39:35,760 --> 02:39:37,600
get back to the result
3919
02:39:37,600 --> 02:39:38,720
then
3920
02:39:38,720 --> 02:39:40,399
run the callback so
3921
02:39:40,399 --> 02:39:43,680
when the file system responds with error
3922
02:39:43,680 --> 02:39:45,120
or the data
3923
02:39:45,120 --> 02:39:47,680
then we invoke this one all right so we
3924
02:39:47,680 --> 02:39:49,600
offload this task and we just keep on
3925
02:39:49,600 --> 02:39:52,000
reading the code that's why we have
3926
02:39:52,000 --> 02:39:54,479
started the first task starting the next
3927
02:39:54,479 --> 02:39:56,800
one so right away go to the next test
3928
02:39:56,800 --> 02:39:59,520
and this one this asynchronous one well
3929
02:39:59,520 --> 02:40:01,120
we're just offloading
3930
02:40:01,120 --> 02:40:03,520
and then once the response comes back
3931
02:40:03,520 --> 02:40:05,359
whether it's an error whether it's a
3932
02:40:05,359 --> 02:40:08,960
success only then we invoke the callback
3933
02:40:08,960 --> 02:40:11,920
hopefully that is clear now as far as
3934
02:40:11,920 --> 02:40:13,520
this comment
3935
02:40:13,520 --> 02:40:16,000
i only added this one because we need to
3936
02:40:16,000 --> 02:40:18,399
keep in mind that of course i'm just
3937
02:40:18,399 --> 02:40:21,200
grabbing this code from the file
3938
02:40:21,200 --> 02:40:24,960
but the file is sitting in the folder
3939
02:40:24,960 --> 02:40:27,600
so if you were to run
3940
02:40:27,600 --> 02:40:30,160
nodemon and then directly the filename
3941
02:40:30,160 --> 02:40:31,600
which is of course going to be in the
3942
02:40:31,600 --> 02:40:34,640
event loop the path won't match so this
3943
02:40:34,640 --> 02:40:36,960
path only matches because of course i
3944
02:40:36,960 --> 02:40:40,160
have app.js but if you'll try to run
3945
02:40:40,160 --> 02:40:43,040
this code directly in this file that's
3946
02:40:43,040 --> 02:40:44,880
why it's not going to match and you need
3947
02:40:44,880 --> 02:40:48,399
to go one level up now my assumption is
3948
02:40:48,399 --> 02:40:51,040
that you're running it the same as me
3949
02:40:51,040 --> 02:40:54,399
and you're running that with npm start
3950
02:40:54,399 --> 02:40:56,800
that's why i kept the path
3951
02:40:56,800 --> 02:40:59,920
matching to the one in the app.js now
3952
02:40:59,920 --> 02:41:02,000
our next example so let me clear my
3953
02:41:02,000 --> 02:41:04,880
app.js i can close the read file as well
3954
02:41:04,880 --> 02:41:07,600
as package.json and then the next one is
3955
02:41:07,600 --> 02:41:10,720
set timeout so select here everything
3956
02:41:10,720 --> 02:41:13,520
and copy and paste in my fgs i'll
3957
02:41:13,520 --> 02:41:17,359
restart and then i have two console logs
3958
02:41:17,359 --> 02:41:20,240
and the third one is e in a set timeout
3959
02:41:20,240 --> 02:41:23,200
and this is pretty typical example for
3960
02:41:23,200 --> 02:41:25,840
javascript loop as well if you have
3961
02:41:25,840 --> 02:41:27,920
taken javascript courses which again i
3962
02:41:27,920 --> 02:41:29,840
assume you have since you're watching
3963
02:41:29,840 --> 02:41:32,000
the note one you probably remember this
3964
02:41:32,000 --> 02:41:34,000
example where i got you here is
3965
02:41:34,000 --> 02:41:35,760
following yeah i have the
3966
02:41:35,760 --> 02:41:37,120
console log
3967
02:41:37,120 --> 02:41:40,240
first and i have the second and third
3968
02:41:40,240 --> 02:41:42,960
and you think that since set timeout is
3969
02:41:42,960 --> 02:41:44,560
set to zero
3970
02:41:44,560 --> 02:41:46,560
so since we're saying yeah call this
3971
02:41:46,560 --> 02:41:49,200
callback function but actually wait only
3972
02:41:49,200 --> 02:41:51,359
zero seconds you'd think
3973
02:41:51,359 --> 02:41:54,560
that the order would be first second and
3974
02:41:54,560 --> 02:41:58,160
third right well wrong because we have
3975
02:41:58,160 --> 02:42:00,319
first third and second
3976
02:42:00,319 --> 02:42:02,560
why well because set timeout is
3977
02:42:02,560 --> 02:42:05,359
asynchronous correct and what happens
3978
02:42:05,359 --> 02:42:07,439
with asynchronous well they get
3979
02:42:07,439 --> 02:42:10,560
offloaded so in this case we run first
3980
02:42:10,560 --> 02:42:11,439
third
3981
02:42:11,439 --> 02:42:13,200
and then the second
3982
02:42:13,200 --> 02:42:16,399
because this one gets offloaded again
3983
02:42:16,399 --> 02:42:18,560
this one goes to the back of the line
3984
02:42:18,560 --> 02:42:20,640
and only when we're done
3985
02:42:20,640 --> 02:42:23,359
with our immediate code pretty much with
3986
02:42:23,359 --> 02:42:27,120
our synchronous code only then we invoke
3987
02:42:27,120 --> 02:42:30,640
that callback all right now i also added
3988
02:42:30,640 --> 02:42:32,960
these comments here where i have started
3989
02:42:32,960 --> 02:42:35,600
operating system process and then
3990
02:42:35,600 --> 02:42:38,240
completed and exited the operating
3991
02:42:38,240 --> 02:42:40,560
system process and the reason why i'm
3992
02:42:40,560 --> 02:42:42,640
showing you that because in next two
3993
02:42:42,640 --> 02:42:45,120
examples we'll do something a little bit
3994
02:42:45,120 --> 02:42:47,920
different so now i would want to go to
3995
02:42:47,920 --> 02:42:49,760
set interval
3996
02:42:49,760 --> 02:42:51,040
and by the way it's going to be a bit
3997
02:42:51,040 --> 02:42:54,319
clearer if we run here node and then
3998
02:42:54,319 --> 02:42:57,200
app.js so let me stop this and i'll go
3999
02:42:57,200 --> 02:42:59,840
with node and objects and you'll notice
4000
02:42:59,840 --> 02:43:02,880
that we started the operating system
4001
02:43:02,880 --> 02:43:04,960
process and then the moment we're done
4002
02:43:04,960 --> 02:43:08,160
with the code that's it we exit now if
4003
02:43:08,160 --> 02:43:10,560
we'll take a look at the set interval
4004
02:43:10,560 --> 02:43:12,880
you'll notice something really
4005
02:43:12,880 --> 02:43:16,399
interesting where if i go with node and
4006
02:43:16,399 --> 02:43:18,160
app.js notice something interesting and
4007
02:43:18,160 --> 02:43:19,840
by the way of course i didn't copy and
4008
02:43:19,840 --> 02:43:22,880
paste so go to the third example
4009
02:43:22,880 --> 02:43:26,080
and then just remove all this code and
4010
02:43:26,080 --> 02:43:27,680
copy and paste so that's my third
4011
02:43:27,680 --> 02:43:30,080
example and in here i have the set
4012
02:43:30,080 --> 02:43:32,880
interval now again yes we'll have to run
4013
02:43:32,880 --> 02:43:34,960
few times node app.js
4014
02:43:34,960 --> 02:43:36,479
and now check it out
4015
02:43:36,479 --> 02:43:38,800
so what i see here is
4016
02:43:38,800 --> 02:43:40,479
i will run first
4017
02:43:40,479 --> 02:43:42,560
so that's my console log
4018
02:43:42,560 --> 02:43:44,640
and then i have the set interval
4019
02:43:44,640 --> 02:43:46,560
and notice how we're not exiting the
4020
02:43:46,560 --> 02:43:47,920
process here
4021
02:43:47,920 --> 02:43:50,960
so we start the process similarly to the
4022
02:43:50,960 --> 02:43:52,319
second example
4023
02:43:52,319 --> 02:43:55,279
but if in the second example we actually
4024
02:43:55,279 --> 02:43:57,760
exited because we completed all our
4025
02:43:57,760 --> 02:43:58,720
tasks
4026
02:43:58,720 --> 02:44:00,720
in this case we're not doing that why
4027
02:44:00,720 --> 02:44:02,960
well because set interval is
4028
02:44:02,960 --> 02:44:04,720
asynchronous
4029
02:44:04,720 --> 02:44:06,399
now the difference between the set
4030
02:44:06,399 --> 02:44:08,319
timeout and set interval is the fact
4031
02:44:08,319 --> 02:44:10,319
that set interval
4032
02:44:10,319 --> 02:44:12,960
runs in those increments in this case of
4033
02:44:12,960 --> 02:44:16,800
course it is those two seconds so every
4034
02:44:16,800 --> 02:44:18,960
two seconds the
4035
02:44:18,960 --> 02:44:20,160
event loop
4036
02:44:20,160 --> 02:44:23,120
is just gonna invoke that callback now
4037
02:44:23,120 --> 02:44:25,600
that's why we're not exiting
4038
02:44:25,600 --> 02:44:27,920
that's why we're still in the process
4039
02:44:27,920 --> 02:44:30,720
and we can only exit the process if we
4040
02:44:30,720 --> 02:44:34,000
kill it so that would be control and c
4041
02:44:34,000 --> 02:44:36,560
or there's some unexpected error
4042
02:44:36,560 --> 02:44:39,600
otherwise it will always stay alive now
4043
02:44:39,600 --> 02:44:42,240
again keep in mind one thing where
4044
02:44:42,240 --> 02:44:45,200
notice i will run first
4045
02:44:45,200 --> 02:44:48,560
was first why well because again this is
4046
02:44:48,560 --> 02:44:50,000
asynchronous and i know i've said this
4047
02:44:50,000 --> 02:44:53,439
before 20 000 times but again this is
4048
02:44:53,439 --> 02:44:55,600
probably the core building block of no
4049
02:44:55,600 --> 02:44:56,880
the fact that
4050
02:44:56,880 --> 02:44:58,720
every time we have some asynchronous
4051
02:44:58,720 --> 02:45:00,800
action it's just going to be offloaded
4052
02:45:00,800 --> 02:45:04,080
and then when it's time we invoke the
4053
02:45:04,080 --> 02:45:06,800
callback and our last example is a
4054
02:45:06,800 --> 02:45:09,040
server and again i just wanted to
4055
02:45:09,040 --> 02:45:13,200
showcase how the process stays alive
4056
02:45:13,200 --> 02:45:15,840
so i'll take all the code
4057
02:45:15,840 --> 02:45:19,840
and copy and paste in app.js
4058
02:45:19,840 --> 02:45:22,240
now i'll stop this one so stop the
4059
02:45:22,240 --> 02:45:24,720
process and then we'll clear the console
4060
02:45:24,720 --> 02:45:25,680
and again
4061
02:45:25,680 --> 02:45:27,680
just to showcase how the process stays
4062
02:45:27,680 --> 02:45:29,520
live i'm going to go with node and
4063
02:45:29,520 --> 02:45:32,160
app.js and i'm doing that so you're not
4064
02:45:32,160 --> 02:45:35,200
confused with nodemon and then check it
4065
02:45:35,200 --> 02:45:38,880
out so we have server listening on port
4066
02:45:38,880 --> 02:45:40,479
5000
4067
02:45:40,479 --> 02:45:44,560
and then every time the request comes in
4068
02:45:44,560 --> 02:45:47,200
well we invoke this callback and in our
4069
02:45:47,200 --> 02:45:49,520
callback we're simply cancel logging
4070
02:45:49,520 --> 02:45:52,399
request event and then we send back the
4071
02:45:52,399 --> 02:45:56,240
hello world so now if i were to go to a
4072
02:45:56,240 --> 02:45:59,600
localhost 5000 there it is
4073
02:45:59,600 --> 02:46:02,160
i have the response of hello world and
4074
02:46:02,160 --> 02:46:05,200
in a console i'll see this request event
4075
02:46:05,200 --> 02:46:08,319
and check it out how again this process
4076
02:46:08,319 --> 02:46:09,600
stays alive
4077
02:46:09,600 --> 02:46:13,680
why well because listen is asynchronous
4078
02:46:13,680 --> 02:46:16,640
and the moment we set it up now event
4079
02:46:16,640 --> 02:46:17,439
loop
4080
02:46:17,439 --> 02:46:19,680
is just waiting for those requests to
4081
02:46:19,680 --> 02:46:20,800
come in
4082
02:46:20,800 --> 02:46:23,040
and then once they come in then of
4083
02:46:23,040 --> 02:46:26,800
course we run our callback now please
4084
02:46:26,800 --> 02:46:29,600
don't confuse this callback with what
4085
02:46:29,600 --> 02:46:31,200
we're responding
4086
02:46:31,200 --> 02:46:34,240
our request i went so this callback is
4087
02:46:34,240 --> 02:46:36,479
just when we're setting up the server
4088
02:46:36,479 --> 02:46:38,640
because in here we can have the success
4089
02:46:38,640 --> 02:46:40,800
over there so in our case as you can see
4090
02:46:40,800 --> 02:46:42,960
everything went great we have server
4091
02:46:42,960 --> 02:46:45,600
listening on port 5000 but of course
4092
02:46:45,600 --> 02:46:46,880
there also
4093
02:46:46,880 --> 02:46:50,479
might be an error and this one of course
4094
02:46:50,479 --> 02:46:53,439
just responds to that request event but
4095
02:46:53,439 --> 02:46:57,120
the whole point is with server.listen we
4096
02:46:57,120 --> 02:47:00,000
just say hey event loop just keep
4097
02:47:00,000 --> 02:47:01,040
listening
4098
02:47:01,040 --> 02:47:03,520
for those incoming requests and the
4099
02:47:03,520 --> 02:47:04,880
moments show up
4100
02:47:04,880 --> 02:47:07,840
then respond to them appropriately
4101
02:47:07,840 --> 02:47:10,160
hopefully it is clear and of course we
4102
02:47:10,160 --> 02:47:12,800
can move on to our next topic all right
4103
02:47:12,800 --> 02:47:14,880
and up next i want to talk about
4104
02:47:14,880 --> 02:47:18,560
asynchronous patterns in node.js
4105
02:47:18,560 --> 02:47:21,279
so if we remember
4106
02:47:21,279 --> 02:47:24,000
when we were setting up the file system
4107
02:47:24,000 --> 02:47:24,960
module
4108
02:47:24,960 --> 02:47:27,600
we covered how we have two flavors we
4109
02:47:27,600 --> 02:47:30,160
have the synchronous one and the
4110
02:47:30,160 --> 02:47:31,680
asynchronous one
4111
02:47:31,680 --> 02:47:34,399
and while asynchronous one is great
4112
02:47:34,399 --> 02:47:37,200
since we're not blocking the event loop
4113
02:47:37,200 --> 02:47:39,520
the problem is that if we're using this
4114
02:47:39,520 --> 02:47:41,120
callback approach
4115
02:47:41,120 --> 02:47:44,399
well it gets messy pretty quickly right
4116
02:47:44,399 --> 02:47:46,800
because we're nesting one callback
4117
02:47:46,800 --> 02:47:49,439
inside of the other one so now in the
4118
02:47:49,439 --> 02:47:51,120
following videos i would want to show
4119
02:47:51,120 --> 02:47:53,279
you the alternative that we'll use
4120
02:47:53,279 --> 02:47:54,720
throughout the course
4121
02:47:54,720 --> 02:47:58,240
and why in my opinion it is much cleaner
4122
02:47:58,240 --> 02:48:00,720
syntax and that's why of course we'll
4123
02:48:00,720 --> 02:48:03,279
use it and first what i would want is
4124
02:48:03,279 --> 02:48:05,760
just to kill everything here and i'll
4125
02:48:05,760 --> 02:48:07,520
start with the nodemon
4126
02:48:07,520 --> 02:48:08,399
since
4127
02:48:08,399 --> 02:48:10,960
i'm done showing you the event loop and
4128
02:48:10,960 --> 02:48:13,359
we're just going to go with npm start
4129
02:48:13,359 --> 02:48:16,080
right that was the proper one and now
4130
02:48:16,080 --> 02:48:18,399
i'll delete everything that i currently
4131
02:48:18,399 --> 02:48:21,120
have not here sorry so i'll leave this
4132
02:48:21,120 --> 02:48:23,600
one the async one i would want to remove
4133
02:48:23,600 --> 02:48:25,760
everything in the app.js
4134
02:48:25,760 --> 02:48:28,640
and now i just want to quickly show you
4135
02:48:28,640 --> 02:48:31,439
the code that would block that event
4136
02:48:31,439 --> 02:48:33,600
loop again this is going to be the video
4137
02:48:33,600 --> 02:48:34,479
where
4138
02:48:34,479 --> 02:48:35,680
if you don't want to
4139
02:48:35,680 --> 02:48:37,520
code along you don't have to you can
4140
02:48:37,520 --> 02:48:39,600
just sit back and relax
4141
02:48:39,600 --> 02:48:43,040
and effectively you'll be in good shape
4142
02:48:43,040 --> 02:48:45,520
and again we'll set up the server like i
4143
02:48:45,520 --> 02:48:48,399
mentioned before already 20 000 times
4144
02:48:48,399 --> 02:48:50,960
don't worry about specific commands
4145
02:48:50,960 --> 02:48:53,279
we have rest of the course where we'll
4146
02:48:53,279 --> 02:48:55,760
be building servers so trust me you'll
4147
02:48:55,760 --> 02:48:59,120
get sick of it so go with http
4148
02:48:59,120 --> 02:49:01,120
and require
4149
02:49:01,120 --> 02:49:02,880
and of course we're looking for http
4150
02:49:02,880 --> 02:49:03,840
module
4151
02:49:03,840 --> 02:49:06,160
and then remember the methods create
4152
02:49:06,160 --> 02:49:07,840
server and listen
4153
02:49:07,840 --> 02:49:09,920
so we go with const and we create the
4154
02:49:09,920 --> 02:49:10,960
server
4155
02:49:10,960 --> 02:49:12,479
that is http
4156
02:49:12,479 --> 02:49:15,760
and create server we pass in callback a
4157
02:49:15,760 --> 02:49:18,000
callback takes two objects request
4158
02:49:18,000 --> 02:49:20,399
object and response object so again
4159
02:49:20,399 --> 02:49:22,800
essentially request object is what is
4160
02:49:22,800 --> 02:49:25,120
coming in and response object just
4161
02:49:25,120 --> 02:49:28,000
represents what we're sending out what
4162
02:49:28,000 --> 02:49:30,560
essentially we are responding and then i
4163
02:49:30,560 --> 02:49:32,399
would want to listen on a port again so
4164
02:49:32,399 --> 02:49:35,359
i run server.listen and then i'll pass
4165
02:49:35,359 --> 02:49:38,399
in the port of 5000 and then of course
4166
02:49:38,399 --> 02:49:41,279
we have that callback once the server is
4167
02:49:41,279 --> 02:49:43,200
ready and in here we can just go with
4168
02:49:43,200 --> 02:49:44,479
console.log
4169
02:49:44,479 --> 02:49:46,479
and we'll simply say
4170
02:49:46,479 --> 02:49:49,120
server is listening on port
4171
02:49:49,120 --> 02:49:52,319
5000 server is
4172
02:49:52,319 --> 02:49:55,120
is listening or just listening
4173
02:49:55,120 --> 02:49:57,279
listening
4174
02:49:57,279 --> 02:50:00,640
on port and then of course in my case
4175
02:50:00,640 --> 02:50:02,720
port is 5000
4176
02:50:02,720 --> 02:50:05,600
let's set that up so we should have this
4177
02:50:05,600 --> 02:50:07,840
listening on port 5000 and then of
4178
02:50:07,840 --> 02:50:10,800
course in my callback function i would
4179
02:50:10,800 --> 02:50:13,120
want to check 40 urls because i want to
4180
02:50:13,120 --> 02:50:16,960
show you how even if we try to get a
4181
02:50:16,960 --> 02:50:19,439
different resource we're still blocked
4182
02:50:19,439 --> 02:50:20,960
by other user
4183
02:50:20,960 --> 02:50:23,600
if he or she is requesting some kind of
4184
02:50:23,600 --> 02:50:26,240
resource where we have the blocking code
4185
02:50:26,240 --> 02:50:28,080
and don't worry if this sounds like
4186
02:50:28,080 --> 02:50:30,640
gibberish you'll see what i mean in a
4187
02:50:30,640 --> 02:50:33,439
second so remember on the request object
4188
02:50:33,439 --> 02:50:35,520
we could check for the url
4189
02:50:35,520 --> 02:50:38,000
so effectively you could check what is
4190
02:50:38,000 --> 02:50:40,880
the resource that the user is requesting
4191
02:50:40,880 --> 02:50:42,399
so for example home page would be
4192
02:50:42,399 --> 02:50:45,040
forward slash and then the about page
4193
02:50:45,040 --> 02:50:47,520
would be forward slash and about and
4194
02:50:47,520 --> 02:50:49,680
that is sitting in the property by the
4195
02:50:49,680 --> 02:50:53,359
name of request and url so request is
4196
02:50:53,359 --> 02:50:56,399
the object and then url is the property
4197
02:50:56,399 --> 02:50:58,640
so again we'll do a quick and dirty way
4198
02:50:58,640 --> 02:51:03,200
we'll just say request url is equal to a
4199
02:51:03,200 --> 02:51:05,760
forward slash that means it's a homepage
4200
02:51:05,760 --> 02:51:07,439
and then we just need to go with a
4201
02:51:07,439 --> 02:51:09,279
res.end
4202
02:51:09,279 --> 02:51:12,080
and then we'll say home page again later
4203
02:51:12,080 --> 02:51:14,160
we'll have way more sophisticated
4204
02:51:14,160 --> 02:51:16,880
approach but for now this will do
4205
02:51:16,880 --> 02:51:19,040
and then we're checking for the about
4206
02:51:19,040 --> 02:51:20,080
page
4207
02:51:20,080 --> 02:51:21,040
okay
4208
02:51:21,040 --> 02:51:23,359
awesome so this is going to be an about
4209
02:51:23,359 --> 02:51:25,120
page and then
4210
02:51:25,120 --> 02:51:28,000
if there's a page that doesn't match
4211
02:51:28,000 --> 02:51:29,760
any of the resources
4212
02:51:29,760 --> 02:51:32,479
then of course i'll just send back
4213
02:51:32,479 --> 02:51:34,560
i don't know error page i'm not going to
4214
02:51:34,560 --> 02:51:37,680
set up the whole link nothing like that
4215
02:51:37,680 --> 02:51:40,720
all right we save it and then this
4216
02:51:40,720 --> 02:51:43,439
should send back about page awesome and
4217
02:51:43,439 --> 02:51:46,000
then of course in home page i can just
4218
02:51:46,000 --> 02:51:48,479
respond with the homepage so life is
4219
02:51:48,479 --> 02:51:51,200
beautiful users can come here and as a
4220
02:51:51,200 --> 02:51:53,279
side note multiple users i'm
4221
02:51:53,279 --> 02:51:56,240
representing with these tabs so imagine
4222
02:51:56,240 --> 02:51:59,439
these are my users and life is beautiful
4223
02:51:59,439 --> 02:52:02,240
we are requesting resources and we're
4224
02:52:02,240 --> 02:52:04,479
just going on about our day now the
4225
02:52:04,479 --> 02:52:08,399
problem is going to be if i go to about
4226
02:52:08,399 --> 02:52:10,720
and if i set up some kind of blocking
4227
02:52:10,720 --> 02:52:13,200
code now what would be a blocking code
4228
02:52:13,200 --> 02:52:14,720
well that could be
4229
02:52:14,720 --> 02:52:16,640
a nested for loop
4230
02:52:16,640 --> 02:52:18,319
so i'm just going to go with blocking
4231
02:52:18,319 --> 02:52:20,560
code and then i'll add some exclamation
4232
02:52:20,560 --> 02:52:23,439
points and let's simply set up a for
4233
02:52:23,439 --> 02:52:26,080
loop let's say i
4234
02:52:26,080 --> 02:52:27,520
is equal
4235
02:52:27,520 --> 02:52:29,279
to zero
4236
02:52:29,279 --> 02:52:32,080
and then let's say i is less than
4237
02:52:32,080 --> 02:52:33,359
thousand
4238
02:52:33,359 --> 02:52:36,240
and yep i plus plus so
4239
02:52:36,240 --> 02:52:38,880
every iteration will increase by one
4240
02:52:38,880 --> 02:52:40,000
however
4241
02:52:40,000 --> 02:52:42,960
i'll set it up as a nested portal so
4242
02:52:42,960 --> 02:52:44,960
copy and paste and i'll just change
4243
02:52:44,960 --> 02:52:46,399
these values
4244
02:52:46,399 --> 02:52:48,080
to j
4245
02:52:48,080 --> 02:52:51,439
so j here and j here
4246
02:52:51,439 --> 02:52:54,160
and then let's just cancel log
4247
02:52:54,160 --> 02:52:57,200
and we'll say j and i again doesn't
4248
02:52:57,200 --> 02:52:59,359
really matter what we type there just
4249
02:52:59,359 --> 02:53:00,800
say i
4250
02:53:00,800 --> 02:53:02,560
and j
4251
02:53:02,560 --> 02:53:04,560
now known dialogue question what do you
4252
02:53:04,560 --> 02:53:06,640
think is gonna happen
4253
02:53:06,640 --> 02:53:10,640
so if the user navigates to about i mean
4254
02:53:10,640 --> 02:53:12,880
we can kind of see that this will take
4255
02:53:12,880 --> 02:53:14,000
some time
4256
02:53:14,000 --> 02:53:14,880
right
4257
02:53:14,880 --> 02:53:17,200
now you would expect though that
4258
02:53:17,200 --> 02:53:20,080
only the user who navigates here
4259
02:53:20,080 --> 02:53:21,600
gets blocked
4260
02:53:21,600 --> 02:53:22,640
and
4261
02:53:22,640 --> 02:53:26,160
you'd be wrong because we'll also block
4262
02:53:26,160 --> 02:53:28,000
the other suckers who are just trying to
4263
02:53:28,000 --> 02:53:30,560
get to the homepage so let me navigate
4264
02:53:30,560 --> 02:53:31,359
here
4265
02:53:31,359 --> 02:53:33,520
and check this out so i run this one and
4266
02:53:33,520 --> 02:53:35,920
notice how we're loading now the same
4267
02:53:35,920 --> 02:53:38,399
thing happens here now and here
4268
02:53:38,399 --> 02:53:41,680
okay and only when we're done here
4269
02:53:41,680 --> 02:53:44,000
then these two can get the resource
4270
02:53:44,000 --> 02:53:45,040
again
4271
02:53:45,040 --> 02:53:46,880
now of course if i go
4272
02:53:46,880 --> 02:53:47,760
to
4273
02:53:47,760 --> 02:53:51,120
my project i can see that in the console
4274
02:53:51,120 --> 02:53:53,120
i have all these values right because i
4275
02:53:53,120 --> 02:53:55,359
have this blocking code i have the
4276
02:53:55,359 --> 02:53:58,479
synchronous code that just takes a long
4277
02:53:58,479 --> 02:54:02,960
time and this is just a representation
4278
02:54:02,960 --> 02:54:04,240
of why
4279
02:54:04,240 --> 02:54:06,399
we prefer this asynchronous
4280
02:54:06,399 --> 02:54:08,800
now yes at the moment this is messy and
4281
02:54:08,800 --> 02:54:10,880
don't worry in the next videos we'll fix
4282
02:54:10,880 --> 02:54:13,840
it but this hopefully gives you an idea
4283
02:54:13,840 --> 02:54:15,680
why you should always strive setting up
4284
02:54:15,680 --> 02:54:18,319
your code asynchronously
4285
02:54:18,319 --> 02:54:20,080
because what we've learned in the
4286
02:54:20,080 --> 02:54:21,520
previous videos
4287
02:54:21,520 --> 02:54:22,960
if we do that
4288
02:54:22,960 --> 02:54:24,399
then those tasks
4289
02:54:24,399 --> 02:54:28,240
are offloaded and then only when the
4290
02:54:28,240 --> 02:54:31,279
data is back only when it's ready
4291
02:54:31,279 --> 02:54:34,080
then we invoke it and that way we're not
4292
02:54:34,080 --> 02:54:36,479
blocking the other users hopefully it is
4293
02:54:36,479 --> 02:54:38,160
clear and now of course let's take a
4294
02:54:38,160 --> 02:54:40,880
look at some other patterns we can use
4295
02:54:40,880 --> 02:54:43,520
so we still get the benefits so we're
4296
02:54:43,520 --> 02:54:46,399
not blocking that event loop
4297
02:54:46,399 --> 02:54:47,200
but
4298
02:54:47,200 --> 02:54:49,920
we also have a cleaner code
4299
02:54:49,920 --> 02:54:53,840
where we don't have this nested callback
4300
02:54:53,840 --> 02:54:56,560
mess all right and once we have covered
4301
02:54:56,560 --> 02:54:58,960
why it's so important to use the
4302
02:54:58,960 --> 02:55:00,479
asynchronous approach
4303
02:55:00,479 --> 02:55:04,160
now of course let's clean up our f a a
4304
02:55:04,160 --> 02:55:05,040
sync
4305
02:55:05,040 --> 02:55:06,880
so instead of using these nested
4306
02:55:06,880 --> 02:55:10,640
callbacks let's see a nicer pattern and
4307
02:55:10,640 --> 02:55:12,240
as i say note if you want to see the
4308
02:55:12,240 --> 02:55:13,439
code
4309
02:55:13,439 --> 02:55:16,319
that we wrote in a previous video and
4310
02:55:16,319 --> 02:55:19,120
the one we are about to write then the
4311
02:55:19,120 --> 02:55:23,279
files are located in two async patterns
4312
02:55:23,279 --> 02:55:26,960
folder so i'll close everything for now
4313
02:55:26,960 --> 02:55:29,200
and then back in app.js
4314
02:55:29,200 --> 02:55:30,960
i can remove it like i said if you want
4315
02:55:30,960 --> 02:55:33,840
to access that code it is sitting in the
4316
02:55:33,840 --> 02:55:37,040
two async patterns folder and we'll
4317
02:55:37,040 --> 02:55:39,840
simply start by recreating the
4318
02:55:39,840 --> 02:55:42,880
read file setup where i'm gonna go with
4319
02:55:42,880 --> 02:55:45,680
const and i'm right away accessing that
4320
02:55:45,680 --> 02:55:48,399
from the file system module and i'll set
4321
02:55:48,399 --> 02:55:51,279
it equal to require of course and then
4322
02:55:51,279 --> 02:55:53,920
file system all right that's awesome we
4323
02:55:53,920 --> 02:55:56,720
know that it is asynchronous so we need
4324
02:55:56,720 --> 02:55:59,680
to set it up with three parameters we go
4325
02:55:59,680 --> 02:56:01,200
with read
4326
02:56:01,200 --> 02:56:04,000
file and then we would want to pass in
4327
02:56:04,000 --> 02:56:06,960
path first so again since i'm in app.js
4328
02:56:06,960 --> 02:56:08,800
i'm going to be looking for the content
4329
02:56:08,800 --> 02:56:11,040
and then forward slash and then first
4330
02:56:11,040 --> 02:56:13,680
txt the file we already created before
4331
02:56:13,680 --> 02:56:16,000
then we have encoding so comma and then
4332
02:56:16,000 --> 02:56:18,560
again we're going to go with utf-8 and
4333
02:56:18,560 --> 02:56:20,000
then last one is going to be that
4334
02:56:20,000 --> 02:56:23,200
callback where the first value is the
4335
02:56:23,200 --> 02:56:26,000
error and then the second one is the
4336
02:56:26,000 --> 02:56:28,240
data that we're getting back okay
4337
02:56:28,240 --> 02:56:32,080
awesome so once the file system responds
4338
02:56:32,080 --> 02:56:34,319
then of course we invoke this callback
4339
02:56:34,319 --> 02:56:36,640
and inside of that callback we have our
4340
02:56:36,640 --> 02:56:39,680
logic so if there's going to be an error
4341
02:56:39,680 --> 02:56:41,279
for time being
4342
02:56:41,279 --> 02:56:44,319
i will just return like so
4343
02:56:44,319 --> 02:56:46,960
and then if i have the data i'll set it
4344
02:56:46,960 --> 02:56:50,640
up as else so say here console log
4345
02:56:50,640 --> 02:56:52,640
and data of course
4346
02:56:52,640 --> 02:56:54,479
and we shouldn't be surprised
4347
02:56:54,479 --> 02:56:57,120
if we've seen a console hello this is a
4348
02:56:57,120 --> 02:56:58,800
first text file because we already
4349
02:56:58,800 --> 02:57:01,520
covered the setup right so we're passing
4350
02:57:01,520 --> 02:57:04,399
the path we pass in the encoding and
4351
02:57:04,399 --> 02:57:07,120
then we set up the callback now the
4352
02:57:07,120 --> 02:57:09,200
problem starts if we would want to
4353
02:57:09,200 --> 02:57:10,319
perform
4354
02:57:10,319 --> 02:57:13,279
multiple actions if i want to read two
4355
02:57:13,279 --> 02:57:16,080
files and then for example write one
4356
02:57:16,080 --> 02:57:18,240
correct so what would be a better
4357
02:57:18,240 --> 02:57:20,800
solution well a better solution
4358
02:57:20,800 --> 02:57:23,840
would be turning this into a promise and
4359
02:57:23,840 --> 02:57:26,800
then eventually we'll set up async await
4360
02:57:26,800 --> 02:57:28,319
now please keep in mind that the only
4361
02:57:28,319 --> 02:57:30,560
reason why i will type out the promise
4362
02:57:30,560 --> 02:57:33,120
stuff is because i would want you to
4363
02:57:33,120 --> 02:57:35,279
understand what is happening behind the
4364
02:57:35,279 --> 02:57:37,200
scenes but don't worry throughout the
4365
02:57:37,200 --> 02:57:40,560
course we'll just stick with a sync and
4366
02:57:40,560 --> 02:57:43,439
a weight syntax and essentially you
4367
02:57:43,439 --> 02:57:45,120
won't have to worry about what is
4368
02:57:45,120 --> 02:57:46,960
happening behind the scenes but the
4369
02:57:46,960 --> 02:57:48,720
first example yes i want you to
4370
02:57:48,720 --> 02:57:51,840
understand everything in detail so what
4371
02:57:51,840 --> 02:57:54,800
we could do is create a new function
4372
02:57:54,800 --> 02:57:57,359
and we can just say const and then
4373
02:57:57,359 --> 02:57:59,760
i'll call this get text now this get
4374
02:57:59,760 --> 02:58:01,760
text will take a path
4375
02:58:01,760 --> 02:58:04,240
since i want to read two files and then
4376
02:58:04,240 --> 02:58:06,720
eventually write one as well just like
4377
02:58:06,720 --> 02:58:08,960
we did here and of course the goal here
4378
02:58:08,960 --> 02:58:11,359
is to showcase how we can make the code
4379
02:58:11,359 --> 02:58:14,000
in the file fs async at least that's how
4380
02:58:14,000 --> 02:58:17,200
i called my file much cleaner and more
4381
02:58:17,200 --> 02:58:19,840
readable so again we pass in the path
4382
02:58:19,840 --> 02:58:22,160
and then what i would want to return
4383
02:58:22,160 --> 02:58:25,600
from this function is a promise so we go
4384
02:58:25,600 --> 02:58:26,880
with return
4385
02:58:26,880 --> 02:58:30,319
new promise and then in the promise
4386
02:58:30,319 --> 02:58:33,040
object we need to pass in is another
4387
02:58:33,040 --> 02:58:35,040
callback function so we pass in the
4388
02:58:35,040 --> 02:58:37,600
callback function and in here we pass in
4389
02:58:37,600 --> 02:58:40,720
two more functions one for resolve and
4390
02:58:40,720 --> 02:58:43,359
then one for reject and then once i have
4391
02:58:43,359 --> 02:58:45,359
set up both of these parameters now of
4392
02:58:45,359 --> 02:58:48,240
course what i would want is to move the
4393
02:58:48,240 --> 02:58:49,760
read file
4394
02:58:49,760 --> 02:58:53,840
and place it inside of the promised one
4395
02:58:53,840 --> 02:58:56,160
okay so once we place it here first of
4396
02:58:56,160 --> 02:58:58,240
all notice here how i'm passing in the
4397
02:58:58,240 --> 02:59:00,640
path so of course i can change this one
4398
02:59:00,640 --> 02:59:03,600
around so i can say path and then the
4399
02:59:03,600 --> 02:59:05,520
encoding stays the same
4400
02:59:05,520 --> 02:59:07,680
as well as the callback however inside
4401
02:59:07,680 --> 02:59:10,240
of the callback once i have access to
4402
02:59:10,240 --> 02:59:13,200
resolve and reject what i could simply
4403
02:59:13,200 --> 02:59:15,120
say that if there is an error
4404
02:59:15,120 --> 02:59:18,960
i will spit back the error so say reject
4405
02:59:18,960 --> 02:59:21,920
an error and if everything goes great
4406
02:59:21,920 --> 02:59:24,399
now of course let's go with resolve and
4407
02:59:24,399 --> 02:59:26,880
then we'll pass in the data and once we
4408
02:59:26,880 --> 02:59:29,439
have this setup what's really cool that
4409
02:59:29,439 --> 02:59:32,800
now i can invoke get text and this is
4410
02:59:32,800 --> 02:59:35,040
going to return a promise and then of
4411
02:59:35,040 --> 02:59:38,640
course we can chain that then and catch
4412
02:59:38,640 --> 02:59:41,279
so below the get text i can go with get
4413
02:59:41,279 --> 02:59:43,279
text and of course we're looking for the
4414
02:59:43,279 --> 02:59:45,359
path so we still would need to use dot
4415
02:59:45,359 --> 02:59:47,520
and then forward slash and then content
4416
02:59:47,520 --> 02:59:50,960
of course and first txt so that's the
4417
02:59:50,960 --> 02:59:53,279
path and then like i said
4418
02:59:53,279 --> 02:59:56,080
we are returning a promise so we can
4419
02:59:56,080 --> 02:59:57,760
chain that then
4420
02:59:57,760 --> 03:00:00,319
and of course here i can have the result
4421
03:00:00,319 --> 03:00:03,120
and i'll just cancel log it for now
4422
03:00:03,120 --> 03:00:06,800
or result and then i can also chain that
4423
03:00:06,800 --> 03:00:09,840
catch and here i'm passing in the error
4424
03:00:09,840 --> 03:00:11,760
so again we can't log it
4425
03:00:11,760 --> 03:00:13,520
and we simply would want to see the
4426
03:00:13,520 --> 03:00:16,960
error i save it and beautiful
4427
03:00:16,960 --> 03:00:19,279
again in a console i have hello this is
4428
03:00:19,279 --> 03:00:22,319
first text file so i know that it worked
4429
03:00:22,319 --> 03:00:24,479
and also if i change the path
4430
03:00:24,479 --> 03:00:27,680
so if i make my path incorrect we should
4431
03:00:27,680 --> 03:00:30,560
see in the console the error
4432
03:00:30,560 --> 03:00:33,439
where of course node cannot find file so
4433
03:00:33,439 --> 03:00:35,840
if that works now of course we can take
4434
03:00:35,840 --> 03:00:37,439
a look at how we can make it even
4435
03:00:37,439 --> 03:00:41,520
cleaner by setting up async 08 and once
4436
03:00:41,520 --> 03:00:44,479
we have our initial setup once we have
4437
03:00:44,479 --> 03:00:47,680
turned this into a promise
4438
03:00:47,680 --> 03:00:50,160
technically we're not out of the woods
4439
03:00:50,160 --> 03:00:53,200
because if i would want to perform again
4440
03:00:53,200 --> 03:00:56,479
two file reads and then eventually write
4441
03:00:56,479 --> 03:00:58,800
the file as well and if i would want to
4442
03:00:58,800 --> 03:01:01,439
do all of that asynchronously it's still
4443
03:01:01,439 --> 03:01:03,920
going to be paying just by using the
4444
03:01:03,920 --> 03:01:06,000
promises so what would be a solution
4445
03:01:06,000 --> 03:01:08,800
well since we're returning a promise if
4446
03:01:08,800 --> 03:01:11,200
i use async await
4447
03:01:11,200 --> 03:01:15,279
i can wait till promise is settled
4448
03:01:15,279 --> 03:01:17,760
and then decide what i would want to do
4449
03:01:17,760 --> 03:01:20,960
and the way we set that one up is by
4450
03:01:20,960 --> 03:01:23,600
creating a new function now please keep
4451
03:01:23,600 --> 03:01:25,680
one thing in mind where
4452
03:01:25,680 --> 03:01:28,399
eventually throughout the course
4453
03:01:28,399 --> 03:01:31,040
those functions that i'm about to set up
4454
03:01:31,040 --> 03:01:32,880
are going to be provided to us
4455
03:01:32,880 --> 03:01:36,080
so we'll just attach that async to the
4456
03:01:36,080 --> 03:01:38,319
callback functions that already provided
4457
03:01:38,319 --> 03:01:41,600
us by the libraries okay but in this
4458
03:01:41,600 --> 03:01:43,600
example i'll have to create one from the
4459
03:01:43,600 --> 03:01:45,520
scratch and again the whole point here
4460
03:01:45,520 --> 03:01:46,880
is because i would want to create a
4461
03:01:46,880 --> 03:01:49,600
function a start function that i'm going
4462
03:01:49,600 --> 03:01:52,240
to set up as a synchronous function and
4463
03:01:52,240 --> 03:01:54,160
i would want to attach a weight
4464
03:01:54,160 --> 03:01:57,840
so i would only respond once the promise
4465
03:01:57,840 --> 03:02:01,200
is settled so in here i go const start
4466
03:02:01,200 --> 03:02:03,279
and that's my function but i need to set
4467
03:02:03,279 --> 03:02:05,760
it up right away as async because i
4468
03:02:05,760 --> 03:02:08,160
would want to use my await keyword
4469
03:02:08,160 --> 03:02:11,279
correct and then we simply go with const
4470
03:02:11,279 --> 03:02:13,359
and i know that of course i'm looking
4471
03:02:13,359 --> 03:02:14,800
for the first file
4472
03:02:14,800 --> 03:02:17,040
and i'll just say first and that one is
4473
03:02:17,040 --> 03:02:18,160
equal to
4474
03:02:18,160 --> 03:02:21,520
get text and basically i want to pass in
4475
03:02:21,520 --> 03:02:24,000
the same thing here now i do need to
4476
03:02:24,000 --> 03:02:26,960
place this one in quotation marks so my
4477
03:02:26,960 --> 03:02:28,800
bad i messed it up a little bit okay i
4478
03:02:28,800 --> 03:02:31,520
passed it in and that should do it
4479
03:02:31,520 --> 03:02:33,760
now of course what i'm missing here is a
4480
03:02:33,760 --> 03:02:36,960
weight so i had this await and now only
4481
03:02:36,960 --> 03:02:39,279
once the promise is resolved
4482
03:02:39,279 --> 03:02:42,160
then i'll get my response so what i
4483
03:02:42,160 --> 03:02:43,359
could do here
4484
03:02:43,359 --> 03:02:46,240
is just comment these two or you know
4485
03:02:46,240 --> 03:02:48,640
what this one i cannot comment out yet
4486
03:02:48,640 --> 03:02:51,120
don't worry we will eventually
4487
03:02:51,120 --> 03:02:52,800
this one i can comment out and then
4488
03:02:52,800 --> 03:02:53,840
maybe
4489
03:02:53,840 --> 03:02:55,439
move this below
4490
03:02:55,439 --> 03:02:57,200
okay so you don't think that i'm running
4491
03:02:57,200 --> 03:02:59,760
this code and since i have my function
4492
03:02:59,760 --> 03:03:02,479
now of course i would need to invoke it
4493
03:03:02,479 --> 03:03:04,720
so invoke start
4494
03:03:04,720 --> 03:03:07,359
and check it out now of course i don't
4495
03:03:07,359 --> 03:03:09,600
have the error which is a good sign i
4496
03:03:09,600 --> 03:03:10,399
guess
4497
03:03:10,399 --> 03:03:12,880
and then we'll simply go with log and
4498
03:03:12,880 --> 03:03:14,240
none first
4499
03:03:14,240 --> 03:03:16,479
and notice the difference
4500
03:03:16,479 --> 03:03:18,160
where previously when we were setting up
4501
03:03:18,160 --> 03:03:20,240
the callbacks we had to nest everything
4502
03:03:20,240 --> 03:03:22,080
now i'm waiting for this promise to be
4503
03:03:22,080 --> 03:03:25,120
resolved and only once it's resolved
4504
03:03:25,120 --> 03:03:27,359
then of course i can do something with
4505
03:03:27,359 --> 03:03:29,840
the value now every time we have this
4506
03:03:29,840 --> 03:03:32,800
async await approach what we would want
4507
03:03:32,800 --> 03:03:35,840
is to wrap this e in the try catch block
4508
03:03:35,840 --> 03:03:38,960
so if something goes wrong then we have
4509
03:03:38,960 --> 03:03:41,520
at least a little bit of control over it
4510
03:03:41,520 --> 03:03:44,640
so i'll set up try catch block and then
4511
03:03:44,640 --> 03:03:46,960
i'll set up my first await and
4512
03:03:46,960 --> 03:03:49,120
eventually we'll add here more
4513
03:03:49,120 --> 03:03:52,000
and then in the error i'll simply go
4514
03:03:52,000 --> 03:03:54,560
with console.log and we'll console log
4515
03:03:54,560 --> 03:03:57,920
the error and let's just save that one
4516
03:03:57,920 --> 03:04:00,479
so now we have nice try and catch block
4517
03:04:00,479 --> 03:04:03,200
and by the way sorry i deleted it we'll
4518
03:04:03,200 --> 03:04:05,040
still take a look at the first one and
4519
03:04:05,040 --> 03:04:07,439
then if we change the path
4520
03:04:07,439 --> 03:04:09,920
then of course we'll get an error okay
4521
03:04:09,920 --> 03:04:12,479
so that's good now i have my start
4522
03:04:12,479 --> 03:04:14,399
function it is the synchronous i'm
4523
03:04:14,399 --> 03:04:16,640
waiting for the promises to resolve
4524
03:04:16,640 --> 03:04:19,920
they're all wrapped in try catch block
4525
03:04:19,920 --> 03:04:22,880
so what's next well if you remember in
4526
03:04:22,880 --> 03:04:23,840
here
4527
03:04:23,840 --> 03:04:27,040
what were the actions that we were doing
4528
03:04:27,040 --> 03:04:30,640
well we read two files correct and then
4529
03:04:30,640 --> 03:04:33,120
we wrote one okay so let's try that one
4530
03:04:33,120 --> 03:04:35,439
with this new approach where essentially
4531
03:04:35,439 --> 03:04:38,800
i have first so just copy and paste and
4532
03:04:38,800 --> 03:04:41,120
then i'll look for the second file
4533
03:04:41,120 --> 03:04:42,319
remember
4534
03:04:42,319 --> 03:04:44,560
we should have that in the content so
4535
03:04:44,560 --> 03:04:47,120
this is going to be the second one
4536
03:04:47,120 --> 03:04:50,000
second over here and then i would want
4537
03:04:50,000 --> 03:04:52,880
to write the file now of course
4538
03:04:52,880 --> 03:04:54,880
you could say well the problem here is
4539
03:04:54,880 --> 03:04:57,040
going to be that i'll have to
4540
03:04:57,040 --> 03:05:00,640
write a new wrapping function to set it
4541
03:05:00,640 --> 03:05:02,640
up as a promise because
4542
03:05:02,640 --> 03:05:05,439
yes we can of course read the file but
4543
03:05:05,439 --> 03:05:07,840
functionality for the write file again
4544
03:05:07,840 --> 03:05:09,520
we would need to wrap it
4545
03:05:09,520 --> 03:05:11,600
don't worry there's a way how we can get
4546
03:05:11,600 --> 03:05:14,319
this actually out of the node
4547
03:05:14,319 --> 03:05:16,080
first i just want to showcase how much
4548
03:05:16,080 --> 03:05:18,560
cleaner this already is
4549
03:05:18,560 --> 03:05:21,120
where i can just wait for first
4550
03:05:21,120 --> 03:05:23,760
second like so and now notice in the
4551
03:05:23,760 --> 03:05:25,840
console hello this is the first text and
4552
03:05:25,840 --> 03:05:28,319
hello this is the second file again i
4553
03:05:28,319 --> 03:05:30,319
fully understand that you're looking at
4554
03:05:30,319 --> 03:05:32,720
this wrapper function and you're like
4555
03:05:32,720 --> 03:05:35,520
well this definitely doesn't look that
4556
03:05:35,520 --> 03:05:37,760
much cleaner than this one don't worry
4557
03:05:37,760 --> 03:05:40,319
there's a way for us to get those
4558
03:05:40,319 --> 03:05:41,520
functions
4559
03:05:41,520 --> 03:05:44,319
in a way that they're already returning
4560
03:05:44,319 --> 03:05:47,040
a promise what i would want you to focus
4561
03:05:47,040 --> 03:05:47,760
on
4562
03:05:47,760 --> 03:05:50,160
is this code what i would want you to
4563
03:05:50,160 --> 03:05:52,560
understand that this definitely
4564
03:05:52,560 --> 03:05:55,439
is much cleaner than what we have here
4565
03:05:55,439 --> 03:05:58,800
we read the file and then we set up
4566
03:05:58,800 --> 03:06:00,560
another nested callback
4567
03:06:00,560 --> 03:06:02,960
and another nested callback and
4568
03:06:02,960 --> 03:06:04,960
hopefully you get the gist
4569
03:06:04,960 --> 03:06:07,279
waiting for these promises to resolve is
4570
03:06:07,279 --> 03:06:11,040
definitely a cleaner and more readable
4571
03:06:11,040 --> 03:06:14,160
approach all right so this works we have
4572
03:06:14,160 --> 03:06:16,080
our start function it is asynchronous
4573
03:06:16,080 --> 03:06:18,640
we're waiting for prompts to resolve and
4574
03:06:18,640 --> 03:06:21,200
we can see that it is already much
4575
03:06:21,200 --> 03:06:22,399
cleaner
4576
03:06:22,399 --> 03:06:24,560
how we can set up this code
4577
03:06:24,560 --> 03:06:27,200
without the wrapping function
4578
03:06:27,200 --> 03:06:29,760
well what's really cool that in node
4579
03:06:29,760 --> 03:06:33,279
there is a module by the name of util
4580
03:06:33,279 --> 03:06:35,760
and we just go here const and we'll
4581
03:06:35,760 --> 03:06:38,399
assign it to a variable and we'll just
4582
03:06:38,399 --> 03:06:41,120
be looking for require of course
4583
03:06:41,120 --> 03:06:43,439
since we would want to import the module
4584
03:06:43,439 --> 03:06:46,479
and again the name is util
4585
03:06:46,479 --> 03:06:48,880
and then instead of this module we have
4586
03:06:48,880 --> 03:06:51,840
a method by the name of promisify
4587
03:06:51,840 --> 03:06:54,080
and with the use of this function we can
4588
03:06:54,080 --> 03:06:57,120
take our read file which of course was
4589
03:06:57,120 --> 03:06:59,520
looking for the callback and turn it
4590
03:06:59,520 --> 03:07:02,560
into the function that returns a promise
4591
03:07:02,560 --> 03:07:05,040
and again this is over simplified
4592
03:07:05,040 --> 03:07:06,720
version what is happening there but
4593
03:07:06,720 --> 03:07:08,560
hopefully you understand that as a
4594
03:07:08,560 --> 03:07:12,399
result we'll get again this promise back
4595
03:07:12,399 --> 03:07:14,640
now the way we'll set that up of course
4596
03:07:14,640 --> 03:07:16,640
we have read file here already so i
4597
03:07:16,640 --> 03:07:18,640
cannot use the same name that's why i'll
4598
03:07:18,640 --> 03:07:20,080
go with read
4599
03:07:20,080 --> 03:07:23,359
file and we'll call this promise and
4600
03:07:23,359 --> 03:07:26,319
that one is equal to util that is of
4601
03:07:26,319 --> 03:07:28,399
course my module dot and then notice the
4602
03:07:28,399 --> 03:07:31,760
function promisify and then we pass in
4603
03:07:31,760 --> 03:07:34,800
that read file function so now i have my
4604
03:07:34,800 --> 03:07:37,520
promise one so now what i could do
4605
03:07:37,520 --> 03:07:39,279
is first of all comment this out and
4606
03:07:39,279 --> 03:07:40,960
move it down
4607
03:07:40,960 --> 03:07:44,080
again so it stays for your reference
4608
03:07:44,080 --> 03:07:45,120
but
4609
03:07:45,120 --> 03:07:48,399
it doesn't compromise my view
4610
03:07:48,399 --> 03:07:50,720
and of course now i have the error
4611
03:07:50,720 --> 03:07:51,600
because
4612
03:07:51,600 --> 03:07:54,479
well get text is not defined that's fine
4613
03:07:54,479 --> 03:07:57,279
don't worry and i'll copy and paste and
4614
03:07:57,279 --> 03:08:00,960
since i also want to write a new file
4615
03:08:00,960 --> 03:08:04,000
i will get from the file system one
4616
03:08:04,000 --> 03:08:07,600
the right file function and then i'll do
4617
03:08:07,600 --> 03:08:08,960
the same thing
4618
03:08:08,960 --> 03:08:10,560
where in this case it's going to be
4619
03:08:10,560 --> 03:08:11,359
called
4620
03:08:11,359 --> 03:08:13,120
write file promise
4621
03:08:13,120 --> 03:08:16,080
and then we pass in write file
4622
03:08:16,080 --> 03:08:18,880
and of course now instead of calling
4623
03:08:18,880 --> 03:08:22,319
get text what i'm gonna do is go with
4624
03:08:22,319 --> 03:08:24,880
right file or i'm sorry in this case
4625
03:08:24,880 --> 03:08:27,520
we're going for read file promise and
4626
03:08:27,520 --> 03:08:30,080
then keep in mind one thing where this
4627
03:08:30,080 --> 03:08:32,640
is not a get text one so we're still
4628
03:08:32,640 --> 03:08:34,399
looking for the path
4629
03:08:34,399 --> 03:08:37,040
as well as the encoding that's well copy
4630
03:08:37,040 --> 03:08:39,520
and paste and then second argument we
4631
03:08:39,520 --> 03:08:43,279
pass in is the encoding and again our
4632
03:08:43,279 --> 03:08:45,920
functionality shouldn't change where i
4633
03:08:45,920 --> 03:08:49,040
should see in console the results from
4634
03:08:49,040 --> 03:08:52,479
the first txt now of course i want to do
4635
03:08:52,479 --> 03:08:55,359
the same thing with the second one so
4636
03:08:55,359 --> 03:08:58,240
have read file promise second txt
4637
03:08:58,240 --> 03:09:01,200
again we have to pass in the encoding
4638
03:09:01,200 --> 03:09:04,080
otherwise we'll get the buffer back
4639
03:09:04,080 --> 03:09:07,840
so copy this and paste and again if we
4640
03:09:07,840 --> 03:09:10,080
cancel log it there it is we have hello
4641
03:09:10,080 --> 03:09:12,319
this is first text file and this is the
4642
03:09:12,319 --> 03:09:15,359
second one and since in the line 4 we
4643
03:09:15,359 --> 03:09:16,560
passed in
4644
03:09:16,560 --> 03:09:20,560
write file into promisify of course what
4645
03:09:20,560 --> 03:09:23,359
we could also do is right away write the
4646
03:09:23,359 --> 03:09:25,680
file so i can go with await and i'm
4647
03:09:25,680 --> 03:09:28,560
simply not assigning this to a variable
4648
03:09:28,560 --> 03:09:31,279
because well i'm getting back on the
4649
03:09:31,279 --> 03:09:33,760
finder as far as data and i'm just going
4650
03:09:33,760 --> 03:09:35,520
to go with await
4651
03:09:35,520 --> 03:09:39,120
write file promise and the same spiel we
4652
03:09:39,120 --> 03:09:42,560
pass in first the path and in this case
4653
03:09:42,560 --> 03:09:44,560
of course if the file is not there
4654
03:09:44,560 --> 03:09:46,560
node will create one
4655
03:09:46,560 --> 03:09:49,520
and the name will be result and i'll
4656
03:09:49,520 --> 03:09:51,520
just call this mine
4657
03:09:51,520 --> 03:09:54,880
grenade and then txt and then the second
4658
03:09:54,880 --> 03:09:57,439
one is data so for that one i'll set up
4659
03:09:57,439 --> 03:09:59,439
my template string and i'll just say
4660
03:09:59,439 --> 03:10:01,120
this is
4661
03:10:01,120 --> 03:10:02,239
awesome
4662
03:10:02,239 --> 03:10:03,279
awesome
4663
03:10:03,279 --> 03:10:06,000
and then let's set up the colon and then
4664
03:10:06,000 --> 03:10:07,680
we'll access both
4665
03:10:07,680 --> 03:10:08,720
first
4666
03:10:08,720 --> 03:10:11,200
as well as the second one so we'll
4667
03:10:11,200 --> 03:10:14,080
interpolate both of them
4668
03:10:14,080 --> 03:10:15,120
second
4669
03:10:15,120 --> 03:10:17,439
and then once we save
4670
03:10:17,439 --> 03:10:19,520
if we take a look at the content there
4671
03:10:19,520 --> 03:10:22,319
it is now we have the mind grenade one
4672
03:10:22,319 --> 03:10:24,720
correct now there's probably one where i
4673
03:10:24,720 --> 03:10:27,200
was testing yep there's one where i was
4674
03:10:27,200 --> 03:10:29,840
testing so i'll remove this one but if i
4675
03:10:29,840 --> 03:10:32,560
take a look at dtxd1 then of course i'll
4676
03:10:32,560 --> 03:10:35,840
have this is awesome hello this is first
4677
03:10:35,840 --> 03:10:38,880
text file and then i have the second one
4678
03:10:38,880 --> 03:10:41,359
now what's even more cool
4679
03:10:41,359 --> 03:10:43,680
that technically we can even skip this
4680
03:10:43,680 --> 03:10:45,359
part as well again
4681
03:10:45,359 --> 03:10:47,920
i'll leave this one for your reference
4682
03:10:47,920 --> 03:10:48,880
but
4683
03:10:48,880 --> 03:10:52,000
if you go to require
4684
03:10:52,000 --> 03:10:54,080
and then you require of course file
4685
03:10:54,080 --> 03:10:56,640
system module but if you add dot
4686
03:10:56,640 --> 03:10:58,000
promises
4687
03:10:58,000 --> 03:10:59,760
what do you think is going to return
4688
03:10:59,760 --> 03:11:02,000
well effectively the same thing so now
4689
03:11:02,000 --> 03:11:04,319
of course i just need to change the name
4690
03:11:04,319 --> 03:11:07,200
from read file promise and write file
4691
03:11:07,200 --> 03:11:10,319
promise back to read file and write file
4692
03:11:10,319 --> 03:11:13,359
since those are the two functions that
4693
03:11:13,359 --> 03:11:15,439
i'm importing and that will right away
4694
03:11:15,439 --> 03:11:18,640
return promises and again we save and
4695
03:11:18,640 --> 03:11:20,960
remember that by default we're just
4696
03:11:20,960 --> 03:11:22,399
overwriting stuff
4697
03:11:22,399 --> 03:11:25,279
so if i were to go
4698
03:11:25,279 --> 03:11:28,160
and add a third argument my options one
4699
03:11:28,160 --> 03:11:30,160
and if i'm just going to say
4700
03:11:30,160 --> 03:11:34,000
flag and i'll set it equal to upend once
4701
03:11:34,000 --> 03:11:37,279
we save well now we have this is awesome
4702
03:11:37,279 --> 03:11:39,279
repeat it one more time again just to
4703
03:11:39,279 --> 03:11:42,560
showcase that our functionality still
4704
03:11:42,560 --> 03:11:43,600
works
4705
03:11:43,600 --> 03:11:45,600
i fully understand that there was a
4706
03:11:45,600 --> 03:11:46,880
little detour
4707
03:11:46,880 --> 03:11:49,600
where we created our own wrapper
4708
03:11:49,600 --> 03:11:50,560
function
4709
03:11:50,560 --> 03:11:54,080
and then we covered how we would consume
4710
03:11:54,080 --> 03:11:57,279
promises but hopefully this helps you
4711
03:11:57,279 --> 03:11:59,680
understand that this approach is
4712
03:11:59,680 --> 03:12:01,279
definitely way better
4713
03:12:01,279 --> 03:12:04,479
so it is more readable it is more
4714
03:12:04,479 --> 03:12:07,279
easier to wrap your head around and
4715
03:12:07,279 --> 03:12:08,960
that's why for the remainder of the
4716
03:12:08,960 --> 03:12:11,520
course we'll stick with this one
4717
03:12:11,520 --> 03:12:12,399
again
4718
03:12:12,399 --> 03:12:14,720
some things will be provided
4719
03:12:14,720 --> 03:12:17,120
to us by default so libraries will
4720
03:12:17,120 --> 03:12:19,040
provide the functions that return
4721
03:12:19,040 --> 03:12:20,720
promises will right away have the
4722
03:12:20,720 --> 03:12:22,800
callback functions that we'll just set
4723
03:12:22,800 --> 03:12:25,279
up as async and hopefully you get the
4724
03:12:25,279 --> 03:12:27,680
gist so a lot of things will be given to
4725
03:12:27,680 --> 03:12:30,319
us but the core functionality won't
4726
03:12:30,319 --> 03:12:33,680
change where instead of using callbacks
4727
03:12:33,680 --> 03:12:35,840
and nesting them inside of the other
4728
03:12:35,840 --> 03:12:38,239
callbacks and nesting more and more and
4729
03:12:38,239 --> 03:12:39,040
more
4730
03:12:39,040 --> 03:12:42,800
we'll set up everything with async await
4731
03:12:42,800 --> 03:12:47,439
because the syntax is cleaner and way
4732
03:12:47,439 --> 03:12:48,880
easier to read
4733
03:12:48,880 --> 03:12:50,800
excellent once we have covered async
4734
03:12:50,800 --> 03:12:52,640
patterns we're going to use throughout
4735
03:12:52,640 --> 03:12:55,120
the course now let's talk about events
4736
03:12:55,120 --> 03:12:56,800
in node.js
4737
03:12:56,800 --> 03:12:59,680
when working on browser javascript apps
4738
03:12:59,680 --> 03:13:02,399
a big part of our work
4739
03:13:02,399 --> 03:13:05,680
is to handle events for example
4740
03:13:05,680 --> 03:13:07,840
user clicks a button
4741
03:13:07,840 --> 03:13:10,319
and of course in our program we handle
4742
03:13:10,319 --> 03:13:13,200
that user hovers over the link
4743
03:13:13,200 --> 03:13:16,160
and again same deal in our program we
4744
03:13:16,160 --> 03:13:18,239
are handling that and hopefully you get
4745
03:13:18,239 --> 03:13:21,439
the gist essentially as our program
4746
03:13:21,439 --> 03:13:24,319
executes at least in part it is
4747
03:13:24,319 --> 03:13:26,080
controlled by events
4748
03:13:26,080 --> 03:13:28,160
of course depending on a program but
4749
03:13:28,160 --> 03:13:30,560
it's safe to say that in browser app
4750
03:13:30,560 --> 03:13:34,239
those events are mostly external
4751
03:13:34,239 --> 03:13:35,920
now that style of programming is
4752
03:13:35,920 --> 03:13:38,720
actually called event driven programming
4753
03:13:38,720 --> 03:13:41,760
and effectively it's just that a style
4754
03:13:41,760 --> 03:13:43,920
in which the flow of the program
4755
03:13:43,920 --> 03:13:46,399
is at least in part determined by the
4756
03:13:46,399 --> 03:13:48,560
events that occur
4757
03:13:48,560 --> 03:13:50,880
as the program executes
4758
03:13:50,880 --> 03:13:52,960
now it's easier to imagine that of
4759
03:13:52,960 --> 03:13:54,080
course
4760
03:13:54,080 --> 03:13:56,560
when you have a gui right the graphical
4761
03:13:56,560 --> 03:13:57,680
interface
4762
03:13:57,680 --> 03:14:00,640
just like the button and links example i
4763
03:14:00,640 --> 03:14:01,760
just mentioned
4764
03:14:01,760 --> 03:14:03,840
but what about server side
4765
03:14:03,840 --> 03:14:06,080
is it also possible and of course the
4766
03:14:06,080 --> 03:14:08,560
answer is yes in fact event driven
4767
03:14:08,560 --> 03:14:11,359
programming is used heavily in node.js
4768
03:14:11,359 --> 03:14:13,200
and in the following videos we'll see
4769
03:14:13,200 --> 03:14:15,520
some of the examples of it
4770
03:14:15,520 --> 03:14:17,520
and basically the idea is following we
4771
03:14:17,520 --> 03:14:19,520
listen for specific events
4772
03:14:19,520 --> 03:14:22,640
and register functions that will execute
4773
03:14:22,640 --> 03:14:24,880
in response to those events
4774
03:14:24,880 --> 03:14:27,439
so once our event takes place callback
4775
03:14:27,439 --> 03:14:30,239
function fires just like in our
4776
03:14:30,239 --> 03:14:32,319
imaginary button example
4777
03:14:32,319 --> 03:14:34,000
now before we continue let me stress
4778
03:14:34,000 --> 03:14:35,840
something our first examples are going
4779
03:14:35,840 --> 03:14:37,520
to be basic and
4780
03:14:37,520 --> 03:14:39,279
even somewhat silly
4781
03:14:39,279 --> 03:14:41,359
but don't let that fool you
4782
03:14:41,359 --> 03:14:44,319
many built-in modules in node.js do use
4783
03:14:44,319 --> 03:14:46,479
events under the hood and therefore
4784
03:14:46,479 --> 03:14:48,560
making events and event driven
4785
03:14:48,560 --> 03:14:52,640
programming a big part of nodejs all
4786
03:14:52,640 --> 03:14:54,399
right and once we have covered general
4787
03:14:54,399 --> 03:14:56,880
concepts behind events as well as
4788
03:14:56,880 --> 03:14:59,120
event-driven programming now let's set
4789
03:14:59,120 --> 03:15:02,319
up our own events in node and the way we
4790
03:15:02,319 --> 03:15:05,040
do that we come up with a variable and
4791
03:15:05,040 --> 03:15:07,840
typically this name is event
4792
03:15:07,840 --> 03:15:10,000
emitter because what we're getting back
4793
03:15:10,000 --> 03:15:11,439
is the class
4794
03:15:11,439 --> 03:15:15,040
and we require the events module so
4795
03:15:15,040 --> 03:15:17,840
require events module and we assign it
4796
03:15:17,840 --> 03:15:19,760
to a variable which essentially is a
4797
03:15:19,760 --> 03:15:20,399
class
4798
03:15:20,399 --> 03:15:23,040
and again a common practice is calling
4799
03:15:23,040 --> 03:15:26,319
this event emitter and at this point you
4800
03:15:26,319 --> 03:15:27,840
have two options if you want to create
4801
03:15:27,840 --> 03:15:29,200
something custom
4802
03:15:29,200 --> 03:15:30,880
you'll need to
4803
03:15:30,880 --> 03:15:32,960
extend the class
4804
03:15:32,960 --> 03:15:35,279
or if you simply want to
4805
03:15:35,279 --> 03:15:36,800
emit an event
4806
03:15:36,800 --> 03:15:39,040
as well as listen for it then you can
4807
03:15:39,040 --> 03:15:40,720
just create the instance so go with the
4808
03:15:40,720 --> 03:15:43,200
second route we'll go with const and
4809
03:15:43,200 --> 03:15:45,120
then whatever name you would want in my
4810
03:15:45,120 --> 03:15:47,359
case i'm just going to call this custom
4811
03:15:47,359 --> 03:15:50,800
emitter like so and i'll assign it to
4812
03:15:50,800 --> 03:15:51,840
new
4813
03:15:51,840 --> 03:15:55,040
emitter so event emitter or however you
4814
03:15:55,040 --> 03:15:57,279
call this variable and we just need to
4815
03:15:57,279 --> 03:16:00,880
invoke it so now we have the instance of
4816
03:16:00,880 --> 03:16:02,800
our class so essentially we have the
4817
03:16:02,800 --> 03:16:05,200
object now there are many methods in
4818
03:16:05,200 --> 03:16:08,000
this object however two that we're
4819
03:16:08,000 --> 03:16:10,560
interested the most are on
4820
03:16:10,560 --> 03:16:12,239
and emit
4821
03:16:12,239 --> 03:16:15,120
so on we'll listen for specific amend
4822
03:16:15,120 --> 03:16:18,319
and then emit of course will emit that
4823
03:16:18,319 --> 03:16:19,279
event
4824
03:16:19,279 --> 03:16:21,600
and the way we set it up we go with
4825
03:16:21,600 --> 03:16:24,560
custom mirror or whatever name you used
4826
03:16:24,560 --> 03:16:26,319
and then we go with on
4827
03:16:26,319 --> 03:16:29,439
and at the very basic setup in the on
4828
03:16:29,439 --> 03:16:31,760
method we just pass in the string
4829
03:16:31,760 --> 03:16:35,040
where we say the name of the event now
4830
03:16:35,040 --> 03:16:38,560
in my case i'll name my one response and
4831
03:16:38,560 --> 03:16:40,479
then of course once i have subscribed to
4832
03:16:40,479 --> 03:16:42,399
it i also would want to pass in the
4833
03:16:42,399 --> 03:16:44,880
callback function so essentially when
4834
03:16:44,880 --> 03:16:47,680
this event takes place
4835
03:16:47,680 --> 03:16:49,120
well then i would want to do something
4836
03:16:49,120 --> 03:16:51,279
and in my case i'll just go with
4837
03:16:51,279 --> 03:16:54,319
console.log and we'll simply say data
4838
03:16:54,319 --> 03:16:57,279
received or i'll place this one in the
4839
03:16:57,279 --> 03:16:58,560
template strings and you'll see in a
4840
03:16:58,560 --> 03:17:01,200
second why so we'll go with data
4841
03:17:01,200 --> 03:17:02,720
received
4842
03:17:02,720 --> 03:17:04,560
and then eventually there's going to be
4843
03:17:04,560 --> 03:17:06,960
some more data and of course once we
4844
03:17:06,960 --> 03:17:09,439
have subscribed to this specific event
4845
03:17:09,439 --> 03:17:12,160
now i would want to admit it and the way
4846
03:17:12,160 --> 03:17:14,239
we do that we just go with custom
4847
03:17:14,239 --> 03:17:17,279
emitter and of course the method is
4848
03:17:17,279 --> 03:17:20,319
surprise surprise emit and of course at
4849
03:17:20,319 --> 03:17:21,520
this point
4850
03:17:21,520 --> 03:17:24,239
these strings need to match so if i'm
4851
03:17:24,239 --> 03:17:25,359
emitting
4852
03:17:25,359 --> 03:17:27,439
the response event
4853
03:17:27,439 --> 03:17:30,160
and just say here response and what
4854
03:17:30,160 --> 03:17:32,720
you'll see in the console is wherever
4855
03:17:32,720 --> 03:17:35,359
you have in callback so of course in my
4856
03:17:35,359 --> 03:17:37,920
case i have console log data received
4857
03:17:37,920 --> 03:17:40,479
and then once i run it i have data
4858
03:17:40,479 --> 03:17:43,200
received in console so that's the most
4859
03:17:43,200 --> 03:17:46,160
basic setup when it comes to events
4860
03:17:46,160 --> 03:17:48,640
again we create an instance from the
4861
03:17:48,640 --> 03:17:51,040
class that we get back from the events
4862
03:17:51,040 --> 03:17:54,319
module and then we have two methods we
4863
03:17:54,319 --> 03:17:57,120
have on method as well as the emit
4864
03:17:57,120 --> 03:17:59,920
method and then in the on method we pass
4865
03:17:59,920 --> 03:18:01,840
in the string so that's going to be the
4866
03:18:01,840 --> 03:18:04,239
name of the event as well as the
4867
03:18:04,239 --> 03:18:05,680
callback function
4868
03:18:05,680 --> 03:18:09,439
so once this event takes place
4869
03:18:09,439 --> 03:18:10,640
then of course we would want to do
4870
03:18:10,640 --> 03:18:12,880
something and in our case we'll just
4871
03:18:12,880 --> 03:18:16,479
console.log data received and then once
4872
03:18:16,479 --> 03:18:19,279
we have subscribed to this event then of
4873
03:18:19,279 --> 03:18:22,239
course we need to emit it and the way we
4874
03:18:22,239 --> 03:18:23,920
do that we just go with
4875
03:18:23,920 --> 03:18:26,160
whatever the name is in my case custom
4876
03:18:26,160 --> 03:18:29,359
emitter then emit method and then we
4877
03:18:29,359 --> 03:18:32,479
pass in the same string that we're
4878
03:18:32,479 --> 03:18:33,680
listening for
4879
03:18:33,680 --> 03:18:36,160
in our case of course response and then
4880
03:18:36,160 --> 03:18:38,000
the moment we do that
4881
03:18:38,000 --> 03:18:40,800
we of course have whatever is in the
4882
03:18:40,800 --> 03:18:42,880
callback function which in of course of
4883
03:18:42,880 --> 03:18:45,279
course is just a simple log data
4884
03:18:45,279 --> 03:18:46,479
received
4885
03:18:46,479 --> 03:18:48,000
all right and once we have covered the
4886
03:18:48,000 --> 03:18:50,720
basic setup now let me show you some
4887
03:18:50,720 --> 03:18:52,479
things that
4888
03:18:52,479 --> 03:18:54,319
i would want you to be aware of
4889
03:18:54,319 --> 03:18:55,279
first
4890
03:18:55,279 --> 03:18:57,920
we can have as many methods we would
4891
03:18:57,920 --> 03:19:00,080
want so for example we have the same
4892
03:19:00,080 --> 03:19:03,439
event we have here response correct and
4893
03:19:03,439 --> 03:19:06,399
we are omitting it so nothing stops me
4894
03:19:06,399 --> 03:19:09,920
here just copy and paste
4895
03:19:09,920 --> 03:19:12,239
and listen for the same event
4896
03:19:12,239 --> 03:19:13,840
which of course in our case is this
4897
03:19:13,840 --> 03:19:17,200
response and then do some other logic in
4898
03:19:17,200 --> 03:19:19,760
my callback again in my case it's just
4899
03:19:19,760 --> 03:19:21,040
going to be simple
4900
03:19:21,040 --> 03:19:22,560
different kinds of log so i'm going to
4901
03:19:22,560 --> 03:19:24,000
say some
4902
03:19:24,000 --> 03:19:24,960
other
4903
03:19:24,960 --> 03:19:27,760
logic here and i'm not going to copy and
4904
03:19:27,760 --> 03:19:28,720
paste
4905
03:19:28,720 --> 03:19:31,520
the same function 20 000 times just to
4906
03:19:31,520 --> 03:19:34,080
prove my point but hopefully understand
4907
03:19:34,080 --> 03:19:37,760
that yes we can emit our event and then
4908
03:19:37,760 --> 03:19:40,960
yes also we can have as many functions
4909
03:19:40,960 --> 03:19:43,040
we would want here that our listening
4910
03:19:43,040 --> 03:19:45,920
for that event and do some other logic
4911
03:19:45,920 --> 03:19:48,239
so that's point number one point number
4912
03:19:48,239 --> 03:19:50,319
two that i would like to make is the
4913
03:19:50,319 --> 03:19:51,520
fact that
4914
03:19:51,520 --> 03:19:54,399
this order matters
4915
03:19:54,399 --> 03:19:58,560
so we first listen for event and then we
4916
03:19:58,560 --> 03:20:02,000
emit it so for example if i will place
4917
03:20:02,000 --> 03:20:05,600
my emit above the second function or
4918
03:20:05,600 --> 03:20:07,760
above both of the functions which you'll
4919
03:20:07,760 --> 03:20:09,040
notice
4920
03:20:09,040 --> 03:20:11,520
that i have nothing in a console
4921
03:20:11,520 --> 03:20:14,560
why well because i first emit the event
4922
03:20:14,560 --> 03:20:16,960
and only then i listen for it
4923
03:20:16,960 --> 03:20:18,560
as you can see that doesn't make sense
4924
03:20:18,560 --> 03:20:20,800
so first we would want to listen for the
4925
03:20:20,800 --> 03:20:21,840
event
4926
03:20:21,840 --> 03:20:24,319
and only then we would admit it so in
4927
03:20:24,319 --> 03:20:26,640
this case notice i don't have this some
4928
03:20:26,640 --> 03:20:29,279
other logic goes in here because i
4929
03:20:29,279 --> 03:20:31,439
listen for response and then i right
4930
03:20:31,439 --> 03:20:33,520
away admit the response and then again i
4931
03:20:33,520 --> 03:20:36,319
listen for the response but i already
4932
03:20:36,319 --> 03:20:38,800
admitted the event correct so it doesn't
4933
03:20:38,800 --> 03:20:41,279
make sense to listen for the event once
4934
03:20:41,279 --> 03:20:44,160
it has been already emitted so that's
4935
03:20:44,160 --> 03:20:45,680
the point number two
4936
03:20:45,680 --> 03:20:48,160
and then thirdly our also on a showcase
4937
03:20:48,160 --> 03:20:50,960
that we can pass the arguments when
4938
03:20:50,960 --> 03:20:53,680
we're omitting the event so for example
4939
03:20:53,680 --> 03:20:56,640
i go here with my emit i pass in the
4940
03:20:56,640 --> 03:20:58,720
name which is of course response and
4941
03:20:58,720 --> 03:21:01,600
then i can pass in more arguments for
4942
03:21:01,600 --> 03:21:04,319
example here i'll go john and then the
4943
03:21:04,319 --> 03:21:08,560
id number 34. so string and a number and
4944
03:21:08,560 --> 03:21:11,279
then in my callback function
4945
03:21:11,279 --> 03:21:13,920
i can access those arguments as
4946
03:21:13,920 --> 03:21:16,880
parameters just like normal functions so
4947
03:21:16,880 --> 03:21:19,840
in this case i'll call them name and id
4948
03:21:19,840 --> 03:21:22,560
and simply in my console.log i'll say
4949
03:21:22,560 --> 03:21:23,439
name
4950
03:21:23,439 --> 03:21:26,800
and the second one will be with id
4951
03:21:26,800 --> 03:21:29,279
and then whatever is that id
4952
03:21:29,279 --> 03:21:32,239
now i'll add here data received
4953
03:21:32,239 --> 03:21:34,800
user and then save it and it's no
4954
03:21:34,800 --> 03:21:36,800
surprise that in the console i see data
4955
03:21:36,800 --> 03:21:40,399
received user john with id34 now in the
4956
03:21:40,399 --> 03:21:42,160
second function as you can see
4957
03:21:42,160 --> 03:21:44,239
i wasn't looking for those arguments i
4958
03:21:44,239 --> 03:21:45,920
just don't have access to the arguments
4959
03:21:45,920 --> 03:21:48,080
because i don't have the parameters but
4960
03:21:48,080 --> 03:21:50,000
it's not like everything broke just
4961
03:21:50,000 --> 03:21:53,200
because in the event i passed in the
4962
03:21:53,200 --> 03:21:55,200
arguments and lastly i would just want
4963
03:21:55,200 --> 03:21:57,439
to reiterate the point that even though
4964
03:21:57,439 --> 03:21:59,279
you might not write
4965
03:21:59,279 --> 03:22:02,399
your own events events are a core
4966
03:22:02,399 --> 03:22:05,359
building block of node and effectively
4967
03:22:05,359 --> 03:22:08,160
as you're building and writing out the
4968
03:22:08,160 --> 03:22:10,960
code for your node application you are
4969
03:22:10,960 --> 03:22:13,359
using those events at the end of the day
4970
03:22:13,359 --> 03:22:16,800
anyway because a lot of built-in modules
4971
03:22:16,800 --> 03:22:19,840
rely on them and as a quick side note if
4972
03:22:19,840 --> 03:22:22,399
you'd like to see the code that we wrote
4973
03:22:22,399 --> 03:22:25,279
in previous two lectures you can just
4974
03:22:25,279 --> 03:22:28,800
navigate to 13 event emitter and in
4975
03:22:28,800 --> 03:22:31,920
order to show that i'll quickly set up a
4976
03:22:31,920 --> 03:22:32,960
server
4977
03:22:32,960 --> 03:22:35,120
and again the code for this one you can
4978
03:22:35,120 --> 03:22:39,120
find at 14 and then request event so
4979
03:22:39,120 --> 03:22:41,520
copy all the contents from the file
4980
03:22:41,520 --> 03:22:44,720
and then just copy and paste
4981
03:22:44,720 --> 03:22:46,720
and notice something interesting where
4982
03:22:46,720 --> 03:22:49,439
again yes we're getting the http module
4983
03:22:49,439 --> 03:22:52,080
and remember the setup that we have used
4984
03:22:52,080 --> 03:22:54,800
so far where we go with http create
4985
03:22:54,800 --> 03:22:57,920
server and then we pass in this callback
4986
03:22:57,920 --> 03:22:58,960
function
4987
03:22:58,960 --> 03:23:00,479
and then of course this callback
4988
03:23:00,479 --> 03:23:03,120
function will be invoked every time
4989
03:23:03,120 --> 03:23:07,200
someone visits our server so every time
4990
03:23:07,200 --> 03:23:08,960
the request
4991
03:23:08,960 --> 03:23:11,120
comes in now there's another way how we
4992
03:23:11,120 --> 03:23:14,960
can set it up by using event emitter api
4993
03:23:14,960 --> 03:23:17,439
so we still create a server we go with
4994
03:23:17,439 --> 03:23:21,439
http and then create server function but
4995
03:23:21,439 --> 03:23:22,800
instead of passing in the callback
4996
03:23:22,800 --> 03:23:25,439
function like we did previously
4997
03:23:25,439 --> 03:23:27,680
server has the method
4998
03:23:27,680 --> 03:23:28,800
on
4999
03:23:28,800 --> 03:23:30,800
does that ring a bell remember our
5000
03:23:30,800 --> 03:23:32,239
previous example
5001
03:23:32,239 --> 03:23:35,120
we had our own instance and
5002
03:23:35,120 --> 03:23:38,239
it had the method of on correct so the
5003
03:23:38,239 --> 03:23:40,800
same goes for server so server has the
5004
03:23:40,800 --> 03:23:42,000
method on
5005
03:23:42,000 --> 03:23:43,840
and we listen for
5006
03:23:43,840 --> 03:23:47,040
request event and when that request
5007
03:23:47,040 --> 03:23:48,560
comes in
5008
03:23:48,560 --> 03:23:50,080
then of course again we have this
5009
03:23:50,080 --> 03:23:52,960
callback function that handles it so
5010
03:23:52,960 --> 03:23:54,239
behind the scenes
5011
03:23:54,239 --> 03:23:57,920
server emits the request event and then
5012
03:23:57,920 --> 03:23:59,920
of course we can listen for it we can
5013
03:23:59,920 --> 03:24:02,720
subscribe to it listen for it or respond
5014
03:24:02,720 --> 03:24:04,880
to it however you'd want to call it and
5015
03:24:04,880 --> 03:24:07,439
the way we do that we go with server on
5016
03:24:07,439 --> 03:24:09,920
and then this is a specific name
5017
03:24:09,920 --> 03:24:13,359
because of course it emits that specific
5018
03:24:13,359 --> 03:24:15,279
name behind the scene so make sure that
5019
03:24:15,279 --> 03:24:18,640
it is a request and then the same spiel
5020
03:24:18,640 --> 03:24:21,600
now how do i know that well if we go to
5021
03:24:21,600 --> 03:24:24,160
node documentation and if we look for
5022
03:24:24,160 --> 03:24:26,080
http we keep on scrolling keep on
5023
03:24:26,080 --> 03:24:28,319
scrolling eventually we'll hit the
5024
03:24:28,319 --> 03:24:29,439
server
5025
03:24:29,439 --> 03:24:30,319
and
5026
03:24:30,319 --> 03:24:33,040
over here i can see that it has a clash
5027
03:24:33,040 --> 03:24:35,279
okay that's a good start and that what
5028
03:24:35,279 --> 03:24:36,640
do you see here
5029
03:24:36,640 --> 03:24:40,000
well i see the event right and what is
5030
03:24:40,000 --> 03:24:43,120
the event well the name is request so
5031
03:24:43,120 --> 03:24:44,560
that's how i know
5032
03:24:44,560 --> 03:24:46,800
that my server the instance that i
5033
03:24:46,800 --> 03:24:50,399
create has the ability to listen for
5034
03:24:50,399 --> 03:24:52,960
request events and if you want to dig
5035
03:24:52,960 --> 03:24:55,520
deeper you can just click on a server
5036
03:24:55,520 --> 03:24:58,560
you notice that it extends the net
5037
03:24:58,560 --> 03:25:01,840
server and if we keep on digging check
5038
03:25:01,840 --> 03:25:04,960
it out so this one extends what it
5039
03:25:04,960 --> 03:25:08,720
extends the event emitter so hopefully
5040
03:25:08,720 --> 03:25:11,359
that makes it clear that even though you
5041
03:25:11,359 --> 03:25:13,840
might not be setting up events on your
5042
03:25:13,840 --> 03:25:17,279
own a bunch of built-in modules rely
5043
03:25:17,279 --> 03:25:18,319
heavily
5044
03:25:18,319 --> 03:25:22,080
on this concept of events all right and
5045
03:25:22,080 --> 03:25:25,359
up next we have streams in node.js
5046
03:25:25,359 --> 03:25:28,000
and at its simplest streams are used to
5047
03:25:28,000 --> 03:25:31,359
read or write sequentially basically
5048
03:25:31,359 --> 03:25:33,760
when we have to handle and manipulate
5049
03:25:33,760 --> 03:25:36,319
streaming data for example continuous
5050
03:25:36,319 --> 03:25:39,040
source or a big file streams come in
5051
03:25:39,040 --> 03:25:40,319
real handy
5052
03:25:40,319 --> 03:25:41,920
and now there are four different types
5053
03:25:41,920 --> 03:25:45,200
of streams we have writable used to
5054
03:25:45,200 --> 03:25:46,880
write data sequentially then we have
5055
03:25:46,880 --> 03:25:49,439
readable used to read data sequentially
5056
03:25:49,439 --> 03:25:50,479
duplex
5057
03:25:50,479 --> 03:25:53,120
used to both read and write data
5058
03:25:53,120 --> 03:25:55,439
sequentially and also transform where
5059
03:25:55,439 --> 03:25:58,880
data can be modified when writing or
5060
03:25:58,880 --> 03:26:00,960
reading just like with events many
5061
03:26:00,960 --> 03:26:02,800
built-in modules in node implement
5062
03:26:02,800 --> 03:26:04,479
streaming interface
5063
03:26:04,479 --> 03:26:06,479
and what's also interesting streams
5064
03:26:06,479 --> 03:26:09,439
extend event emitters class which simply
5065
03:26:09,439 --> 03:26:12,319
means that we can use events like data
5066
03:26:12,319 --> 03:26:14,640
and and on streams
5067
03:26:14,640 --> 03:26:16,960
since streams are somewhat off an
5068
03:26:16,960 --> 03:26:19,520
advanced topic in node i'll try to keep
5069
03:26:19,520 --> 03:26:22,080
it short and sweet mostly by showing you
5070
03:26:22,080 --> 03:26:24,880
examples of streams and hopefully that
5071
03:26:24,880 --> 03:26:27,840
way you get the main idea without being
5072
03:26:27,840 --> 03:26:29,040
overwhelmed
5073
03:26:29,040 --> 03:26:30,880
all right and once we know the theory
5074
03:26:30,880 --> 03:26:33,040
behind the streams now let's take a look
5075
03:26:33,040 --> 03:26:34,960
at the practical example
5076
03:26:34,960 --> 03:26:38,000
and a very good use case is using
5077
03:26:38,000 --> 03:26:40,880
streams when we are reading files
5078
03:26:40,880 --> 03:26:43,359
because we need to understand one thing
5079
03:26:43,359 --> 03:26:46,880
where when we use the sync or
5080
03:26:46,880 --> 03:26:49,439
asynchronous approach what happens we're
5081
03:26:49,439 --> 03:26:51,760
reading the whole file and of course in
5082
03:26:51,760 --> 03:26:54,399
our example we were setting this equal
5083
03:26:54,399 --> 03:26:58,319
to a variable correct but if we have a
5084
03:26:58,319 --> 03:26:59,680
big file
5085
03:26:59,680 --> 03:27:01,840
well first of all you're just using all
5086
03:27:01,840 --> 03:27:04,319
that memory and second
5087
03:27:04,319 --> 03:27:06,399
as the file size gets bigger and bigger
5088
03:27:06,399 --> 03:27:09,520
bigger eventually the variable is not
5089
03:27:09,520 --> 03:27:11,520
going to be good enough you will get an
5090
03:27:11,520 --> 03:27:13,120
error that
5091
03:27:13,120 --> 03:27:14,880
i mean the size is too big and you
5092
03:27:14,880 --> 03:27:19,600
cannot place everything in the string so
5093
03:27:19,600 --> 03:27:21,760
what would be a solution one solution
5094
03:27:21,760 --> 03:27:24,720
would be read stream option
5095
03:27:24,720 --> 03:27:27,520
and the way we'll set this up
5096
03:27:27,520 --> 03:27:30,640
i'm going to start app.js from scratch
5097
03:27:30,640 --> 03:27:34,319
but if you take a look at the repo
5098
03:27:34,319 --> 03:27:37,920
you'll notice this 15 create big file so
5099
03:27:37,920 --> 03:27:39,359
previously when we were working with
5100
03:27:39,359 --> 03:27:43,040
files i just had some small files right
5101
03:27:43,040 --> 03:27:45,600
so i had the first txt blah blah blah
5102
03:27:45,600 --> 03:27:49,120
result and whatever now before we set up
5103
03:27:49,120 --> 03:27:51,520
the read stream i would want to create a
5104
03:27:51,520 --> 03:27:54,399
big file again this is optional you
5105
03:27:54,399 --> 03:27:56,800
don't have to do that but in my case in
5106
03:27:56,800 --> 03:27:58,720
order to show you how the streams work
5107
03:27:58,720 --> 03:28:02,000
yes i'll have to set up a big file and
5108
03:28:02,000 --> 03:28:04,239
first i'll remove it because of course
5109
03:28:04,239 --> 03:28:07,040
i already have it since i was testing it
5110
03:28:07,040 --> 03:28:09,279
and now i'll stop my server and of
5111
03:28:09,279 --> 03:28:12,319
course in my case i'll run 15 the create
5112
03:28:12,319 --> 03:28:14,080
big file js
5113
03:28:14,080 --> 03:28:16,000
if you want to set it on your own and if
5114
03:28:16,000 --> 03:28:17,760
you don't want to use the repo you can
5115
03:28:17,760 --> 03:28:18,479
just
5116
03:28:18,479 --> 03:28:21,040
get the right file sync from the fs
5117
03:28:21,040 --> 03:28:24,000
module and then notice i have the loop
5118
03:28:24,000 --> 03:28:28,239
where i believe i have here 10 000
5119
03:28:28,239 --> 03:28:32,399
and then every iteration i just write a
5120
03:28:32,399 --> 03:28:35,439
big txt i of course i have the flag
5121
03:28:35,439 --> 03:28:37,600
upend and then i'm just adding hello
5122
03:28:37,600 --> 03:28:41,040
world and starting a new line so if i'll
5123
03:28:41,040 --> 03:28:42,640
run node
5124
03:28:42,640 --> 03:28:44,560
and then of course i'm going to go with
5125
03:28:44,560 --> 03:28:46,800
my file name which is 15
5126
03:28:46,800 --> 03:28:48,560
then hyphen
5127
03:28:48,560 --> 03:28:49,760
create
5128
03:28:49,760 --> 03:28:50,840
big
5129
03:28:50,840 --> 03:28:54,239
file and js like so
5130
03:28:54,239 --> 03:28:57,200
then in the content i should have this
5131
03:28:57,200 --> 03:28:58,399
big file
5132
03:28:58,399 --> 03:29:01,680
and once i have my big file i'll go back
5133
03:29:01,680 --> 03:29:04,880
to app.js and i'll use my npm start
5134
03:29:04,880 --> 03:29:06,880
since of course i have known mine in
5135
03:29:06,880 --> 03:29:10,239
place and then let's go with cons now
5136
03:29:10,239 --> 03:29:12,560
i'm not going to show you how the setup
5137
03:29:12,560 --> 03:29:16,399
would look like with file sync or the
5138
03:29:16,399 --> 03:29:18,399
asynchronous one again hopefully you
5139
03:29:18,399 --> 03:29:20,160
understand that the setup would be
5140
03:29:20,160 --> 03:29:23,439
exactly the same like we have in 10 and
5141
03:29:23,439 --> 03:29:28,160
11 where we had the fa sync and fa async
5142
03:29:28,160 --> 03:29:30,399
and now of course let's just go with the
5143
03:29:30,399 --> 03:29:33,200
stream option so the method name is
5144
03:29:33,200 --> 03:29:34,399
create
5145
03:29:34,399 --> 03:29:35,439
read
5146
03:29:35,439 --> 03:29:39,439
stream and we require that from the fs
5147
03:29:39,439 --> 03:29:41,359
module like so
5148
03:29:41,359 --> 03:29:42,720
and then
5149
03:29:42,720 --> 03:29:45,359
we create a new variable and in my case
5150
03:29:45,359 --> 03:29:48,960
i'll call this stream and we invoke the
5151
03:29:48,960 --> 03:29:52,000
create read stream and the only thing by
5152
03:29:52,000 --> 03:29:54,640
default we need to pass in is the path
5153
03:29:54,640 --> 03:29:56,080
so of course in my case i'm going to go
5154
03:29:56,080 --> 03:29:58,000
with content and then forward slash and
5155
03:29:58,000 --> 03:30:00,880
since i just created that text file the
5156
03:30:00,880 --> 03:30:02,720
big one of course i'm going to go with
5157
03:30:02,720 --> 03:30:05,600
big txt and now remember the good old
5158
03:30:05,600 --> 03:30:07,680
friends events well
5159
03:30:07,680 --> 03:30:10,479
once we create this instance we actually
5160
03:30:10,479 --> 03:30:12,720
have access to them and the ones that
5161
03:30:12,720 --> 03:30:15,840
we're going to use is going to be data
5162
03:30:15,840 --> 03:30:19,279
and error so we can go with stream
5163
03:30:19,279 --> 03:30:22,319
and then on and remember the event
5164
03:30:22,319 --> 03:30:25,359
syntax and then the event that you would
5165
03:30:25,359 --> 03:30:28,560
want to listen for is a data and then we
5166
03:30:28,560 --> 03:30:31,359
have our callback function and again it
5167
03:30:31,359 --> 03:30:33,200
is a parameter so we can call whatever
5168
03:30:33,200 --> 03:30:34,560
we would want but in my case i'm going
5169
03:30:34,560 --> 03:30:38,000
to go result and let's just console.log
5170
03:30:38,000 --> 03:30:40,560
the result like so
5171
03:30:40,560 --> 03:30:43,200
and what you'll notice
5172
03:30:43,200 --> 03:30:45,000
is that we have
5173
03:30:45,000 --> 03:30:47,680
64 kilobytes here that's our first
5174
03:30:47,680 --> 03:30:51,680
console log then we have another one 464
5175
03:30:51,680 --> 03:30:53,680
kilobytes and the reason why i know that
5176
03:30:53,680 --> 03:30:56,239
is because by default that's the size
5177
03:30:56,239 --> 03:30:58,479
we're getting and then we're getting the
5178
03:30:58,479 --> 03:31:01,920
reminder now just to prove my point i'm
5179
03:31:01,920 --> 03:31:03,680
going to go to my folder
5180
03:31:03,680 --> 03:31:07,200
and i'm going to look for my content
5181
03:31:07,200 --> 03:31:10,080
and then in there if you take a look the
5182
03:31:10,080 --> 03:31:13,000
big text file we created is
5183
03:31:13,000 --> 03:31:16,160
169 kilobytes so as you can see now
5184
03:31:16,160 --> 03:31:19,200
we're reading data in chunks
5185
03:31:19,200 --> 03:31:22,560
and by default that chunk is 64
5186
03:31:22,560 --> 03:31:23,840
kilobytes
5187
03:31:23,840 --> 03:31:27,120
and every time we cancel log
5188
03:31:27,120 --> 03:31:30,479
we see that we have 64 64 and eventually
5189
03:31:30,479 --> 03:31:33,600
we have the remainder so instead of
5190
03:31:33,600 --> 03:31:36,479
reading everything and placing that in
5191
03:31:36,479 --> 03:31:39,120
the variable we're doing that in chunks
5192
03:31:39,120 --> 03:31:42,319
and as you can see we're using the event
5193
03:31:42,319 --> 03:31:45,120
and the event is data and in order to
5194
03:31:45,120 --> 03:31:46,560
hammer this home why don't we take a
5195
03:31:46,560 --> 03:31:48,319
look at the docs
5196
03:31:48,319 --> 03:31:50,239
so first i'm going to look for my file
5197
03:31:50,239 --> 03:31:51,680
system
5198
03:31:51,680 --> 03:31:53,600
and then let's look for
5199
03:31:53,600 --> 03:31:56,960
our create read stream so we go here as
5200
03:31:56,960 --> 03:32:00,319
we can see we have this class read
5201
03:32:00,319 --> 03:32:02,800
stream and then on this class we have
5202
03:32:02,800 --> 03:32:05,520
these three ones we have close open
5203
03:32:05,520 --> 03:32:08,080
and ready and as a side note we'll use
5204
03:32:08,080 --> 03:32:10,560
this one a little bit later as well the
5205
03:32:10,560 --> 03:32:13,279
open one so keep an eye on this one and
5206
03:32:13,279 --> 03:32:16,319
then as far as the read stream well as
5207
03:32:16,319 --> 03:32:19,040
you can see it is created when we go
5208
03:32:19,040 --> 03:32:22,239
with create read stream and invoke it
5209
03:32:22,239 --> 03:32:25,920
right so i got the create read stream
5210
03:32:25,920 --> 03:32:28,000
that's my function i just passed in the
5211
03:32:28,000 --> 03:32:31,840
past and i invoked it and in turn
5212
03:32:31,840 --> 03:32:33,279
that returns
5213
03:32:33,279 --> 03:32:35,200
the read stream
5214
03:32:35,200 --> 03:32:36,880
right then
5215
03:32:36,880 --> 03:32:40,200
actual read stream extends from
5216
03:32:40,200 --> 03:32:42,720
stream.readable so right away i have
5217
03:32:42,720 --> 03:32:44,800
these events i have close open and ready
5218
03:32:44,800 --> 03:32:46,800
and like i said we use this open a
5219
03:32:46,800 --> 03:32:49,040
little bit later now since it is
5220
03:32:49,040 --> 03:32:52,560
extending from the stream check it out
5221
03:32:52,560 --> 03:32:54,640
now of course i have the data one all
5222
03:32:54,640 --> 03:32:57,040
right so this is a chunk of data
5223
03:32:57,040 --> 03:32:59,520
and as you can see every time we are
5224
03:32:59,520 --> 03:33:01,200
getting that data
5225
03:33:01,200 --> 03:33:02,880
we can do something with it now at the
5226
03:33:02,880 --> 03:33:04,560
moment we're just going to logging it's
5227
03:33:04,560 --> 03:33:07,120
very simple but eventually as you can
5228
03:33:07,120 --> 03:33:10,399
already imagine we can do way more
5229
03:33:10,399 --> 03:33:13,439
useful things than that beautiful once
5230
03:33:13,439 --> 03:33:15,600
we understand the basics now let's take
5231
03:33:15,600 --> 03:33:18,479
a look at some additional info like i
5232
03:33:18,479 --> 03:33:21,840
said by default the size of the buffer
5233
03:33:21,840 --> 03:33:25,279
is going to be 64 kilobytes however we
5234
03:33:25,279 --> 03:33:27,920
can also control it and the way we do
5235
03:33:27,920 --> 03:33:32,000
that is by passing in the object when
5236
03:33:32,000 --> 03:33:34,640
we're setting up create read stream so
5237
03:33:34,640 --> 03:33:36,800
first argument is going to be the path
5238
03:33:36,800 --> 03:33:38,479
which again in my case is going to be
5239
03:33:38,479 --> 03:33:42,560
that big text file that i created using
5240
03:33:42,560 --> 03:33:46,080
my file using my for loop and then i can
5241
03:33:46,080 --> 03:33:48,960
pass in the options object and in there
5242
03:33:48,960 --> 03:33:50,399
the property that you're looking for the
5243
03:33:50,399 --> 03:33:53,359
property that controls the size of the
5244
03:33:53,359 --> 03:33:54,479
buffer
5245
03:33:54,479 --> 03:33:57,200
has the name of high water mark so for
5246
03:33:57,200 --> 03:34:01,520
example if i go with 90 000 here then of
5247
03:34:01,520 --> 03:34:03,680
course you'll see that i'll have two
5248
03:34:03,680 --> 03:34:04,960
console logs
5249
03:34:04,960 --> 03:34:06,319
one for
5250
03:34:06,319 --> 03:34:08,560
90 kilobytes and the second one will be
5251
03:34:08,560 --> 03:34:11,279
remainder again keep in mind that
5252
03:34:11,279 --> 03:34:13,720
our file size is
5253
03:34:13,720 --> 03:34:16,800
169 kilobytes that's number one number
5254
03:34:16,800 --> 03:34:17,920
two
5255
03:34:17,920 --> 03:34:20,080
we can right away set up the encoding as
5256
03:34:20,080 --> 03:34:23,200
well so i go with comma and then the
5257
03:34:23,200 --> 03:34:25,600
property name is encoding and again
5258
03:34:25,600 --> 03:34:29,920
we'll set it up equal to utf-8 like so
5259
03:34:29,920 --> 03:34:31,359
and then you'll see in a console of
5260
03:34:31,359 --> 03:34:33,840
course the hello world why well because
5261
03:34:33,840 --> 03:34:36,800
that is the content coming from the file
5262
03:34:36,800 --> 03:34:38,960
and lastly i also would want to mention
5263
03:34:38,960 --> 03:34:42,960
that we have the error event as well so
5264
03:34:42,960 --> 03:34:46,000
if i go with stream and then on and
5265
03:34:46,000 --> 03:34:48,479
we're checking for the error and as
5266
03:34:48,479 --> 03:34:50,560
always we can do something once that
5267
03:34:50,560 --> 03:34:52,399
error happens so that's our callback
5268
03:34:52,399 --> 03:34:54,640
function and in this case i'll just
5269
03:34:54,640 --> 03:34:56,640
access the error property and i'll
5270
03:34:56,640 --> 03:34:59,520
console.log the error so i'll say here
5271
03:34:59,520 --> 03:35:02,000
error and the way we check that one out
5272
03:35:02,000 --> 03:35:04,960
is by providing wrong path so in my case
5273
03:35:04,960 --> 03:35:07,520
i'm just going to add two dots and then
5274
03:35:07,520 --> 03:35:10,720
once i save check it out in a console
5275
03:35:10,720 --> 03:35:13,439
i'll have the error where it tells me
5276
03:35:13,439 --> 03:35:17,279
that no such file or directory
5277
03:35:17,279 --> 03:35:18,880
all right and now let's take a look at
5278
03:35:18,880 --> 03:35:22,239
the practical example when streams come
5279
03:35:22,239 --> 03:35:24,319
in handy now again this is going to be
5280
03:35:24,319 --> 03:35:26,239
one of those videos where you don't have
5281
03:35:26,239 --> 03:35:29,279
to code along you can just sit back and
5282
03:35:29,279 --> 03:35:33,120
relax and see how i struggle and first
5283
03:35:33,120 --> 03:35:36,160
what i want is to make it even a bigger
5284
03:35:36,160 --> 03:35:39,920
file so remember d15 create big file
5285
03:35:39,920 --> 03:35:43,439
i believe i had 10 iterations right so
5286
03:35:43,439 --> 03:35:45,680
i'm gonna add one more
5287
03:35:45,680 --> 03:35:48,720
meaning i'll add one more zero i'll stop
5288
03:35:48,720 --> 03:35:50,800
my server right now again this is
5289
03:35:50,800 --> 03:35:53,120
optional you don't have to do that and
5290
03:35:53,120 --> 03:35:55,680
i'll remove the big file
5291
03:35:55,680 --> 03:35:56,880
like so
5292
03:35:56,880 --> 03:35:59,279
and then i'll run one more time
5293
03:35:59,279 --> 03:36:01,359
the file with node
5294
03:36:01,359 --> 03:36:03,760
and then of course this file is going to
5295
03:36:03,760 --> 03:36:05,200
be way bigger
5296
03:36:05,200 --> 03:36:07,279
so let me navigate to my folder just to
5297
03:36:07,279 --> 03:36:08,720
showcase that
5298
03:36:08,720 --> 03:36:10,640
and i'm looking for content and now the
5299
03:36:10,640 --> 03:36:14,479
big file is 1.8 megs
5300
03:36:14,479 --> 03:36:16,960
now once i have the big file
5301
03:36:16,960 --> 03:36:19,120
i'll go with npm
5302
03:36:19,120 --> 03:36:20,800
start at the moment i just have the
5303
03:36:20,800 --> 03:36:23,600
stream example but if you want to see
5304
03:36:23,600 --> 03:36:26,319
the whole code just take a look at the
5305
03:36:26,319 --> 03:36:28,560
file number 17 and notice again we're
5306
03:36:28,560 --> 03:36:31,279
creating a http server and i'm just
5307
03:36:31,279 --> 03:36:33,680
using the read file stick method i'm
5308
03:36:33,680 --> 03:36:36,319
looking for big txt the encoding is
5309
03:36:36,319 --> 03:36:40,160
utf-8 and then i just place my variable
5310
03:36:40,160 --> 03:36:42,960
my text one into
5311
03:36:42,960 --> 03:36:47,040
res dot and method and effectively i'm
5312
03:36:47,040 --> 03:36:50,160
just sending my big text file
5313
03:36:50,160 --> 03:36:53,279
and let me restart the server
5314
03:36:53,279 --> 03:36:57,279
here so let me go with npm start right
5315
03:36:57,279 --> 03:36:59,279
now so let me select everything from
5316
03:36:59,279 --> 03:37:00,560
this file
5317
03:37:00,560 --> 03:37:03,920
and copy and paste
5318
03:37:03,920 --> 03:37:07,880
and now if we navigate to localhost
5319
03:37:07,880 --> 03:37:09,680
5000
5320
03:37:09,680 --> 03:37:12,640
we should see a bunch of hello world
5321
03:37:12,640 --> 03:37:14,720
now the problem with the setup is
5322
03:37:14,720 --> 03:37:17,200
following where if i go to developer
5323
03:37:17,200 --> 03:37:19,520
tools and if i take a look at the
5324
03:37:19,520 --> 03:37:21,840
network and if i refresh
5325
03:37:21,840 --> 03:37:22,640
yep
5326
03:37:22,640 --> 03:37:25,040
this request was successful
5327
03:37:25,040 --> 03:37:26,800
take a look at the size
5328
03:37:26,800 --> 03:37:28,000
do you think
5329
03:37:28,000 --> 03:37:30,640
it is the smartest thing to send these
5330
03:37:30,640 --> 03:37:33,920
type of files over the wire and of
5331
03:37:33,920 --> 03:37:35,840
course this is just going to make it
5332
03:37:35,840 --> 03:37:38,960
very difficult to all your users because
5333
03:37:38,960 --> 03:37:42,000
you're just sending large chunks of data
5334
03:37:42,000 --> 03:37:44,399
effectively i'm sending the whole file
5335
03:37:44,399 --> 03:37:47,279
and more specifically if i click check
5336
03:37:47,279 --> 03:37:49,920
it out so i have the request url okay
5337
03:37:49,920 --> 03:37:52,319
that is good the method is get
5338
03:37:52,319 --> 03:37:55,439
now as you can see the content length
5339
03:37:55,439 --> 03:37:58,880
is my 1.8 megs right
5340
03:37:58,880 --> 03:38:01,279
and you'll see in a second that once we
5341
03:38:01,279 --> 03:38:05,200
refactor this to read stream method
5342
03:38:05,200 --> 03:38:07,600
that we're sending data in the chunks
5343
03:38:07,600 --> 03:38:10,479
and again the best way to see that is by
5344
03:38:10,479 --> 03:38:12,080
looking at the headers where at the
5345
03:38:12,080 --> 03:38:14,399
moment i can see the content length and
5346
03:38:14,399 --> 03:38:18,080
that one is at 1.8 megs but once we
5347
03:38:18,080 --> 03:38:20,880
refactor that the setup is going to be
5348
03:38:20,880 --> 03:38:23,200
different so at the moment i have this
5349
03:38:23,200 --> 03:38:26,319
text i'm accessing that by read file
5350
03:38:26,319 --> 03:38:28,880
sync so comment this out and let's set
5351
03:38:28,880 --> 03:38:31,680
it up with our create read stream so
5352
03:38:31,680 --> 03:38:33,840
i'll go with const and i'll call this
5353
03:38:33,840 --> 03:38:34,880
file
5354
03:38:34,880 --> 03:38:37,040
stream and this is going to be equal to
5355
03:38:37,040 --> 03:38:40,720
fs and then create read stream now use
5356
03:38:40,720 --> 03:38:42,560
the same address
5357
03:38:42,560 --> 03:38:44,960
so use the same path
5358
03:38:44,960 --> 03:38:47,359
and then i'll use the same encoding as
5359
03:38:47,359 --> 03:38:48,160
well
5360
03:38:48,160 --> 03:38:51,199
so let's go with utf-8 okay
5361
03:38:51,199 --> 03:38:53,040
awesome and once we set up the stream
5362
03:38:53,040 --> 03:38:55,279
remember we have access to
5363
03:38:55,279 --> 03:38:56,880
to events
5364
03:38:56,880 --> 03:38:58,640
file stream on
5365
03:38:58,640 --> 03:39:00,160
and of course i'm going to be looking
5366
03:39:00,160 --> 03:39:01,920
for the open
5367
03:39:01,920 --> 03:39:04,560
and i'll have my callback function and
5368
03:39:04,560 --> 03:39:06,640
we'll set that functionality up in a
5369
03:39:06,640 --> 03:39:08,800
second and the second one of course is
5370
03:39:08,800 --> 03:39:11,920
the error so go with file stream and
5371
03:39:11,920 --> 03:39:15,040
then on and again we're looking for the
5372
03:39:15,040 --> 03:39:17,520
event by the name of error and then in
5373
03:39:17,520 --> 03:39:19,279
our callback function we can access the
5374
03:39:19,279 --> 03:39:23,279
error and i'll simply pass it in in my
5375
03:39:23,279 --> 03:39:25,520
response so if there is an error i'll
5376
03:39:25,520 --> 03:39:28,399
just grab the error from my parameter
5377
03:39:28,399 --> 03:39:31,439
and pass it in now as far as the open
5378
03:39:31,439 --> 03:39:34,239
here instead of setting it equal to
5379
03:39:34,239 --> 03:39:36,800
instead of going res dot end and then
5380
03:39:36,800 --> 03:39:40,160
text file stream also have the pipe
5381
03:39:40,160 --> 03:39:43,040
method so we can go to file stream and
5382
03:39:43,040 --> 03:39:46,319
then pipe and what the pipe is doing it
5383
03:39:46,319 --> 03:39:49,120
is pushing from the read stream into
5384
03:39:49,120 --> 03:39:51,760
right stream so you can imagine that if
5385
03:39:51,760 --> 03:39:55,600
we can read data in chunks we can also
5386
03:39:55,600 --> 03:39:58,080
write data in chunks and what happens
5387
03:39:58,080 --> 03:40:00,880
under the herd response object can be
5388
03:40:00,880 --> 03:40:03,600
set up as a writable stream so we have
5389
03:40:03,600 --> 03:40:05,439
our file stream so that's our read
5390
03:40:05,439 --> 03:40:08,239
stream we have method of pipe so we're
5391
03:40:08,239 --> 03:40:11,439
piping this into a writable stream and
5392
03:40:11,439 --> 03:40:14,399
now of course i'll pass in my response
5393
03:40:14,399 --> 03:40:17,120
object and if we go back to the browser
5394
03:40:17,120 --> 03:40:18,960
and if we'll refresh
5395
03:40:18,960 --> 03:40:21,680
now notice something interesting yeah
5396
03:40:21,680 --> 03:40:24,160
still same request right to localhost
5397
03:40:24,160 --> 03:40:27,279
5000 still same size but
5398
03:40:27,279 --> 03:40:30,800
if i take a look at the headers now i
5399
03:40:30,800 --> 03:40:34,479
can see that my response headers are
5400
03:40:34,479 --> 03:40:37,040
chunked so instead of sending our file
5401
03:40:37,040 --> 03:40:39,680
back in one large instance we're sending
5402
03:40:39,680 --> 03:40:43,439
it back in chunks awesome congrats on
5403
03:40:43,439 --> 03:40:45,520
completing section number three
5404
03:40:45,520 --> 03:40:48,319
node fundamentals and hopefully after
5405
03:40:48,319 --> 03:40:50,560
watching the section you have a good
5406
03:40:50,560 --> 03:40:53,439
general understanding of how node works
5407
03:40:53,439 --> 03:40:55,600
and now that we're big shots let's apply
5408
03:40:55,600 --> 03:40:58,239
that knowledge and build some servers
5409
03:40:58,239 --> 03:41:00,560
shall we and we're going to start
5410
03:41:00,560 --> 03:41:03,279
section 4 with the general info of how
5411
03:41:03,279 --> 03:41:06,000
the web works more specifically how we
5412
03:41:06,000 --> 03:41:08,960
exchange data on the web
5413
03:41:08,960 --> 03:41:10,960
and we're going to do that with the help
5414
03:41:10,960 --> 03:41:12,720
of few slides
5415
03:41:12,720 --> 03:41:14,720
now if you're one of the people who's
5416
03:41:14,720 --> 03:41:17,279
already reached a daily slide limit i
5417
03:41:17,279 --> 03:41:18,239
hear you
5418
03:41:18,239 --> 03:41:20,000
once we're done with these suckers i
5419
03:41:20,000 --> 03:41:22,880
promise we'll mostly actually write code
5420
03:41:22,880 --> 03:41:25,199
for the remainder of the course
5421
03:41:25,199 --> 03:41:27,520
i do believe though that before we build
5422
03:41:27,520 --> 03:41:30,160
an actual web server it's crucial for us
5423
03:41:30,160 --> 03:41:32,399
to understand how it works under the
5424
03:41:32,399 --> 03:41:34,640
hood and that way we'll have clear
5425
03:41:34,640 --> 03:41:37,520
understanding of what tasks need to be
5426
03:41:37,520 --> 03:41:40,160
done if something is iffy you can always
5427
03:41:40,160 --> 03:41:42,880
go back to the slides you already know
5428
03:41:42,880 --> 03:41:45,840
where they're located and also as we're
5429
03:41:45,840 --> 03:41:48,239
going to be progressing with the course
5430
03:41:48,239 --> 03:41:50,239
from time to time i will swing back to
5431
03:41:50,239 --> 03:41:52,640
them when i'll need to emphasize a
5432
03:41:52,640 --> 03:41:53,920
specific point
5433
03:41:53,920 --> 03:41:56,160
with that said let's talk about how we
5434
03:41:56,160 --> 03:41:58,000
exchange data
5435
03:41:58,000 --> 03:41:59,600
on the web
5436
03:41:59,600 --> 03:42:01,760
um it's safe to say that if you're
5437
03:42:01,760 --> 03:42:03,279
watching this video
5438
03:42:03,279 --> 03:42:06,000
you know how to use a web browser
5439
03:42:06,000 --> 03:42:08,319
but under the hood the way it works
5440
03:42:08,319 --> 03:42:10,479
every time we open up a browser
5441
03:42:10,479 --> 03:42:13,920
and we type the url so the web address
5442
03:42:13,920 --> 03:42:17,199
we're actually performing a request
5443
03:42:17,199 --> 03:42:20,000
and we are performing a request to the
5444
03:42:20,000 --> 03:42:21,279
server
5445
03:42:21,279 --> 03:42:22,960
that is responsible
5446
03:42:22,960 --> 03:42:26,239
serving those resources so for example
5447
03:42:26,239 --> 03:42:28,720
when you look for cnn.com
5448
03:42:28,720 --> 03:42:31,279
you're looking for a server that has
5449
03:42:31,279 --> 03:42:34,239
those resources and then that server
5450
03:42:34,239 --> 03:42:37,520
sends you back the response and the same
5451
03:42:37,520 --> 03:42:39,439
works with youtube the same works with
5452
03:42:39,439 --> 03:42:41,120
johnsmeld.com
5453
03:42:41,120 --> 03:42:44,080
or whatever so you just pick a resource
5454
03:42:44,080 --> 03:42:45,199
you say hey
5455
03:42:45,199 --> 03:42:48,399
can i get this data and then server who
5456
03:42:48,399 --> 03:42:51,600
is responsible for those resources just
5457
03:42:51,600 --> 03:42:54,640
sends you back the response now that is
5458
03:42:54,640 --> 03:42:58,080
done using http protocol and that's why
5459
03:42:58,080 --> 03:43:01,439
these ones are called http messages
5460
03:43:01,439 --> 03:43:04,080
so the user sends the http
5461
03:43:04,080 --> 03:43:08,160
request message and then the server
5462
03:43:08,160 --> 03:43:11,840
sends the http response message
5463
03:43:11,840 --> 03:43:15,439
and that's how we exchange data on web
5464
03:43:15,439 --> 03:43:17,920
and for the remainder of the course
5465
03:43:17,920 --> 03:43:21,199
we'll be responsible for building
5466
03:43:21,199 --> 03:43:24,800
such web server using node and more
5467
03:43:24,800 --> 03:43:27,600
specifically express now i'll talk about
5468
03:43:27,600 --> 03:43:30,080
it while we use express as well show you
5469
03:43:30,080 --> 03:43:31,760
a few examples
5470
03:43:31,760 --> 03:43:34,080
later on in a course but for now just
5471
03:43:34,080 --> 03:43:37,439
remember that yes we'll use node but in
5472
03:43:37,439 --> 03:43:40,319
order to make our lives easier we'll use
5473
03:43:40,319 --> 03:43:44,000
the framework by the name of express js
5474
03:43:44,000 --> 03:43:45,920
and lastly i would just want to mention
5475
03:43:45,920 --> 03:43:48,720
that even though name server definitely
5476
03:43:48,720 --> 03:43:51,920
sounds way cooler than the computer
5477
03:43:51,920 --> 03:43:53,920
at the end of the day they're just
5478
03:43:53,920 --> 03:43:56,000
computers whose job
5479
03:43:56,000 --> 03:43:59,439
is to always make that resource
5480
03:43:59,439 --> 03:44:00,800
available
5481
03:44:00,800 --> 03:44:04,479
so yes those are computers and yes there
5482
03:44:04,479 --> 03:44:06,560
are some differences for example
5483
03:44:06,560 --> 03:44:08,319
they most likely don't have the
5484
03:44:08,319 --> 03:44:11,040
graphical interface or the gui
5485
03:44:11,040 --> 03:44:13,680
and also they're probably much bigger
5486
03:44:13,680 --> 03:44:16,080
than your laptop but at the end of the
5487
03:44:16,080 --> 03:44:18,560
day they are computers
5488
03:44:18,560 --> 03:44:20,000
whose job
5489
03:44:20,000 --> 03:44:22,560
is to always make sure that that
5490
03:44:22,560 --> 03:44:26,000
resource is available because imagine if
5491
03:44:26,000 --> 03:44:28,239
you would have a server that only works
5492
03:44:28,239 --> 03:44:31,359
from eight to five so if you go to your
5493
03:44:31,359 --> 03:44:34,160
website at i don't know six o'clock at
5494
03:44:34,160 --> 03:44:37,040
night you cannot access the resource
5495
03:44:37,040 --> 03:44:39,840
probably that's not the service that you
5496
03:44:39,840 --> 03:44:42,399
would pick so next time when someone
5497
03:44:42,399 --> 03:44:46,080
asks you if it rains does the cloud
5498
03:44:46,080 --> 03:44:48,000
still work you know what to answer
5499
03:44:48,000 --> 03:44:49,840
because at the end of the day when we
5500
03:44:49,840 --> 03:44:53,040
talk about cloud cloud is just a bunch
5501
03:44:53,040 --> 03:44:54,960
of these servers bunch of these
5502
03:44:54,960 --> 03:44:58,560
computers connected together beautiful
5503
03:44:58,560 --> 03:45:00,319
and once we have a general understanding
5504
03:45:00,319 --> 03:45:03,199
how data is exchanged on a web
5505
03:45:03,199 --> 03:45:06,640
let's also cover how http messages are
5506
03:45:06,640 --> 03:45:08,560
structured please keep in mind that
5507
03:45:08,560 --> 03:45:11,680
since it's going to be a big part of our
5508
03:45:11,680 --> 03:45:12,399
job
5509
03:45:12,399 --> 03:45:14,640
we'll come back to this particular slide
5510
03:45:14,640 --> 03:45:16,800
more than once so if you're if you're
5511
03:45:16,800 --> 03:45:18,960
about something just please be patient
5512
03:45:18,960 --> 03:45:22,080
and i'll answer all of your questions as
5513
03:45:22,080 --> 03:45:24,479
we move along with the course with that
5514
03:45:24,479 --> 03:45:26,560
said the general structure
5515
03:45:26,560 --> 03:45:29,600
for both messages is similar they both
5516
03:45:29,600 --> 03:45:31,680
have a start line
5517
03:45:31,680 --> 03:45:34,239
they both have optional headers a blank
5518
03:45:34,239 --> 03:45:36,479
line that indicates that all the meta
5519
03:45:36,479 --> 03:45:39,359
info has been sent and effectively
5520
03:45:39,359 --> 03:45:43,040
headers are that meta info as well as
5521
03:45:43,040 --> 03:45:45,920
optional body and before we continue
5522
03:45:45,920 --> 03:45:49,439
again let me stress something so request
5523
03:45:49,439 --> 03:45:50,800
messages
5524
03:45:50,800 --> 03:45:54,319
are what the user is sending so for
5525
03:45:54,319 --> 03:45:57,040
example if you open up the browser to
5526
03:45:57,040 --> 03:46:00,319
search the web or that could be your web
5527
03:46:00,319 --> 03:46:02,960
application because remember with web
5528
03:46:02,960 --> 03:46:05,840
applications we also can make http
5529
03:46:05,840 --> 03:46:09,680
requests correct and then response
5530
03:46:09,680 --> 03:46:12,239
is going to be our responsibility so
5531
03:46:12,239 --> 03:46:15,120
we'll have to set up a proper server
5532
03:46:15,120 --> 03:46:19,120
that sends a correct response
5533
03:46:19,120 --> 03:46:21,600
and in general when we talk about the
5534
03:46:21,600 --> 03:46:22,399
request
5535
03:46:22,399 --> 03:46:23,359
message
5536
03:46:23,359 --> 03:46:25,760
in start line there's going to be a
5537
03:46:25,760 --> 03:46:26,880
method
5538
03:46:26,880 --> 03:46:31,199
then the url as well as the http version
5539
03:46:31,199 --> 03:46:32,239
now
5540
03:46:32,239 --> 03:46:34,560
we are mostly going to be interested in
5541
03:46:34,560 --> 03:46:37,199
these two things in the method as well
5542
03:46:37,199 --> 03:46:39,760
as the url now there's going to be a
5543
03:46:39,760 --> 03:46:43,520
separate slide about the methods as well
5544
03:46:43,520 --> 03:46:45,680
and don't worry we'll cover that in a
5545
03:46:45,680 --> 03:46:48,720
greater detail once we cover them with
5546
03:46:48,720 --> 03:46:51,439
actual code examples so i'll come back
5547
03:46:51,439 --> 03:46:53,439
to that slide just understand the
5548
03:46:53,439 --> 03:46:55,920
general idea that when we talk about
5549
03:46:55,920 --> 03:46:58,800
methods effectively we're communicating
5550
03:46:58,800 --> 03:47:01,680
what we would want to do so for example
5551
03:47:01,680 --> 03:47:04,479
if i want to get the resource i'm going
5552
03:47:04,479 --> 03:47:08,000
to set it up as a get request now if i
5553
03:47:08,000 --> 03:47:09,600
want to add the resource then of course
5554
03:47:09,600 --> 03:47:12,000
this is going to be a post request and
5555
03:47:12,000 --> 03:47:15,199
then of course you can read the rest now
5556
03:47:15,199 --> 03:47:18,239
why i'm displaying here this get because
5557
03:47:18,239 --> 03:47:20,640
that is the default request
5558
03:47:20,640 --> 03:47:22,720
that the browser performs
5559
03:47:22,720 --> 03:47:24,960
since when we open up the browser we
5560
03:47:24,960 --> 03:47:27,120
want to get some kind of resource
5561
03:47:27,120 --> 03:47:29,840
correct that's why we're performing a
5562
03:47:29,840 --> 03:47:33,279
get request and the url is just the
5563
03:47:33,279 --> 03:47:35,520
address and that could be for example
5564
03:47:35,520 --> 03:47:38,239
johnsmilk.com or that could be i don't
5565
03:47:38,239 --> 03:47:42,000
know espn forward slash basketball both
5566
03:47:42,000 --> 03:47:45,600
of them are just a web addresses now
5567
03:47:45,600 --> 03:47:46,640
headers
5568
03:47:46,640 --> 03:47:48,880
is essentially optional
5569
03:47:48,880 --> 03:47:51,840
where this is a meta information about
5570
03:47:51,840 --> 03:47:54,000
our request and just to give you a
5571
03:47:54,000 --> 03:47:55,840
general idea how the headers would look
5572
03:47:55,840 --> 03:47:58,080
like they would look something like this
5573
03:47:58,080 --> 03:48:01,359
so it is a key value pairs now don't
5574
03:48:01,359 --> 03:48:02,479
freak out
5575
03:48:02,479 --> 03:48:04,880
as you know when you search the web you
5576
03:48:04,880 --> 03:48:07,359
don't need to add them manually and the
5577
03:48:07,359 --> 03:48:10,399
same is going to be with our server a
5578
03:48:10,399 --> 03:48:12,560
lot of it is going to be taken care of
5579
03:48:12,560 --> 03:48:15,840
but yes some things we'll have to add
5580
03:48:15,840 --> 03:48:18,080
on our own basically this is going to be
5581
03:48:18,080 --> 03:48:21,760
information about our message
5582
03:48:21,760 --> 03:48:25,279
and then we also have optional body
5583
03:48:25,279 --> 03:48:28,080
so when the user is requesting something
5584
03:48:28,080 --> 03:48:30,479
if you just want the resource there is
5585
03:48:30,479 --> 03:48:33,199
nobody there's nothing basically what to
5586
03:48:33,199 --> 03:48:36,720
send however if we would want to add a
5587
03:48:36,720 --> 03:48:39,120
resource onto the server yes
5588
03:48:39,120 --> 03:48:41,680
then you are expected to provide that
5589
03:48:41,680 --> 03:48:45,040
data and it's also called payload so
5590
03:48:45,040 --> 03:48:47,920
again i might use them interchangeably
5591
03:48:47,920 --> 03:48:50,160
so that's the general structure of the
5592
03:48:50,160 --> 03:48:51,520
request message
5593
03:48:51,520 --> 03:48:53,199
we have the start line we have the
5594
03:48:53,199 --> 03:48:56,239
headers that is the information about
5595
03:48:56,239 --> 03:48:58,479
our message as well as the black line
5596
03:48:58,479 --> 03:49:00,800
which just indicates that we're done
5597
03:49:00,800 --> 03:49:03,520
with the meta information and optional
5598
03:49:03,520 --> 03:49:06,479
body which we'll use from time to time
5599
03:49:06,479 --> 03:49:08,479
and now of course we need to talk about
5600
03:49:08,479 --> 03:49:11,840
the response message so response message
5601
03:49:11,840 --> 03:49:14,880
is what we're going to be creating again
5602
03:49:14,880 --> 03:49:17,760
most of it will be done for us but some
5603
03:49:17,760 --> 03:49:20,640
parts yes we'll have to do it manually
5604
03:49:20,640 --> 03:49:22,800
and it's going to be our responsibility
5605
03:49:22,800 --> 03:49:24,479
so the start line
5606
03:49:24,479 --> 03:49:26,640
has the http version which most likely
5607
03:49:26,640 --> 03:49:28,560
is going to be 1.1
5608
03:49:28,560 --> 03:49:32,080
then we have a status code and status
5609
03:49:32,080 --> 03:49:36,560
text now when it comes to status code
5610
03:49:36,560 --> 03:49:38,640
it just signals
5611
03:49:38,640 --> 03:49:41,199
what is the result of the request so for
5612
03:49:41,199 --> 03:49:43,520
example notice here this 200
5613
03:49:43,520 --> 03:49:46,960
that means that request was successful
5614
03:49:46,960 --> 03:49:49,600
so that's what we're sending back and
5615
03:49:49,600 --> 03:49:52,080
yes there's quite a few of those codes
5616
03:49:52,080 --> 03:49:54,560
and as we move along with the course of
5617
03:49:54,560 --> 03:49:56,960
course we will cover them but please
5618
03:49:56,960 --> 03:50:00,000
please please don't rush over to the
5619
03:50:00,000 --> 03:50:02,399
search engine and start memorizing the
5620
03:50:02,399 --> 03:50:04,560
status codes pretty much as we're going
5621
03:50:04,560 --> 03:50:06,880
to be building the application i'll tell
5622
03:50:06,880 --> 03:50:09,600
you which code means what and for
5623
03:50:09,600 --> 03:50:12,640
example 400 means that there was some
5624
03:50:12,640 --> 03:50:14,880
kind of request error
5625
03:50:14,880 --> 03:50:18,239
so the user was trying to request some
5626
03:50:18,239 --> 03:50:21,920
kind of research and with 400 there was
5627
03:50:21,920 --> 03:50:24,720
a request error now for example the code
5628
03:50:24,720 --> 03:50:28,560
404 means that the resource is not found
5629
03:50:28,560 --> 03:50:30,560
so when i'm sending back the message i'm
5630
03:50:30,560 --> 03:50:33,840
like hey listen here's the status code
5631
03:50:33,840 --> 03:50:36,160
for the message if you are successful
5632
03:50:36,160 --> 03:50:38,720
here's 200 if there was an error for
5633
03:50:38,720 --> 03:50:42,720
example you get a 400 if the
5634
03:50:42,720 --> 03:50:44,880
request is unauthorized then you can get
5635
03:50:44,880 --> 03:50:47,920
401 and hopefully you get the gist and
5636
03:50:47,920 --> 03:50:50,560
yes of course as we're progressing we
5637
03:50:50,560 --> 03:50:53,920
will cover more status code then again
5638
03:50:53,920 --> 03:50:55,760
we have the headers
5639
03:50:55,760 --> 03:50:59,040
where basically we provide info
5640
03:50:59,040 --> 03:51:03,199
about our message our response message
5641
03:51:03,199 --> 03:51:06,080
and again like i said you won't have to
5642
03:51:06,080 --> 03:51:08,960
type everything line by line but
5643
03:51:08,960 --> 03:51:10,960
effectively it is a
5644
03:51:10,960 --> 03:51:13,920
setup of key value pairs and the ones
5645
03:51:13,920 --> 03:51:15,680
that i would like to mention are these
5646
03:51:15,680 --> 03:51:17,760
ones notice this content type we have
5647
03:51:17,760 --> 03:51:20,960
text html that is when we're sending
5648
03:51:20,960 --> 03:51:25,920
back the html but also our second option
5649
03:51:25,920 --> 03:51:28,640
is going to be sending back the data
5650
03:51:28,640 --> 03:51:30,479
so hopefully you are aware that when
5651
03:51:30,479 --> 03:51:31,920
you're building a web application when
5652
03:51:31,920 --> 03:51:34,880
we communicate with api most of the time
5653
03:51:34,880 --> 03:51:38,000
we're getting back the json data because
5654
03:51:38,000 --> 03:51:40,800
over the web effectively we just send
5655
03:51:40,800 --> 03:51:43,840
over the string so if for example i have
5656
03:51:43,840 --> 03:51:47,279
some kind of array we transfer this into
5657
03:51:47,279 --> 03:51:50,000
application json and i clearly indicate
5658
03:51:50,000 --> 03:51:52,560
that in my headers where i say that no
5659
03:51:52,560 --> 03:51:55,840
i'm not sending text and html i'm
5660
03:51:55,840 --> 03:51:58,960
actually sending a application json and
5661
03:51:58,960 --> 03:52:00,960
then that application who is requesting
5662
03:52:00,960 --> 03:52:03,040
the web application knows that hey from
5663
03:52:03,040 --> 03:52:05,439
the server i'm getting a content type
5664
03:52:05,439 --> 03:52:08,239
which is application json so for example
5665
03:52:08,239 --> 03:52:12,239
in that case i could send back here this
5666
03:52:12,239 --> 03:52:15,520
array with bunch of these objects and
5667
03:52:15,520 --> 03:52:19,040
just to give you a real-world taste why
5668
03:52:19,040 --> 03:52:20,560
don't we do this
5669
03:52:20,560 --> 03:52:23,040
open up a new browser tab and i'm simply
5670
03:52:23,040 --> 03:52:25,279
going to course api because i think
5671
03:52:25,279 --> 03:52:27,199
there's going to be less data so there's
5672
03:52:27,199 --> 03:52:30,399
going to be less clutter so go to course
5673
03:52:30,399 --> 03:52:33,520
api here and then you can either pick
5674
03:52:33,520 --> 03:52:35,279
the slides or you can just look at the
5675
03:52:35,279 --> 03:52:38,399
home page and if you inspect
5676
03:52:38,399 --> 03:52:40,720
and if you look in the browser tab
5677
03:52:40,720 --> 03:52:42,720
you'll find a network
5678
03:52:42,720 --> 03:52:45,040
and very useful one is this one where we
5679
03:52:45,040 --> 03:52:47,760
can just look for all the requests we
5680
03:52:47,760 --> 03:52:50,880
refresh and then check it out so these
5681
03:52:50,880 --> 03:52:52,319
are the requests
5682
03:52:52,319 --> 03:52:54,399
that browser performed and i'll talk
5683
03:52:54,399 --> 03:52:56,960
about it a bit later why there's also
5684
03:52:56,960 --> 03:52:59,199
this style css in fact that's going to
5685
03:52:59,199 --> 03:53:01,520
be one of our first examples but take a
5686
03:53:01,520 --> 03:53:04,160
look at this one so i have this course
5687
03:53:04,160 --> 03:53:06,560
api and then forward slash and if you
5688
03:53:06,560 --> 03:53:08,239
remember when we were building our
5689
03:53:08,239 --> 03:53:11,359
original server what i said that if we
5690
03:53:11,359 --> 03:53:13,120
have this forward slash that just means
5691
03:53:13,120 --> 03:53:16,000
home page so if we're just navigating to
5692
03:53:16,000 --> 03:53:17,920
courseapi.com
5693
03:53:17,920 --> 03:53:18,880
and
5694
03:53:18,880 --> 03:53:21,760
that is our homepage we simply add this
5695
03:53:21,760 --> 03:53:23,040
forward slash now of course in the
5696
03:53:23,040 --> 03:53:25,199
browser that's automatically added but
5697
03:53:25,199 --> 03:53:26,560
that's why when we're setting up the
5698
03:53:26,560 --> 03:53:28,800
server one of the cases is going to be
5699
03:53:28,800 --> 03:53:31,680
if the user navigates to the homepage
5700
03:53:31,680 --> 03:53:34,720
then we simply look for forward slash so
5701
03:53:34,720 --> 03:53:38,479
if for example you go to the slides
5702
03:53:38,479 --> 03:53:40,880
you'll notice that uh in this case i
5703
03:53:40,880 --> 03:53:43,120
would have to refresh notice here i have
5704
03:53:43,120 --> 03:53:45,040
the course api
5705
03:53:45,040 --> 03:53:48,399
forward slash slides so that is already
5706
03:53:48,399 --> 03:53:53,279
a page in my website correct so i have
5707
03:53:53,279 --> 03:53:55,840
the home page course api and then i have
5708
03:53:55,840 --> 03:53:58,800
the slides page where i have the slides
5709
03:53:58,800 --> 03:54:01,120
and if you take a look if you click on
5710
03:54:01,120 --> 03:54:05,199
it notice all the useful data that we
5711
03:54:05,199 --> 03:54:07,840
get over here so for example this is
5712
03:54:07,840 --> 03:54:10,560
going to be our request url which is
5713
03:54:10,560 --> 03:54:12,880
going to be again course api.com so that
5714
03:54:12,880 --> 03:54:15,760
tells me well what the browser is trying
5715
03:54:15,760 --> 03:54:16,640
to get
5716
03:54:16,640 --> 03:54:18,720
then check it out the method why it's
5717
03:54:18,720 --> 03:54:20,800
get because that is default in the
5718
03:54:20,800 --> 03:54:22,399
browsers every time you open up the
5719
03:54:22,399 --> 03:54:24,000
browser and search something then of
5720
03:54:24,000 --> 03:54:25,520
course you'll be performing a get
5721
03:54:25,520 --> 03:54:27,840
request now check out this one the
5722
03:54:27,840 --> 03:54:30,640
status code it is 200 why because
5723
03:54:30,640 --> 03:54:33,199
everything is successful because servers
5724
03:54:33,199 --> 03:54:35,439
send back the data then we have this
5725
03:54:35,439 --> 03:54:37,600
remote address and basically that is the
5726
03:54:37,600 --> 03:54:40,399
ip address for my server as well as
5727
03:54:40,399 --> 03:54:43,279
notice this colon 443
5728
03:54:43,279 --> 03:54:45,279
and we'll come back to this one when we
5729
03:54:45,279 --> 03:54:47,600
set up our own server because if you
5730
03:54:47,600 --> 03:54:49,439
remember we were always setting up the
5731
03:54:49,439 --> 03:54:51,359
5000 so you're probably wondering well
5732
03:54:51,359 --> 03:54:54,479
what is this 443 and don't worry again
5733
03:54:54,479 --> 03:54:57,600
we'll cover that one once we set up our
5734
03:54:57,600 --> 03:54:59,359
first server
5735
03:54:59,359 --> 03:55:02,160
and then again we have response headers
5736
03:55:02,160 --> 03:55:05,199
for example and we also have the request
5737
03:55:05,199 --> 03:55:07,600
adders so as you can see using this
5738
03:55:07,600 --> 03:55:10,239
network tab we can actually find a bunch
5739
03:55:10,239 --> 03:55:13,120
of useful information now
5740
03:55:13,120 --> 03:55:17,520
notice here in the second set of tabs i
5741
03:55:17,520 --> 03:55:21,120
can see this response and preview so if
5742
03:55:21,120 --> 03:55:22,880
you take a look at the preview
5743
03:55:22,880 --> 03:55:25,359
this is going to be basically my site
5744
03:55:25,359 --> 03:55:28,319
and also i can see the response so as
5745
03:55:28,319 --> 03:55:30,720
you can see pretty much sending back the
5746
03:55:30,720 --> 03:55:32,000
html
5747
03:55:32,000 --> 03:55:34,800
so i'm sending back the website so you
5748
03:55:34,800 --> 03:55:37,120
go into the browser you request a
5749
03:55:37,120 --> 03:55:38,319
resource
5750
03:55:38,319 --> 03:55:40,800
by default it is a get request
5751
03:55:40,800 --> 03:55:42,720
and then in that body
5752
03:55:42,720 --> 03:55:44,640
when the server sends it back
5753
03:55:44,640 --> 03:55:47,600
this is what we get back so we get back
5754
03:55:47,600 --> 03:55:50,080
the site so hopefully this gives you a
5755
03:55:50,080 --> 03:55:53,840
clear understanding of how the http
5756
03:55:53,840 --> 03:55:56,800
messages are structured and now let's
5757
03:55:56,800 --> 03:55:59,920
dive into creating our own server
5758
03:55:59,920 --> 03:56:02,239
awesome once we're done with the slides
5759
03:56:02,239 --> 03:56:05,359
now let's start setting up our server
5760
03:56:05,359 --> 03:56:07,840
and in order to follow along with the
5761
03:56:07,840 --> 03:56:10,560
course you'll need to have a starter
5762
03:56:10,560 --> 03:56:12,560
project and you can get it if you
5763
03:56:12,560 --> 03:56:16,000
navigate to my site again the url is
5764
03:56:16,000 --> 03:56:17,760
johnsmilk.com
5765
03:56:17,760 --> 03:56:20,640
so navigate there in the project or in
5766
03:56:20,640 --> 03:56:24,560
home page look for node express tutorial
5767
03:56:24,560 --> 03:56:26,880
project in a home page is going to be
5768
03:56:26,880 --> 03:56:29,439
at the bottom in latest project if you
5769
03:56:29,439 --> 03:56:32,160
cannot find it there then look for all
5770
03:56:32,160 --> 03:56:34,960
projects page or simply projects page
5771
03:56:34,960 --> 03:56:37,040
then filter for node in order to save
5772
03:56:37,040 --> 03:56:38,880
some time and then like i already
5773
03:56:38,880 --> 03:56:41,199
previously mentioned any of these links
5774
03:56:41,199 --> 03:56:44,479
will get you to the repo and then
5775
03:56:44,479 --> 03:56:46,319
bravely get the repo in my case i'm
5776
03:56:46,319 --> 03:56:49,279
going to clone it so i'll copy the url
5777
03:56:49,279 --> 03:56:52,080
i'll bump up the sound size in my
5778
03:56:52,080 --> 03:56:55,199
terminal navigate to the desktop
5779
03:56:55,199 --> 03:56:57,439
get clone here
5780
03:56:57,439 --> 03:57:00,399
and then i'm gonna get the repo and then
5781
03:57:00,399 --> 03:57:03,120
once the project is on my machine
5782
03:57:03,120 --> 03:57:05,600
i'm just gonna drag and drop and then
5783
03:57:05,600 --> 03:57:08,319
the first thing that i would suggest
5784
03:57:08,319 --> 03:57:12,160
doing is removing the remote url and you
5785
03:57:12,160 --> 03:57:14,800
can simply do that by just wiping out
5786
03:57:14,800 --> 03:57:17,520
the git folder so that way if you ever
5787
03:57:17,520 --> 03:57:20,000
do decide you push this up to the github
5788
03:57:20,000 --> 03:57:21,920
you won't have some dumb permission
5789
03:57:21,920 --> 03:57:24,479
errors so i'm just going to open up the
5790
03:57:24,479 --> 03:57:26,640
integrated terminal now in my case i'm
5791
03:57:26,640 --> 03:57:29,040
not going to run the command but
5792
03:57:29,040 --> 03:57:32,399
if you're on mac i suggest running rm
5793
03:57:32,399 --> 03:57:36,160
then hyphen rf and then get now if you
5794
03:57:36,160 --> 03:57:38,160
are on windows
5795
03:57:38,160 --> 03:57:40,319
some students have just these two
5796
03:57:40,319 --> 03:57:42,479
commands now i haven't tested them yet
5797
03:57:42,479 --> 03:57:45,040
but some people have replied to
5798
03:57:45,040 --> 03:57:48,080
my tweet that yes they do work and if
5799
03:57:48,080 --> 03:57:49,520
you want to see the whole tweet just
5800
03:57:49,520 --> 03:57:52,080
navigate to my twitter the handle is
5801
03:57:52,080 --> 03:57:54,800
john underscore smilga and then as you
5802
03:57:54,800 --> 03:57:57,199
can see here is the mac command and for
5803
03:57:57,199 --> 03:58:00,239
windows i have these two so run this
5804
03:58:00,239 --> 03:58:02,479
command and then in next video
5805
03:58:02,479 --> 03:58:05,680
i'll give you a brief overview of the
5806
03:58:05,680 --> 03:58:09,279
repo all right and before we get busy
5807
03:58:09,279 --> 03:58:12,960
why don't i give you a brief overview of
5808
03:58:12,960 --> 03:58:14,239
the ripple
5809
03:58:14,239 --> 03:58:16,800
and if you take a look at the first
5810
03:58:16,800 --> 03:58:19,439
project this should look very very
5811
03:58:19,439 --> 03:58:22,319
familiar because essentially this is all
5812
03:58:22,319 --> 03:58:25,199
the code we have written so far
5813
03:58:25,199 --> 03:58:27,920
everything starting with our most basic
5814
03:58:27,920 --> 03:58:30,239
application and then of course we also
5815
03:58:30,239 --> 03:58:34,000
have our last one our http streams
5816
03:58:34,000 --> 03:58:36,800
example so hopefully that is clear and
5817
03:58:36,800 --> 03:58:39,040
if you ever need to jog your memory
5818
03:58:39,040 --> 03:58:41,040
essentially this is just here for your
5819
03:58:41,040 --> 03:58:42,160
reference
5820
03:58:42,160 --> 03:58:44,800
and then from now on we'll work in a
5821
03:58:44,800 --> 03:58:48,239
folder number two by the name of express
5822
03:58:48,239 --> 03:58:49,359
tutorial
5823
03:58:49,359 --> 03:58:51,840
and the idea is gonna be exactly the
5824
03:58:51,840 --> 03:58:53,920
same where basically
5825
03:58:53,920 --> 03:58:56,720
we build a bunch of examples and in the
5826
03:58:56,720 --> 03:58:59,279
process we learned what is express and
5827
03:58:59,279 --> 03:59:01,760
why we would prefer using express
5828
03:59:01,760 --> 03:59:04,080
instead of straight up http module but
5829
03:59:04,080 --> 03:59:06,160
again let me stress something where
5830
03:59:06,160 --> 03:59:07,840
express is built
5831
03:59:07,840 --> 03:59:10,319
on top of node and more specifically on
5832
03:59:10,319 --> 03:59:13,840
top of http module so it's not like you
5833
03:59:13,840 --> 03:59:17,359
can use express without node and in
5834
03:59:17,359 --> 03:59:18,800
folder two
5835
03:59:18,800 --> 03:59:20,399
the setup is following
5836
03:59:20,399 --> 03:59:22,800
where there are gonna be some assets
5837
03:59:22,800 --> 03:59:24,560
that i provided
5838
03:59:24,560 --> 03:59:27,520
just so we can make more real world
5839
03:59:27,520 --> 03:59:29,920
examples so hopefully that gives you a
5840
03:59:29,920 --> 03:59:33,600
better idea how the express works and
5841
03:59:33,600 --> 03:59:34,800
i'm not going to cover them right now
5842
03:59:34,800 --> 03:59:37,439
for example methods public or navbar app
5843
03:59:37,439 --> 03:59:39,279
once we get there i'll explain
5844
03:59:39,279 --> 03:59:41,359
everything that's happening i think that
5845
03:59:41,359 --> 03:59:42,960
it's going to be waste of our time if i
5846
03:59:42,960 --> 03:59:45,760
do that right now however if you take a
5847
03:59:45,760 --> 03:59:48,560
look at the final folder you'll
5848
03:59:48,560 --> 03:59:49,920
basically see
5849
03:59:49,920 --> 03:59:52,880
all our examples so the same spiel
5850
03:59:52,880 --> 03:59:54,399
if you ever
5851
03:59:54,399 --> 03:59:56,640
need to reference the code just navigate
5852
03:59:56,640 --> 04:00:00,000
to the final look for the file name and
5853
04:00:00,000 --> 04:00:01,840
then of course you'll find the code just
5854
04:00:01,840 --> 04:00:04,319
keep in mind that in order to run
5855
04:00:04,319 --> 04:00:07,120
this code you'll have to copy and paste
5856
04:00:07,120 --> 04:00:10,160
and run it in the app.js why
5857
04:00:10,160 --> 04:00:11,600
because in some cases there's going to
5858
04:00:11,600 --> 04:00:14,160
be paths and since these files are
5859
04:00:14,160 --> 04:00:17,840
located already in the final the paths
5860
04:00:17,840 --> 04:00:18,960
won't match
5861
04:00:18,960 --> 04:00:22,479
okay hopefully that one is clear as well
5862
04:00:22,479 --> 04:00:25,760
and then we also should be familiar with
5863
04:00:25,760 --> 04:00:27,359
these files
5864
04:00:27,359 --> 04:00:30,080
as you can see i have package.json i
5865
04:00:30,080 --> 04:00:33,199
have package.json lock and then app.js
5866
04:00:33,199 --> 04:00:35,600
now of course app.js could be renamed
5867
04:00:35,600 --> 04:00:38,000
server or whatever you would want but i
5868
04:00:38,000 --> 04:00:40,800
already have in a package.json a script
5869
04:00:40,800 --> 04:00:43,120
that spins up the nodemon and
5870
04:00:43,120 --> 04:00:45,439
effectively we have successfully set up
5871
04:00:45,439 --> 04:00:48,160
a node application so package.json is of
5872
04:00:48,160 --> 04:00:50,960
course needed because we'll have some
5873
04:00:50,960 --> 04:00:53,920
dependencies more specifically two one
5874
04:00:53,920 --> 04:00:55,920
is going to be nodemod and the second
5875
04:00:55,920 --> 04:00:58,160
one is going to be express
5876
04:00:58,160 --> 04:01:00,560
and in order to of course keep track of
5877
04:01:00,560 --> 04:01:02,840
those dependencies we have
5878
04:01:02,840 --> 04:01:06,000
package.json and then if you notice i
5879
04:01:06,000 --> 04:01:08,640
don't have the node modules here and of
5880
04:01:08,640 --> 04:01:10,319
course the reason for that is because i
5881
04:01:10,319 --> 04:01:13,040
don't want this repo to be massive and
5882
04:01:13,040 --> 04:01:15,359
you can clearly see that in a git ignore
5883
04:01:15,359 --> 04:01:18,000
where i have node modules included so
5884
04:01:18,000 --> 04:01:20,479
when i was pushing this up to the github
5885
04:01:20,479 --> 04:01:21,600
and of course
5886
04:01:21,600 --> 04:01:24,160
i omitted the node modules since i added
5887
04:01:24,160 --> 04:01:27,439
them in the git ignore and then in the
5888
04:01:27,439 --> 04:01:29,600
package json like i said
5889
04:01:29,600 --> 04:01:31,279
you'll find some dependencies and i'll
5890
04:01:31,279 --> 04:01:33,359
talk about express once we get there the
5891
04:01:33,359 --> 04:01:35,680
moment let's not worry about it the one
5892
04:01:35,680 --> 04:01:37,920
that we should be familiar with is node
5893
04:01:37,920 --> 04:01:39,680
one which is going to be watching our
5894
04:01:39,680 --> 04:01:42,720
application and i have a script
5895
04:01:42,720 --> 04:01:45,120
that sets up nodemon
5896
04:01:45,120 --> 04:01:48,160
and passes in the amp.js so any changes
5897
04:01:48,160 --> 04:01:50,399
in our project nodemon will be watching
5898
04:01:50,399 --> 04:01:53,520
and restarting our application however
5899
04:01:53,520 --> 04:01:57,120
we cannot do anything before we install
5900
04:01:57,120 --> 04:01:59,520
all dependencies otherwise you'll just
5901
04:01:59,520 --> 04:02:01,680
get an error so that's why the first
5902
04:02:01,680 --> 04:02:03,760
thing that i would want you to do is
5903
04:02:03,760 --> 04:02:07,120
navigate to the folder number two
5904
04:02:07,120 --> 04:02:08,800
and the fastest way probably is just
5905
04:02:08,800 --> 04:02:12,800
going to be cd and then take the
5906
04:02:12,800 --> 04:02:14,640
project number two and drag and drop
5907
04:02:14,640 --> 04:02:17,600
like so and then once you can see
5908
04:02:17,600 --> 04:02:19,920
that you are in this folder
5909
04:02:19,920 --> 04:02:22,399
so not in a root not in node express
5910
04:02:22,399 --> 04:02:25,040
course but once you are actually in the
5911
04:02:25,040 --> 04:02:27,680
folder number two before you do anything
5912
04:02:27,680 --> 04:02:29,600
just run npm
5913
04:02:29,600 --> 04:02:32,319
install first and then it's really up to
5914
04:02:32,319 --> 04:02:34,239
you you can either wait for all the
5915
04:02:34,239 --> 04:02:36,399
dependencies to be installed and then
5916
04:02:36,399 --> 04:02:39,439
run npm start or you can simply chain it
5917
04:02:39,439 --> 04:02:41,840
and say npm
5918
04:02:41,840 --> 04:02:44,080
and once you run this one
5919
04:02:44,080 --> 04:02:45,359
then of course you'll install
5920
04:02:45,359 --> 04:02:47,920
dependencies and if you see in a
5921
04:02:47,920 --> 04:02:51,359
terminal express tutorial that means
5922
04:02:51,359 --> 04:02:53,439
that everything is correct and you can
5923
04:02:53,439 --> 04:02:56,000
continue with the videos if you don't
5924
04:02:56,000 --> 04:02:58,160
see that then please troubleshoot
5925
04:02:58,160 --> 04:02:59,520
because none of the things that we're
5926
04:02:59,520 --> 04:03:01,920
about to do next are going to make sense
5927
04:03:01,920 --> 04:03:04,720
if you're not running the project number
5928
04:03:04,720 --> 04:03:06,880
two with all the dependencies more
5929
04:03:06,880 --> 04:03:10,640
specifically nodemon and express
5930
04:03:10,640 --> 04:03:12,399
and lastly i just want to mention yes
5931
04:03:12,399 --> 04:03:15,279
there's also this data.js and again this
5932
04:03:15,279 --> 04:03:17,680
is one of the assets that i added that
5933
04:03:17,680 --> 04:03:19,920
we're going to use a little bit later so
5934
04:03:19,920 --> 04:03:22,399
if you see express tutorial in a console
5935
04:03:22,399 --> 04:03:24,479
and the reason why it's there because
5936
04:03:24,479 --> 04:03:26,319
that's the only line of code i have in
5937
04:03:26,319 --> 04:03:28,479
my app.js that means that everything is
5938
04:03:28,479 --> 04:03:31,600
correct and we can move on to our next
5939
04:03:31,600 --> 04:03:34,800
topic and why don't we start
5940
04:03:34,800 --> 04:03:37,760
the express tutorial part by setting up
5941
04:03:37,760 --> 04:03:40,640
the server one more time with
5942
04:03:40,640 --> 04:03:42,720
straight up http module
5943
04:03:42,720 --> 04:03:44,880
and in a process let's struggle a little
5944
04:03:44,880 --> 04:03:45,760
bit
5945
04:03:45,760 --> 04:03:47,920
let's see why we would want to use
5946
04:03:47,920 --> 04:03:50,720
express e in the first place
5947
04:03:50,720 --> 04:03:54,239
so yes we'll build one more time server
5948
04:03:54,239 --> 04:03:58,000
with http module however now of course
5949
04:03:58,000 --> 04:04:01,600
we'll dive deep into the topics that we
5950
04:04:01,600 --> 04:04:03,680
kind of skipped over before
5951
04:04:03,680 --> 04:04:05,840
and in order to set up the server we
5952
04:04:05,840 --> 04:04:08,560
know that we would need to require the
5953
04:04:08,560 --> 04:04:10,239
module right so i'm going to come up
5954
04:04:10,239 --> 04:04:12,000
with some kind of variable and in my
5955
04:04:12,000 --> 04:04:14,319
case that is going to be http and of
5956
04:04:14,319 --> 04:04:16,239
course the name is not required you can
5957
04:04:16,239 --> 04:04:18,239
call this whatever you'd want but it is
5958
04:04:18,239 --> 04:04:20,720
somewhat off a convention to call this
5959
04:04:20,720 --> 04:04:23,600
hdp as far as the variable name then we
5960
04:04:23,600 --> 04:04:27,439
go with require and we just go with http
5961
04:04:27,439 --> 04:04:29,040
again keep in mind
5962
04:04:29,040 --> 04:04:31,760
this comes built in node
5963
04:04:31,760 --> 04:04:33,840
so you don't have to install it we
5964
04:04:33,840 --> 04:04:36,880
didn't install when we ran npm installed
5965
04:04:36,880 --> 04:04:38,319
in last video
5966
04:04:38,319 --> 04:04:40,640
we installed two other dependencies but
5967
04:04:40,640 --> 04:04:42,479
this one is built in so we don't have to
5968
04:04:42,479 --> 04:04:45,439
do anything we just say require and it's
5969
04:04:45,439 --> 04:04:47,520
always going to be available to us
5970
04:04:47,520 --> 04:04:49,760
then we need to set up the server and
5971
04:04:49,760 --> 04:04:52,160
again convention is calling the server
5972
04:04:52,160 --> 04:04:54,479
but you can call this box bunny and in
5973
04:04:54,479 --> 04:04:57,040
order to do that we go with http and
5974
04:04:57,040 --> 04:05:00,160
then on this object we have a method by
5975
04:05:00,160 --> 04:05:03,600
the name of create server now this
5976
04:05:03,600 --> 04:05:04,880
method
5977
04:05:04,880 --> 04:05:06,640
takes a callback which is going to be
5978
04:05:06,640 --> 04:05:09,920
invoked every time the user
5979
04:05:09,920 --> 04:05:13,439
hits our server and in this callback we
5980
04:05:13,439 --> 04:05:15,920
have two parameters and again we can
5981
04:05:15,920 --> 04:05:18,160
call them whatever we would want so
5982
04:05:18,160 --> 04:05:21,359
again common convention is just shorten
5983
04:05:21,359 --> 04:05:24,560
this up a little bit and call it request
5984
04:05:24,560 --> 04:05:27,680
and res so rec and res and the first
5985
04:05:27,680 --> 04:05:30,960
parameter references the request object
5986
04:05:30,960 --> 04:05:33,760
and second one is the response object
5987
04:05:33,760 --> 04:05:36,720
and at this point it should be clear why
5988
04:05:36,720 --> 04:05:39,279
we have access to those two parameters
5989
04:05:39,279 --> 04:05:40,319
because
5990
04:05:40,319 --> 04:05:43,600
in the http cycle we have request
5991
04:05:43,600 --> 04:05:44,640
message
5992
04:05:44,640 --> 04:05:47,600
that is coming in every time user
5993
04:05:47,600 --> 04:05:50,160
requests the resource and also we have
5994
04:05:50,160 --> 04:05:52,640
the response message right so that's
5995
04:05:52,640 --> 04:05:54,640
what we need to send out so that's why
5996
04:05:54,640 --> 04:05:56,160
in this function
5997
04:05:56,160 --> 04:05:58,479
that runs every time user hits the
5998
04:05:58,479 --> 04:05:59,520
server
5999
04:05:59,520 --> 04:06:02,080
we have access to rec
6000
04:06:02,080 --> 04:06:03,520
and res
6001
04:06:03,520 --> 04:06:06,560
so that way we can find info about the
6002
04:06:06,560 --> 04:06:08,720
incoming request because it makes a
6003
04:06:08,720 --> 04:06:10,479
total sense if we have access to that
6004
04:06:10,479 --> 04:06:11,439
info
6005
04:06:11,439 --> 04:06:14,319
i do want to know what is the method so
6006
04:06:14,319 --> 04:06:16,560
what the user is trying to do i do want
6007
04:06:16,560 --> 04:06:19,279
to know what resource the user is trying
6008
04:06:19,279 --> 04:06:20,239
to get
6009
04:06:20,239 --> 04:06:22,720
whether that is the contact page or
6010
04:06:22,720 --> 04:06:25,439
about page and also i would want to know
6011
04:06:25,439 --> 04:06:27,680
whether i'm getting some data
6012
04:06:27,680 --> 04:06:31,279
whether user is trying to add something
6013
04:06:31,279 --> 04:06:33,439
onto the server and as far as the
6014
04:06:33,439 --> 04:06:36,319
response well in order to
6015
04:06:36,319 --> 04:06:37,680
serve the data
6016
04:06:37,680 --> 04:06:41,199
i need to be extremely specific about
6017
04:06:41,199 --> 04:06:43,520
what i'm sending so i need to explain to
6018
04:06:43,520 --> 04:06:46,399
the browser what is coming in hopefully
6019
04:06:46,399 --> 04:06:49,199
that is clear and in here we can just go
6020
04:06:49,199 --> 04:06:50,479
with log
6021
04:06:50,479 --> 04:06:53,279
and we'll say user hit the
6022
04:06:53,279 --> 04:06:56,560
server and then we return the object
6023
04:06:56,560 --> 04:06:58,800
from this create server and on that
6024
04:06:58,800 --> 04:07:01,520
object we have a method by the name of
6025
04:07:01,520 --> 04:07:04,399
listen so in order for the server to
6026
04:07:04,399 --> 04:07:06,640
work we need to invoke listen and we
6027
04:07:06,640 --> 04:07:08,399
need to be a bit more specific and we
6028
04:07:08,399 --> 04:07:10,720
need to say well what port are we
6029
04:07:10,720 --> 04:07:12,399
listening for and you're probably
6030
04:07:12,399 --> 04:07:14,640
wondering well what's up with this port
6031
04:07:14,640 --> 04:07:18,399
what's up with this 5000 value
6032
04:07:18,399 --> 04:07:20,800
and the best explanation i can give you
6033
04:07:20,800 --> 04:07:23,760
is this one imagine scenario where you
6034
04:07:23,760 --> 04:07:26,479
have some kind of issue with your bank
6035
04:07:26,479 --> 04:07:28,560
what do you do or that could be a phone
6036
04:07:28,560 --> 04:07:30,319
company it doesn't really matter but i'm
6037
04:07:30,319 --> 04:07:32,319
just going to give you a bank example
6038
04:07:32,319 --> 04:07:34,319
what do you do you call the customer
6039
04:07:34,319 --> 04:07:36,080
service and what is the first question
6040
04:07:36,080 --> 04:07:37,600
that they ask you
6041
04:07:37,600 --> 04:07:38,880
they say
6042
04:07:38,880 --> 04:07:41,120
please describe your issue
6043
04:07:41,120 --> 04:07:44,239
so we may better assist with your call
6044
04:07:44,239 --> 04:07:45,680
and what do they do they give you
6045
04:07:45,680 --> 04:07:48,159
options right so they say if you want to
6046
04:07:48,159 --> 04:07:50,000
increase your credit limit this is going
6047
04:07:50,000 --> 04:07:52,560
to be option number one all the way to
6048
04:07:52,560 --> 04:07:54,560
canceling the account which maybe is
6049
04:07:54,560 --> 04:07:57,760
going to be option number eight so they
6050
04:07:57,760 --> 04:08:00,560
don't just randomly assist you a person
6051
04:08:00,560 --> 04:08:02,640
they say hey what is the issue
6052
04:08:02,640 --> 04:08:05,120
and this is the person that can serve
6053
04:08:05,120 --> 04:08:07,520
you the best so if you press 8
6054
04:08:07,520 --> 04:08:08,960
you can assume that you're probably
6055
04:08:08,960 --> 04:08:10,239
going to be speaking with someone who
6056
04:08:10,239 --> 04:08:12,880
deals with that all the time
6057
04:08:12,880 --> 04:08:15,840
and same goes here so if you go online
6058
04:08:15,840 --> 04:08:18,319
and if you search for port this is just
6059
04:08:18,319 --> 04:08:21,680
going to be a communication endpoint
6060
04:08:21,680 --> 04:08:24,880
and we need to understand that for http
6061
04:08:24,880 --> 04:08:28,560
traffic we have specific ports
6062
04:08:28,560 --> 04:08:32,800
so for http we actually have port 80
6063
04:08:32,800 --> 04:08:34,479
because there's other ports as well if
6064
04:08:34,479 --> 04:08:35,840
you keep on scrolling here and by the
6065
04:08:35,840 --> 04:08:38,319
way this is wikipedia example we have
6066
04:08:38,319 --> 04:08:41,120
port 20 which is for
6067
04:08:41,120 --> 04:08:43,120
file transfer protocol so this is going
6068
04:08:43,120 --> 04:08:45,439
to be for data transfer then we have
6069
04:08:45,439 --> 04:08:46,880
port 22
6070
04:08:46,880 --> 04:08:50,560
for secure shell so http traffic
6071
04:08:50,560 --> 04:08:53,040
is only one of the things
6072
04:08:53,040 --> 04:08:57,120
that we set up on a server and once our
6073
04:08:57,120 --> 04:08:59,920
application is in deployment we have
6074
04:08:59,920 --> 04:09:02,800
specific ports for that we have 80
6075
04:09:02,800 --> 04:09:05,359
that is going to be for just http and we
6076
04:09:05,359 --> 04:09:08,479
also have 443 which is going to be 40
6077
04:09:08,479 --> 04:09:10,640
secure communication that's why
6078
04:09:10,640 --> 04:09:14,080
when you go to course api or any other
6079
04:09:14,080 --> 04:09:17,439
website that uses https you'll notice
6080
04:09:17,439 --> 04:09:18,800
that
6081
04:09:18,800 --> 04:09:21,520
there is a ip address and then more
6082
04:09:21,520 --> 04:09:24,560
specifically 443 and you can also think
6083
04:09:24,560 --> 04:09:27,040
of this as a apartment building
6084
04:09:27,040 --> 04:09:30,080
so every apartment building has the same
6085
04:09:30,080 --> 04:09:33,359
exact address right but the apartments
6086
04:09:33,359 --> 04:09:34,479
differ
6087
04:09:34,479 --> 04:09:37,120
so the same works here so on that server
6088
04:09:37,120 --> 04:09:40,479
if i want to access it using ssh so
6089
04:09:40,479 --> 04:09:43,359
secure shell i'm going to use
6090
04:09:43,359 --> 04:09:47,359
port 22 but then if for example
6091
04:09:47,359 --> 04:09:50,560
i want to use http protocol and if i
6092
04:09:50,560 --> 04:09:52,960
want to access resource that way then
6093
04:09:52,960 --> 04:09:56,800
i'm going to use port 443 so once your
6094
04:09:56,800 --> 04:09:59,520
server is already in production yes
6095
04:09:59,520 --> 04:10:01,600
these ports are not random there are
6096
04:10:01,600 --> 04:10:04,319
specific ports for specific things we
6097
04:10:04,319 --> 04:10:06,000
would want to do again back to our bank
6098
04:10:06,000 --> 04:10:09,120
example if i want to cancel the account
6099
04:10:09,120 --> 04:10:10,800
then i'm always going to be directed to
6100
04:10:10,800 --> 04:10:13,439
that person because i'm going to click
6101
04:10:13,439 --> 04:10:14,880
option number eight if i want to
6102
04:10:14,880 --> 04:10:16,960
increase the limit then of course i'm
6103
04:10:16,960 --> 04:10:19,040
going to be directed to a different
6104
04:10:19,040 --> 04:10:22,399
person now while we're in development
6105
04:10:22,399 --> 04:10:24,960
this is really arbitrary
6106
04:10:24,960 --> 04:10:26,800
i can go here with five thousand i can
6107
04:10:26,800 --> 04:10:28,239
go here with three thousand eight
6108
04:10:28,239 --> 04:10:30,319
thousand wherever you would want i
6109
04:10:30,319 --> 04:10:33,279
believe the ports between zero and
6110
04:10:33,279 --> 04:10:34,880
thousand twenty four
6111
04:10:34,880 --> 04:10:36,880
are already taken so you shouldn't be
6112
04:10:36,880 --> 04:10:39,199
using them but in local development
6113
04:10:39,199 --> 04:10:41,359
again this is an arbitrary number and
6114
04:10:41,359 --> 04:10:43,279
that's why you can use anything you
6115
04:10:43,279 --> 04:10:44,319
would want
6116
04:10:44,319 --> 04:10:47,279
and again the most common example create
6117
04:10:47,279 --> 04:10:51,600
react app where we spin up the server on
6118
04:10:51,600 --> 04:10:55,120
localhost 3000 then gatsby has i believe
6119
04:10:55,120 --> 04:10:58,399
eight thousand and then netlify cli has
6120
04:10:58,399 --> 04:11:01,600
8080 and i can go on and on and on so in
6121
04:11:01,600 --> 04:11:04,560
my case i just went with 5000 but if you
6122
04:11:04,560 --> 04:11:07,359
don't like this number just use a
6123
04:11:07,359 --> 04:11:09,600
different port but of course please
6124
04:11:09,600 --> 04:11:12,560
remember the actual value for the port
6125
04:11:12,560 --> 04:11:14,960
because you'll need it once you need to
6126
04:11:14,960 --> 04:11:17,359
access now if you're wondering well what
6127
04:11:17,359 --> 04:11:21,040
happens once we deploy our application
6128
04:11:21,040 --> 04:11:24,720
just put the pin on that and once we get
6129
04:11:24,720 --> 04:11:26,560
to deployment of course i'm going to
6130
04:11:26,560 --> 04:11:29,760
cover how actually we set it up that we
6131
04:11:29,760 --> 04:11:31,439
serve that traffic
6132
04:11:31,439 --> 04:11:32,720
on the
6133
04:11:32,720 --> 04:11:37,439
port 80 for example for http or 443 for
6134
04:11:37,439 --> 04:11:38,880
https
6135
04:11:38,880 --> 04:11:41,040
and now of course we can go and test it
6136
04:11:41,040 --> 04:11:44,479
out so let me navigate to my browser
6137
04:11:44,479 --> 04:11:47,120
and i'm just going to go with localhost
6138
04:11:47,120 --> 04:11:50,239
and eight thousand and or i'm sorry i
6139
04:11:50,239 --> 04:11:51,920
just said five thousand and for some
6140
04:11:51,920 --> 04:11:53,760
reason i went to eight which is gonna be
6141
04:11:53,760 --> 04:11:56,640
the gatsby one and you'll see this
6142
04:11:56,640 --> 04:11:58,080
interesting spinner
6143
04:11:58,080 --> 04:12:00,720
so what's happening over here well the
6144
04:12:00,720 --> 04:12:04,560
thing is yeah the user hit the server as
6145
04:12:04,560 --> 04:12:07,120
you can see user hit the server that is
6146
04:12:07,120 --> 04:12:09,920
what i see in the terminal however
6147
04:12:09,920 --> 04:12:11,040
remember
6148
04:12:11,040 --> 04:12:13,279
we have the response right so we have
6149
04:12:13,279 --> 04:12:15,359
access to the response object and now we
6150
04:12:15,359 --> 04:12:18,080
need to send back some info to the
6151
04:12:18,080 --> 04:12:20,159
browser otherwise the browser is like
6152
04:12:20,159 --> 04:12:20,960
okay
6153
04:12:20,960 --> 04:12:23,600
so i hit this resource but nothing is
6154
04:12:23,600 --> 04:12:24,560
happening
6155
04:12:24,560 --> 04:12:26,479
and there's a method that we always
6156
04:12:26,479 --> 04:12:28,479
always need to include
6157
04:12:28,479 --> 04:12:31,520
in our response and the method name is
6158
04:12:31,520 --> 04:12:33,840
and and i right away went to node docs
6159
04:12:33,840 --> 04:12:35,920
of course again if you want to navigate
6160
04:12:35,920 --> 04:12:38,640
there yourself please do so but i
6161
04:12:38,640 --> 04:12:41,199
already right away went for response dot
6162
04:12:41,199 --> 04:12:43,279
end and as you can see this method
6163
04:12:43,279 --> 04:12:45,600
signals to the server that all the
6164
04:12:45,600 --> 04:12:48,080
response headers and body have been sent
6165
04:12:48,080 --> 04:12:49,840
and as a side note we'll do the headers
6166
04:12:49,840 --> 04:12:51,840
in the next video because this one is
6167
04:12:51,840 --> 04:12:53,760
already getting quite long
6168
04:12:53,760 --> 04:12:55,920
that server should consider this message
6169
04:12:55,920 --> 04:12:58,720
complete the method response and
6170
04:12:58,720 --> 04:13:02,000
must be called on each response
6171
04:13:02,000 --> 04:13:02,880
so
6172
04:13:02,880 --> 04:13:05,439
for the browser not to be confused we
6173
04:13:05,439 --> 04:13:09,680
need to go with res that end so res dot
6174
04:13:09,680 --> 04:13:11,680
and that is one of the methods that is
6175
04:13:11,680 --> 04:13:15,520
available on this response object and
6176
04:13:15,520 --> 04:13:17,920
here we can either pass the text or we
6177
04:13:17,920 --> 04:13:21,279
can set up for example html and in this
6178
04:13:21,279 --> 04:13:23,439
case i'm just going to say home page
6179
04:13:23,439 --> 04:13:25,840
that's it i'll just pass in the string
6180
04:13:25,840 --> 04:13:28,880
and if we were to go back to localhost
6181
04:13:28,880 --> 04:13:32,640
5000 there it is now we have our
6182
04:13:32,640 --> 04:13:35,199
homepage response and we can clearly see
6183
04:13:35,199 --> 04:13:37,680
that in our terminal because every time
6184
04:13:37,680 --> 04:13:40,000
you refresh the browser you'll have this
6185
04:13:40,000 --> 04:13:43,120
console log in a terminal user hit the
6186
04:13:43,120 --> 04:13:44,080
service
6187
04:13:44,080 --> 04:13:46,720
hopefully everything is clear again we
6188
04:13:46,720 --> 04:13:49,199
have access to http module and that is
6189
04:13:49,199 --> 04:13:52,720
built in node and we just set it up some
6190
04:13:52,720 --> 04:13:54,880
kind of variable most common convention
6191
04:13:54,880 --> 04:13:57,680
is calling this http then on there we
6192
04:13:57,680 --> 04:14:01,120
have create server method that takes in
6193
04:14:01,120 --> 04:14:03,040
the callback which is going to be
6194
04:14:03,040 --> 04:14:05,120
invoked every time the user hits the
6195
04:14:05,120 --> 04:14:08,080
server and as parameters we have a
6196
04:14:08,080 --> 04:14:09,359
request
6197
04:14:09,359 --> 04:14:11,439
and response object and common
6198
04:14:11,439 --> 04:14:12,560
convention
6199
04:14:12,560 --> 04:14:14,960
is just calling the american res now of
6200
04:14:14,960 --> 04:14:16,399
course you can call this stockhold
6201
04:14:16,399 --> 04:14:18,720
burrito if you want but again common
6202
04:14:18,720 --> 04:14:21,040
convention is calling this iraq and res
6203
04:14:21,040 --> 04:14:23,040
and the reason why we have access to
6204
04:14:23,040 --> 04:14:24,319
those objects
6205
04:14:24,319 --> 04:14:27,359
is because in the http cycle
6206
04:14:27,359 --> 04:14:29,840
that's what happens we have a request
6207
04:14:29,840 --> 04:14:32,000
message that is coming in where we can
6208
04:14:32,000 --> 04:14:34,800
find a bunch of useful info about the
6209
04:14:34,800 --> 04:14:37,040
request that is coming in and then of
6210
04:14:37,040 --> 04:14:40,159
course we need to respond to the browser
6211
04:14:40,159 --> 04:14:42,479
in a meaningful manner so that's why we
6212
04:14:42,479 --> 04:14:46,560
also have the response object and in
6213
04:14:46,560 --> 04:14:49,120
every response we should always have end
6214
04:14:49,120 --> 04:14:51,279
which is going to signal that the
6215
04:14:51,279 --> 04:14:54,800
communication is over and then we just
6216
04:14:54,800 --> 04:14:57,279
need to set up the port and again in
6217
04:14:57,279 --> 04:14:58,720
development we just come up with
6218
04:14:58,720 --> 04:15:01,439
arbitrary numbers and in my case we go
6219
04:15:01,439 --> 04:15:04,399
with 5000 and we pass in that port
6220
04:15:04,399 --> 04:15:07,040
in the dot listen method which is
6221
04:15:07,040 --> 04:15:09,439
available when we invoke create server
6222
04:15:09,439 --> 04:15:11,680
because we get back the object hopefully
6223
04:15:11,680 --> 04:15:13,760
the basics are clear and now we can
6224
04:15:13,760 --> 04:15:16,399
start expanding on our knowledge all
6225
04:15:16,399 --> 04:15:18,399
right we're done with the basics but
6226
04:15:18,399 --> 04:15:20,960
there are two major issues with our
6227
04:15:20,960 --> 04:15:22,720
current setup first of all we don't
6228
04:15:22,720 --> 04:15:26,319
provide any info about the data that
6229
04:15:26,319 --> 04:15:27,680
we're sending back
6230
04:15:27,680 --> 04:15:30,800
so we don't have any metadata about the
6231
04:15:30,800 --> 04:15:34,000
body that we're sending out so we're not
6232
04:15:34,000 --> 04:15:36,239
providing any information we just go
6233
04:15:36,239 --> 04:15:38,319
rest that end and then pass in the
6234
04:15:38,319 --> 04:15:40,960
string so that's issue number one and
6235
04:15:40,960 --> 04:15:43,279
then issue number two
6236
04:15:43,279 --> 04:15:45,040
if i were to go
6237
04:15:45,040 --> 04:15:48,560
to the local host 5000 and if i type
6238
04:15:48,560 --> 04:15:49,520
about
6239
04:15:49,520 --> 04:15:52,560
or if i type contact or whatever you'll
6240
04:15:52,560 --> 04:15:54,159
see that of course we're sending back
6241
04:15:54,159 --> 04:15:57,439
the same response each and every time so
6242
04:15:57,439 --> 04:15:59,359
if we go with contact again this is
6243
04:15:59,359 --> 04:16:01,760
going to be a home page so why don't we
6244
04:16:01,760 --> 04:16:04,319
deal with issue number one first
6245
04:16:04,319 --> 04:16:07,439
where we provide more info to the
6246
04:16:07,439 --> 04:16:10,080
browser of what we're actually sending
6247
04:16:10,080 --> 04:16:11,199
back
6248
04:16:11,199 --> 04:16:13,359
and then in the next video we'll deal
6249
04:16:13,359 --> 04:16:15,840
with the request
6250
04:16:15,840 --> 04:16:18,640
and the way we provide more info we just
6251
04:16:18,640 --> 04:16:20,159
need to add
6252
04:16:20,159 --> 04:16:23,040
more methods now specifically one more
6253
04:16:23,040 --> 04:16:26,159
method and that is going to be res dot
6254
04:16:26,159 --> 04:16:27,279
write head
6255
04:16:27,279 --> 04:16:29,040
and in there you can probably already
6256
04:16:29,040 --> 04:16:32,239
guess that we're just providing headers
6257
04:16:32,239 --> 04:16:35,199
so we're providing metadata about our
6258
04:16:35,199 --> 04:16:38,720
response and we go here with res dot
6259
04:16:38,720 --> 04:16:41,040
and then we go with right
6260
04:16:41,040 --> 04:16:44,239
head so right head and then we need to
6261
04:16:44,239 --> 04:16:47,120
provide a few things we start with a
6262
04:16:47,120 --> 04:16:49,120
status code and i'll talk about the
6263
04:16:49,120 --> 04:16:51,520
status code in a second and for time
6264
04:16:51,520 --> 04:16:53,840
being i'll just pass in 200 which just
6265
04:16:53,840 --> 04:16:56,720
means that the request was successful
6266
04:16:56,720 --> 04:16:59,279
and then we pass in the headers
6267
04:16:59,279 --> 04:17:02,080
and one of the most common headers is
6268
04:17:02,080 --> 04:17:03,439
the content type
6269
04:17:03,439 --> 04:17:05,279
so this is where i specifically say to
6270
04:17:05,279 --> 04:17:07,760
the browser hey listen i'm sending back
6271
04:17:07,760 --> 04:17:10,479
html or i'm sending back css so where
6272
04:17:10,479 --> 04:17:12,800
i'm sending back the image so browser
6273
04:17:12,800 --> 04:17:13,760
knows
6274
04:17:13,760 --> 04:17:16,239
well what to do how to render that
6275
04:17:16,239 --> 04:17:18,880
content and like i mentioned already
6276
04:17:18,880 --> 04:17:21,439
previously this is done using key value
6277
04:17:21,439 --> 04:17:24,080
pairs so we go with content type so
6278
04:17:24,080 --> 04:17:26,479
that's the name of the header and then
6279
04:17:26,479 --> 04:17:28,640
we pass in text
6280
04:17:28,640 --> 04:17:31,600
and then if for example i want to pass
6281
04:17:31,600 --> 04:17:34,479
in the html i go with text
6282
04:17:34,479 --> 04:17:37,120
forward slash and then html and now of
6283
04:17:37,120 --> 04:17:39,279
course in this case where i have the
6284
04:17:39,279 --> 04:17:42,880
res.end i can set up the html so for
6285
04:17:42,880 --> 04:17:46,080
example i could go with heading 1
6286
04:17:46,080 --> 04:17:47,600
and i'll say
6287
04:17:47,600 --> 04:17:51,520
homepage same deal home page now if you
6288
04:17:51,520 --> 04:17:54,080
want you can keep it this way where you
6289
04:17:54,080 --> 04:17:57,439
have res that end and you directly pass
6290
04:17:57,439 --> 04:18:01,279
in the html however i like to use better
6291
04:18:01,279 --> 04:18:03,920
this approach where i go with res dot
6292
04:18:03,920 --> 04:18:06,399
write and we do the same thing we pass
6293
04:18:06,399 --> 04:18:09,040
in essentially the body and i set up my
6294
04:18:09,040 --> 04:18:12,319
html here in the right or any content
6295
04:18:12,319 --> 04:18:14,080
that i'm sending back and then
6296
04:18:14,080 --> 04:18:17,439
explicitly call res.n again if you want
6297
04:18:17,439 --> 04:18:21,199
you can pass this heading 1 into the end
6298
04:18:21,199 --> 04:18:23,359
just make sure that you have the end and
6299
04:18:23,359 --> 04:18:25,680
you saw that in a docs where it says if
6300
04:18:25,680 --> 04:18:28,000
data is specified in a similar effect to
6301
04:18:28,000 --> 04:18:30,960
calling response.write and then we pass
6302
04:18:30,960 --> 04:18:34,560
in the data followed by res.n but you
6303
04:18:34,560 --> 04:18:37,520
always always need to call res.n so this
6304
04:18:37,520 --> 04:18:39,279
is really up to you if you want you can
6305
04:18:39,279 --> 04:18:41,920
pass this directly into res.n
6306
04:18:41,920 --> 04:18:44,720
i like to write it a bit explicitly
6307
04:18:44,720 --> 04:18:47,680
where i say res.right so that is my body
6308
04:18:47,680 --> 04:18:50,720
and then i just end the communication by
6309
04:18:50,720 --> 04:18:51,560
calling
6310
04:18:51,560 --> 04:18:54,560
res.n and now of course
6311
04:18:54,560 --> 04:18:57,120
if i go back to the browser again i'll
6312
04:18:57,120 --> 04:18:59,120
still have the same issue where it's
6313
04:18:59,120 --> 04:19:02,960
going to be displayed for every request
6314
04:19:02,960 --> 04:19:05,439
meaning either contact page homepage or
6315
04:19:05,439 --> 04:19:08,000
whatever but now i provide way more
6316
04:19:08,000 --> 04:19:10,960
useful info to the browser where browser
6317
04:19:10,960 --> 04:19:14,319
knows hey i'm getting back the html so
6318
04:19:14,319 --> 04:19:16,399
i'll need to render html
6319
04:19:16,399 --> 04:19:18,880
and this stuff matters so let me show
6320
04:19:18,880 --> 04:19:22,319
you if i change the content type back
6321
04:19:22,319 --> 04:19:24,800
for example to plain
6322
04:19:24,800 --> 04:19:27,760
and if i save and if you refresh now
6323
04:19:27,760 --> 04:19:31,680
notice this is now treated as a text
6324
04:19:31,680 --> 04:19:34,720
it's not treated as html so whatever you
6325
04:19:34,720 --> 04:19:37,520
set up over here in the content type yes
6326
04:19:37,520 --> 04:19:40,080
it does matter now express takes care of
6327
04:19:40,080 --> 04:19:42,319
that so we won't have to do that but i
6328
04:19:42,319 --> 04:19:43,760
just want to let you know that if you
6329
04:19:43,760 --> 04:19:46,080
will be setting up the headers yourself
6330
04:19:46,080 --> 04:19:48,640
yes the stuff that you type here matters
6331
04:19:48,640 --> 04:19:50,800
and as the note it is also called mime
6332
04:19:50,800 --> 04:19:54,000
type or you can think of it as media
6333
04:19:54,000 --> 04:19:58,159
type so whatever we're sending back and
6334
04:19:58,159 --> 04:20:00,640
now let's go over the status codes as
6335
04:20:00,640 --> 04:20:04,399
well as other options for mime types and
6336
04:20:04,399 --> 04:20:06,239
this is where i would like to introduce
6337
04:20:06,239 --> 04:20:09,439
an awesome resource for anything http
6338
04:20:09,439 --> 04:20:11,760
related and that is going to be no other
6339
04:20:11,760 --> 04:20:14,560
than the mozilla docs so if you go to
6340
04:20:14,560 --> 04:20:18,479
http and status codes you'll see
6341
04:20:18,479 --> 04:20:20,159
probably the first link is going to be
6342
04:20:20,159 --> 04:20:21,600
to the mdn
6343
04:20:21,600 --> 04:20:23,840
and as you can see http response status
6344
04:20:23,840 --> 04:20:26,800
codes indicate whether specific http
6345
04:20:26,800 --> 04:20:30,399
request has been successfully completed
6346
04:20:30,399 --> 04:20:32,640
and like i mentioned before there are
6347
04:20:32,640 --> 04:20:34,720
quite a few groups over here and there
6348
04:20:34,720 --> 04:20:36,720
are quite a few codes but i would
6349
04:20:36,720 --> 04:20:38,319
strongly discourage you from spending
6350
04:20:38,319 --> 04:20:41,600
your weekend on memorizing the codes as
6351
04:20:41,600 --> 04:20:42,800
we're going to be progressing with the
6352
04:20:42,800 --> 04:20:45,040
course as we're going to be creating
6353
04:20:45,040 --> 04:20:47,120
more complex applications of course
6354
04:20:47,120 --> 04:20:49,359
we'll introduce more and more status
6355
04:20:49,359 --> 04:20:51,760
codes but the main idea is following
6356
04:20:51,760 --> 04:20:52,880
where
6357
04:20:52,880 --> 04:20:55,359
when we send back the response
6358
04:20:55,359 --> 04:20:58,080
i'm just saying is it successful
6359
04:20:58,080 --> 04:21:01,199
maybe the resource wasn't available so
6360
04:21:01,199 --> 04:21:04,319
then i sent back 404 or maybe there was
6361
04:21:04,319 --> 04:21:06,800
a bad request that's why there's also
6362
04:21:06,800 --> 04:21:09,920
option 400 and hopefully you get an idea
6363
04:21:09,920 --> 04:21:12,640
where we are setting up that status code
6364
04:21:12,640 --> 04:21:15,680
so the browser knows what is happening
6365
04:21:15,680 --> 04:21:18,960
with the request was it successful
6366
04:21:18,960 --> 04:21:21,279
was there an error maybe i don't know
6367
04:21:21,279 --> 04:21:23,680
you're not authorized to access it and
6368
04:21:23,680 --> 04:21:27,120
on and on and on and if you keep on
6369
04:21:27,120 --> 04:21:30,159
scrolling notice so the 100 ones are
6370
04:21:30,159 --> 04:21:32,479
going to be informational responses then
6371
04:21:32,479 --> 04:21:35,600
for successful one we have 200
6372
04:21:35,600 --> 04:21:37,359
so that probably is going to be the one
6373
04:21:37,359 --> 04:21:39,760
that you'll use the most so that means
6374
04:21:39,760 --> 04:21:42,640
that request has succeeded then we have
6375
04:21:42,640 --> 04:21:44,880
201 also something that we'll use
6376
04:21:44,880 --> 04:21:47,520
throughout the course a lot and this one
6377
04:21:47,520 --> 04:21:48,560
is
6378
04:21:48,560 --> 04:21:51,359
once the post request is successful so
6379
04:21:51,359 --> 04:21:52,960
if you add the resource successfully
6380
04:21:52,960 --> 04:21:55,439
onto the server then you just send back
6381
04:21:55,439 --> 04:21:56,800
to one
6382
04:21:56,800 --> 04:22:00,720
we have 300s for redirect and then these
6383
04:22:00,720 --> 04:22:02,080
are the ones that you probably don't
6384
04:22:02,080 --> 04:22:03,199
want to get
6385
04:22:03,199 --> 04:22:07,040
if you are surfing the web so 400 is for
6386
04:22:07,040 --> 04:22:08,319
bad requests
6387
04:22:08,319 --> 04:22:11,120
so for example you are requesting some
6388
04:22:11,120 --> 04:22:12,640
kind of data or you're trying to do
6389
04:22:12,640 --> 04:22:14,720
something on the server but you don't
6390
04:22:14,720 --> 04:22:16,560
provide the info
6391
04:22:16,560 --> 04:22:18,640
so i don't know i'm trying to add the
6392
04:22:18,640 --> 04:22:21,600
user but i don't provide the username
6393
04:22:21,600 --> 04:22:24,080
something along those lines then 401 is
6394
04:22:24,080 --> 04:22:27,359
for unauthorized then 403 forbidden and
6395
04:22:27,359 --> 04:22:30,960
404 not found so in our case we'll set
6396
04:22:30,960 --> 04:22:33,840
up a response if the user is trying to
6397
04:22:33,840 --> 04:22:37,680
access the resource that does not exist
6398
04:22:37,680 --> 04:22:40,560
so hopefully it is clear that is the
6399
04:22:40,560 --> 04:22:42,399
status code again
6400
04:22:42,399 --> 04:22:45,120
i wouldn't suggest memorizing it as we
6401
04:22:45,120 --> 04:22:47,120
are moving along with the course of
6402
04:22:47,120 --> 04:22:49,279
course we'll cover more status codes
6403
04:22:49,279 --> 04:22:51,359
just understand the main idea
6404
04:22:51,359 --> 04:22:54,159
where you attach that status code to let
6405
04:22:54,159 --> 04:22:56,960
the browser know hey what's happening
6406
04:22:56,960 --> 04:23:00,000
and it is very important that you use
6407
04:23:00,000 --> 04:23:01,920
the correct status codes because
6408
04:23:01,920 --> 04:23:04,720
we are in charge here nothing stops me
6409
04:23:04,720 --> 04:23:08,239
from sending back the 404 here meaning
6410
04:23:08,239 --> 04:23:10,720
the resource is not found
6411
04:23:10,720 --> 04:23:12,960
and then when you
6412
04:23:12,960 --> 04:23:15,120
navigate to the contact page for example
6413
04:23:15,120 --> 04:23:18,239
here and if you inspect again the
6414
04:23:18,239 --> 04:23:20,479
browser network tab you'll see this
6415
04:23:20,479 --> 04:23:23,520
confusion info where 404
6416
04:23:23,520 --> 04:23:25,600
is technically not found
6417
04:23:25,600 --> 04:23:28,239
right but at the same time we are
6418
04:23:28,239 --> 04:23:31,040
sending back the correct resource so yes
6419
04:23:31,040 --> 04:23:32,880
whatever we type as far as the status
6420
04:23:32,880 --> 04:23:35,600
code it does matter it's not just some
6421
04:23:35,600 --> 04:23:38,800
random info and that just reflects what
6422
04:23:38,800 --> 04:23:42,479
is happening with the request and then
6423
04:23:42,479 --> 04:23:44,399
lastly let's talk about mime types same
6424
04:23:44,399 --> 04:23:46,399
deal just go to your favorite search
6425
04:23:46,399 --> 04:23:49,520
engine and just type mime
6426
04:23:49,520 --> 04:23:52,479
and types and again i suggest using the
6427
04:23:52,479 --> 04:23:53,760
mdn
6428
04:23:53,760 --> 04:23:55,600
and these are just going to be media
6429
04:23:55,600 --> 04:23:58,239
types so whatever we're sending back
6430
04:23:58,239 --> 04:24:01,279
there's quite a few out there now again
6431
04:24:01,279 --> 04:24:03,680
express takes care of that
6432
04:24:03,680 --> 04:24:06,319
so we don't need to worry about it but
6433
04:24:06,319 --> 04:24:08,479
if you ever need to set it up i don't
6434
04:24:08,479 --> 04:24:10,560
suggest memorizing them
6435
04:24:10,560 --> 04:24:12,560
the ones that you'll need to use
6436
04:24:12,560 --> 04:24:14,640
you'll pretty much be able to google it
6437
04:24:14,640 --> 04:24:16,960
right away you can also use the npm
6438
04:24:16,960 --> 04:24:20,399
packages so no you don't need to go and
6439
04:24:20,399 --> 04:24:23,040
memorize them again just remember the
6440
04:24:23,040 --> 04:24:26,319
main idea where you're sending back
6441
04:24:26,319 --> 04:24:29,439
something right and you need to describe
6442
04:24:29,439 --> 04:24:31,520
to the browser well what are you sending
6443
04:24:31,520 --> 04:24:32,399
back
6444
04:24:32,399 --> 04:24:33,840
are you sending back the image are you
6445
04:24:33,840 --> 04:24:36,080
sending back css are you sending back
6446
04:24:36,080 --> 04:24:39,120
the html and as you saw in a previous
6447
04:24:39,120 --> 04:24:40,159
example
6448
04:24:40,159 --> 04:24:42,720
if you change that mime type if you
6449
04:24:42,720 --> 04:24:45,439
change that content type header yes the
6450
04:24:45,439 --> 04:24:48,720
browser will interpret that differently
6451
04:24:48,720 --> 04:24:51,279
and once we successfully have set up
6452
04:24:51,279 --> 04:24:53,920
more proper response now let's start
6453
04:24:53,920 --> 04:24:57,120
dealing with the request object
6454
04:24:57,120 --> 04:25:00,479
beautiful and with our basic headers in
6455
04:25:00,479 --> 04:25:03,439
place now let's deal with the request
6456
04:25:03,439 --> 04:25:04,319
object
6457
04:25:04,319 --> 04:25:06,319
and like i already previously mentioned
6458
04:25:06,319 --> 04:25:09,359
it's just a giant object and if of
6459
04:25:09,359 --> 04:25:11,040
course we can slog it
6460
04:25:11,040 --> 04:25:13,600
then we'll see it in a terminal so i'm
6461
04:25:13,600 --> 04:25:16,800
going to go back to my localhost 5000
6462
04:25:16,800 --> 04:25:19,040
and as i said note this is getting quite
6463
04:25:19,040 --> 04:25:21,840
busy so i can just remove some of the
6464
04:25:21,840 --> 04:25:25,920
tabs and if i just refresh the browser i
6465
04:25:25,920 --> 04:25:29,600
should see in a terminal a giant object
6466
04:25:29,600 --> 04:25:31,520
so what are we looking for
6467
04:25:31,520 --> 04:25:34,399
well let's go back to our slide
6468
04:25:34,399 --> 04:25:36,319
and remember the start line
6469
04:25:36,319 --> 04:25:38,080
we had a method
6470
04:25:38,080 --> 04:25:40,000
so that signals what the user is trying
6471
04:25:40,000 --> 04:25:42,159
to do either get the resource post a
6472
04:25:42,159 --> 04:25:45,120
resource or whatever and then we have
6473
04:25:45,120 --> 04:25:46,239
the url
6474
04:25:46,239 --> 04:25:47,920
now in order to save us a little bit of
6475
04:25:47,920 --> 04:25:51,840
time i won't cover the methods with http
6476
04:25:51,840 --> 04:25:55,439
we'll just deal with the url but we will
6477
04:25:55,439 --> 04:25:58,000
cover of course methods once we hit the
6478
04:25:58,000 --> 04:26:00,720
express for now i'm just going to assume
6479
04:26:00,720 --> 04:26:03,120
that the only thing that the users are
6480
04:26:03,120 --> 04:26:05,359
trying to do they just want to get the
6481
04:26:05,359 --> 04:26:08,080
resources from our server so that's why
6482
04:26:08,080 --> 04:26:09,600
we won't look for the method just keep
6483
04:26:09,600 --> 04:26:12,000
in mind that yes of course those methods
6484
04:26:12,000 --> 04:26:15,439
are available on this request object
6485
04:26:15,439 --> 04:26:18,159
there is a property by the name of i
6486
04:26:18,159 --> 04:26:20,399
believe it was just simply a method so
6487
04:26:20,399 --> 04:26:22,080
if we cancel log
6488
04:26:22,080 --> 04:26:25,840
and if i refresh notice here in a
6489
04:26:25,840 --> 04:26:28,159
terminal it says get
6490
04:26:28,159 --> 04:26:31,840
so the user is performing a get request
6491
04:26:31,840 --> 04:26:34,319
so the user is trying to get the
6492
04:26:34,319 --> 04:26:36,159
resource so that's one thing that we
6493
04:26:36,159 --> 04:26:38,319
could take a look at and the other one
6494
04:26:38,319 --> 04:26:41,520
is actually the url so let me uncomment
6495
04:26:41,520 --> 04:26:44,319
this one and i'm looking for the url
6496
04:26:44,319 --> 04:26:45,600
property
6497
04:26:45,600 --> 04:26:47,760
and again i refresh and now in the
6498
04:26:47,760 --> 04:26:49,520
console i should have this
6499
04:26:49,520 --> 04:26:52,640
forward slash contact so that just means
6500
04:26:52,640 --> 04:26:55,600
that the user is trying to access the
6501
04:26:55,600 --> 04:26:59,359
resource by the name of contact and if
6502
04:26:59,359 --> 04:27:01,359
of course the resource is there awesome
6503
04:27:01,359 --> 04:27:03,359
we send something back if not then most
6504
04:27:03,359 --> 04:27:06,479
likely would send back the 404 now if i
6505
04:27:06,479 --> 04:27:08,800
delete that contact and if i simply go
6506
04:27:08,800 --> 04:27:11,439
with 5000 notice i'm going to have the
6507
04:27:11,439 --> 04:27:14,880
forward slash so again this just signals
6508
04:27:14,880 --> 04:27:16,640
that we're getting the home page so
6509
04:27:16,640 --> 04:27:18,000
forward slash is going to be the home
6510
04:27:18,000 --> 04:27:21,040
page and then whatever other resource we
6511
04:27:21,040 --> 04:27:23,520
would want we would have port slash and
6512
04:27:23,520 --> 04:27:25,520
then the resource now sometimes those
6513
04:27:25,520 --> 04:27:27,359
resources are going to be forward slash
6514
04:27:27,359 --> 04:27:29,840
resource name and then another forward
6515
04:27:29,840 --> 04:27:31,680
slash and another resource name and on
6516
04:27:31,680 --> 04:27:34,080
and on and on you can really type here
6517
04:27:34,080 --> 04:27:36,080
whatever you want so again i go with
6518
04:27:36,080 --> 04:27:39,600
localhost about and then info and i
6519
04:27:39,600 --> 04:27:41,840
don't know something john right so i can
6520
04:27:41,840 --> 04:27:44,319
add that here and there it is now i'm
6521
04:27:44,319 --> 04:27:47,199
requesting this resource and if it's not
6522
04:27:47,199 --> 04:27:49,359
there we send back 404 if it's there
6523
04:27:49,359 --> 04:27:52,080
then of course we send back the useful
6524
04:27:52,080 --> 04:27:53,040
info
6525
04:27:53,040 --> 04:27:55,760
so hopefully that is clear yes this will
6526
04:27:55,760 --> 04:27:57,920
just mean that it is a homepage and
6527
04:27:57,920 --> 04:28:00,319
whatever comes after the homepage well
6528
04:28:00,319 --> 04:28:02,880
that's the resource that the user is
6529
04:28:02,880 --> 04:28:04,800
trying to access and now of course we
6530
04:28:04,800 --> 04:28:07,120
can set up the if statement whereas say
6531
04:28:07,120 --> 04:28:10,159
yes if it is a home page please send
6532
04:28:10,159 --> 04:28:11,040
back
6533
04:28:11,040 --> 04:28:14,800
this html if it is about page then send
6534
04:28:14,800 --> 04:28:17,520
something else and then if
6535
04:28:17,520 --> 04:28:18,960
i cannot find that resource that you're
6536
04:28:18,960 --> 04:28:22,000
looking for then we'll send back 404.
6537
04:28:22,000 --> 04:28:23,840
now there's going to be a little bit of
6538
04:28:23,840 --> 04:28:26,399
copy and pasting just so we can save a
6539
04:28:26,399 --> 04:28:28,000
little bit of time we're just going to
6540
04:28:28,000 --> 04:28:30,560
go with if and actually we can set this
6541
04:28:30,560 --> 04:28:32,960
up as a property so let's go to const
6542
04:28:32,960 --> 04:28:37,279
url is equal to request url and then
6543
04:28:37,279 --> 04:28:39,680
we'll say if url is equal to forward
6544
04:28:39,680 --> 04:28:41,840
slash what does that mean well that
6545
04:28:41,840 --> 04:28:44,720
means that it is a homepage so let's
6546
04:28:44,720 --> 04:28:46,880
grab these three lines of code
6547
04:28:46,880 --> 04:28:49,279
and move them up and now of course i
6548
04:28:49,279 --> 04:28:51,279
want to set up else if
6549
04:28:51,279 --> 04:28:53,680
now i'll just set up one for the about
6550
04:28:53,680 --> 04:28:58,399
page so if url is equal to about page
6551
04:28:58,399 --> 04:29:01,840
then we'll do something else and if
6552
04:29:01,840 --> 04:29:04,080
none of those match well then i'll just
6553
04:29:04,080 --> 04:29:04,880
say
6554
04:29:04,880 --> 04:29:09,439
else and i'll send back 404 now i will
6555
04:29:09,439 --> 04:29:11,279
add some comments here
6556
04:29:11,279 --> 04:29:12,640
just so
6557
04:29:12,640 --> 04:29:14,479
we are clear on what is happening so
6558
04:29:14,479 --> 04:29:16,640
this is going to be about page
6559
04:29:16,640 --> 04:29:18,399
then this is going to be called
6560
04:29:18,399 --> 04:29:19,760
home page
6561
04:29:19,760 --> 04:29:21,120
home page
6562
04:29:21,120 --> 04:29:24,239
and then we're going to have the 404 so
6563
04:29:24,239 --> 04:29:26,319
resource not found
6564
04:29:26,319 --> 04:29:28,239
so again in order to save us a little
6565
04:29:28,239 --> 04:29:30,479
bit of time let's just copy and paste
6566
04:29:30,479 --> 04:29:33,600
one and two and again content type will
6567
04:29:33,600 --> 04:29:35,600
be exactly the same so that is going to
6568
04:29:35,600 --> 04:29:38,800
be html because we'll send back the html
6569
04:29:38,800 --> 04:29:40,479
i'll say about
6570
04:29:40,479 --> 04:29:43,120
and i don't know page let's call that
6571
04:29:43,120 --> 04:29:45,439
and then last one is going to be the 404
6572
04:29:45,439 --> 04:29:47,279
so in here of course we're not going to
6573
04:29:47,279 --> 04:29:51,600
send back 200. 200 is for successful
6574
04:29:51,600 --> 04:29:52,720
request
6575
04:29:52,720 --> 04:29:55,199
now of course i want to say hey listen
6576
04:29:55,199 --> 04:29:56,720
the resource that you're trying to
6577
04:29:56,720 --> 04:29:59,199
access well it doesn't exist on my
6578
04:29:59,199 --> 04:30:03,760
server so here i go with 404 and again i
6579
04:30:03,760 --> 04:30:06,880
go with the same content type text html
6580
04:30:06,880 --> 04:30:09,680
and then let's just send back the
6581
04:30:09,680 --> 04:30:12,080
heading 1 with page
6582
04:30:12,080 --> 04:30:15,279
not found so save this one
6583
04:30:15,279 --> 04:30:17,520
and now of course once i refresh check
6584
04:30:17,520 --> 04:30:20,000
it out i get this page not found why
6585
04:30:20,000 --> 04:30:21,760
well because there is no resource
6586
04:30:21,760 --> 04:30:24,800
forward slash about info and john
6587
04:30:24,800 --> 04:30:27,439
but if i go to just about
6588
04:30:27,439 --> 04:30:29,680
there it is now of course i have my
6589
04:30:29,680 --> 04:30:32,560
about page and then if i go back to the
6590
04:30:32,560 --> 04:30:35,920
homepage of course i have heading 1 with
6591
04:30:35,920 --> 04:30:39,680
a home page now we optionally if we want
6592
04:30:39,680 --> 04:30:40,800
we can
6593
04:30:40,800 --> 04:30:45,279
add the status text as well and in order
6594
04:30:45,279 --> 04:30:47,199
to see that let's just go back to the
6595
04:30:47,199 --> 04:30:49,199
node docs so this is going to be the
6596
04:30:49,199 --> 04:30:50,479
http
6597
04:30:50,479 --> 04:30:53,680
and of course i'm looking for right head
6598
04:30:53,680 --> 04:30:57,040
and we have a right head here notice we
6599
04:30:57,040 --> 04:30:59,600
have status code optionally we can pass
6600
04:30:59,600 --> 04:31:02,399
in status message and then we have the
6601
04:31:02,399 --> 04:31:04,960
headers so again let's go back to our
6602
04:31:04,960 --> 04:31:05,920
slide
6603
04:31:05,920 --> 04:31:07,920
this is the most important one status
6604
04:31:07,920 --> 04:31:10,159
code and then the status text
6605
04:31:10,159 --> 04:31:12,399
essentially is just going to be added in
6606
04:31:12,399 --> 04:31:14,720
this case we don't need to do anything
6607
04:31:14,720 --> 04:31:17,840
because again if we were to go back to
6608
04:31:17,840 --> 04:31:20,479
the browser network tab
6609
04:31:20,479 --> 04:31:23,120
and if we take a look at our localhost
6610
04:31:23,120 --> 04:31:24,640
there is first of all this is forward
6611
04:31:24,640 --> 04:31:26,880
slash that just means that we're going
6612
04:31:26,880 --> 04:31:29,359
to the homepage and the status code is
6613
04:31:29,359 --> 04:31:32,239
200 and then we have this status text
6614
04:31:32,239 --> 04:31:34,720
now again if i navigate to a page that
6615
04:31:34,720 --> 04:31:37,680
doesn't exist for example john then of
6616
04:31:37,680 --> 04:31:40,399
course i'm going to get the 404 so
6617
04:31:40,399 --> 04:31:42,560
that's the status code and then the
6618
04:31:42,560 --> 04:31:45,760
status text is not found
6619
04:31:45,760 --> 04:31:47,680
so hopefully it is clear
6620
04:31:47,680 --> 04:31:50,479
now we have a bit more
6621
04:31:50,479 --> 04:31:53,760
meaningful server where we have
6622
04:31:53,760 --> 04:31:56,560
multiple resources we have home page we
6623
04:31:56,560 --> 04:31:58,319
have the about page
6624
04:31:58,319 --> 04:32:00,880
and also we have the error page and with
6625
04:32:00,880 --> 04:32:03,040
this in place now of course we can start
6626
04:32:03,040 --> 04:32:06,399
working on more complex setup
6627
04:32:06,399 --> 04:32:09,199
nice and with our most basic example in
6628
04:32:09,199 --> 04:32:11,439
place now i'm going to start throwing
6629
04:32:11,439 --> 04:32:14,000
mine grenades at you first of all i want
6630
04:32:14,000 --> 04:32:16,239
to let you know that of course we're not
6631
04:32:16,239 --> 04:32:20,880
limited to passing in the html directly
6632
04:32:20,880 --> 04:32:25,279
into the dot right or that end meaning
6633
04:32:25,279 --> 04:32:28,239
imagine if you would have to every time
6634
04:32:28,239 --> 04:32:31,680
just set it up or your html in methods
6635
04:32:31,680 --> 04:32:33,680
directly of course what we can do
6636
04:32:33,680 --> 04:32:34,399
instead
6637
04:32:34,399 --> 04:32:36,479
is set up the file
6638
04:32:36,479 --> 04:32:38,479
then require the file using the file
6639
04:32:38,479 --> 04:32:41,760
system and then just pass it in now keep
6640
04:32:41,760 --> 04:32:44,000
in mind one thing though where we will
6641
04:32:44,000 --> 04:32:45,520
be passing in the
6642
04:32:45,520 --> 04:32:49,279
contents of the file not the file itself
6643
04:32:49,279 --> 04:32:50,560
and that's very important we'll be
6644
04:32:50,560 --> 04:32:53,040
passing in the contents of the file so
6645
04:32:53,040 --> 04:32:54,640
that means we still need to use this
6646
04:32:54,640 --> 04:32:56,640
content type just because we're going to
6647
04:32:56,640 --> 04:32:59,920
be getting the data from the html file
6648
04:32:59,920 --> 04:33:01,760
doesn't really mean anything
6649
04:33:01,760 --> 04:33:03,920
yes it's nice it's going to make our
6650
04:33:03,920 --> 04:33:06,080
lives easier if you want to set up a
6651
04:33:06,080 --> 04:33:08,561
more meaningful web page but we still
6652
04:33:08,561 --> 04:33:11,199
need to set up the content type now this
6653
04:33:11,199 --> 04:33:13,919
is going to be a temporary file because
6654
04:33:13,919 --> 04:33:18,000
in the next video we'll cover a more
6655
04:33:18,000 --> 04:33:21,278
serious example and i already prepared
6656
04:33:21,278 --> 04:33:23,840
all the files for you so this is where
6657
04:33:23,840 --> 04:33:26,879
we'll really struggle and this is where
6658
04:33:26,879 --> 04:33:29,520
i'll show you why we use the express but
6659
04:33:29,520 --> 04:33:32,160
for now if you want to follow along
6660
04:33:32,160 --> 04:33:34,080
just create a simple
6661
04:33:34,080 --> 04:33:36,400
index html or about html doesn't really
6662
04:33:36,400 --> 04:33:38,320
matter how you call the file
6663
04:33:38,320 --> 04:33:41,278
in the folder again this is really
6664
04:33:41,278 --> 04:33:43,599
optional i'm going to go with new file
6665
04:33:43,599 --> 04:33:46,561
and i'm gonna call this index html and
6666
04:33:46,561 --> 04:33:48,000
again the whole reason why i'm doing
6667
04:33:48,000 --> 04:33:50,561
that because i wanna set up a more
6668
04:33:50,561 --> 04:33:53,199
proper page and i don't wanna type
6669
04:33:53,199 --> 04:33:56,320
everything here in the right or end i
6670
04:33:56,320 --> 04:33:58,561
mean it's much more nicer here right so
6671
04:33:58,561 --> 04:34:00,561
i'm just going to go with emma setup in
6672
04:34:00,561 --> 04:34:03,438
visual studio code i get my basics and i
6673
04:34:03,438 --> 04:34:04,561
can say
6674
04:34:04,561 --> 04:34:07,438
home page home page
6675
04:34:07,438 --> 04:34:08,958
and then i'm just going to go with a
6676
04:34:08,958 --> 04:34:11,599
heading 4 of hello world again this is
6677
04:34:11,599 --> 04:34:14,958
just a showcase that sky is the limit
6678
04:34:14,958 --> 04:34:16,400
just like you normally would set up to
6679
04:34:16,400 --> 04:34:19,359
html you can do exactly the same thing
6680
04:34:19,359 --> 04:34:22,160
and then i'm going to go back to app.js
6681
04:34:22,160 --> 04:34:24,400
and first of all i'm going to get the
6682
04:34:24,400 --> 04:34:26,561
read file so i'm gonna go with const and
6683
04:34:26,561 --> 04:34:29,840
then read file and sync and i'm gonna
6684
04:34:29,840 --> 04:34:32,000
talk about why i'm using this thing in a
6685
04:34:32,000 --> 04:34:34,000
second so don't freak out i know i
6686
04:34:34,000 --> 04:34:35,840
mentioned before that we need to be
6687
04:34:35,840 --> 04:34:36,799
mindful
6688
04:34:36,799 --> 04:34:39,680
of the methods that we use
6689
04:34:39,680 --> 04:34:41,199
that there is a difference between the
6690
04:34:41,199 --> 04:34:43,118
blocking and non-blocking and i'm just
6691
04:34:43,118 --> 04:34:45,520
going to go with fs over here
6692
04:34:45,520 --> 04:34:48,160
and we need to come up with some kind of
6693
04:34:48,160 --> 04:34:50,879
name and again in the next video there's
6694
04:34:50,879 --> 04:34:51,840
going to be
6695
04:34:51,840 --> 04:34:54,400
more data here so that's why i'll say
6696
04:34:54,400 --> 04:34:55,199
get
6697
04:34:55,199 --> 04:34:56,480
all files
6698
04:34:56,480 --> 04:34:58,160
for time being in this video i'll just
6699
04:34:58,160 --> 04:34:59,919
get one and i'm going to call this home
6700
04:34:59,919 --> 04:35:01,199
page again
6701
04:35:01,199 --> 04:35:03,118
call it whatever you would want you can
6702
04:35:03,118 --> 04:35:04,958
call this bobby lee doesn't really
6703
04:35:04,958 --> 04:35:06,879
matter it's just a variable and we're
6704
04:35:06,879 --> 04:35:09,759
going to go with read file and sync and
6705
04:35:09,759 --> 04:35:11,520
of course where's the file it is in the
6706
04:35:11,520 --> 04:35:14,000
same folder so we go with relative path
6707
04:35:14,000 --> 04:35:15,680
and i'm just going to call this index
6708
04:35:15,680 --> 04:35:18,240
html now again we're getting the
6709
04:35:18,240 --> 04:35:19,278
contents
6710
04:35:19,278 --> 04:35:20,160
now
6711
04:35:20,160 --> 04:35:23,039
why i'm using here they read file sync
6712
04:35:23,039 --> 04:35:25,199
well there's two reasons first of all we
6713
04:35:25,199 --> 04:35:27,438
need to keep in mind that we're not
6714
04:35:27,438 --> 04:35:29,278
invoking this
6715
04:35:29,278 --> 04:35:31,919
every time someone comes to the server
6716
04:35:31,919 --> 04:35:34,719
please keep in mind that we require that
6717
04:35:34,719 --> 04:35:35,599
file
6718
04:35:35,599 --> 04:35:38,719
when we instantiate our server
6719
04:35:38,719 --> 04:35:41,199
so basically that initial time
6720
04:35:41,199 --> 04:35:43,840
when the server starts running so it's
6721
04:35:43,840 --> 04:35:45,680
not like when the request comes in then
6722
04:35:45,680 --> 04:35:47,199
again every time we're requesting the
6723
04:35:47,199 --> 04:35:51,359
file yes of course if i were to place
6724
04:35:51,359 --> 04:35:53,278
this in the if block yeah that's a
6725
04:35:53,278 --> 04:35:55,359
different scenario meaning if i were to
6726
04:35:55,359 --> 04:35:57,840
even place this in a create server again
6727
04:35:57,840 --> 04:36:00,561
same scenario but not in here again
6728
04:36:00,561 --> 04:36:02,719
we're just requesting this once that's
6729
04:36:02,719 --> 04:36:04,320
the first reason why i went with that
6730
04:36:04,320 --> 04:36:06,160
and second it's just an example so i
6731
04:36:06,160 --> 04:36:08,160
want to make my life a little bit easier
6732
04:36:08,160 --> 04:36:09,840
what i want to focus on is something
6733
04:36:09,840 --> 04:36:12,639
else with this home page in place now of
6734
04:36:12,639 --> 04:36:15,599
course what i would want is to go back
6735
04:36:15,599 --> 04:36:17,680
to my if where i'm checking for the
6736
04:36:17,680 --> 04:36:20,240
homepage and instead of going with
6737
04:36:20,240 --> 04:36:22,958
res.right and then typing it out i'm
6738
04:36:22,958 --> 04:36:24,719
just going to go with our content now
6739
04:36:24,719 --> 04:36:26,879
again we're still keeping this one the
6740
04:36:26,879 --> 04:36:28,639
text.html and i'll show you in a second
6741
04:36:28,639 --> 04:36:31,039
what happens if we change it to a text
6742
04:36:31,039 --> 04:36:34,480
plane and now we go with a homepage
6743
04:36:34,480 --> 04:36:37,118
and now be prepared to be amazed because
6744
04:36:37,118 --> 04:36:39,680
if i go to not john
6745
04:36:39,680 --> 04:36:41,919
my resource of course doesn't exist
6746
04:36:41,919 --> 04:36:43,759
there it is check it out we have home
6747
04:36:43,759 --> 04:36:46,400
page as well as the hello world
6748
04:36:46,400 --> 04:36:49,438
so we can start making some meaningful
6749
04:36:49,438 --> 04:36:53,118
html pages and we can serve them with
6750
04:36:53,118 --> 04:36:56,080
our awesome server and just to
6751
04:36:56,080 --> 04:36:58,160
re-emphasize my point
6752
04:36:58,160 --> 04:37:01,520
if we change this to a plane and save
6753
04:37:01,520 --> 04:37:02,879
and refresh
6754
04:37:02,879 --> 04:37:05,438
now of course we're getting a text so
6755
04:37:05,438 --> 04:37:07,359
yes it is very important
6756
04:37:07,359 --> 04:37:10,320
of what content type we are setting up
6757
04:37:10,320 --> 04:37:12,879
so let's go back to html and now of
6758
04:37:12,879 --> 04:37:15,520
course we are in good shape awesome we
6759
04:37:15,520 --> 04:37:18,639
have our homepage and now of course i
6760
04:37:18,639 --> 04:37:22,639
can go to index.html and go wild and
6761
04:37:22,639 --> 04:37:23,759
crazy
6762
04:37:23,759 --> 04:37:25,919
with my page setup
6763
04:37:25,919 --> 04:37:28,240
not bad not bad now we know that we can
6764
04:37:28,240 --> 04:37:31,840
access the contents of the file directly
6765
04:37:31,840 --> 04:37:35,840
so of course we can set up proper pages
6766
04:37:35,840 --> 04:37:39,278
but now let me throw you a mine grenade
6767
04:37:39,278 --> 04:37:42,799
where we'll have to add way more code
6768
04:37:42,799 --> 04:37:43,599
here
6769
04:37:43,599 --> 04:37:46,320
if we want to really serve some
6770
04:37:46,320 --> 04:37:48,240
meaningful web page
6771
04:37:48,240 --> 04:37:50,400
and the example is going to be following
6772
04:37:50,400 --> 04:37:53,599
if you navigate to navbar app so that's
6773
04:37:53,599 --> 04:37:56,561
the folder you'll find index.html
6774
04:37:56,561 --> 04:37:59,438
you'll find style css logo svg as well
6775
04:37:59,438 --> 04:38:02,799
as browser hyphen app.js and i simply
6776
04:38:02,799 --> 04:38:04,879
call this browser app file so you're not
6777
04:38:04,879 --> 04:38:07,039
confused between the two so this is
6778
04:38:07,039 --> 04:38:09,759
going to be for our server and this is
6779
04:38:09,759 --> 04:38:12,639
our browser app and effectively this is
6780
04:38:12,639 --> 04:38:16,400
a complete app that we set up i believe
6781
04:38:16,400 --> 04:38:18,719
in my javascript course when we were
6782
04:38:18,719 --> 04:38:21,840
building projects so effectively this is
6783
04:38:21,840 --> 04:38:24,561
a little navbar with a toggle
6784
04:38:24,561 --> 04:38:26,879
functionality and let me assure you
6785
04:38:26,879 --> 04:38:29,438
something right from the get go the app
6786
04:38:29,438 --> 04:38:30,400
works
6787
04:38:30,400 --> 04:38:33,359
so if you get any issues along the way
6788
04:38:33,359 --> 04:38:35,759
it's because the node is not working
6789
04:38:35,759 --> 04:38:37,759
the app itself is going to work and i
6790
04:38:37,759 --> 04:38:40,000
can simply showcase that if i go to
6791
04:38:40,000 --> 04:38:41,759
navbar app and i'm going to copy that
6792
04:38:41,759 --> 04:38:43,759
one and i'll place it
6793
04:38:43,759 --> 04:38:46,080
on my desktop
6794
04:38:46,080 --> 04:38:49,680
and then if you spin up the index html
6795
04:38:49,680 --> 04:38:51,759
i can guarantee you that this is going
6796
04:38:51,759 --> 04:38:53,680
to work there it is that's my
6797
04:38:53,680 --> 04:38:55,039
application
6798
04:38:55,039 --> 04:38:57,039
and i can make it smaller and there it
6799
04:38:57,039 --> 04:38:59,840
is now i'm toggling again our goal is
6800
04:38:59,840 --> 04:39:00,958
following
6801
04:39:00,958 --> 04:39:05,199
i want to take this project index.html
6802
04:39:05,199 --> 04:39:08,160
with style css logo as well as the
6803
04:39:08,160 --> 04:39:09,438
javascript file
6804
04:39:09,438 --> 04:39:11,919
so the html structure
6805
04:39:11,919 --> 04:39:12,799
the
6806
04:39:12,799 --> 04:39:15,118
styling the logo that you can see over
6807
04:39:15,118 --> 04:39:17,759
here as well as the logic i want to take
6808
04:39:17,759 --> 04:39:20,879
all of it and serve it on my server
6809
04:39:20,879 --> 04:39:24,160
and i know that the app works so all
6810
04:39:24,160 --> 04:39:27,438
that is good so let's see what struggles
6811
04:39:27,438 --> 04:39:29,520
we're going to have along the way so
6812
04:39:29,520 --> 04:39:31,278
here i have the home page right
6813
04:39:31,278 --> 04:39:33,278
and instead of getting it from the
6814
04:39:33,278 --> 04:39:35,039
index.html which by the way i'm going to
6815
04:39:35,039 --> 04:39:36,799
delete because again this was just
6816
04:39:36,799 --> 04:39:38,878
temporary now of course i would want to
6817
04:39:38,878 --> 04:39:40,320
access this one
6818
04:39:40,320 --> 04:39:43,280
from the navbar app that's the folder
6819
04:39:43,280 --> 04:39:45,360
name right and then i'm looking for the
6820
04:39:45,360 --> 04:39:47,920
index html and i simply just need to
6821
04:39:47,920 --> 04:39:50,400
change my path where instead of getting
6822
04:39:50,400 --> 04:39:53,040
it index html from the root which
6823
04:39:53,040 --> 04:39:55,520
doesn't exist anymore i'm going to go to
6824
04:39:55,520 --> 04:39:58,000
navbar hyphen app
6825
04:39:58,000 --> 04:40:00,160
and then of course i still get the
6826
04:40:00,160 --> 04:40:01,280
homepage
6827
04:40:01,280 --> 04:40:04,080
and as you can see i'm still serving
6828
04:40:04,080 --> 04:40:07,440
also the contents from that index.html
6829
04:40:07,440 --> 04:40:09,200
the only thing i did
6830
04:40:09,200 --> 04:40:11,760
is just change the path that's it
6831
04:40:11,760 --> 04:40:14,798
and technically we should see exactly
6832
04:40:14,798 --> 04:40:16,480
the same right
6833
04:40:16,480 --> 04:40:17,280
but
6834
04:40:17,280 --> 04:40:19,520
here's the kicker we navigate here and
6835
04:40:19,520 --> 04:40:21,040
if i refresh
6836
04:40:21,040 --> 04:40:22,798
what is happening
6837
04:40:22,798 --> 04:40:24,958
i don't have the logo
6838
04:40:24,958 --> 04:40:26,560
i don't have the button
6839
04:40:26,560 --> 04:40:28,000
i mean i do have the button but it
6840
04:40:28,000 --> 04:40:31,040
doesn't do anything and i can see only
6841
04:40:31,040 --> 04:40:32,798
the structure
6842
04:40:32,798 --> 04:40:36,878
and we have quite a few 404s here
6843
04:40:36,878 --> 04:40:39,680
and actually to give you a good idea why
6844
04:40:39,680 --> 04:40:40,958
is this happening
6845
04:40:40,958 --> 04:40:43,920
i want to navigate back to our project
6846
04:40:43,920 --> 04:40:45,920
and let's just console log
6847
04:40:45,920 --> 04:40:47,440
the urls
6848
04:40:47,440 --> 04:40:49,760
and i think this is going to give you a
6849
04:40:49,760 --> 04:40:52,240
clear message why this is happening so
6850
04:40:52,240 --> 04:40:54,480
let me go back and i'm just going to
6851
04:40:54,480 --> 04:40:56,560
refresh one more time
6852
04:40:56,560 --> 04:40:59,600
and check it out so we're requesting the
6853
04:40:59,600 --> 04:41:02,240
homepage so that makes sense right so i
6854
04:41:02,240 --> 04:41:04,480
come here i request the home page and i
6855
04:41:04,480 --> 04:41:07,040
serve this html file
6856
04:41:07,040 --> 04:41:08,798
from the navbar app
6857
04:41:08,798 --> 04:41:10,958
and then i have three more requests
6858
04:41:10,958 --> 04:41:13,760
i have one for styles one for browser
6859
04:41:13,760 --> 04:41:15,840
and one for logo now why is that
6860
04:41:15,840 --> 04:41:19,440
happening well because in my index html
6861
04:41:19,440 --> 04:41:21,200
if you check it out
6862
04:41:21,200 --> 04:41:23,840
of course i do reference the style css
6863
04:41:23,840 --> 04:41:25,840
because i want to add the styling right
6864
04:41:25,840 --> 04:41:27,760
i do reference the logo
6865
04:41:27,760 --> 04:41:30,000
because the logo is right here in my
6866
04:41:30,000 --> 04:41:30,958
folder
6867
04:41:30,958 --> 04:41:33,840
and also of course i do have the one for
6868
04:41:33,840 --> 04:41:36,798
the app right the one where i have my
6869
04:41:36,798 --> 04:41:37,840
logic
6870
04:41:37,840 --> 04:41:41,200
so this is what happens we send back
6871
04:41:41,200 --> 04:41:42,638
the initial
6872
04:41:42,638 --> 04:41:45,040
html content and the browser starts
6873
04:41:45,040 --> 04:41:46,400
reading the content
6874
04:41:46,400 --> 04:41:49,440
and every time we have basically a path
6875
04:41:49,440 --> 04:41:52,240
to our server browser is like hey server
6876
04:41:52,240 --> 04:41:54,560
give me this resource so give me style
6877
04:41:54,560 --> 04:41:59,360
css give me logo svg as well as whatever
6878
04:41:59,360 --> 04:42:01,520
we have here the browser app right so
6879
04:42:01,520 --> 04:42:03,280
keep in mind that these ones are for
6880
04:42:03,280 --> 04:42:04,638
icons
6881
04:42:04,638 --> 04:42:07,680
and actually they are external so this
6882
04:42:07,680 --> 04:42:09,920
one goes to phantasm that's a little bit
6883
04:42:09,920 --> 04:42:12,240
different scenario yes the browser still
6884
04:42:12,240 --> 04:42:14,638
performs this request but of course this
6885
04:42:14,638 --> 04:42:16,320
is external resource
6886
04:42:16,320 --> 04:42:19,200
now these ones are on our server but the
6887
04:42:19,200 --> 04:42:20,798
problem is following
6888
04:42:20,798 --> 04:42:23,200
are we handling these requests
6889
04:42:23,200 --> 04:42:25,440
in our create server
6890
04:42:25,440 --> 04:42:28,080
and of course the answer is no we handle
6891
04:42:28,080 --> 04:42:30,878
forward slash we handle about and then
6892
04:42:30,878 --> 04:42:34,480
for everything else we have this 404
6893
04:42:34,480 --> 04:42:37,120
so to answer your question yes
6894
04:42:37,120 --> 04:42:41,200
now manually we'll have to request
6895
04:42:41,200 --> 04:42:45,360
all the files so lower svg style css as
6896
04:42:45,360 --> 04:42:47,040
well as the browser app
6897
04:42:47,040 --> 04:42:49,200
assign them to some kind of variable and
6898
04:42:49,200 --> 04:42:51,760
set up these paths again this is just an
6899
04:42:51,760 --> 04:42:53,440
example if you don't want to follow
6900
04:42:53,440 --> 04:42:56,080
along if you just want to see how it's
6901
04:42:56,080 --> 04:42:57,520
going to work
6902
04:42:57,520 --> 04:42:59,680
just sit back and relax
6903
04:42:59,680 --> 04:43:02,480
but in my case i'm going to request
6904
04:43:02,480 --> 04:43:04,718
all these resources here
6905
04:43:04,718 --> 04:43:08,240
and then set up more else if statements
6906
04:43:08,240 --> 04:43:10,400
where if the browser wants to get the
6907
04:43:10,400 --> 04:43:13,120
css then of course we'll search css if
6908
04:43:13,120 --> 04:43:15,280
the browser wants to have logo then of
6909
04:43:15,280 --> 04:43:18,240
course the browser is going to get local
6910
04:43:18,240 --> 04:43:20,480
and again if you take a look here in the
6911
04:43:20,480 --> 04:43:23,280
elements you'll again see exactly why
6912
04:43:23,280 --> 04:43:26,160
because we have our html structure and
6913
04:43:26,160 --> 04:43:29,040
then there it is we have style css so
6914
04:43:29,040 --> 04:43:32,080
notice how browser is going to http
6915
04:43:32,080 --> 04:43:35,200
localhost 5000 forward slash what well
6916
04:43:35,200 --> 04:43:38,160
style css right well do we have that
6917
04:43:38,160 --> 04:43:42,400
path and again the answer is no so let's
6918
04:43:42,400 --> 04:43:44,480
do this way i'm going to copy and paste
6919
04:43:44,480 --> 04:43:45,680
and i'm just going to come up with
6920
04:43:45,680 --> 04:43:48,000
different names here i'm going to say
6921
04:43:48,000 --> 04:43:50,000
home styles
6922
04:43:50,000 --> 04:43:52,160
and then the third one i guess is going
6923
04:43:52,160 --> 04:43:54,240
to be home image
6924
04:43:54,240 --> 04:43:56,400
and then the fourth one will be home
6925
04:43:56,400 --> 04:43:59,920
logic so that is going to be my app one
6926
04:43:59,920 --> 04:44:01,680
then we'll have to change some files
6927
04:44:01,680 --> 04:44:04,080
here as well so i'm looking for styles
6928
04:44:04,080 --> 04:44:06,480
css i believe right
6929
04:44:06,480 --> 04:44:08,560
that's the file name then i have logo
6930
04:44:08,560 --> 04:44:10,718
svg
6931
04:44:10,718 --> 04:44:14,638
that's where my logo is sitting so logo
6932
04:44:14,638 --> 04:44:18,000
svg and then finally let's just delete
6933
04:44:18,000 --> 04:44:21,520
this one as well and call this browser
6934
04:44:21,520 --> 04:44:23,040
hyphen app
6935
04:44:23,040 --> 04:44:26,160
and js so home styles home image and
6936
04:44:26,160 --> 04:44:28,878
logic and like i said yes manually one
6937
04:44:28,878 --> 04:44:31,280
by one we'll have to add all these
6938
04:44:31,280 --> 04:44:34,480
resources so instead of about page
6939
04:44:34,480 --> 04:44:36,958
i'll just call this styles and the value
6940
04:44:36,958 --> 04:44:39,520
that i want to check for here is exactly
6941
04:44:39,520 --> 04:44:42,480
the same like in my browser so they need
6942
04:44:42,480 --> 04:44:44,958
to match so then of course the browser
6943
04:44:44,958 --> 04:44:46,560
will get the contents
6944
04:44:46,560 --> 04:44:49,840
of the css file in this example so i'm
6945
04:44:49,840 --> 04:44:53,840
going to go with styles and then css
6946
04:44:53,840 --> 04:44:55,760
now i do want to change right now the
6947
04:44:55,760 --> 04:44:57,760
content type because am i sending back
6948
04:44:57,760 --> 04:45:00,000
the html of course the answer is no
6949
04:45:00,000 --> 04:45:02,638
we're sending back the css so we go text
6950
04:45:02,638 --> 04:45:04,718
css now let me say this one more time
6951
04:45:04,718 --> 04:45:08,080
please don't zero in on these mime types
6952
04:45:08,080 --> 04:45:10,718
if you will ever need them you'll be
6953
04:45:10,718 --> 04:45:13,040
able to find them within a matter of
6954
04:45:13,040 --> 04:45:14,718
seconds just
6955
04:45:14,718 --> 04:45:17,280
type along with me and we're going to be
6956
04:45:17,280 --> 04:45:20,240
in good shape now as far as the right
6957
04:45:20,240 --> 04:45:23,280
in our body so in our response that
6958
04:45:23,280 --> 04:45:25,040
we're sending back are we going to send
6959
04:45:25,040 --> 04:45:27,520
back html of course the answer is no now
6960
04:45:27,520 --> 04:45:29,920
of course i want to send the styles
6961
04:45:29,920 --> 04:45:32,320
right and the variable for that is home
6962
04:45:32,320 --> 04:45:34,000
styles so that's where
6963
04:45:34,000 --> 04:45:38,560
my content is for the styles css and
6964
04:45:38,560 --> 04:45:41,200
once i save check it out
6965
04:45:41,200 --> 04:45:43,760
now we are moving along in the right
6966
04:45:43,760 --> 04:45:46,560
direction because i do have the styles
6967
04:45:46,560 --> 04:45:49,440
so it looks already somewhat decent now
6968
04:45:49,440 --> 04:45:52,000
the logo is still missing and there's no
6969
04:45:52,000 --> 04:45:55,600
logic because i don't have the app.js or
6970
04:45:55,600 --> 04:45:59,680
the logo svg but we are moving in the
6971
04:45:59,680 --> 04:46:02,240
right direction so copy and paste and
6972
04:46:02,240 --> 04:46:04,160
you can probably already guess that
6973
04:46:04,160 --> 04:46:07,200
we'll just repeat over here we'll say if
6974
04:46:07,200 --> 04:46:10,798
the url is equal to logo svg and i'll
6975
04:46:10,798 --> 04:46:11,600
say
6976
04:46:11,600 --> 04:46:14,160
image logo as far as the comment
6977
04:46:14,160 --> 04:46:16,638
and we're just going to go with logo
6978
04:46:16,638 --> 04:46:19,520
and 3g and as far as the mime type for
6979
04:46:19,520 --> 04:46:22,480
this sucker it is image
6980
04:46:22,480 --> 04:46:26,840
image forward slash svg and plus
6981
04:46:26,840 --> 04:46:29,120
xml like so
6982
04:46:29,120 --> 04:46:31,440
and let's go with home
6983
04:46:31,440 --> 04:46:34,560
and i believe i named this home and
6984
04:46:34,560 --> 04:46:36,240
image like so
6985
04:46:36,240 --> 04:46:38,320
and then the last one of course
6986
04:46:38,320 --> 04:46:40,480
is going to be our javascript so
6987
04:46:40,480 --> 04:46:42,560
let's say here logic
6988
04:46:42,560 --> 04:46:44,240
and the resource that i would want to
6989
04:46:44,240 --> 04:46:46,878
provide is browser app
6990
04:46:46,878 --> 04:46:49,440
js like so
6991
04:46:49,440 --> 04:46:50,718
and then
6992
04:46:50,718 --> 04:46:54,080
the content type is equal to text
6993
04:46:54,080 --> 04:46:56,240
and then javascript
6994
04:46:56,240 --> 04:46:59,120
and of course now i would want to send
6995
04:46:59,120 --> 04:47:01,680
the home and logic
6996
04:47:01,680 --> 04:47:03,440
so let's save this one
6997
04:47:03,440 --> 04:47:06,000
and now if i go to my browser
6998
04:47:06,000 --> 04:47:08,160
and if i refresh
6999
04:47:08,160 --> 04:47:10,718
check it out now of course we have
7000
04:47:10,718 --> 04:47:14,560
everything we have the logo as well as
7001
04:47:14,560 --> 04:47:17,440
the proper functionality with browser
7002
04:47:17,440 --> 04:47:19,680
app and notice how
7003
04:47:19,680 --> 04:47:23,840
all our requests are 200 instead of 404
7004
04:47:23,840 --> 04:47:26,080
where the previously browser wasn't able
7005
04:47:26,080 --> 04:47:27,920
to find those resources and now of
7006
04:47:27,920 --> 04:47:30,798
course we are providing them and if we
7007
04:47:30,798 --> 04:47:32,480
want to test out the functionality there
7008
04:47:32,480 --> 04:47:33,280
it is
7009
04:47:33,280 --> 04:47:36,080
now i can just go here and notice how i
7010
04:47:36,080 --> 04:47:39,120
can toggle the menu as well and now of
7011
04:47:39,120 --> 04:47:40,958
course we'll switch gears and start
7012
04:47:40,958 --> 04:47:43,520
working with express because hopefully
7013
04:47:43,520 --> 04:47:46,798
it's clear that yes we can
7014
04:47:46,798 --> 04:47:50,560
set up our server with just http module
7015
04:47:50,560 --> 04:47:54,160
but imagine a scenario where you have a
7016
04:47:54,160 --> 04:47:56,560
website with tons of resources
7017
04:47:56,560 --> 04:47:58,400
and then of course you need to set up
7018
04:47:58,400 --> 04:48:01,920
every single resource in this matter now
7019
04:48:01,920 --> 04:48:03,440
i don't know about you
7020
04:48:03,440 --> 04:48:07,280
but i would go nuts somewhat quickly
7021
04:48:07,280 --> 04:48:09,280
all right and once we have struggled a
7022
04:48:09,280 --> 04:48:10,080
bit
7023
04:48:10,080 --> 04:48:12,320
now let's make our lives easier by
7024
04:48:12,320 --> 04:48:15,120
getting to know express js
7025
04:48:15,120 --> 04:48:17,920
express is a minimal and flexible
7026
04:48:17,920 --> 04:48:19,120
node.js
7027
04:48:19,120 --> 04:48:21,680
web app framework designed to make
7028
04:48:21,680 --> 04:48:25,360
developing websites web apps and apis
7029
04:48:25,360 --> 04:48:28,638
much faster and easier if i have to be
7030
04:48:28,638 --> 04:48:31,040
honest it's almost unfair how easy and
7031
04:48:31,040 --> 04:48:34,080
fast it is to spin up the api with the
7032
04:48:34,080 --> 04:48:36,160
help of express
7033
04:48:36,160 --> 04:48:38,240
and while it's not officially part of
7034
04:48:38,240 --> 04:48:41,760
node meaning unlike http module express
7035
04:48:41,760 --> 04:48:44,798
is not one of the built-in modules at
7036
04:48:44,798 --> 04:48:46,320
this point in time express is pretty
7037
04:48:46,320 --> 04:48:48,798
much a standard when creating web
7038
04:48:48,798 --> 04:48:52,638
applications with node.js express has
7039
04:48:52,638 --> 04:48:54,958
awesome documentation which we will
7040
04:48:54,958 --> 04:48:57,120
reference from time to time and you can
7041
04:48:57,120 --> 04:48:59,680
find the docs at expressjs.com
7042
04:48:59,680 --> 04:49:03,040
again the site is expressjs.com
7043
04:49:03,040 --> 04:49:04,958
and as far as the install you simply
7044
04:49:04,958 --> 04:49:09,120
need to run the command of npm install
7045
04:49:09,120 --> 04:49:12,080
and the package name is express
7046
04:49:12,080 --> 04:49:15,200
now they do suggest this hyphen hyphen
7047
04:49:15,200 --> 04:49:16,480
save flag
7048
04:49:16,480 --> 04:49:18,560
and effectively the reason why they do
7049
04:49:18,560 --> 04:49:21,200
that is because in the earlier node
7050
04:49:21,200 --> 04:49:22,638
versions
7051
04:49:22,638 --> 04:49:25,120
if you did not add this flag
7052
04:49:25,120 --> 04:49:27,680
then package wasn't saved to the package
7053
04:49:27,680 --> 04:49:28,958
json
7054
04:49:28,958 --> 04:49:30,638
meaning when you push this up to the
7055
04:49:30,638 --> 04:49:32,320
github the next person who was getting
7056
04:49:32,320 --> 04:49:34,958
your project well he or she
7057
04:49:34,958 --> 04:49:36,480
did not have the reference for the
7058
04:49:36,480 --> 04:49:38,480
package so of course that caused the
7059
04:49:38,480 --> 04:49:42,080
errors now currently that issue is fixed
7060
04:49:42,080 --> 04:49:44,638
so this is just a good precaution
7061
04:49:44,638 --> 04:49:46,400
but technically you shouldn't have any
7062
04:49:46,400 --> 04:49:48,240
issues if you don't run the command
7063
04:49:48,240 --> 04:49:49,840
again nothing bad is going to happen if
7064
04:49:49,840 --> 04:49:53,520
you add this dash save but technically
7065
04:49:53,520 --> 04:49:56,160
these days you can skip it so just grab
7066
04:49:56,160 --> 04:49:58,560
the command like so
7067
04:49:58,560 --> 04:50:00,798
and just navigate back to the project
7068
04:50:00,798 --> 04:50:02,560
just keep in mind that of course i
7069
04:50:02,560 --> 04:50:05,280
already installed the express
7070
04:50:05,280 --> 04:50:08,080
for the express tutorial this is just
7071
04:50:08,080 --> 04:50:09,920
for your own project if you want to
7072
04:50:09,920 --> 04:50:12,000
install the express if you set up your
7073
04:50:12,000 --> 04:50:14,080
own package.json and all that if you
7074
04:50:14,080 --> 04:50:16,718
want to install the express just run
7075
04:50:16,718 --> 04:50:18,958
this command now one thing that i would
7076
04:50:18,958 --> 04:50:22,160
like to mention though is that i'm using
7077
04:50:22,160 --> 04:50:24,958
version 4.17
7078
04:50:24,958 --> 04:50:26,638
so maybe by the time you're watching
7079
04:50:26,638 --> 04:50:27,440
this
7080
04:50:27,440 --> 04:50:30,000
they already have a version 5. now at
7081
04:50:30,000 --> 04:50:33,040
the moment version 5 is in alpha meaning
7082
04:50:33,040 --> 04:50:34,958
they're still testing everything but
7083
04:50:34,958 --> 04:50:36,958
maybe by the time you're watching this
7084
04:50:36,958 --> 04:50:39,920
this is already stable now if that is
7085
04:50:39,920 --> 04:50:42,718
the case when you run npm install on
7086
04:50:42,718 --> 04:50:45,280
express of course you'll get the latest
7087
04:50:45,280 --> 04:50:48,240
version so your version is not going to
7088
04:50:48,240 --> 04:50:51,760
be this 4.17 now i wouldn't worry if
7089
04:50:51,760 --> 04:50:53,680
it's for example four point i don't know
7090
04:50:53,680 --> 04:50:56,878
24 but if it's five there might be some
7091
04:50:56,878 --> 04:50:59,120
breaking changes and at that point you
7092
04:50:59,120 --> 04:51:01,680
have two options if that is the case if
7093
04:51:01,680 --> 04:51:04,798
the version is already five then you can
7094
04:51:04,798 --> 04:51:05,680
either
7095
04:51:05,680 --> 04:51:08,718
reference the api docs for yourself
7096
04:51:08,718 --> 04:51:10,000
meaning you can take a look at the
7097
04:51:10,000 --> 04:51:12,400
methods and all that what the version
7098
04:51:12,400 --> 04:51:15,120
five provides or
7099
04:51:15,120 --> 04:51:16,638
if you want to use
7100
04:51:16,638 --> 04:51:19,040
all the methods and properties that we
7101
04:51:19,040 --> 04:51:22,480
use in tutorial simply install the same
7102
04:51:22,480 --> 04:51:24,958
version how you can do that well
7103
04:51:24,958 --> 04:51:27,440
you just need to go to npm install
7104
04:51:27,440 --> 04:51:30,240
express so this stays the same and again
7105
04:51:30,240 --> 04:51:32,080
you can remove it you can leave the dash
7106
04:51:32,080 --> 04:51:34,480
dash save doesn't really matter and
7107
04:51:34,480 --> 04:51:38,080
after express you go with add and four
7108
04:51:38,080 --> 04:51:39,680
and then again i'm gonna go with the
7109
04:51:39,680 --> 04:51:41,520
same one just keep in mind as long as
7110
04:51:41,520 --> 04:51:43,680
you have four you're not gonna have any
7111
04:51:43,680 --> 04:51:46,400
issues and then one so this is going to
7112
04:51:46,400 --> 04:51:48,240
install express
7113
04:51:48,240 --> 04:51:51,280
with this specific version hopefully
7114
04:51:51,280 --> 04:51:53,760
everything is clear and now let's get to
7115
04:51:53,760 --> 04:51:56,240
know express all right and once we have
7116
04:51:56,240 --> 04:51:59,120
covered the express library basics now
7117
04:51:59,120 --> 04:52:01,920
of course let's spin the sucker up and
7118
04:52:01,920 --> 04:52:04,878
see how we can make a server
7119
04:52:04,878 --> 04:52:08,638
way easier and with way less headache
7120
04:52:08,638 --> 04:52:10,878
and i'm going to start by removing all
7121
04:52:10,878 --> 04:52:14,320
the code in my app.js just keep in mind
7122
04:52:14,320 --> 04:52:16,878
that if you ever need a reference
7123
04:52:16,878 --> 04:52:19,120
go to the final one and then of course
7124
04:52:19,120 --> 04:52:21,840
http basics is going to be where we set
7125
04:52:21,840 --> 04:52:24,878
up the basic http server and then for
7126
04:52:24,878 --> 04:52:26,000
example
7127
04:52:26,000 --> 04:52:29,040
the http app example is going to be in
7128
04:52:29,040 --> 04:52:31,200
the file number two
7129
04:52:31,200 --> 04:52:33,920
and the way we work with express
7130
04:52:33,920 --> 04:52:36,000
we start by setting up some kind of
7131
04:52:36,000 --> 04:52:38,878
variable and of course we'll have to use
7132
04:52:38,878 --> 04:52:41,280
the require so we go with require and
7133
04:52:41,280 --> 04:52:43,840
then we're looking for express library
7134
04:52:43,840 --> 04:52:46,480
again we can do that because we have
7135
04:52:46,480 --> 04:52:48,240
installed the library
7136
04:52:48,240 --> 04:52:50,240
but if you haven't then of course you'll
7137
04:52:50,240 --> 04:52:52,400
get the error keep that in mind and then
7138
04:52:52,400 --> 04:52:55,280
we go with const and then app is equal
7139
04:52:55,280 --> 04:52:58,878
to express to whatever we imported and
7140
04:52:58,878 --> 04:53:01,680
then we invoke it now
7141
04:53:01,680 --> 04:53:04,240
if you're a bit iffy about this syntax
7142
04:53:04,240 --> 04:53:06,320
just keep in mind that it is very
7143
04:53:06,320 --> 04:53:09,600
similar to our previous example where we
7144
04:53:09,600 --> 04:53:11,440
went with http
7145
04:53:11,440 --> 04:53:14,000
then created the server and
7146
04:53:14,000 --> 04:53:17,440
as a result we had our
7147
04:53:17,440 --> 04:53:19,760
server instance correct so this is
7148
04:53:19,760 --> 04:53:21,840
similar we're getting a function back
7149
04:53:21,840 --> 04:53:24,638
from express and we just invoke it and
7150
04:53:24,638 --> 04:53:27,520
we right away have our server instance
7151
04:53:27,520 --> 04:53:30,320
with bunch of cool methods now what
7152
04:53:30,320 --> 04:53:32,160
you'll also see
7153
04:53:32,160 --> 04:53:34,638
on a web is something like this where
7154
04:53:34,638 --> 04:53:36,560
since this is a function we can invoke
7155
04:53:36,560 --> 04:53:37,920
it right away
7156
04:53:37,920 --> 04:53:40,320
and then just call this one app
7157
04:53:40,320 --> 04:53:41,280
again
7158
04:53:41,280 --> 04:53:43,600
this is really your preference but
7159
04:53:43,600 --> 04:53:46,638
mostly you'll see the first option where
7160
04:53:46,638 --> 04:53:50,958
we first import the module and only then
7161
04:53:50,958 --> 04:53:52,560
we invoke it
7162
04:53:52,560 --> 04:53:54,878
so once we have this setup then of
7163
04:53:54,878 --> 04:53:57,600
course we have a object with bunch of
7164
04:53:57,600 --> 04:53:59,200
useful methods
7165
04:53:59,200 --> 04:54:01,440
now the methods that we'll use the most
7166
04:54:01,440 --> 04:54:04,160
are following amp.get and i'll just copy
7167
04:54:04,160 --> 04:54:05,520
and paste here
7168
04:54:05,520 --> 04:54:07,280
and i'll just change it around a little
7169
04:54:07,280 --> 04:54:08,958
bit post
7170
04:54:08,958 --> 04:54:10,878
and put
7171
04:54:10,878 --> 04:54:13,280
as well as delete
7172
04:54:13,280 --> 04:54:16,958
and also there's going to be all
7173
04:54:16,958 --> 04:54:18,560
use
7174
04:54:18,560 --> 04:54:21,680
and a listen now listen
7175
04:54:21,680 --> 04:54:24,320
we already covered before in the http
7176
04:54:24,320 --> 04:54:26,958
module and this one is pretty much the
7177
04:54:26,958 --> 04:54:28,840
same where we just go with
7178
04:54:28,840 --> 04:54:31,120
app.listen and then we just say what
7179
04:54:31,120 --> 04:54:33,120
port we're going to be listening on so
7180
04:54:33,120 --> 04:54:34,560
in this case of course this is going to
7181
04:54:34,560 --> 04:54:37,360
be 5000 and then we pass in the callback
7182
04:54:37,360 --> 04:54:40,240
function so when we instantiate that
7183
04:54:40,240 --> 04:54:43,600
server we will run this function and a
7184
04:54:43,600 --> 04:54:46,638
common convention is just setting up a
7185
04:54:46,638 --> 04:54:49,120
console log where we say that yeah the
7186
04:54:49,120 --> 04:54:51,840
server is listening on port such and
7187
04:54:51,840 --> 04:54:53,680
such now for time being we're hard
7188
04:54:53,680 --> 04:54:55,920
coding this value later i'll show you
7189
04:54:55,920 --> 04:54:58,480
how we can make this one dynamic so for
7190
04:54:58,480 --> 04:55:00,798
now i'll just say console log and then
7191
04:55:00,798 --> 04:55:02,000
server
7192
04:55:02,000 --> 04:55:04,958
is listening
7193
04:55:04,958 --> 04:55:09,040
on port and then we go 5 000. so let's
7194
04:55:09,040 --> 04:55:12,798
save this one and i'll run my npm start
7195
04:55:12,798 --> 04:55:15,440
and i should see in a console server is
7196
04:55:15,440 --> 04:55:18,878
running or listening on port 5000
7197
04:55:18,878 --> 04:55:22,400
awesome so what about the other methods
7198
04:55:22,400 --> 04:55:25,280
and the first four methods here just
7199
04:55:25,280 --> 04:55:27,760
represent http verbs
7200
04:55:27,760 --> 04:55:29,520
now if you remember
7201
04:55:29,520 --> 04:55:33,440
when we talked about the http request
7202
04:55:33,440 --> 04:55:35,920
and response messages
7203
04:55:35,920 --> 04:55:38,320
well one of the things that we're
7204
04:55:38,320 --> 04:55:41,200
looking for in the request message was
7205
04:55:41,200 --> 04:55:43,200
the http verb
7206
04:55:43,200 --> 04:55:44,400
and yes
7207
04:55:44,400 --> 04:55:46,878
i set up some more meaningful examples
7208
04:55:46,878 --> 04:55:49,760
where we'll see all of them in action
7209
04:55:49,760 --> 04:55:53,120
for now just remember two things first
7210
04:55:53,120 --> 04:55:54,400
this just represents
7211
04:55:54,400 --> 04:55:57,760
what the user is trying to do
7212
04:55:57,760 --> 04:56:00,560
whether read the data insert data update
7213
04:56:00,560 --> 04:56:02,958
data or delete data
7214
04:56:02,958 --> 04:56:05,920
and by default all browsers perform
7215
04:56:05,920 --> 04:56:07,520
a get request
7216
04:56:07,520 --> 04:56:10,240
so that's why we have here amp.get that
7217
04:56:10,240 --> 04:56:13,680
post put and delete now all
7218
04:56:13,680 --> 04:56:16,080
just works with all of them and we'll
7219
04:56:16,080 --> 04:56:18,638
see that in a second when we set up the
7220
04:56:18,638 --> 04:56:22,400
404 page so essentially a response if we
7221
04:56:22,400 --> 04:56:25,280
cannot find the resource on a server and
7222
04:56:25,280 --> 04:56:28,878
app.use is responsible for middleware
7223
04:56:28,878 --> 04:56:31,760
and since it's such a crucial part of
7224
04:56:31,760 --> 04:56:33,920
express of course i prepared more
7225
04:56:33,920 --> 04:56:36,000
examples on that where we cover
7226
04:56:36,000 --> 04:56:38,638
everything from the scratch so for now
7227
04:56:38,638 --> 04:56:41,440
just remember this is middleware but
7228
04:56:41,440 --> 04:56:43,120
don't lose your sleep over it we'll
7229
04:56:43,120 --> 04:56:45,200
cover it a little bit later in the
7230
04:56:45,200 --> 04:56:48,000
course so we have app.listen beautiful
7231
04:56:48,000 --> 04:56:51,120
we're listening on port 5000 but since i
7232
04:56:51,120 --> 04:56:52,878
know that all the browsers are
7233
04:56:52,878 --> 04:56:55,360
performing a get request i simply go
7234
04:56:55,360 --> 04:56:58,080
with app dot get
7235
04:56:58,080 --> 04:57:00,240
and then i need to specifically add two
7236
04:57:00,240 --> 04:57:03,280
things a path so what resource the user
7237
04:57:03,280 --> 04:57:06,320
is trying to access and it would make
7238
04:57:06,320 --> 04:57:08,320
sense if we would start with root
7239
04:57:08,320 --> 04:57:10,798
correct and then the second thing
7240
04:57:10,798 --> 04:57:13,120
is the callback function so this
7241
04:57:13,120 --> 04:57:16,240
callback function will be invoked every
7242
04:57:16,240 --> 04:57:17,120
time
7243
04:57:17,120 --> 04:57:21,040
user is performing a get request
7244
04:57:21,040 --> 04:57:25,040
on our route so basically on our domain
7245
04:57:25,040 --> 04:57:27,520
and then this callback function gets the
7246
04:57:27,520 --> 04:57:31,280
same two arguments we go with request
7247
04:57:31,280 --> 04:57:34,240
as well as the response so this is going
7248
04:57:34,240 --> 04:57:36,958
to deal with incoming request message
7249
04:57:36,958 --> 04:57:39,200
and then this is going to deal with our
7250
04:57:39,200 --> 04:57:43,040
response and in express we go with res
7251
04:57:43,040 --> 04:57:45,840
and then the method name is send so in
7252
04:57:45,840 --> 04:57:47,840
here we can pass the string we can pass
7253
04:57:47,840 --> 04:57:50,240
in the html and i'm just going to simply
7254
04:57:50,240 --> 04:57:52,240
start with home
7255
04:57:52,240 --> 04:57:55,120
and page so we save this one
7256
04:57:55,120 --> 04:57:56,958
and now of course i'm going to navigate
7257
04:57:56,958 --> 04:58:00,240
to my browser i'm going to say localhost
7258
04:58:00,240 --> 04:58:04,000
and then 5000 and there it is now we
7259
04:58:04,000 --> 04:58:04,878
should
7260
04:58:04,878 --> 04:58:07,200
have homepage if you don't again please
7261
04:58:07,200 --> 04:58:09,840
troubleshoot because otherwise it's not
7262
04:58:09,840 --> 04:58:11,040
going to make sense
7263
04:58:11,040 --> 04:58:13,680
what we are about to do next but if you
7264
04:58:13,680 --> 04:58:16,480
see the home page you are in good shape
7265
04:58:16,480 --> 04:58:19,360
so we're listening for get request on
7266
04:58:19,360 --> 04:58:23,120
our route and then every time the user
7267
04:58:23,120 --> 04:58:26,000
navigates to the root then of course we
7268
04:58:26,000 --> 04:58:28,080
just send back the homepage now if you
7269
04:58:28,080 --> 04:58:30,320
want you can of course go to log
7270
04:58:30,320 --> 04:58:32,240
and user hit
7271
04:58:32,240 --> 04:58:34,480
the resource or something like that
7272
04:58:34,480 --> 04:58:37,120
doesn't really matter and then if you go
7273
04:58:37,120 --> 04:58:39,440
back to the browser and if you refresh
7274
04:58:39,440 --> 04:58:42,000
there it is we have user hit the
7275
04:58:42,000 --> 04:58:43,520
resource awesome
7276
04:58:43,520 --> 04:58:46,560
and just like in our previous example
7277
04:58:46,560 --> 04:58:49,120
the basics one let's set up the about
7278
04:58:49,120 --> 04:58:52,000
page as well as the 404
7279
04:58:52,000 --> 04:58:54,798
so i'm going to go back to my app js
7280
04:58:54,798 --> 04:58:57,760
and right above the app that listen i'm
7281
04:58:57,760 --> 04:59:00,878
going to go with another app that get
7282
04:59:00,878 --> 04:59:03,440
and in this case i'm looking for about
7283
04:59:03,440 --> 04:59:06,320
so that's the resource and of course in
7284
04:59:06,320 --> 04:59:09,520
here again we have rec and res
7285
04:59:09,520 --> 04:59:12,000
and then as far as the response well
7286
04:59:12,000 --> 04:59:13,760
i'll cheat a little bit and i'll just
7287
04:59:13,760 --> 04:59:15,680
say res.send
7288
04:59:15,680 --> 04:59:18,958
and we're going to go with about page
7289
04:59:18,958 --> 04:59:20,240
and then of course i would want to
7290
04:59:20,240 --> 04:59:23,280
handle the 404 as well
7291
04:59:23,280 --> 04:59:25,360
so if the user
7292
04:59:25,360 --> 04:59:28,240
comes to my server and tries to access a
7293
04:59:28,240 --> 04:59:30,240
resource that doesn't exist well what am
7294
04:59:30,240 --> 04:59:31,760
i going to send back
7295
04:59:31,760 --> 04:59:33,440
and we can take a look at the default
7296
04:59:33,440 --> 04:59:36,240
one so if i'm going to go with about i
7297
04:59:36,240 --> 04:59:37,280
should
7298
04:59:37,280 --> 04:59:39,920
see the about page but if i go with for
7299
04:59:39,920 --> 04:59:43,360
example a contact page let's see what
7300
04:59:43,360 --> 04:59:45,920
the express is doing and in this case i
7301
04:59:45,920 --> 04:59:48,480
have cannot get the contact and if i
7302
04:59:48,480 --> 04:59:50,638
inspect i can clearly see
7303
04:59:50,638 --> 04:59:54,638
that in my network tab i have contact
7304
04:59:54,638 --> 04:59:57,280
and this is a404 so that's going to be
7305
04:59:57,280 --> 04:59:59,600
the default response now
7306
04:59:59,600 --> 05:00:02,080
i can alter this of course and i can set
7307
05:00:02,080 --> 05:00:05,200
up my own 404 response so i'm going to
7308
05:00:05,200 --> 05:00:07,440
go with app and this is the case where
7309
05:00:07,440 --> 05:00:09,600
i'm going to use all methods because
7310
05:00:09,600 --> 05:00:10,560
again
7311
05:00:10,560 --> 05:00:13,440
user can do multiple things on a server
7312
05:00:13,440 --> 05:00:15,120
and i want to cover them all
7313
05:00:15,120 --> 05:00:17,200
not just getting the resource or
7314
05:00:17,200 --> 05:00:19,440
inserting the resource or whatever i
7315
05:00:19,440 --> 05:00:22,000
want to cover them all so that's why i'm
7316
05:00:22,000 --> 05:00:24,400
going to use my own method again this
7317
05:00:24,400 --> 05:00:28,638
just handles all http verbs whether get
7318
05:00:28,638 --> 05:00:31,520
post or whatever and again this method
7319
05:00:31,520 --> 05:00:34,000
takes two arguments first one is going
7320
05:00:34,000 --> 05:00:36,000
to be the path and the second one is
7321
05:00:36,000 --> 05:00:38,400
going to be a callback function and as
7322
05:00:38,400 --> 05:00:41,120
far as the path i can say all of them
7323
05:00:41,120 --> 05:00:43,600
so whatever resource you're trying to
7324
05:00:43,600 --> 05:00:45,680
access the response is going to be
7325
05:00:45,680 --> 05:00:47,520
exactly the same if i cannot find the
7326
05:00:47,520 --> 05:00:48,400
resource
7327
05:00:48,400 --> 05:00:51,280
then i'll just send back this response
7328
05:00:51,280 --> 05:00:53,840
so again i have a callback function rec
7329
05:00:53,840 --> 05:00:56,320
and res in here and then we're going to
7330
05:00:56,320 --> 05:00:59,760
go with res dot send and just like in
7331
05:00:59,760 --> 05:01:02,320
our http example i'm gonna go with
7332
05:01:02,320 --> 05:01:03,840
heading one
7333
05:01:03,840 --> 05:01:06,240
and we'll say resource
7334
05:01:06,240 --> 05:01:07,920
not found
7335
05:01:07,920 --> 05:01:10,718
and let's close the heading one but of
7336
05:01:10,718 --> 05:01:12,480
course i also would want to add the
7337
05:01:12,480 --> 05:01:15,120
status right so that would make sense i
7338
05:01:15,120 --> 05:01:17,600
don't want to send back 200. that would
7339
05:01:17,600 --> 05:01:19,200
be very confusing
7340
05:01:19,200 --> 05:01:21,920
so before we invoke send method i can
7341
05:01:21,920 --> 05:01:25,200
also add status here and as you can see
7342
05:01:25,200 --> 05:01:28,958
i can chain it so i have res not status
7343
05:01:28,958 --> 05:01:30,958
so this is where i'll pass in my status
7344
05:01:30,958 --> 05:01:34,958
code and then i go with send so in our
7345
05:01:34,958 --> 05:01:37,040
not found example of course i would want
7346
05:01:37,040 --> 05:01:40,480
to pass in the status code of 404 that
7347
05:01:40,480 --> 05:01:42,638
is going to be more correct and the same
7348
05:01:42,638 --> 05:01:45,120
goes here technically we can rely on
7349
05:01:45,120 --> 05:01:46,240
express
7350
05:01:46,240 --> 05:01:49,200
and it does a decent job of adding those
7351
05:01:49,200 --> 05:01:52,718
status codes but a more common approach
7352
05:01:52,718 --> 05:01:55,840
is explicitly setting up the status code
7353
05:01:55,840 --> 05:01:58,240
so that way you have more control of
7354
05:01:58,240 --> 05:02:00,560
what is sent back to the browser so in
7355
05:02:00,560 --> 05:02:03,840
here i go with res and that send but
7356
05:02:03,840 --> 05:02:07,040
before i set up that method
7357
05:02:07,040 --> 05:02:07,840
i
7358
05:02:07,840 --> 05:02:10,718
set up a status one first and i go with
7359
05:02:10,718 --> 05:02:13,760
200 so this just means that the request
7360
05:02:13,760 --> 05:02:16,878
was successful so similarly i'll do that
7361
05:02:16,878 --> 05:02:20,240
in the about where i go with status we
7362
05:02:20,240 --> 05:02:23,760
pass in the 200 and then we set up a
7363
05:02:23,760 --> 05:02:26,718
chained send so now if i go back to my
7364
05:02:26,718 --> 05:02:29,440
browser and if i refresh now of course i
7365
05:02:29,440 --> 05:02:31,600
have resource not found that is my
7366
05:02:31,600 --> 05:02:35,840
heading 1 and i can clearly see my 404
7367
05:02:35,840 --> 05:02:38,638
and if for example i'm looking for about
7368
05:02:38,638 --> 05:02:41,440
there it is i have my about page as well
7369
05:02:41,440 --> 05:02:44,560
as the homepage so if we go to localhost
7370
05:02:44,560 --> 05:02:47,440
and 5000 that's going to be our route
7371
05:02:47,440 --> 05:02:50,240
that is going to be our homepage all
7372
05:02:50,240 --> 05:02:52,240
right and we're done with our basic
7373
05:02:52,240 --> 05:02:55,120
example hopefully you can see that it is
7374
05:02:55,120 --> 05:02:57,760
already way less code
7375
05:02:57,760 --> 05:03:01,760
than just using the built-in http module
7376
05:03:01,760 --> 05:03:04,480
and in the next example you'll see how
7377
05:03:04,480 --> 05:03:07,600
express truly shines when it comes to
7378
05:03:07,600 --> 05:03:10,718
setting up a server beautiful we're done
7379
05:03:10,718 --> 05:03:13,200
with our basic example now let's tackle
7380
05:03:13,200 --> 05:03:16,480
the big beast our navbar app
7381
05:03:16,480 --> 05:03:20,000
and the setup for the following examples
7382
05:03:20,000 --> 05:03:23,040
is really up to you if you're not a fan
7383
05:03:23,040 --> 05:03:25,120
of retyping
7384
05:03:25,120 --> 05:03:27,680
something you have already learned you
7385
05:03:27,680 --> 05:03:29,280
don't have to do that you can just
7386
05:03:29,280 --> 05:03:31,360
remove app.get
7387
05:03:31,360 --> 05:03:34,718
all the way to app.all so basically
7388
05:03:34,718 --> 05:03:36,638
leave the import as well as
7389
05:03:36,638 --> 05:03:39,200
instantiation and
7390
05:03:39,200 --> 05:03:42,878
listen as well however i am a fan
7391
05:03:42,878 --> 05:03:43,920
of
7392
05:03:43,920 --> 05:03:45,840
repeating something because that way
7393
05:03:45,840 --> 05:03:48,000
whenever i need to create something from
7394
05:03:48,000 --> 05:03:50,400
scratch i already have done it quite a
7395
05:03:50,400 --> 05:03:52,480
few times so i don't have that blank
7396
05:03:52,480 --> 05:03:54,240
page syndrome where you're like looking
7397
05:03:54,240 --> 05:03:56,958
at the empty file and you don't know
7398
05:03:56,958 --> 05:03:58,958
what to do so in my case i'm going to
7399
05:03:58,958 --> 05:04:00,480
remove everything and we'll start from
7400
05:04:00,480 --> 05:04:03,760
the scratch so const express is equal to
7401
05:04:03,760 --> 05:04:05,920
require and of course we're looking for
7402
05:04:05,920 --> 05:04:07,040
express
7403
05:04:07,040 --> 05:04:09,120
and then i'm going to set up my app that
7404
05:04:09,120 --> 05:04:11,440
is equal to express we invoke it and
7405
05:04:11,440 --> 05:04:14,080
again we go with app.listen
7406
05:04:14,080 --> 05:04:16,320
and we're going to go with 5000. now i'm
7407
05:04:16,320 --> 05:04:18,000
not saying that i'm going to do that for
7408
05:04:18,000 --> 05:04:20,080
every example
7409
05:04:20,080 --> 05:04:22,560
but i'm just showcasing what is in my
7410
05:04:22,560 --> 05:04:26,000
approach when i'm starting something new
7411
05:04:26,000 --> 05:04:27,840
so when i'm learning something new yes i
7412
05:04:27,840 --> 05:04:30,798
do like to retype some of the stuff
7413
05:04:30,798 --> 05:04:32,400
quite a few times because
7414
05:04:32,400 --> 05:04:34,958
that just makes sure that i remember it
7415
05:04:34,958 --> 05:04:37,280
better and in here i'm going to go with
7416
05:04:37,280 --> 05:04:40,638
server here's listening
7417
05:04:40,638 --> 05:04:43,120
on port and we go with
7418
05:04:43,120 --> 05:04:47,760
5000 and then dot and in order to set
7419
05:04:47,760 --> 05:04:49,840
everything up we're going to go with
7420
05:04:49,840 --> 05:04:52,160
app.get so again i'm looking for the
7421
05:04:52,160 --> 05:04:54,480
root and of course i would want to start
7422
05:04:54,480 --> 05:04:57,280
with my index html correct that's the
7423
05:04:57,280 --> 05:04:58,320
start
7424
05:04:58,320 --> 05:05:01,040
and we go with app.get so we're
7425
05:05:01,040 --> 05:05:04,000
listening for all the incoming requests
7426
05:05:04,000 --> 05:05:05,840
that go to our route
7427
05:05:05,840 --> 05:05:07,360
and of course we're specifically
7428
05:05:07,360 --> 05:05:09,760
listening for get requests then we have
7429
05:05:09,760 --> 05:05:13,040
our callback function rack and res
7430
05:05:13,040 --> 05:05:14,878
and we'll set up the functionality in a
7431
05:05:14,878 --> 05:05:17,200
second before we do anything let's also
7432
05:05:17,200 --> 05:05:18,560
set up all
7433
05:05:18,560 --> 05:05:20,240
and this is of course going to be for
7434
05:05:20,240 --> 05:05:21,840
all the requests
7435
05:05:21,840 --> 05:05:24,638
that will hit 404 so we're going to go
7436
05:05:24,638 --> 05:05:26,718
with rack and res
7437
05:05:26,718 --> 05:05:29,760
and here let's say res and then status
7438
05:05:29,760 --> 05:05:32,958
so let's add here a 200 or i'm sorry
7439
05:05:32,958 --> 05:05:36,240
four four my bad and then we're gonna go
7440
05:05:36,240 --> 05:05:37,680
with send
7441
05:05:37,680 --> 05:05:39,360
and instead of the send let's just go
7442
05:05:39,360 --> 05:05:41,200
with the resource
7443
05:05:41,200 --> 05:05:42,958
not found
7444
05:05:42,958 --> 05:05:45,360
now when it comes to get
7445
05:05:45,360 --> 05:05:46,958
in this case i would want to send the
7446
05:05:46,958 --> 05:05:49,120
file more specifically i would want to
7447
05:05:49,120 --> 05:05:52,160
send back the index.html
7448
05:05:52,160 --> 05:05:55,040
and in order to do that i need to use
7449
05:05:55,040 --> 05:05:58,878
send file method that comes with express
7450
05:05:58,878 --> 05:06:01,120
now in there though i do need to provide
7451
05:06:01,120 --> 05:06:04,160
a absolute path so we'll have to use one
7452
05:06:04,160 --> 05:06:06,080
of the modules we covered before and
7453
05:06:06,080 --> 05:06:08,480
that is going to be a path module so i
7454
05:06:08,480 --> 05:06:13,120
go with const path is equal to require
7455
05:06:13,120 --> 05:06:14,878
and i'm looking for path module again we
7456
05:06:14,878 --> 05:06:16,958
don't have to install it comes
7457
05:06:16,958 --> 05:06:20,000
preinstalled with node so we are in good
7458
05:06:20,000 --> 05:06:22,560
shape and then where we have our
7459
05:06:22,560 --> 05:06:25,360
callback function we go with res
7460
05:06:25,360 --> 05:06:29,120
and send file method and here let's pass
7461
05:06:29,120 --> 05:06:33,360
in path and then resolve so path dot
7462
05:06:33,360 --> 05:06:35,520
resolve remember that was one of the
7463
05:06:35,520 --> 05:06:36,958
methods we cover
7464
05:06:36,958 --> 05:06:39,360
and effectively i would want to pass in
7465
05:06:39,360 --> 05:06:40,878
the dirt name
7466
05:06:40,878 --> 05:06:43,040
so this is going to be path to our
7467
05:06:43,040 --> 05:06:45,120
directory because we do need to provide
7468
05:06:45,120 --> 05:06:47,600
that absolute path and then of course
7469
05:06:47,600 --> 05:06:49,920
we're looking for our index.html which
7470
05:06:49,920 --> 05:06:52,638
is sitting in the navbar app so forward
7471
05:06:52,638 --> 05:06:55,840
slash and then navbar
7472
05:06:55,840 --> 05:06:58,480
hyphen app and then we have another
7473
05:06:58,480 --> 05:07:02,160
forward slash index html now if i have
7474
05:07:02,160 --> 05:07:04,878
to be perfectly honest in this case we
7475
05:07:04,878 --> 05:07:07,360
can also use path dot join it doesn't
7476
05:07:07,360 --> 05:07:09,360
really matter since their name provides
7477
05:07:09,360 --> 05:07:12,718
that absolute path but just to be a bit
7478
05:07:12,718 --> 05:07:15,200
explicit that we are providing
7479
05:07:15,200 --> 05:07:17,680
absolute path i went with path dot
7480
05:07:17,680 --> 05:07:20,400
resolve and then the their name and then
7481
05:07:20,400 --> 05:07:24,080
whatever is the path to my index html
7482
05:07:24,080 --> 05:07:26,240
and the moment we save it
7483
05:07:26,240 --> 05:07:28,320
we'll have the same errors
7484
05:07:28,320 --> 05:07:31,440
just like in the http module so i go to
7485
05:07:31,440 --> 05:07:34,160
my localhost 5000 and i still have the
7486
05:07:34,160 --> 05:07:37,520
same issues i still don't have style css
7487
05:07:37,520 --> 05:07:40,480
there's no sign of browser app as well
7488
05:07:40,480 --> 05:07:41,760
as the logo
7489
05:07:41,760 --> 05:07:44,000
now in express though we don't have to
7490
05:07:44,000 --> 05:07:46,958
do this whole song and dance
7491
05:07:46,958 --> 05:07:49,200
like in http module
7492
05:07:49,200 --> 05:07:52,480
in express i can simply go above all my
7493
05:07:52,480 --> 05:07:56,080
app.gets and amp.alls and i can go with
7494
05:07:56,080 --> 05:07:57,040
app
7495
05:07:57,040 --> 05:07:59,680
dot use and this is going to be the case
7496
05:07:59,680 --> 05:08:02,320
where i'll type out the code and then
7497
05:08:02,320 --> 05:08:04,638
i'll explain everything that is
7498
05:08:04,638 --> 05:08:07,520
happening and we'll pass in the express
7499
05:08:07,520 --> 05:08:08,240
so
7500
05:08:08,240 --> 05:08:10,480
this is what we're importing so this is
7501
05:08:10,480 --> 05:08:12,878
not going to be our server instance
7502
05:08:12,878 --> 05:08:15,040
instead we're going to go with express
7503
05:08:15,040 --> 05:08:18,160
and then static that's the method and in
7504
05:08:18,160 --> 05:08:20,080
here again we just need to provide a
7505
05:08:20,080 --> 05:08:22,638
path now common name is setting this up
7506
05:08:22,638 --> 05:08:25,280
as the folder by the name of public
7507
05:08:25,280 --> 05:08:26,718
please keep in mind technically you
7508
05:08:26,718 --> 05:08:28,798
don't have to do that you can just point
7509
05:08:28,798 --> 05:08:32,080
to our navbar app but in my case i will
7510
05:08:32,080 --> 05:08:34,798
do that i will set up here
7511
05:08:34,798 --> 05:08:37,440
dot forward slash and i'll call this
7512
05:08:37,440 --> 05:08:40,160
public so now of course what i need is
7513
05:08:40,160 --> 05:08:43,280
to set up a folder by the name of public
7514
05:08:43,280 --> 05:08:44,480
and then
7515
05:08:44,480 --> 05:08:47,680
all my static resources all my static
7516
05:08:47,680 --> 05:08:49,600
files i would want to transfer there now
7517
05:08:49,600 --> 05:08:51,280
don't worry about it i'll cover in
7518
05:08:51,280 --> 05:08:52,160
length
7519
05:08:52,160 --> 05:08:54,958
what in a server land means static
7520
05:08:54,958 --> 05:08:56,878
resources for now what i would want you
7521
05:08:56,878 --> 05:09:01,120
to do is go back to our folder
7522
05:09:01,120 --> 05:09:03,520
and that is going to be express tutorial
7523
05:09:03,520 --> 05:09:04,958
and of course you can do that in visual
7524
05:09:04,958 --> 05:09:06,798
studio code i just think that this is
7525
05:09:06,798 --> 05:09:09,040
going to be a bit easier to see so i'm
7526
05:09:09,040 --> 05:09:11,120
going to create a new folder again
7527
05:09:11,120 --> 05:09:13,600
common convention is calling this public
7528
05:09:13,600 --> 05:09:16,080
but you can call it whatever lobster it
7529
05:09:16,080 --> 05:09:18,878
doesn't really matter and then in my
7530
05:09:18,878 --> 05:09:20,878
case i'm just going to copy these files
7531
05:09:20,878 --> 05:09:24,638
so take browser app take logo and take
7532
05:09:24,638 --> 05:09:26,560
styles if you want you can move them
7533
05:09:26,560 --> 05:09:28,638
just remember that the previous code is
7534
05:09:28,638 --> 05:09:31,280
not going to work as far as the http
7535
05:09:31,280 --> 05:09:33,600
module setup so i'm going to copy these
7536
05:09:33,600 --> 05:09:34,400
ones
7537
05:09:34,400 --> 05:09:38,000
and paste it here in my public so now of
7538
05:09:38,000 --> 05:09:41,200
course i have browser app logo svg and
7539
05:09:41,200 --> 05:09:42,718
style css
7540
05:09:42,718 --> 05:09:43,520
so
7541
05:09:43,520 --> 05:09:48,000
of course now i can zoom out and once i
7542
05:09:48,000 --> 05:09:49,520
save
7543
05:09:49,520 --> 05:09:50,878
check it out
7544
05:09:50,878 --> 05:09:54,958
when we navigate to our route there it
7545
05:09:54,958 --> 05:09:57,920
is this is our application and what's
7546
05:09:57,920 --> 05:10:01,040
really cool all those resources are
7547
05:10:01,040 --> 05:10:03,920
right away available so if i go to my
7548
05:10:03,920 --> 05:10:07,200
localhost 5000 remember browser was
7549
05:10:07,200 --> 05:10:08,878
looking for what well it was looking for
7550
05:10:08,878 --> 05:10:12,878
example for style css if you go here
7551
05:10:12,878 --> 05:10:14,798
there is a resource by the name of style
7552
05:10:14,798 --> 05:10:18,480
css and of course in here i have all my
7553
05:10:18,480 --> 05:10:21,200
css code and notice how
7554
05:10:21,200 --> 05:10:24,560
we didn't have to set up the statuses we
7555
05:10:24,560 --> 05:10:27,120
didn't have to set up the content types
7556
05:10:27,120 --> 05:10:30,480
or any of that express takes care of it
7557
05:10:30,480 --> 05:10:32,798
all now of course you probably at this
7558
05:10:32,798 --> 05:10:35,200
point have more questions than answers
7559
05:10:35,200 --> 05:10:37,360
so let me start clearing them up and
7560
05:10:37,360 --> 05:10:39,440
first i'm just going to add a comment of
7561
05:10:39,440 --> 05:10:40,638
setup
7562
05:10:40,638 --> 05:10:41,920
static
7563
05:10:41,920 --> 05:10:44,798
and middleware middleware
7564
05:10:44,798 --> 05:10:48,400
and like i mentioned app.use is for
7565
05:10:48,400 --> 05:10:51,200
setting up the middleware and we have
7566
05:10:51,200 --> 05:10:54,320
more serious examples coming up
7567
05:10:54,320 --> 05:10:56,400
so for time being please don't worry
7568
05:10:56,400 --> 05:10:58,638
about this line okay so please don't
7569
05:10:58,638 --> 05:11:00,958
worry about app.use what is express
7570
05:11:00,958 --> 05:11:02,878
static that is essentially a built in
7571
05:11:02,878 --> 05:11:05,920
middleware i will cover everything step
7572
05:11:05,920 --> 05:11:07,040
by step
7573
05:11:07,040 --> 05:11:08,240
in this video i would want you to
7574
05:11:08,240 --> 05:11:11,120
understand what the term static asset
7575
05:11:11,120 --> 05:11:11,920
means
7576
05:11:11,920 --> 05:11:13,440
and it simply means
7577
05:11:13,440 --> 05:11:16,320
that it is a file that server doesn't
7578
05:11:16,320 --> 05:11:20,560
have to change it so instead of our http
7579
05:11:20,560 --> 05:11:23,680
example where we created a path
7580
05:11:23,680 --> 05:11:26,080
for every such resource
7581
05:11:26,080 --> 05:11:29,120
and if i were to have i don't know 20
7582
05:11:29,120 --> 05:11:30,718
000 images
7583
05:11:30,718 --> 05:11:33,680
i would have to repeat the same steps
7584
05:11:33,680 --> 05:11:37,280
instead since this is a static asset
7585
05:11:37,280 --> 05:11:40,320
meaning an asset where the server
7586
05:11:40,320 --> 05:11:43,040
doesn't need to change it we simply
7587
05:11:43,040 --> 05:11:45,440
place it in designated
7588
05:11:45,440 --> 05:11:47,920
folder again the common name for those
7589
05:11:47,920 --> 05:11:51,600
folders are public or static
7590
05:11:51,600 --> 05:11:54,240
and then we just dump all those assets
7591
05:11:54,240 --> 05:11:56,958
in there so if i were to have i don't
7592
05:11:56,958 --> 05:12:00,718
know 20 000 extra images in here i can
7593
05:12:00,718 --> 05:12:02,080
just dump them
7594
05:12:02,080 --> 05:12:04,958
and express will take care of it all it
7595
05:12:04,958 --> 05:12:07,200
will set up the paths it will set up the
7596
05:12:07,200 --> 05:12:09,120
mime types it will set up the status
7597
05:12:09,120 --> 05:12:12,480
codes and all of that good stuff so
7598
05:12:12,480 --> 05:12:15,520
hopefully that is clear static assets
7599
05:12:15,520 --> 05:12:18,480
are just files that server doesn't have
7600
05:12:18,480 --> 05:12:21,440
to change and an example of a static
7601
05:12:21,440 --> 05:12:24,320
asset is an image file here's the style
7602
05:12:24,320 --> 05:12:28,560
file and also a javascript file and here
7603
05:12:28,560 --> 05:12:30,480
comes the next question
7604
05:12:30,480 --> 05:12:32,958
what is this guy with weird eastern
7605
05:12:32,958 --> 05:12:34,958
european accent talking about
7606
05:12:34,958 --> 05:12:37,520
because all my life i've been told
7607
05:12:37,520 --> 05:12:39,360
that the javascript
7608
05:12:39,360 --> 05:12:42,638
makes my apps dynamic
7609
05:12:42,638 --> 05:12:45,120
it adds all the functionality
7610
05:12:45,120 --> 05:12:47,600
so how come this is just another static
7611
05:12:47,600 --> 05:12:48,638
asset
7612
05:12:48,638 --> 05:12:51,040
and to answer your question yeah you're
7613
05:12:51,040 --> 05:12:52,878
right javascript
7614
05:12:52,878 --> 05:12:54,000
does make
7615
05:12:54,000 --> 05:12:57,760
our apps dynamic however think about it
7616
05:12:57,760 --> 05:12:59,920
this is a browser app so it makes
7617
05:12:59,920 --> 05:13:02,320
dynamic on a browser
7618
05:13:02,320 --> 05:13:04,480
as far as servers is concerned
7619
05:13:04,480 --> 05:13:06,638
it is just a asset doesn't need to
7620
05:13:06,638 --> 05:13:07,600
change
7621
05:13:07,600 --> 05:13:09,440
now if you're wondering well how to set
7622
05:13:09,440 --> 05:13:11,280
up something dynamic
7623
05:13:11,280 --> 05:13:13,680
just please put the pin on that and
7624
05:13:13,680 --> 05:13:15,600
we're going to cover that when we cover
7625
05:13:15,600 --> 05:13:18,080
server-side rendering because there is
7626
05:13:18,080 --> 05:13:20,878
such thing as template engines
7627
05:13:20,878 --> 05:13:22,958
and the simplest way for me to explain
7628
05:13:22,958 --> 05:13:26,000
that is imagine the scenario where you
7629
05:13:26,000 --> 05:13:28,718
can actually log in
7630
05:13:28,718 --> 05:13:32,400
or in other way just showcase whoever is
7631
05:13:32,400 --> 05:13:34,878
visiting the site and then dynamically i
7632
05:13:34,878 --> 05:13:37,040
would display for example name
7633
05:13:37,040 --> 05:13:40,160
so if the peter logs in then i'm sending
7634
05:13:40,160 --> 05:13:43,920
back the html with the text of hi peter
7635
05:13:43,920 --> 05:13:46,240
now if the john logs in then of course
7636
05:13:46,240 --> 05:13:49,280
i'm sending back the username with the
7637
05:13:49,280 --> 05:13:51,600
value of john so hopefully you get the
7638
05:13:51,600 --> 05:13:54,320
gist where in this case notice this is
7639
05:13:54,320 --> 05:13:55,200
just
7640
05:13:55,200 --> 05:13:58,160
same old index html that i'm sending
7641
05:13:58,160 --> 05:14:00,480
back regardless of who is visiting the
7642
05:14:00,480 --> 05:14:03,040
site but yes there's also an option of
7643
05:14:03,040 --> 05:14:06,080
setting this dynamically where depending
7644
05:14:06,080 --> 05:14:08,000
on who's visiting the site or what the
7645
05:14:08,000 --> 05:14:10,560
user is trying to do i'm actually
7646
05:14:10,560 --> 05:14:14,480
modifying my file before i'm sending it
7647
05:14:14,480 --> 05:14:17,520
back so hopefully it is clear how much
7648
05:14:17,520 --> 05:14:20,480
easier it is to work with express
7649
05:14:20,480 --> 05:14:23,520
where if we have static assets we just
7650
05:14:23,520 --> 05:14:26,320
set up designated folder and just dump
7651
05:14:26,320 --> 05:14:29,520
them all in and static asset just means
7652
05:14:29,520 --> 05:14:31,680
a file that server doesn't need to
7653
05:14:31,680 --> 05:14:32,560
change
7654
05:14:32,560 --> 05:14:34,718
and of course we can start working on
7655
05:14:34,718 --> 05:14:38,000
our next example nice hopefully we are
7656
05:14:38,000 --> 05:14:41,200
clear on static acids and before we
7657
05:14:41,200 --> 05:14:45,040
cover more complex expressed topics i
7658
05:14:45,040 --> 05:14:47,120
would want to throw a mine grenade at
7659
05:14:47,120 --> 05:14:48,160
attribute
7660
05:14:48,160 --> 05:14:50,560
and it goes something like this
7661
05:14:50,560 --> 05:14:54,000
so if we put two and two together
7662
05:14:54,000 --> 05:14:56,718
i talked about static assets in the
7663
05:14:56,718 --> 05:14:58,320
previous video
7664
05:14:58,320 --> 05:15:00,840
but if we're looking at the
7665
05:15:00,840 --> 05:15:02,400
index.html
7666
05:15:02,400 --> 05:15:05,440
isn't this a static asset as well
7667
05:15:05,440 --> 05:15:08,160
and of course the answer is yes
7668
05:15:08,160 --> 05:15:10,958
so instead of sending this file
7669
05:15:10,958 --> 05:15:13,840
we can add it to static assets and we're
7670
05:15:13,840 --> 05:15:16,480
going to be in good shape and if i have
7671
05:15:16,480 --> 05:15:19,120
to be perfectly honest with you when it
7672
05:15:19,120 --> 05:15:20,400
comes to
7673
05:15:20,400 --> 05:15:22,638
send file
7674
05:15:22,638 --> 05:15:24,798
if we're using it to send back for
7675
05:15:24,798 --> 05:15:27,280
example index html
7676
05:15:27,280 --> 05:15:30,240
actually we use other two methods
7677
05:15:30,240 --> 05:15:33,040
instead so first i'll show you the first
7678
05:15:33,040 --> 05:15:35,520
one where we just dump everything
7679
05:15:35,520 --> 05:15:38,080
as far as the static assets so i'll just
7680
05:15:38,080 --> 05:15:41,200
add index.html to all my static assets
7681
05:15:41,200 --> 05:15:42,560
in the public
7682
05:15:42,560 --> 05:15:45,760
and that will effectively do exactly the
7683
05:15:45,760 --> 05:15:48,480
same like we're doing here with send
7684
05:15:48,480 --> 05:15:50,638
file and the other one is going to be
7685
05:15:50,638 --> 05:15:52,878
using templating engine so of course
7686
05:15:52,878 --> 05:15:55,520
that one will cover when we go to server
7687
05:15:55,520 --> 05:15:57,760
set rendering so i'm just trying to
7688
05:15:57,760 --> 05:15:59,840
showcase that yes there is this option
7689
05:15:59,840 --> 05:16:01,760
of send file and we might use it from
7690
05:16:01,760 --> 05:16:04,240
time to time throughout the course but
7691
05:16:04,240 --> 05:16:07,680
not for sending back index html files so
7692
05:16:07,680 --> 05:16:10,240
first let me just say that the other
7693
05:16:10,240 --> 05:16:12,798
option one of the two other options that
7694
05:16:12,798 --> 05:16:14,798
we'll use the most throughout the course
7695
05:16:14,798 --> 05:16:15,840
is just
7696
05:16:15,840 --> 05:16:17,760
adding two
7697
05:16:17,760 --> 05:16:19,680
static
7698
05:16:19,680 --> 05:16:20,798
assets
7699
05:16:20,798 --> 05:16:22,958
like so and then the second one is going
7700
05:16:22,958 --> 05:16:24,798
to be server side rendering
7701
05:16:24,798 --> 05:16:26,560
where basically we'll use template
7702
05:16:26,560 --> 05:16:27,840
engine for that
7703
05:16:27,840 --> 05:16:30,718
so in order to set everything up now of
7704
05:16:30,718 --> 05:16:33,040
course i just need to take my index html
7705
05:16:33,040 --> 05:16:34,878
and again if you want you can just move
7706
05:16:34,878 --> 05:16:35,600
it
7707
05:16:35,600 --> 05:16:38,000
or in my case i'm just going to copy and
7708
05:16:38,000 --> 05:16:39,120
paste
7709
05:16:39,120 --> 05:16:40,798
so again i'm going to go back to my
7710
05:16:40,798 --> 05:16:42,000
folder
7711
05:16:42,000 --> 05:16:44,480
i'll zoom in massively
7712
05:16:44,480 --> 05:16:46,080
so you can see better so express
7713
05:16:46,080 --> 05:16:47,280
tutorial
7714
05:16:47,280 --> 05:16:49,040
there is my navbar app and i'll just
7715
05:16:49,040 --> 05:16:51,200
take the index.html
7716
05:16:51,200 --> 05:16:53,760
and i'll copy and paste and now it is in
7717
05:16:53,760 --> 05:16:54,878
the public
7718
05:16:54,878 --> 05:16:58,080
so what happens index.html
7719
05:16:58,080 --> 05:17:00,160
is always going to be a root
7720
05:17:00,160 --> 05:17:00,958
so
7721
05:17:00,958 --> 05:17:04,000
when the user hits the server by default
7722
05:17:04,000 --> 05:17:06,958
server will serve this index.html and
7723
05:17:06,958 --> 05:17:09,360
since our index.html basically has all
7724
05:17:09,360 --> 05:17:12,718
the paths to browser app to logo svg and
7725
05:17:12,718 --> 05:17:13,680
all that
7726
05:17:13,680 --> 05:17:15,680
we're going to be in good shape more
7727
05:17:15,680 --> 05:17:18,080
effectively we don't even need to set up
7728
05:17:18,080 --> 05:17:20,718
this send file option
7729
05:17:20,718 --> 05:17:23,680
so now of course i can just save it and
7730
05:17:23,680 --> 05:17:26,160
once i refresh notice how nothing
7731
05:17:26,160 --> 05:17:29,440
changed i'm still serving my app
7732
05:17:29,440 --> 05:17:32,718
so we're still in good shape and if you
7733
05:17:32,718 --> 05:17:35,600
take a look at the network tab notice
7734
05:17:35,600 --> 05:17:37,680
everything still works correctly i'm
7735
05:17:37,680 --> 05:17:41,280
still getting all the css all the
7736
05:17:41,280 --> 05:17:44,320
browser javascript functionality as well
7737
05:17:44,320 --> 05:17:47,680
as the logo and to answer your question
7738
05:17:47,680 --> 05:17:50,160
yes that is effectively how you can set
7739
05:17:50,160 --> 05:17:53,440
up the simple sites you can simply just
7740
05:17:53,440 --> 05:17:56,240
dump all your files in the public just
7741
05:17:56,240 --> 05:17:58,638
make sure that you serve them up
7742
05:17:58,638 --> 05:18:00,400
and that's it and you're in good shape
7743
05:18:00,400 --> 05:18:03,440
now we still have the 404 but as far as
7744
05:18:03,440 --> 05:18:06,638
just serving straight up sites with html
7745
05:18:06,638 --> 05:18:09,200
css and javascript yes you can simply
7746
05:18:09,200 --> 05:18:11,840
dump them into public you can simply set
7747
05:18:11,840 --> 05:18:14,400
up the middleware and serve all the
7748
05:18:14,400 --> 05:18:17,200
static assets and you're going to be in
7749
05:18:17,200 --> 05:18:20,000
good shape beautiful we are successfully
7750
05:18:20,000 --> 05:18:22,638
done with the initial express setup
7751
05:18:22,638 --> 05:18:25,520
and ready to cover more complex express
7752
05:18:25,520 --> 05:18:28,080
topics before we do that though
7753
05:18:28,080 --> 05:18:30,000
there's something important i want to
7754
05:18:30,000 --> 05:18:33,280
mention you see when it comes to express
7755
05:18:33,280 --> 05:18:35,440
in most cases you'll use one of the two
7756
05:18:35,440 --> 05:18:36,958
following options
7757
05:18:36,958 --> 05:18:39,360
you'll use it to set up api
7758
05:18:39,360 --> 05:18:42,638
or templates with server side rendering
7759
05:18:42,638 --> 05:18:45,600
now since term api is probably one of
7760
05:18:45,600 --> 05:18:48,718
the most overused terms in the community
7761
05:18:48,718 --> 05:18:51,600
and in various scenarios it can mean
7762
05:18:51,600 --> 05:18:53,520
different things let's start by
7763
05:18:53,520 --> 05:18:56,000
understanding that in express
7764
05:18:56,000 --> 05:18:58,400
or in http case
7765
05:18:58,400 --> 05:19:01,200
when we talk about api we mean setting
7766
05:19:01,200 --> 05:19:03,680
up an http interface
7767
05:19:03,680 --> 05:19:06,400
to interact with our data
7768
05:19:06,400 --> 05:19:09,360
now data is sent using json which stands
7769
05:19:09,360 --> 05:19:13,120
for javascript object notation and in
7770
05:19:13,120 --> 05:19:15,360
order to send back our response we're
7771
05:19:15,360 --> 05:19:17,160
going to use
7772
05:19:17,160 --> 05:19:20,160
res.json method which will do all the
7773
05:19:20,160 --> 05:19:22,400
heavy lifting like for example setting
7774
05:19:22,400 --> 05:19:25,440
up the proper content type and stringify
7775
05:19:25,440 --> 05:19:26,958
our data
7776
05:19:26,958 --> 05:19:29,840
the other flavor we have is server side
7777
05:19:29,840 --> 05:19:32,958
rendering where we will set up templates
7778
05:19:32,958 --> 05:19:36,638
and send back entire html and css and
7779
05:19:36,638 --> 05:19:38,638
javascript ourselves
7780
05:19:38,638 --> 05:19:40,840
and we're going to do that using
7781
05:19:40,840 --> 05:19:43,040
res.render method
7782
05:19:43,040 --> 05:19:45,760
now since i'm a big believer in actual
7783
05:19:45,760 --> 05:19:48,480
examples over slides if you are a bit
7784
05:19:48,480 --> 05:19:51,040
iffy on either of these flavors just
7785
05:19:51,040 --> 05:19:53,680
hold on a bit and once we cover some
7786
05:19:53,680 --> 05:19:55,840
examples i promise you it will all make
7787
05:19:55,840 --> 05:19:56,798
sense
7788
05:19:56,798 --> 05:19:59,200
now why am i telling you all of this you
7789
05:19:59,200 --> 05:20:01,440
see when it comes to more complex
7790
05:20:01,440 --> 05:20:04,878
express topics it only makes sense if we
7791
05:20:04,878 --> 05:20:07,440
cover them properly using one of these
7792
05:20:07,440 --> 05:20:10,240
flavors instead of just bunch of random
7793
05:20:10,240 --> 05:20:11,440
examples
7794
05:20:11,440 --> 05:20:14,878
and the option i picked is the api one
7795
05:20:14,878 --> 05:20:17,200
so in the following examples we're going
7796
05:20:17,200 --> 05:20:20,480
to construct api response using more
7797
05:20:20,480 --> 05:20:23,360
advanced express setups the reason why i
7798
05:20:23,360 --> 05:20:26,480
picked apr route is because i believe it
7799
05:20:26,480 --> 05:20:30,000
lets us focus more on the actual express
7800
05:20:30,000 --> 05:20:32,000
since templates by themselves add a bit
7801
05:20:32,000 --> 05:20:34,480
more unnecessary overhead especially
7802
05:20:34,480 --> 05:20:36,558
while we're just starting out with
7803
05:20:36,558 --> 05:20:38,798
express with that said let me be very
7804
05:20:38,798 --> 05:20:42,878
clear if you grasp the concepts with api
7805
05:20:42,878 --> 05:20:46,160
so using the api flavor you'll have no
7806
05:20:46,160 --> 05:20:48,638
problem implementing them with templates
7807
05:20:48,638 --> 05:20:51,120
as well since for the most part express
7808
05:20:51,120 --> 05:20:54,080
concept setup is exactly the same
7809
05:20:54,080 --> 05:20:56,240
we'll cover server side rendering later
7810
05:20:56,240 --> 05:20:58,400
in the course so you'll have to wait a
7811
05:20:58,400 --> 05:21:00,718
bit for the actual example and since
7812
05:21:00,718 --> 05:21:02,878
we're going to start with api let me
7813
05:21:02,878 --> 05:21:05,120
just stress the main point one more time
7814
05:21:05,120 --> 05:21:07,680
and how it looks like in a real world
7815
05:21:07,680 --> 05:21:11,280
so like i said the main idea with apis
7816
05:21:11,280 --> 05:21:14,798
is that our server provides data
7817
05:21:14,798 --> 05:21:16,958
and what that means that any front-end
7818
05:21:16,958 --> 05:21:19,920
app that wants to access it and use it
7819
05:21:19,920 --> 05:21:23,040
can simply perform a http request
7820
05:21:23,040 --> 05:21:26,240
and using our data set up the api and
7821
05:21:26,240 --> 05:21:29,120
functionality how does that look like in
7822
05:21:29,120 --> 05:21:32,480
a real world well if you navigate again
7823
05:21:32,480 --> 05:21:35,760
back to course api and of course not the
7824
05:21:35,760 --> 05:21:39,520
slides slides are just images but if you
7825
05:21:39,520 --> 05:21:42,480
take a look at any of these examples
7826
05:21:42,480 --> 05:21:44,240
you'll notice something interesting
7827
05:21:44,240 --> 05:21:46,718
where we're sending back the json and
7828
05:21:46,718 --> 05:21:49,280
you can clearly see that if you go again
7829
05:21:49,280 --> 05:21:53,280
to your browser's network tab and here
7830
05:21:53,280 --> 05:21:55,040
again let's refresh
7831
05:21:55,040 --> 05:21:58,240
and you'll see that this is our response
7832
05:21:58,240 --> 05:22:01,680
react tabs project so again the full url
7833
05:22:01,680 --> 05:22:05,920
is course api.com and then react tabs
7834
05:22:05,920 --> 05:22:07,520
project
7835
05:22:07,520 --> 05:22:10,558
and if we take a closer look we can see
7836
05:22:10,558 --> 05:22:12,320
that this is the response that we're
7837
05:22:12,320 --> 05:22:15,440
sending so we're sending back a
7838
05:22:15,440 --> 05:22:18,558
json data and i can clearly see that in
7839
05:22:18,558 --> 05:22:21,520
my headers so in response headers as you
7840
05:22:21,520 --> 05:22:25,680
can see content type is application json
7841
05:22:25,680 --> 05:22:29,600
now where is this data used if you took
7842
05:22:29,600 --> 05:22:32,958
either my react course or the vanilla
7843
05:22:32,958 --> 05:22:35,200
javascript course you know that in the
7844
05:22:35,200 --> 05:22:36,160
course
7845
05:22:36,160 --> 05:22:39,120
we build quite a few projects where we
7846
05:22:39,120 --> 05:22:42,000
practice data fetching either using
7847
05:22:42,000 --> 05:22:45,680
vanilla jazz or react and in some
7848
05:22:45,680 --> 05:22:49,040
examples we use external apis and in sum
7849
05:22:49,040 --> 05:22:52,958
we use the apis that are here in the
7850
05:22:52,958 --> 05:22:56,000
course api so for example with this
7851
05:22:56,000 --> 05:22:59,360
react tabs project if you navigate to
7852
05:22:59,360 --> 05:23:02,638
react project nutlify.app
7853
05:23:02,638 --> 05:23:05,440
and keep on scrolling keep on scrolling
7854
05:23:05,440 --> 05:23:08,638
you'll hit the tabs project and here
7855
05:23:08,638 --> 05:23:11,040
this is the app that we build
7856
05:23:11,040 --> 05:23:14,798
using the data so again on a server
7857
05:23:14,798 --> 05:23:18,240
i set up my data i set up the api and i
7858
05:23:18,240 --> 05:23:21,680
share the data so i create the http
7859
05:23:21,680 --> 05:23:24,718
interface and then the frontend app just
7860
05:23:24,718 --> 05:23:27,360
simply grabs this data and again if you
7861
05:23:27,360 --> 05:23:29,440
want you can check it out here if you
7862
05:23:29,440 --> 05:23:30,480
refresh
7863
05:23:30,480 --> 05:23:33,200
notice this request request goes to
7864
05:23:33,200 --> 05:23:37,040
course api react tabs project so we grab
7865
05:23:37,040 --> 05:23:40,080
the data on a front end and then we set
7866
05:23:40,080 --> 05:23:43,120
up the functionality as well as user
7867
05:23:43,120 --> 05:23:45,760
interface hopefully this is clear from
7868
05:23:45,760 --> 05:23:48,240
now on we are going to be responsible
7869
05:23:48,240 --> 05:23:51,600
for sending back the data so now since
7870
05:23:51,600 --> 05:23:53,760
we're setting up the server
7871
05:23:53,760 --> 05:23:55,680
it's not going to be our responsibility
7872
05:23:55,680 --> 05:23:58,160
to do something with it like we were
7873
05:23:58,160 --> 05:24:01,200
doing on a front-end in this case we are
7874
05:24:01,200 --> 05:24:04,000
responsible for setting up the responses
7875
05:24:04,000 --> 05:24:06,000
so we're going to be setting up apis
7876
05:24:06,000 --> 05:24:09,840
that our http interfaces to interact
7877
05:24:09,840 --> 05:24:11,680
with our data
7878
05:24:11,680 --> 05:24:14,240
not bad not bad i think we clearly
7879
05:24:14,240 --> 05:24:17,040
covered our two options so one is
7880
05:24:17,040 --> 05:24:19,360
sending back the json data and the
7881
05:24:19,360 --> 05:24:21,840
second one is server set
7882
05:24:21,840 --> 05:24:23,760
so why don't we start by covering the
7883
05:24:23,760 --> 05:24:25,360
most basic
7884
05:24:25,360 --> 05:24:27,440
json response
7885
05:24:27,440 --> 05:24:30,718
and here's what i'm trying to mimic
7886
05:24:30,718 --> 05:24:33,680
if you go to course api
7887
05:24:33,680 --> 05:24:35,760
and then if you look for the tours
7888
05:24:35,760 --> 05:24:36,718
project
7889
05:24:36,718 --> 05:24:39,280
you'll hook the url of course and here
7890
05:24:39,280 --> 05:24:41,680
you can see that we're getting back a
7891
05:24:41,680 --> 05:24:45,120
json data it is an array and each object
7892
05:24:45,120 --> 05:24:48,558
represents a tor and if you took my
7893
05:24:48,558 --> 05:24:50,080
react course you know that we were
7894
05:24:50,080 --> 05:24:52,638
practicing data fetching in react
7895
05:24:52,638 --> 05:24:56,240
and effectively we hit this url
7896
05:24:56,240 --> 05:24:58,958
and we got the data we got our json data
7897
05:24:58,958 --> 05:25:01,840
back and then we built an app
7898
05:25:01,840 --> 05:25:03,440
using that data
7899
05:25:03,440 --> 05:25:05,360
but again the whole point here is
7900
05:25:05,360 --> 05:25:07,840
following where it doesn't have to be
7901
05:25:07,840 --> 05:25:09,280
react
7902
05:25:09,280 --> 05:25:10,320
it can be
7903
05:25:10,320 --> 05:25:13,120
vanilla javascript application it can be
7904
05:25:13,120 --> 05:25:15,760
swelled framework it can be
7905
05:25:15,760 --> 05:25:19,040
any setup where you're able to fetch
7906
05:25:19,040 --> 05:25:20,240
that data
7907
05:25:20,240 --> 05:25:23,440
so in here on the server we just share
7908
05:25:23,440 --> 05:25:24,878
the json
7909
05:25:24,878 --> 05:25:27,760
so in this case of course that is stores
7910
05:25:27,760 --> 05:25:29,760
and then anywhere
7911
05:25:29,760 --> 05:25:32,000
anyone who wants to access this data
7912
05:25:32,000 --> 05:25:34,638
they can just access it and build
7913
05:25:34,638 --> 05:25:36,878
something using that data and that's why
7914
05:25:36,878 --> 05:25:40,240
it is so so powerful
7915
05:25:40,240 --> 05:25:41,120
and
7916
05:25:41,120 --> 05:25:42,558
essentially this is what we're going to
7917
05:25:42,558 --> 05:25:44,958
do we're just going to send back first
7918
05:25:44,958 --> 05:25:48,160
a most basic array and then we'll see
7919
05:25:48,160 --> 05:25:50,958
how we can make this more dynamic please
7920
05:25:50,958 --> 05:25:53,520
keep in mind two things first of course
7921
05:25:53,520 --> 05:25:56,160
eventually we'll use database for that
7922
05:25:56,160 --> 05:25:57,520
and of course i will mention that
7923
05:25:57,520 --> 05:26:00,080
probably 20 000 times as we're building
7924
05:26:00,080 --> 05:26:01,520
these examples
7925
05:26:01,520 --> 05:26:03,200
and the second thing that i would like
7926
05:26:03,200 --> 05:26:06,718
you to understand that when it comes to
7927
05:26:06,718 --> 05:26:09,600
express basics so essentially how we set
7928
05:26:09,600 --> 05:26:10,958
up the server
7929
05:26:10,958 --> 05:26:12,798
it doesn't really matter
7930
05:26:12,798 --> 05:26:14,558
whether we're using server side or
7931
05:26:14,558 --> 05:26:16,798
whether we're using json again
7932
05:26:16,798 --> 05:26:18,400
if we understand the principles we'll
7933
05:26:18,400 --> 05:26:20,320
have no problem
7934
05:26:20,320 --> 05:26:23,760
using any of those options so first what
7935
05:26:23,760 --> 05:26:26,400
i would want is to navigate back to my
7936
05:26:26,400 --> 05:26:28,798
project and again this is going to be
7937
05:26:28,798 --> 05:26:30,798
the time where i do write everything
7938
05:26:30,798 --> 05:26:33,040
from scratch but then starting with next
7939
05:26:33,040 --> 05:26:37,040
example i'll just use some options that
7940
05:26:37,040 --> 05:26:39,840
we already covered so for now
7941
05:26:39,840 --> 05:26:41,440
yes i'll remove everything and again
7942
05:26:41,440 --> 05:26:43,040
we'll set up express
7943
05:26:43,040 --> 05:26:45,120
one last time so we're gonna go with
7944
05:26:45,120 --> 05:26:47,200
express require
7945
05:26:47,200 --> 05:26:50,718
and express okay awesome then const app
7946
05:26:50,718 --> 05:26:52,958
is equal to require or i'm sorry not
7947
05:26:52,958 --> 05:26:56,160
require we need to invoke the express
7948
05:26:56,160 --> 05:26:58,160
like so and then let's go with
7949
05:26:58,160 --> 05:26:59,680
app.listen
7950
05:26:59,680 --> 05:27:01,600
port 5000
7951
05:27:01,600 --> 05:27:03,600
and then let's call our callback
7952
05:27:03,600 --> 05:27:04,958
function
7953
05:27:04,958 --> 05:27:07,200
and then let's go again with console.log
7954
05:27:07,200 --> 05:27:08,878
and server
7955
05:27:08,878 --> 05:27:10,878
is listening
7956
05:27:10,878 --> 05:27:12,878
on port
7957
05:27:12,878 --> 05:27:16,878
on port and of course the port number is
7958
05:27:16,878 --> 05:27:20,000
5000. then again i want to set up
7959
05:27:20,000 --> 05:27:23,600
app.get because that is the http method
7960
05:27:23,600 --> 05:27:26,798
that all browsers perform by default so
7961
05:27:26,798 --> 05:27:29,440
i'm going to go with app.get and i'm not
7962
05:27:29,440 --> 05:27:31,200
going to go with specific path just keep
7963
05:27:31,200 --> 05:27:33,120
in mind that you can and of course we'll
7964
05:27:33,120 --> 05:27:35,040
do that later for example i can go here
7965
05:27:35,040 --> 05:27:36,798
with api
7966
05:27:36,798 --> 05:27:38,798
and then i don't know i can call this
7967
05:27:38,798 --> 05:27:41,120
product or whatever for time being let's
7968
05:27:41,120 --> 05:27:43,040
just make it simple and we'll just
7969
05:27:43,040 --> 05:27:45,360
handle all the root requests so that way
7970
05:27:45,360 --> 05:27:47,200
we can save a little bit of typing in
7971
05:27:47,200 --> 05:27:50,080
the browser and again we have our
7972
05:27:50,080 --> 05:27:52,878
function our callback function rack and
7973
05:27:52,878 --> 05:27:53,840
res
7974
05:27:53,840 --> 05:27:55,600
and as far as the response we're going
7975
05:27:55,600 --> 05:27:57,440
to go with res
7976
05:27:57,440 --> 05:28:01,200
and then the method name is json
7977
05:28:01,200 --> 05:28:02,240
now
7978
05:28:02,240 --> 05:28:04,400
we will go to the docs just so you
7979
05:28:04,400 --> 05:28:06,320
understand where i'm getting this
7980
05:28:06,320 --> 05:28:07,840
information from
7981
05:28:07,840 --> 05:28:10,320
but before we do that let me just tell
7982
05:28:10,320 --> 05:28:13,280
you that i might omit here and there the
7983
05:28:13,280 --> 05:28:16,160
status one just so we can save a little
7984
05:28:16,160 --> 05:28:18,480
bit of typing but don't worry once we go
7985
05:28:18,480 --> 05:28:21,440
to some more serious examples of course
7986
05:28:21,440 --> 05:28:24,320
we'll still use the statuses because in
7987
05:28:24,320 --> 05:28:26,400
my opinion that is just a better
7988
05:28:26,400 --> 05:28:28,958
approach where we actually have control
7989
05:28:28,958 --> 05:28:30,878
over the status
7990
05:28:30,878 --> 05:28:33,280
and as far as documentation if we go
7991
05:28:33,280 --> 05:28:34,878
back to express documentation we're
7992
05:28:34,878 --> 05:28:37,520
looking for api reference again we are
7993
05:28:37,520 --> 05:28:38,480
using
7994
05:28:38,480 --> 05:28:41,520
four point something so we are using
7995
05:28:41,520 --> 05:28:44,000
version four so make sure you look for
7996
05:28:44,000 --> 05:28:46,240
the same one and don't confuse with
7997
05:28:46,240 --> 05:28:49,360
express json so this is going to be a
7998
05:28:49,360 --> 05:28:51,760
middleware that we pass in effectively
7999
05:28:51,760 --> 05:28:54,878
we're looking for response in a docs and
8000
05:28:54,878 --> 05:28:57,760
then we're looking for this json option
8001
05:28:57,760 --> 05:29:02,480
so res object as the json method and
8002
05:29:02,480 --> 05:29:05,280
what happens we send back the json
8003
05:29:05,280 --> 05:29:07,680
response and this method sends a
8004
05:29:07,680 --> 05:29:10,958
response with correct content type that
8005
05:29:10,958 --> 05:29:13,280
is a parameter converted to a json
8006
05:29:13,280 --> 05:29:16,638
string using json stringify this
8007
05:29:16,638 --> 05:29:19,120
parameter can be any json type including
8008
05:29:19,120 --> 05:29:22,080
object array string and blah blah blah
8009
05:29:22,080 --> 05:29:24,080
so hopefully you get the gist so the
8010
05:29:24,080 --> 05:29:26,798
only thing we need to do is go back and
8011
05:29:26,798 --> 05:29:30,718
for example i'll provide an array where
8012
05:29:30,718 --> 05:29:32,798
there's going to be two objects and
8013
05:29:32,798 --> 05:29:35,440
first one will be named john
8014
05:29:35,440 --> 05:29:38,320
and the second one will be susan so
8015
05:29:38,320 --> 05:29:39,920
another object
8016
05:29:39,920 --> 05:29:41,600
and we'll say name
8017
05:29:41,600 --> 05:29:45,440
is equal to susan and we have our most
8018
05:29:45,440 --> 05:29:46,878
basic api
8019
05:29:46,878 --> 05:29:50,638
so if i go back to my localhost 5000 and
8020
05:29:50,638 --> 05:29:51,840
refresh
8021
05:29:51,840 --> 05:29:55,200
there it is so now anywhere in the world
8022
05:29:55,200 --> 05:29:57,200
i can access this data
8023
05:29:57,200 --> 05:29:59,680
and build something using this data now
8024
05:29:59,680 --> 05:30:02,400
there's tiny caveat when i say anywhere
8025
05:30:02,400 --> 05:30:04,480
in the world there's still going to be
8026
05:30:04,480 --> 05:30:07,280
at the moment a course error and i'll
8027
05:30:07,280 --> 05:30:09,280
cover that once we actually cover
8028
05:30:09,280 --> 05:30:11,600
middleware so again please please please
8029
05:30:11,600 --> 05:30:14,558
please be patient we will get there
8030
05:30:14,558 --> 05:30:17,200
but hopefully you get the gist where
8031
05:30:17,200 --> 05:30:20,958
this serves our data and now we can just
8032
05:30:20,958 --> 05:30:23,440
access that data and build
8033
05:30:23,440 --> 05:30:26,400
the front-end app using this data now
8034
05:30:26,400 --> 05:30:28,638
this default setup is nice
8035
05:30:28,638 --> 05:30:30,638
but of course we can do something more
8036
05:30:30,638 --> 05:30:31,920
meaningful
8037
05:30:31,920 --> 05:30:32,878
where
8038
05:30:32,878 --> 05:30:35,200
if you take a look at the folder you'll
8039
05:30:35,200 --> 05:30:38,958
notice this data js now what is data.js
8040
05:30:38,958 --> 05:30:43,680
it is simply a file with some arrays
8041
05:30:43,680 --> 05:30:45,600
and the first array is going to be
8042
05:30:45,600 --> 05:30:48,160
products and here as you can see i have
8043
05:30:48,160 --> 05:30:50,400
objects and then each of these objects
8044
05:30:50,400 --> 05:30:52,080
represents a
8045
05:30:52,080 --> 05:30:54,798
single product and all the way in the
8046
05:30:54,798 --> 05:30:57,280
bottom we're exporting both arrays we're
8047
05:30:57,280 --> 05:30:58,878
exporting products
8048
05:30:58,878 --> 05:31:00,878
as well as the people array that we're
8049
05:31:00,878 --> 05:31:03,440
going to cover a bit later so now what i
8050
05:31:03,440 --> 05:31:07,440
would want to do is import product array
8051
05:31:07,440 --> 05:31:09,440
in my address
8052
05:31:09,440 --> 05:31:11,680
and then instead of hard coding this
8053
05:31:11,680 --> 05:31:14,878
value like so where i just pass in the
8054
05:31:14,878 --> 05:31:17,920
array with two objects i'll just dump
8055
05:31:17,920 --> 05:31:20,558
the whole products data so let's go with
8056
05:31:20,558 --> 05:31:23,200
const we already know how to do that now
8057
05:31:23,200 --> 05:31:24,798
since i'm exporting multiple things
8058
05:31:24,798 --> 05:31:28,160
since i'm exporting an object i will be
8059
05:31:28,160 --> 05:31:30,240
explicit of what i would want so i'm
8060
05:31:30,240 --> 05:31:31,280
going to go with
8061
05:31:31,280 --> 05:31:32,480
product
8062
05:31:32,480 --> 05:31:34,718
and basically i'm destructing right away
8063
05:31:34,718 --> 05:31:37,520
i'm going say product is equal to
8064
05:31:37,520 --> 05:31:39,840
require and remember
8065
05:31:39,840 --> 05:31:43,440
the file is data js like so
8066
05:31:43,440 --> 05:31:45,840
and then instead of passing array
8067
05:31:45,840 --> 05:31:49,360
directly i can simply go with product
8068
05:31:49,360 --> 05:31:52,080
and what do you know when you navigate
8069
05:31:52,080 --> 05:31:55,600
to the root there it is now we have more
8070
05:31:55,600 --> 05:31:58,718
meaningful json response where actually
8071
05:31:58,718 --> 05:32:02,718
this is a product data so on my front
8072
05:32:02,718 --> 05:32:05,760
end i can grab it and i can build some
8073
05:32:05,760 --> 05:32:09,200
kind of nice front end using this data
8074
05:32:09,200 --> 05:32:10,958
now lastly before we go let me just
8075
05:32:10,958 --> 05:32:13,040
showcase that of course there's going to
8076
05:32:13,040 --> 05:32:15,520
be a content type set up correctly as
8077
05:32:15,520 --> 05:32:18,080
well so if you go to network tab and
8078
05:32:18,080 --> 05:32:21,360
again if you refresh localhost like so
8079
05:32:21,360 --> 05:32:24,638
and take a look now of course what is
8080
05:32:24,638 --> 05:32:26,000
the content type
8081
05:32:26,000 --> 05:32:28,798
that is application json so now of
8082
05:32:28,798 --> 05:32:31,840
course we're correctly getting our json
8083
05:32:31,840 --> 05:32:34,958
data so hopefully you have a clear
8084
05:32:34,958 --> 05:32:38,000
understanding how to sound json data so
8085
05:32:38,000 --> 05:32:39,360
now of course let's make things a bit
8086
05:32:39,360 --> 05:32:41,520
more interesting and cover some more
8087
05:32:41,520 --> 05:32:44,638
advanced express topics nice once we're
8088
05:32:44,638 --> 05:32:48,320
familiar with the most basic json setup
8089
05:32:48,320 --> 05:32:52,080
now let's build a more meaningful api
8090
05:32:52,080 --> 05:32:54,638
and in a process we'll cover
8091
05:32:54,638 --> 05:32:58,480
route parameters as well as query string
8092
05:32:58,480 --> 05:33:01,200
parameters and i would want to start in
8093
05:33:01,200 --> 05:33:02,558
following way
8094
05:33:02,558 --> 05:33:05,280
where essentially i'll leave all this
8095
05:33:05,280 --> 05:33:08,000
code but when it comes to my route when
8096
05:33:08,000 --> 05:33:09,760
it comes to my home bridge i'll
8097
05:33:09,760 --> 05:33:12,878
essentially send back the heading 1
8098
05:33:12,878 --> 05:33:16,480
with the link as well and that link will
8099
05:33:16,480 --> 05:33:20,638
direct a user to a forward slash api and
8100
05:33:20,638 --> 05:33:21,920
then product
8101
05:33:21,920 --> 05:33:24,878
and then later we'll cover the params as
8102
05:33:24,878 --> 05:33:27,520
well as the query string so let's start
8103
05:33:27,520 --> 05:33:30,160
with sending back html instead so i'm
8104
05:33:30,160 --> 05:33:32,798
not going with json one and i'm not
8105
05:33:32,798 --> 05:33:34,878
gonna send back the file i'll go
8106
05:33:34,878 --> 05:33:37,520
directly and i'll say send and here we
8107
05:33:37,520 --> 05:33:38,958
have the string of course so i'm gonna
8108
05:33:38,958 --> 05:33:40,480
go with heading one
8109
05:33:40,480 --> 05:33:42,480
and then home
8110
05:33:42,480 --> 05:33:43,520
page
8111
05:33:43,520 --> 05:33:46,000
then i'll make sure that i close
8112
05:33:46,000 --> 05:33:47,680
because i believe in one of the previous
8113
05:33:47,680 --> 05:33:48,718
videos
8114
05:33:48,718 --> 05:33:51,360
i believe it was the express
8115
05:33:51,360 --> 05:33:52,480
basics
8116
05:33:52,480 --> 05:33:54,558
i think i messed it up here notice i
8117
05:33:54,558 --> 05:33:57,600
didn't add the closing one so now
8118
05:33:57,600 --> 05:33:59,520
everything is correct and now i'm gonna
8119
05:33:59,520 --> 05:34:01,120
go with my link
8120
05:34:01,120 --> 05:34:03,760
then as far as the href i'm gonna direct
8121
05:34:03,760 --> 05:34:05,840
a user to api
8122
05:34:05,840 --> 05:34:09,600
and then forward slash
8123
05:34:09,840 --> 05:34:12,080
and then of course
8124
05:34:12,080 --> 05:34:15,360
i'll also set up a text here so products
8125
05:34:15,360 --> 05:34:18,480
and i'll close my link so that should do
8126
05:34:18,480 --> 05:34:21,760
it that is my home page and of course if
8127
05:34:21,760 --> 05:34:24,558
i go to localhost 5000 there it is i
8128
05:34:24,558 --> 05:34:26,480
have my homepage and once i click the
8129
05:34:26,480 --> 05:34:29,920
link i navigate to api products now of
8130
05:34:29,920 --> 05:34:32,718
course the moment i have the
8131
05:34:32,718 --> 05:34:35,840
default response from the express
8132
05:34:35,840 --> 05:34:39,120
meaning the default 404 response where
8133
05:34:39,120 --> 05:34:42,000
the resource cannot be found and as i
8134
05:34:42,000 --> 05:34:43,680
said note if you want to take a look
8135
05:34:43,680 --> 05:34:45,760
what response do they send again go to
8136
05:34:45,760 --> 05:34:47,360
the network this is going to be very
8137
05:34:47,360 --> 05:34:50,718
very useful tab refresh and take a look
8138
05:34:50,718 --> 05:34:53,680
at their response so notice the response
8139
05:34:53,680 --> 05:34:57,360
that's the html that they send back so
8140
05:34:57,360 --> 05:35:00,240
cannot get and then api products now we
8141
05:35:00,240 --> 05:35:01,360
already know
8142
05:35:01,360 --> 05:35:04,320
why there is this get the http verb
8143
05:35:04,320 --> 05:35:06,400
because that's the method that the
8144
05:35:06,400 --> 05:35:08,958
browser performs okay awesome so now of
8145
05:35:08,958 --> 05:35:10,878
course our job
8146
05:35:10,878 --> 05:35:12,638
is to set up a
8147
05:35:12,638 --> 05:35:14,320
get request
8148
05:35:14,320 --> 05:35:18,558
for this resource so we go with app.get
8149
05:35:18,558 --> 05:35:22,240
and i specifically want to handle api
8150
05:35:22,240 --> 05:35:24,878
and product so this one just looks a
8151
05:35:24,878 --> 05:35:25,840
little bit
8152
05:35:25,840 --> 05:35:28,958
more realistic so in here i go with api
8153
05:35:28,958 --> 05:35:31,520
and products and keep in mind of course
8154
05:35:31,520 --> 05:35:33,440
those values need to match otherwise
8155
05:35:33,440 --> 05:35:36,000
you'll get that 404 again i have my
8156
05:35:36,000 --> 05:35:37,280
callback function
8157
05:35:37,280 --> 05:35:39,200
and now of course i would want to send
8158
05:35:39,200 --> 05:35:41,440
back the product however in this case
8159
05:35:41,440 --> 05:35:43,120
we'll make it a little bit different
8160
05:35:43,120 --> 05:35:44,480
where previously
8161
05:35:44,480 --> 05:35:47,200
notice the product we send back pretty
8162
05:35:47,200 --> 05:35:49,680
much the whole thing right but
8163
05:35:49,680 --> 05:35:52,400
a more realistic approach is following
8164
05:35:52,400 --> 05:35:55,120
where when you're requesting a bunch of
8165
05:35:55,120 --> 05:35:57,120
data so when you're requesting
8166
05:35:57,120 --> 05:35:59,760
collection of the data you're not always
8167
05:35:59,760 --> 05:36:02,160
returning everything for that one
8168
05:36:02,160 --> 05:36:05,120
specific product and to give you a real
8169
05:36:05,120 --> 05:36:07,440
world example again i'm gonna go back to
8170
05:36:07,440 --> 05:36:09,440
my react projects and i'm simply using
8171
05:36:09,440 --> 05:36:11,840
them because this gives you a visual
8172
05:36:11,840 --> 05:36:14,160
representation during the course we
8173
05:36:14,160 --> 05:36:17,360
built a e-commerce and then in the
8174
05:36:17,360 --> 05:36:19,840
products page notice how we're fetching
8175
05:36:19,840 --> 05:36:21,120
the products
8176
05:36:21,120 --> 05:36:23,520
but you need to keep in mind one thing
8177
05:36:23,520 --> 05:36:26,160
where we're only fetching about the
8178
05:36:26,160 --> 05:36:29,600
product the title the price the image
8179
05:36:29,600 --> 05:36:32,558
and i believe also the id and only when
8180
05:36:32,558 --> 05:36:35,280
i go to that single product page
8181
05:36:35,280 --> 05:36:37,840
this is where i get the rest of the data
8182
05:36:37,840 --> 05:36:38,718
whether
8183
05:36:38,718 --> 05:36:42,798
that is for example a stars the reviews
8184
05:36:42,798 --> 05:36:44,480
the description
8185
05:36:44,480 --> 05:36:46,718
the availability and all that stuff so
8186
05:36:46,718 --> 05:36:48,400
what i'm trying to say is when you're
8187
05:36:48,400 --> 05:36:50,798
requesting a collection
8188
05:36:50,798 --> 05:36:53,120
of data there's going to be cases where
8189
05:36:53,120 --> 05:36:56,320
i simply want to send back some minimal
8190
05:36:56,320 --> 05:36:58,718
response so for example again that could
8191
05:36:58,718 --> 05:37:01,680
be name image and the pricing scenario
8192
05:37:01,680 --> 05:37:04,798
and only if i look for this specific
8193
05:37:04,798 --> 05:37:05,760
product
8194
05:37:05,760 --> 05:37:08,480
then i send back everything
8195
05:37:08,480 --> 05:37:10,558
and in order to mimic that in our
8196
05:37:10,558 --> 05:37:11,840
example
8197
05:37:11,840 --> 05:37:13,920
in this response
8198
05:37:13,920 --> 05:37:16,160
what i'm going to do is i'm just going
8199
05:37:16,160 --> 05:37:18,638
to iterate over my product i'll use the
8200
05:37:18,638 --> 05:37:22,400
map method and i'll just remove
8201
05:37:22,400 --> 05:37:25,120
my description so when i'm sending back
8202
05:37:25,120 --> 05:37:27,280
the product it's going to be without a
8203
05:37:27,280 --> 05:37:30,400
description so again if we were to send
8204
05:37:30,400 --> 05:37:32,558
back everything i would simply go with
8205
05:37:32,558 --> 05:37:35,120
response json and then i pass in the
8206
05:37:35,120 --> 05:37:38,160
product so again if we go here and if we
8207
05:37:38,160 --> 05:37:40,400
refresh now everything is cool
8208
05:37:40,400 --> 05:37:43,120
i'm getting my product on the api
8209
05:37:43,120 --> 05:37:45,440
product route however
8210
05:37:45,440 --> 05:37:47,200
i'm going to make it a bit more
8211
05:37:47,200 --> 05:37:48,958
realistic and we're going to go with
8212
05:37:48,958 --> 05:37:50,080
const
8213
05:37:50,080 --> 05:37:52,718
new product is equal to
8214
05:37:52,718 --> 05:37:55,120
product dot map so i'm mapping over and
8215
05:37:55,120 --> 05:37:57,200
i'm creating a new array and i'll
8216
05:37:57,200 --> 05:38:00,480
reference each item as a product and
8217
05:38:00,480 --> 05:38:03,360
then as far as the return well first i
8218
05:38:03,360 --> 05:38:06,558
want to structure the properties out of
8219
05:38:06,558 --> 05:38:08,958
the product and the reason why i can
8220
05:38:08,958 --> 05:38:11,200
access them is because those are the
8221
05:38:11,200 --> 05:38:13,520
names that i used when i'm setting it up
8222
05:38:13,520 --> 05:38:17,360
so id name image and all that and that
8223
05:38:17,360 --> 05:38:19,360
is equal to the product again i'm just
8224
05:38:19,360 --> 05:38:22,400
using simple javascript structuring and
8225
05:38:22,400 --> 05:38:24,160
then as far as the return whatever i'm
8226
05:38:24,160 --> 05:38:25,360
going to return
8227
05:38:25,360 --> 05:38:26,320
from
8228
05:38:26,320 --> 05:38:27,920
my function
8229
05:38:27,920 --> 05:38:29,600
well that is going to be the new value
8230
05:38:29,600 --> 05:38:32,000
so now i'm going to go with id name as
8231
05:38:32,000 --> 05:38:34,638
well as the image and once i have this
8232
05:38:34,638 --> 05:38:36,480
setup in place instead of returning
8233
05:38:36,480 --> 05:38:37,680
product
8234
05:38:37,680 --> 05:38:39,920
now of course i'm going to go with new
8235
05:38:39,920 --> 05:38:41,040
products
8236
05:38:41,040 --> 05:38:44,160
and if i go back to my browser there it
8237
05:38:44,160 --> 05:38:47,440
is so now i'm getting a collection
8238
05:38:47,440 --> 05:38:49,680
but i'm not sending
8239
05:38:49,680 --> 05:38:52,080
everything that i know about this
8240
05:38:52,080 --> 05:38:54,878
specific item so i'm being selective of
8241
05:38:54,878 --> 05:38:57,200
what i'm sending back so yes
8242
05:38:57,200 --> 05:38:59,440
there is a resource by the name of api
8243
05:38:59,440 --> 05:39:02,240
product and we're sending back the array
8244
05:39:02,240 --> 05:39:04,798
but we know that if
8245
05:39:04,798 --> 05:39:06,240
the user
8246
05:39:06,240 --> 05:39:08,558
wants to access that description he or
8247
05:39:08,558 --> 05:39:10,400
she will have to look for a specific
8248
05:39:10,400 --> 05:39:13,600
product and in the process we'll cover
8249
05:39:13,600 --> 05:39:17,280
what are the url parameters all right so
8250
05:39:17,280 --> 05:39:20,080
we're successfully sharing a list of
8251
05:39:20,080 --> 05:39:22,400
items so now of course let's take a look
8252
05:39:22,400 --> 05:39:24,400
at how we can provide
8253
05:39:24,400 --> 05:39:25,360
info
8254
05:39:25,360 --> 05:39:28,798
about that one specific product so for
8255
05:39:28,798 --> 05:39:31,760
example if i navigate to
8256
05:39:31,760 --> 05:39:34,558
product and then forward slash and one
8257
05:39:34,558 --> 05:39:36,958
and then i'm going to get only the info
8258
05:39:36,958 --> 05:39:39,600
about this first product because it has
8259
05:39:39,600 --> 05:39:41,040
the id of one
8260
05:39:41,040 --> 05:39:42,000
and
8261
05:39:42,000 --> 05:39:43,920
not only i'll get
8262
05:39:43,920 --> 05:39:46,958
these three properties but also get the
8263
05:39:46,958 --> 05:39:48,160
description
8264
05:39:48,160 --> 05:39:50,878
and our initial approach would go
8265
05:39:50,878 --> 05:39:52,240
something like this
8266
05:39:52,240 --> 05:39:54,080
where yes i see the product okay
8267
05:39:54,080 --> 05:39:55,520
beautiful
8268
05:39:55,520 --> 05:39:56,958
and we already know how to set up the
8269
05:39:56,958 --> 05:39:59,120
route now since i'll delete it
8270
05:39:59,120 --> 05:40:01,200
eventually anyway i'm just going to copy
8271
05:40:01,200 --> 05:40:03,280
and paste so we can speed this up so i'm
8272
05:40:03,280 --> 05:40:05,280
going to go with api products and then
8273
05:40:05,280 --> 05:40:06,798
forward slash one
8274
05:40:06,798 --> 05:40:08,558
that just means that i'm going to be
8275
05:40:08,558 --> 05:40:09,680
looking for
8276
05:40:09,680 --> 05:40:12,798
item number one in here
8277
05:40:12,798 --> 05:40:15,520
and then instead of map we'll use the
8278
05:40:15,520 --> 05:40:16,718
find method
8279
05:40:16,718 --> 05:40:18,958
so i'm importing all my products and in
8280
05:40:18,958 --> 05:40:21,200
order to get the single product
8281
05:40:21,200 --> 05:40:23,760
i'm going to write the following code
8282
05:40:23,760 --> 05:40:25,760
where we're going to go with const
8283
05:40:25,760 --> 05:40:27,120
single
8284
05:40:27,120 --> 05:40:29,520
product and don't worry we will reuse
8285
05:40:29,520 --> 05:40:31,040
this code so
8286
05:40:31,040 --> 05:40:33,440
it's not like we're just randomly typing
8287
05:40:33,440 --> 05:40:36,000
something so let's go with products and
8288
05:40:36,000 --> 05:40:38,240
then find now again
8289
05:40:38,240 --> 05:40:40,480
i'll reference each product
8290
05:40:40,480 --> 05:40:43,040
with a parameter of product
8291
05:40:43,040 --> 05:40:45,920
in my callback function and then if the
8292
05:40:45,920 --> 05:40:47,680
product id
8293
05:40:47,680 --> 05:40:49,120
product id
8294
05:40:49,120 --> 05:40:50,958
matches one
8295
05:40:50,958 --> 05:40:54,160
because that is my route here
8296
05:40:54,160 --> 05:40:56,240
then of course i'll return that single
8297
05:40:56,240 --> 05:40:58,558
product so i'm going to say single
8298
05:40:58,558 --> 05:41:01,360
product and technically this works
8299
05:41:01,360 --> 05:41:05,120
so notice now of course i have only info
8300
05:41:05,120 --> 05:41:08,320
about this one specific product and i
8301
05:41:08,320 --> 05:41:10,958
have the description however it feels
8302
05:41:10,958 --> 05:41:14,240
like using bazooka on a cockroach yeah
8303
05:41:14,240 --> 05:41:16,320
it gets the job done but
8304
05:41:16,320 --> 05:41:19,200
probably is an overkill
8305
05:41:19,200 --> 05:41:21,680
because keep in mind at the moment we
8306
05:41:21,680 --> 05:41:22,558
have
8307
05:41:22,558 --> 05:41:25,680
only four products so yes
8308
05:41:25,680 --> 05:41:28,480
if i really wanted to i could set up
8309
05:41:28,480 --> 05:41:30,240
four routes
8310
05:41:30,240 --> 05:41:32,240
but what if i have hundred two hundred
8311
05:41:32,240 --> 05:41:33,280
three hundred
8312
05:41:33,280 --> 05:41:35,680
well that's not gonna work and in
8313
05:41:35,680 --> 05:41:37,840
express we have something called route
8314
05:41:37,840 --> 05:41:40,000
parameters which essentially is going to
8315
05:41:40,000 --> 05:41:42,320
be way better solution
8316
05:41:42,320 --> 05:41:46,638
so instead of hard coding this 1 2 3 or
8317
05:41:46,638 --> 05:41:50,240
whatever id we want we set up a route
8318
05:41:50,240 --> 05:41:52,000
parameter and it's going to look
8319
05:41:52,000 --> 05:41:54,160
something like this where i go with my
8320
05:41:54,160 --> 05:41:56,160
route and then i have forward slash and
8321
05:41:56,160 --> 05:41:57,840
then i go with colon
8322
05:41:57,840 --> 05:41:59,040
and then
8323
05:41:59,040 --> 05:42:01,920
whatever name out one so think of this
8324
05:42:01,920 --> 05:42:04,480
as a placeholder so you can call this
8325
05:42:04,480 --> 05:42:06,080
bobby lee you can call this chicken
8326
05:42:06,080 --> 05:42:09,120
burrito in my case i'm gonna go with
8327
05:42:09,120 --> 05:42:10,400
product
8328
05:42:10,400 --> 05:42:12,638
and the id
8329
05:42:12,638 --> 05:42:14,798
just to be explicit
8330
05:42:14,798 --> 05:42:17,680
of what i'm expecting over here but
8331
05:42:17,680 --> 05:42:18,480
again
8332
05:42:18,480 --> 05:42:21,200
naming here is really up to you
8333
05:42:21,200 --> 05:42:23,680
and what is more important is the fact
8334
05:42:23,680 --> 05:42:26,320
how we can access it now if you want you
8335
05:42:26,320 --> 05:42:28,638
can actually console log the request
8336
05:42:28,638 --> 05:42:30,718
object again this is just going to be a
8337
05:42:30,718 --> 05:42:32,958
giant object and for now i'm just going
8338
05:42:32,958 --> 05:42:34,878
to leave this one the way it is
8339
05:42:34,878 --> 05:42:37,440
so i'm still going to be returning a
8340
05:42:37,440 --> 05:42:40,080
product number one and also let's look
8341
05:42:40,080 --> 05:42:40,958
for
8342
05:42:40,958 --> 05:42:42,160
request
8343
05:42:42,160 --> 05:42:45,600
and then we're looking for the params
8344
05:42:45,600 --> 05:42:47,760
property so let me count to lock this
8345
05:42:47,760 --> 05:42:50,480
one and then again once i navigate back
8346
05:42:50,480 --> 05:42:54,080
and if i refresh in the api products
8347
05:42:54,080 --> 05:42:55,520
number one
8348
05:42:55,520 --> 05:42:57,840
what you'll notice again this giant
8349
05:42:57,840 --> 05:43:00,240
request object where we have bunch of
8350
05:43:00,240 --> 05:43:02,400
useful properties and methods
8351
05:43:02,400 --> 05:43:04,240
and of course i'm not going to cover all
8352
05:43:04,240 --> 05:43:07,920
of them right now but similar to http of
8353
05:43:07,920 --> 05:43:11,120
course in the express we also have
8354
05:43:11,120 --> 05:43:14,240
access to bunch of useful things in that
8355
05:43:14,240 --> 05:43:17,520
request object and one of them is the
8356
05:43:17,520 --> 05:43:21,040
params so check it out now i have this
8357
05:43:21,040 --> 05:43:24,798
product id and the value is one now
8358
05:43:24,798 --> 05:43:26,958
please keep in mind one thing that
8359
05:43:26,958 --> 05:43:28,958
whatever you're going to be setting up
8360
05:43:28,958 --> 05:43:33,520
here in the url as that route parameter
8361
05:43:33,520 --> 05:43:36,558
is always going to come back as a string
8362
05:43:36,558 --> 05:43:38,400
and this is important in this case
8363
05:43:38,400 --> 05:43:41,600
because if you take a look at our data
8364
05:43:41,600 --> 05:43:44,638
the id is a number and of course we'll
8365
05:43:44,638 --> 05:43:46,718
deal with that in a second
8366
05:43:46,718 --> 05:43:50,000
and whatever we set up as far as our
8367
05:43:50,000 --> 05:43:51,040
name
8368
05:43:51,040 --> 05:43:52,878
is going to be right in here it's going
8369
05:43:52,878 --> 05:43:55,440
to be in this request and then
8370
05:43:55,440 --> 05:43:57,600
parameters object so now of course what
8371
05:43:57,600 --> 05:43:58,638
i could do
8372
05:43:58,638 --> 05:44:01,200
is i could just destructure it from the
8373
05:44:01,200 --> 05:44:02,320
params
8374
05:44:02,320 --> 05:44:05,520
and then use it in order to find that
8375
05:44:05,520 --> 05:44:07,920
specific product so that way i don't
8376
05:44:07,920 --> 05:44:10,320
have to hard code products number one
8377
05:44:10,320 --> 05:44:13,040
product number two and on and on and on
8378
05:44:13,040 --> 05:44:16,558
instead i just set up my route parameter
8379
05:44:16,558 --> 05:44:18,240
and just come up with whatever name i
8380
05:44:18,240 --> 05:44:19,200
would want
8381
05:44:19,200 --> 05:44:20,878
just make sure that you add this colon
8382
05:44:20,878 --> 05:44:21,680
here
8383
05:44:21,680 --> 05:44:24,878
and then we'll access that value and get
8384
05:44:24,878 --> 05:44:27,440
a specific product so i'll leave these
8385
05:44:27,440 --> 05:44:29,440
two suckers for your reference
8386
05:44:29,440 --> 05:44:31,920
just in case you would ever
8387
05:44:31,920 --> 05:44:34,000
want to console log them and we'll
8388
05:44:34,000 --> 05:44:36,958
simply go with const and the name is
8389
05:44:36,958 --> 05:44:38,718
product id
8390
05:44:38,718 --> 05:44:41,840
and that is equal to request
8391
05:44:41,840 --> 05:44:44,958
that's my request object and params
8392
05:44:44,958 --> 05:44:47,760
then i'm going to use this product id to
8393
05:44:47,760 --> 05:44:51,120
get my product but remember
8394
05:44:51,120 --> 05:44:54,240
it is a string we can clearly see that
8395
05:44:54,240 --> 05:44:57,120
and in fact here in the array
8396
05:44:57,120 --> 05:45:00,558
well the ids are numbers so if we'll
8397
05:45:00,558 --> 05:45:02,718
just try to search it the way it is
8398
05:45:02,718 --> 05:45:04,798
of course we won't be able to get our
8399
05:45:04,798 --> 05:45:07,600
product so instead what we want is pass
8400
05:45:07,600 --> 05:45:08,958
in the number
8401
05:45:08,958 --> 05:45:12,480
and then product id now of course if
8402
05:45:12,480 --> 05:45:15,120
your ids are set up as strings
8403
05:45:15,120 --> 05:45:16,400
which is
8404
05:45:16,400 --> 05:45:19,360
somewhat typical setup for the databases
8405
05:45:19,360 --> 05:45:20,400
and all that
8406
05:45:20,400 --> 05:45:22,320
as well as the headless cms is then of
8407
05:45:22,320 --> 05:45:23,920
course you don't need to worry about it
8408
05:45:23,920 --> 05:45:26,400
then you can just pass in the string and
8409
05:45:26,400 --> 05:45:29,440
now of course i'm going to be able to
8410
05:45:29,440 --> 05:45:33,600
get my one product so now if i go back
8411
05:45:33,600 --> 05:45:35,040
and if i refresh
8412
05:45:35,040 --> 05:45:39,040
everything still works and if i go to my
8413
05:45:39,040 --> 05:45:41,360
url and start changing these values
8414
05:45:41,360 --> 05:45:43,520
hopefully you can see that now of course
8415
05:45:43,520 --> 05:45:46,160
i'm getting a different product now i'm
8416
05:45:46,160 --> 05:45:48,480
getting product number two and i go to
8417
05:45:48,480 --> 05:45:50,958
product number three and on and on and
8418
05:45:50,958 --> 05:45:53,760
on so now with these few simple lines of
8419
05:45:53,760 --> 05:45:54,718
code
8420
05:45:54,718 --> 05:45:58,878
i can access any product in my array now
8421
05:45:58,878 --> 05:46:00,798
of course there's also a case where we
8422
05:46:00,798 --> 05:46:03,360
cannot find the product because keep in
8423
05:46:03,360 --> 05:46:04,958
mind here i can type whatever i would
8424
05:46:04,958 --> 05:46:07,600
want so i can go with products and then
8425
05:46:07,600 --> 05:46:11,440
let's imagine that the user types abc
8426
05:46:11,440 --> 05:46:14,160
now do we have a product with an id of
8427
05:46:14,160 --> 05:46:15,200
abc
8428
05:46:15,200 --> 05:46:17,200
of course the answer is no so what's
8429
05:46:17,200 --> 05:46:19,120
happening over here now i don't get
8430
05:46:19,120 --> 05:46:21,520
anything back right well i don't get
8431
05:46:21,520 --> 05:46:24,958
anything back because if i go to log and
8432
05:46:24,958 --> 05:46:25,840
then
8433
05:46:25,840 --> 05:46:28,160
single product let's see what we're
8434
05:46:28,160 --> 05:46:30,240
going to get back again let me refresh
8435
05:46:30,240 --> 05:46:33,600
abc and this one is undefined so that's
8436
05:46:33,600 --> 05:46:36,798
what we're sending back as a single
8437
05:46:36,798 --> 05:46:39,840
product so what would be a solution well
8438
05:46:39,840 --> 05:46:42,400
a solution is setting up a if statement
8439
05:46:42,400 --> 05:46:43,200
here
8440
05:46:43,200 --> 05:46:46,320
where i say get me the single product if
8441
05:46:46,320 --> 05:46:48,480
you cannot find that single product if
8442
05:46:48,480 --> 05:46:51,600
basically that product doesn't exist if
8443
05:46:51,600 --> 05:46:52,958
the id
8444
05:46:52,958 --> 05:46:55,200
that the user passed in does not make
8445
05:46:55,200 --> 05:46:56,160
sense
8446
05:46:56,160 --> 05:46:58,798
you cannot find the product with that id
8447
05:46:58,798 --> 05:47:02,400
then return 404 so how's that going to
8448
05:47:02,400 --> 05:47:05,120
look like well we can go here if and
8449
05:47:05,120 --> 05:47:08,558
then single product so we set up a if
8450
05:47:08,558 --> 05:47:10,558
condition and of course i'm going to go
8451
05:47:10,558 --> 05:47:13,280
with if single product doesn't exist so
8452
05:47:13,280 --> 05:47:15,840
i'm going to add exclamation point and
8453
05:47:15,840 --> 05:47:17,040
of course this is going to be our case
8454
05:47:17,040 --> 05:47:18,480
with undefined
8455
05:47:18,480 --> 05:47:21,440
this will be true if it is undefined and
8456
05:47:21,440 --> 05:47:23,600
then we just go with a return
8457
05:47:23,600 --> 05:47:26,558
res and in this case i will add a status
8458
05:47:26,558 --> 05:47:28,240
because that is extremely important that
8459
05:47:28,240 --> 05:47:32,160
i go with 404 and then we go with send
8460
05:47:32,160 --> 05:47:34,400
and product
8461
05:47:34,400 --> 05:47:35,920
does not
8462
05:47:35,920 --> 05:47:37,200
does not
8463
05:47:37,200 --> 05:47:39,280
exist like so
8464
05:47:39,280 --> 05:47:41,680
now if everything is correct then of
8465
05:47:41,680 --> 05:47:43,840
course we go with res
8466
05:47:43,840 --> 05:47:46,798
and json so in here i say return
8467
05:47:46,798 --> 05:47:51,280
and res dot json so once i go right now
8468
05:47:51,280 --> 05:47:53,840
to product and then abc
8469
05:47:53,840 --> 05:47:55,680
there it is now of course i have proper
8470
05:47:55,680 --> 05:47:56,798
404
8471
05:47:56,798 --> 05:47:59,360
product does not exist and if i again
8472
05:47:59,360 --> 05:48:00,958
take a look at the tab
8473
05:48:00,958 --> 05:48:04,000
and i have my abc now if i'm going to
8474
05:48:04,000 --> 05:48:07,440
navigate to a route where i can find the
8475
05:48:07,440 --> 05:48:10,000
product then of course i'm going to get
8476
05:48:10,000 --> 05:48:13,120
my proper product response so whenever
8477
05:48:13,120 --> 05:48:16,080
you think of route parameters think of
8478
05:48:16,080 --> 05:48:19,680
them as placeholders where user provides
8479
05:48:19,680 --> 05:48:23,040
a data and then using requests and
8480
05:48:23,040 --> 05:48:26,000
params we can access that data and then
8481
05:48:26,000 --> 05:48:28,160
set up some kind of logic
8482
05:48:28,160 --> 05:48:30,718
and before we continue and cover query
8483
05:48:30,718 --> 05:48:33,040
string parameters let me just mention
8484
05:48:33,040 --> 05:48:36,400
that route parameters can get way more
8485
05:48:36,400 --> 05:48:39,920
complex than this so for example imagine
8486
05:48:39,920 --> 05:48:42,878
this scenario i can go with app.get
8487
05:48:42,878 --> 05:48:45,520
again i need to come up with some kind
8488
05:48:45,520 --> 05:48:48,000
of routes i'm going to go with api
8489
05:48:48,000 --> 05:48:50,160
then products so i'm looking for
8490
05:48:50,160 --> 05:48:52,480
specific products so i'm going to use a
8491
05:48:52,480 --> 05:48:55,520
route parameter let's call this product
8492
05:48:55,520 --> 05:48:56,400
id
8493
05:48:56,400 --> 05:48:58,958
then i'm going to look for all the
8494
05:48:58,958 --> 05:49:02,080
reviews and then maybe there's a review
8495
05:49:02,080 --> 05:49:02,958
id
8496
05:49:02,958 --> 05:49:04,160
so review
8497
05:49:04,160 --> 05:49:06,718
and then id and now of course again i
8498
05:49:06,718 --> 05:49:10,558
have rec and res like so and then i'm
8499
05:49:10,558 --> 05:49:12,480
simply going to send back some kind of
8500
05:49:12,480 --> 05:49:15,760
dummy data but i would want to console
8501
05:49:15,760 --> 05:49:18,240
log the rec params just so you
8502
05:49:18,240 --> 05:49:20,558
understand how everything works and now
8503
05:49:20,558 --> 05:49:22,120
let me send
8504
05:49:22,120 --> 05:49:24,718
res.send so res
8505
05:49:24,718 --> 05:49:26,718
not send and i'm just going to say
8506
05:49:26,718 --> 05:49:31,120
simple hello world let's save this one
8507
05:49:31,120 --> 05:49:33,920
and again in the browser let's go here
8508
05:49:33,920 --> 05:49:36,878
we have products again some kind of id
8509
05:49:36,878 --> 05:49:39,520
whether that's abc whether that is a
8510
05:49:39,520 --> 05:49:41,040
number in this case it doesn't really
8511
05:49:41,040 --> 05:49:43,440
matter since you can see that there's
8512
05:49:43,440 --> 05:49:46,000
not much functionality in there but we
8513
05:49:46,000 --> 05:49:48,638
need to type in here reviews and then
8514
05:49:48,638 --> 05:49:51,680
for example the review id would be i
8515
05:49:51,680 --> 05:49:54,878
don't know abc so once i navigate here
8516
05:49:54,878 --> 05:49:56,718
of course i'm going to get my hello
8517
05:49:56,718 --> 05:49:59,120
world and then in the console notice how
8518
05:49:59,120 --> 05:50:01,840
i'm accessing all of them so i have the
8519
05:50:01,840 --> 05:50:05,360
product id as well as the review id so
8520
05:50:05,360 --> 05:50:08,878
again they can get way more complex than
8521
05:50:08,878 --> 05:50:12,000
just this simple approach and one more
8522
05:50:12,000 --> 05:50:13,760
thing i would like to mention this
8523
05:50:13,760 --> 05:50:16,400
reviews though is hard coded
8524
05:50:16,400 --> 05:50:20,240
so if i go back i can change the abc and
8525
05:50:20,240 --> 05:50:23,040
4 however i would like but if i'll
8526
05:50:23,040 --> 05:50:24,160
change
8527
05:50:24,160 --> 05:50:26,798
from reviews to review
8528
05:50:26,798 --> 05:50:30,400
i'm going to get a 404 why well because
8529
05:50:30,400 --> 05:50:32,958
review is not a route parameter so
8530
05:50:32,958 --> 05:50:34,798
that's not a placeholder
8531
05:50:34,798 --> 05:50:36,240
so if
8532
05:50:36,240 --> 05:50:38,400
this is incorrect then of course i get
8533
05:50:38,400 --> 05:50:41,120
d404 hopefully that is clear and now
8534
05:50:41,120 --> 05:50:43,280
let's talk about the query string
8535
05:50:43,280 --> 05:50:44,558
parameters
8536
05:50:44,558 --> 05:50:46,718
all right and once we're familiar with
8537
05:50:46,718 --> 05:50:49,120
rot parameters let's talk about the
8538
05:50:49,120 --> 05:50:51,680
query string parameters or they're also
8539
05:50:51,680 --> 05:50:54,558
called url parameters
8540
05:50:54,558 --> 05:50:57,120
and essentially that is a way for us to
8541
05:50:57,120 --> 05:51:00,480
send small amounts of information to the
8542
05:51:00,480 --> 05:51:01,440
server
8543
05:51:01,440 --> 05:51:03,280
using the url
8544
05:51:03,280 --> 05:51:06,080
now this information is usually used as
8545
05:51:06,080 --> 05:51:09,120
parameters to for example query database
8546
05:51:09,120 --> 05:51:11,760
or filter results and that's really up
8547
05:51:11,760 --> 05:51:13,280
to the people who are setting up the
8548
05:51:13,280 --> 05:51:16,798
server they decide what parameters are
8549
05:51:16,798 --> 05:51:18,240
going to be accepted
8550
05:51:18,240 --> 05:51:21,520
and what functionality will depend on
8551
05:51:21,520 --> 05:51:24,400
those values and to give you a real
8552
05:51:24,400 --> 05:51:28,240
world example let me go to my search
8553
05:51:28,240 --> 05:51:30,480
engine and i'm just looking for hacker
8554
05:51:30,480 --> 05:51:32,718
news algolia api
8555
05:51:32,718 --> 05:51:34,718
so when you're working on a front-end
8556
05:51:34,718 --> 05:51:36,718
app i'm not sure whether you worked with
8557
05:51:36,718 --> 05:51:40,798
this api but that is a very cool api and
8558
05:51:40,798 --> 05:51:43,520
essentially it's going to work as a good
8559
05:51:43,520 --> 05:51:45,280
example of how we should be setting up
8560
05:51:45,280 --> 05:51:47,680
the server and this is as a side note
8561
05:51:47,680 --> 05:51:48,480
but
8562
05:51:48,480 --> 05:51:51,280
notice the url here
8563
05:51:51,280 --> 05:51:54,400
so they go with a domain which in our
8564
05:51:54,400 --> 05:51:57,360
case is of course localhost 5000 but
8565
05:51:57,360 --> 05:52:00,920
here it is actually a algolia domain so
8566
05:52:00,920 --> 05:52:02,480
algolia.com
8567
05:52:02,480 --> 05:52:05,440
forward slash api like i said that is a
8568
05:52:05,440 --> 05:52:07,040
pretty common practice
8569
05:52:07,040 --> 05:52:10,480
then a version number and then whatever
8570
05:52:10,480 --> 05:52:13,520
list you're getting so in here it is
8571
05:52:13,520 --> 05:52:16,240
items now check out this one
8572
05:52:16,240 --> 05:52:18,080
doesn't that ring a bell
8573
05:52:18,080 --> 05:52:20,878
of course that is a route parameter
8574
05:52:20,878 --> 05:52:23,280
where they say yeah here's the list of
8575
05:52:23,280 --> 05:52:24,718
items
8576
05:52:24,718 --> 05:52:26,878
but if you want to be more specific
8577
05:52:26,878 --> 05:52:29,200
please provide the id
8578
05:52:29,200 --> 05:52:31,440
and then if you keep on scrolling you'll
8579
05:52:31,440 --> 05:52:33,520
notice the same thing with the users
8580
05:52:33,520 --> 05:52:36,480
again we have the main domain
8581
05:52:36,480 --> 05:52:40,480
api version one users that's the list
8582
05:52:40,480 --> 05:52:42,480
and then if you want to get a specific
8583
05:52:42,480 --> 05:52:44,798
one then of course again there is a
8584
05:52:44,798 --> 05:52:46,798
route parameter now this is just to
8585
05:52:46,798 --> 05:52:49,280
showcase that i'm not randomly coming up
8586
05:52:49,280 --> 05:52:52,558
with those things no that's how actually
8587
05:52:52,558 --> 05:52:56,160
the servers work in real world and let's
8588
05:52:56,160 --> 05:52:58,160
keep on scrolling and now we come to
8589
05:52:58,160 --> 05:53:00,000
this interesting part
8590
05:53:00,000 --> 05:53:01,920
where we can sort
8591
05:53:01,920 --> 05:53:02,718
so
8592
05:53:02,718 --> 05:53:05,360
we're getting the data from the algolia
8593
05:53:05,360 --> 05:53:06,718
api
8594
05:53:06,718 --> 05:53:08,878
but then in my app
8595
05:53:08,878 --> 05:53:11,520
i can sort i can say hey you know what
8596
05:53:11,520 --> 05:53:15,200
get me specific hacker news story or get
8597
05:53:15,200 --> 05:53:18,558
me stories based on some kind of search
8598
05:53:18,558 --> 05:53:20,400
term and hopefully you get the gist
8599
05:53:20,400 --> 05:53:22,718
where instead of just grabbing the whole
8600
05:53:22,718 --> 05:53:23,840
thing
8601
05:53:23,840 --> 05:53:25,200
i can say you know what
8602
05:53:25,200 --> 05:53:28,080
get me all the stories that match
8603
05:53:28,080 --> 05:53:31,120
through so again we go with the url and
8604
05:53:31,120 --> 05:53:32,638
this is really up to you how you set
8605
05:53:32,638 --> 05:53:35,600
this up in their case they use search
8606
05:53:35,600 --> 05:53:36,718
and then
8607
05:53:36,718 --> 05:53:38,878
here we have this question mark
8608
05:53:38,878 --> 05:53:42,240
and whatever is after this question mark
8609
05:53:42,240 --> 05:53:44,638
is not technically part of this url
8610
05:53:44,638 --> 05:53:45,520
meaning
8611
05:53:45,520 --> 05:53:47,920
it's just a way for us to send that data
8612
05:53:47,920 --> 05:53:50,080
to the server and then server decides
8613
05:53:50,080 --> 05:53:52,160
what to do with this data
8614
05:53:52,160 --> 05:53:54,638
so the url is still this one
8615
05:53:54,638 --> 05:53:56,878
the one that ends with search and then
8616
05:53:56,878 --> 05:53:59,200
we have a question mark and basically
8617
05:53:59,200 --> 05:54:02,160
this is just a specific info
8618
05:54:02,160 --> 05:54:05,120
about the data that i'm requesting so
8619
05:54:05,120 --> 05:54:09,600
here the user adds a query parameter and
8620
05:54:09,600 --> 05:54:13,120
then the value is full and then also has
8621
05:54:13,120 --> 05:54:15,280
tags of story and that means that we're
8622
05:54:15,280 --> 05:54:17,360
going to get all the stories matching
8623
05:54:17,360 --> 05:54:19,840
full now what else you could set up here
8624
05:54:19,840 --> 05:54:22,558
in a query string pretty much anything a
8625
05:54:22,558 --> 05:54:24,798
pretty typical is going to be page for
8626
05:54:24,798 --> 05:54:27,920
example if you have a list of things and
8627
05:54:27,920 --> 05:54:30,000
i don't want to get 100 items at a time
8628
05:54:30,000 --> 05:54:32,080
i can say you know what split it up in
8629
05:54:32,080 --> 05:54:34,798
pages and get me initially page number
8630
05:54:34,798 --> 05:54:37,600
one and then only if the user clicks on
8631
05:54:37,600 --> 05:54:39,920
a button that fetches the page number
8632
05:54:39,920 --> 05:54:42,558
two then i get the second page then for
8633
05:54:42,558 --> 05:54:44,558
example in here they have hits per page
8634
05:54:44,558 --> 05:54:46,160
so that is going to be how many items
8635
05:54:46,160 --> 05:54:48,400
per page so again
8636
05:54:48,400 --> 05:54:51,520
after that question mark if the setup is
8637
05:54:51,520 --> 05:54:53,520
supported by the server
8638
05:54:53,520 --> 05:54:55,920
then of course you can add those key
8639
05:54:55,920 --> 05:54:58,400
value pairs and the way we add them as
8640
05:54:58,400 --> 05:55:00,878
you can see is by using key value pairs
8641
05:55:00,878 --> 05:55:03,200
so we have question mark and then we
8642
05:55:03,200 --> 05:55:04,240
have a key
8643
05:55:04,240 --> 05:55:06,718
which in this case is query and then the
8644
05:55:06,718 --> 05:55:08,400
value now
8645
05:55:08,400 --> 05:55:10,638
before we continue let me make something
8646
05:55:10,638 --> 05:55:12,798
very very clear it's not like you can
8647
05:55:12,798 --> 05:55:15,920
randomly search the web and just start
8648
05:55:15,920 --> 05:55:18,160
adding these query string parameters and
8649
05:55:18,160 --> 05:55:19,440
then expect
8650
05:55:19,440 --> 05:55:21,920
that as a miracle you'll just get the
8651
05:55:21,920 --> 05:55:24,958
data that you're looking for a as far as
8652
05:55:24,958 --> 05:55:26,080
the keys
8653
05:55:26,080 --> 05:55:28,240
they're designed on a server so if i'll
8654
05:55:28,240 --> 05:55:30,878
set up a key of chocolate milkshake
8655
05:55:30,878 --> 05:55:33,120
algolia api is going to be like i don't
8656
05:55:33,120 --> 05:55:34,718
know what you're talking about and the
8657
05:55:34,718 --> 05:55:36,718
same goes for the value so where i'm
8658
05:55:36,718 --> 05:55:39,360
going with this now we are in charge of
8659
05:55:39,360 --> 05:55:42,878
that server so it's up to us to handle
8660
05:55:42,878 --> 05:55:45,680
those query string parameters
8661
05:55:45,680 --> 05:55:47,760
and i'm going to go back to our project
8662
05:55:47,760 --> 05:55:50,320
and i'll purposely set up a new route
8663
05:55:50,320 --> 05:55:52,958
please keep in mind one thing where a
8664
05:55:52,958 --> 05:55:54,878
pretty typical setup
8665
05:55:54,878 --> 05:55:57,360
is adding this to the list
8666
05:55:57,360 --> 05:56:00,480
so again we'll look for specific
8667
05:56:00,480 --> 05:56:02,878
property on our request object and then
8668
05:56:02,878 --> 05:56:05,360
if that property is provided then of
8669
05:56:05,360 --> 05:56:09,200
course we return a more detailed
8670
05:56:09,200 --> 05:56:11,040
response meaning there's maybe some
8671
05:56:11,040 --> 05:56:12,958
filtering or something like that but if
8672
05:56:12,958 --> 05:56:14,320
not then we'll send back all the
8673
05:56:14,320 --> 05:56:16,958
products now i'll purposely set up a new
8674
05:56:16,958 --> 05:56:20,400
route just so we don't jam all our code
8675
05:56:20,400 --> 05:56:23,600
in this one route but then
8676
05:56:23,600 --> 05:56:26,080
in the next video probably i'll show you
8677
05:56:26,080 --> 05:56:28,958
how we can combine both routes so let's
8678
05:56:28,958 --> 05:56:30,878
keep on scrolling so this is going to be
8679
05:56:30,878 --> 05:56:32,840
our more complex
8680
05:56:32,840 --> 05:56:37,280
params example and let's go with app.get
8681
05:56:37,280 --> 05:56:38,400
and again
8682
05:56:38,400 --> 05:56:40,000
the route is really up to you i'm going
8683
05:56:40,000 --> 05:56:42,480
to go with api now in order to make it
8684
05:56:42,480 --> 05:56:43,680
interesting i'm going to add that
8685
05:56:43,680 --> 05:56:47,360
version 1 just like the api of algolia
8686
05:56:47,360 --> 05:56:50,400
has it and then i'll say query now i
8687
05:56:50,400 --> 05:56:52,160
don't need to add question marks nothing
8688
05:56:52,160 --> 05:56:55,040
like that i just set up query that is my
8689
05:56:55,040 --> 05:56:56,718
route and then i have my callback
8690
05:56:56,718 --> 05:56:59,440
function rack and res
8691
05:56:59,440 --> 05:57:01,600
now in this callback function in order
8692
05:57:01,600 --> 05:57:04,400
to access those query string parameters
8693
05:57:04,400 --> 05:57:07,280
i need to go with rec and query so let's
8694
05:57:07,280 --> 05:57:09,040
go with console.log and we're going to
8695
05:57:09,040 --> 05:57:10,080
look for
8696
05:57:10,080 --> 05:57:11,200
request
8697
05:57:11,200 --> 05:57:14,320
and not params sorry we're going to go
8698
05:57:14,320 --> 05:57:17,520
with query over here and again let's go
8699
05:57:17,520 --> 05:57:19,920
with simple hello world just so we can
8700
05:57:19,920 --> 05:57:23,680
speed this up and then if i go to the
8701
05:57:23,680 --> 05:57:24,958
local host
8702
05:57:24,958 --> 05:57:26,878
and more specifically
8703
05:57:26,878 --> 05:57:28,840
i'm going to look for
8704
05:57:28,840 --> 05:57:33,280
localhost then 5000 then api then
8705
05:57:33,280 --> 05:57:34,798
version number one
8706
05:57:34,798 --> 05:57:37,920
and then query and then if i add this
8707
05:57:37,920 --> 05:57:41,040
question mark i can add as many query
8708
05:57:41,040 --> 05:57:43,360
string parameters as i would like so
8709
05:57:43,360 --> 05:57:45,520
here i'm going to go with the name that
8710
05:57:45,520 --> 05:57:47,440
will be equal to john
8711
05:57:47,440 --> 05:57:50,320
then in order to combine them we just
8712
05:57:50,320 --> 05:57:52,558
add this ampersand so we're going to go
8713
05:57:52,558 --> 05:57:55,760
with name john and then id4 just keep in
8714
05:57:55,760 --> 05:57:57,520
mind that you can add as many query
8715
05:57:57,520 --> 05:58:00,480
string parameters as you'd want and then
8716
05:58:00,480 --> 05:58:02,638
once i navigate there at the moment i'm
8717
05:58:02,638 --> 05:58:04,558
just going to get the hello world
8718
05:58:04,558 --> 05:58:07,200
so that's my default response from this
8719
05:58:07,200 --> 05:58:08,240
url
8720
05:58:08,240 --> 05:58:10,718
but in a terminal notice i have name
8721
05:58:10,718 --> 05:58:13,360
john and id number four again this is a
8722
05:58:13,360 --> 05:58:16,480
string and this is going to be important
8723
05:58:16,480 --> 05:58:19,360
in a second but what this allows us to
8724
05:58:19,360 --> 05:58:22,400
do is access those parameters and then
8725
05:58:22,400 --> 05:58:24,080
based on them do some kind of
8726
05:58:24,080 --> 05:58:25,760
functionality now first of all i would
8727
05:58:25,760 --> 05:58:27,360
want to change this around a little bit
8728
05:58:27,360 --> 05:58:28,558
where i'm not going to be looking for
8729
05:58:28,558 --> 05:58:31,920
name or id i'm going to be looking for
8730
05:58:31,920 --> 05:58:34,558
search query parameter as well as the
8731
05:58:34,558 --> 05:58:37,760
limit so if the user wants to search for
8732
05:58:37,760 --> 05:58:39,840
a specific product he or she needs to
8733
05:58:39,840 --> 05:58:42,718
provide that search query parameter as
8734
05:58:42,718 --> 05:58:44,000
well as limiting
8735
05:58:44,000 --> 05:58:46,240
where the user can limit of how many
8736
05:58:46,240 --> 05:58:49,280
products they are getting back so let me
8737
05:58:49,280 --> 05:58:51,760
navigate back and just to showcase that
8738
05:58:51,760 --> 05:58:55,760
so again instead of name i will zoom in
8739
05:58:55,760 --> 05:58:57,280
we'll say search
8740
05:58:57,280 --> 05:59:00,320
is equal and then whatever i would want
8741
05:59:00,320 --> 05:59:01,440
so in this case i'm just going to go
8742
05:59:01,440 --> 05:59:04,320
with a so effectively this will return
8743
05:59:04,320 --> 05:59:06,638
all the products that start with a and
8744
05:59:06,638 --> 05:59:10,878
then the second one is the what that was
8745
05:59:10,878 --> 05:59:13,280
the limit right so i'm gonna say limit
8746
05:59:13,280 --> 05:59:14,240
and
8747
05:59:14,240 --> 05:59:16,400
for now let's just go with two of them
8748
05:59:16,400 --> 05:59:19,760
so this will return two products
8749
05:59:19,760 --> 05:59:22,718
i'll zoom out and again i have search
8750
05:59:22,718 --> 05:59:25,200
and i have limit after you so now of
8751
05:59:25,200 --> 05:59:27,600
course let's set up that functionality
8752
05:59:27,600 --> 05:59:30,080
and we'll simply start by creating new
8753
05:59:30,080 --> 05:59:32,480
instance of those products i'm gonna go
8754
05:59:32,480 --> 05:59:34,240
with let and you'll see in a second why
8755
05:59:34,240 --> 05:59:36,400
so say sorted
8756
05:59:36,400 --> 05:59:37,360
product
8757
05:59:37,360 --> 05:59:39,360
and in here we'll use the spread
8758
05:59:39,360 --> 05:59:42,558
operator so we imported the product
8759
05:59:42,558 --> 05:59:44,160
and now i'm just going to copy the
8760
05:59:44,160 --> 05:59:46,400
values so this is going to be my new
8761
05:59:46,400 --> 05:59:48,080
array and the reason why i'm using let
8762
05:59:48,080 --> 05:59:49,120
because
8763
05:59:49,120 --> 05:59:51,040
we will modify
8764
05:59:51,040 --> 05:59:54,240
this value a bit now instead of just
8765
05:59:54,240 --> 05:59:56,320
cancel logging the query i'll comment
8766
05:59:56,320 --> 05:59:59,520
this out and we'll set it up as const
8767
05:59:59,520 --> 06:00:02,160
and then again i'm looking for two
8768
06:00:02,160 --> 06:00:04,638
specific keys i'm looking for the search
8769
06:00:04,638 --> 06:00:06,798
and i'm looking for the limit so if the
8770
06:00:06,798 --> 06:00:08,798
user doesn't provide them
8771
06:00:08,798 --> 06:00:10,718
well then we'll send back all the
8772
06:00:10,718 --> 06:00:11,760
product
8773
06:00:11,760 --> 06:00:13,120
so we're gonna go
8774
06:00:13,120 --> 06:00:14,958
with query
8775
06:00:14,958 --> 06:00:17,120
like so so i can see the search and
8776
06:00:17,120 --> 06:00:18,400
limit beautiful
8777
06:00:18,400 --> 06:00:19,440
and now
8778
06:00:19,440 --> 06:00:21,840
instead of just sending back hello world
8779
06:00:21,840 --> 06:00:23,280
i'm going to check
8780
06:00:23,280 --> 06:00:24,080
if
8781
06:00:24,080 --> 06:00:25,600
the search
8782
06:00:25,600 --> 06:00:28,480
is in my query string parameters then i
8783
06:00:28,480 --> 06:00:30,718
would want to filter my product so i'm
8784
06:00:30,718 --> 06:00:34,558
going to say if and then search like so
8785
06:00:34,558 --> 06:00:35,920
and we're going to go with sorted
8786
06:00:35,920 --> 06:00:37,840
products since i'm using let i can do
8787
06:00:37,840 --> 06:00:40,080
that i'm going to go with sorted product
8788
06:00:40,080 --> 06:00:42,798
and then filter method again
8789
06:00:42,798 --> 06:00:45,280
straight up javascript again i'll call
8790
06:00:45,280 --> 06:00:47,440
this a product
8791
06:00:47,440 --> 06:00:50,400
like so and what i would want to return
8792
06:00:50,400 --> 06:00:53,520
are the products that start with the
8793
06:00:53,520 --> 06:00:57,040
value of my search term so here i can
8794
06:00:57,040 --> 06:00:58,558
say return
8795
06:00:58,558 --> 06:00:59,520
product
8796
06:00:59,520 --> 06:01:02,798
and name so if the product name starts
8797
06:01:02,798 --> 06:01:04,240
with now again this is straight up
8798
06:01:04,240 --> 06:01:05,600
javascript
8799
06:01:05,600 --> 06:01:07,520
and i'll pass in the search if that is
8800
06:01:07,520 --> 06:01:10,160
the case that is going to be my value in
8801
06:01:10,160 --> 06:01:12,240
stored product and i can right away
8802
06:01:12,240 --> 06:01:14,558
check for the limit as well
8803
06:01:14,558 --> 06:01:16,240
where i'm going to say if
8804
06:01:16,240 --> 06:01:18,320
and then limit
8805
06:01:18,320 --> 06:01:20,638
and if it exists
8806
06:01:20,638 --> 06:01:22,958
so if the user has provided it again
8807
06:01:22,958 --> 06:01:24,798
let's go with sort product
8808
06:01:24,798 --> 06:01:27,680
and filter more and in this case i'm
8809
06:01:27,680 --> 06:01:29,680
going to use the slice method where i'm
8810
06:01:29,680 --> 06:01:31,600
just going to get specific items from
8811
06:01:31,600 --> 06:01:34,240
the array so i'm going to start with 0
8812
06:01:34,240 --> 06:01:36,798
and then remember we're getting a string
8813
06:01:36,798 --> 06:01:38,798
so we need to go with number and then
8814
06:01:38,798 --> 06:01:41,440
again we'll pass in the limit let's go
8815
06:01:41,440 --> 06:01:44,240
here below both of them and let's just
8816
06:01:44,240 --> 06:01:47,360
say res dot and then status and we're
8817
06:01:47,360 --> 06:01:51,200
going to go with 200 like so and then a
8818
06:01:51,200 --> 06:01:52,798
json response
8819
06:01:52,798 --> 06:01:56,320
now when it comes to json response we're
8820
06:01:56,320 --> 06:01:58,320
just gonna go with our sword product
8821
06:01:58,320 --> 06:02:00,240
right so we go back
8822
06:02:00,240 --> 06:02:02,558
and let me just showcase something where
8823
06:02:02,558 --> 06:02:03,920
if
8824
06:02:03,920 --> 06:02:05,920
none of the query string parameters are
8825
06:02:05,920 --> 06:02:08,240
provided i'm gonna send back my whole
8826
06:02:08,240 --> 06:02:11,760
data why well because i copied my
8827
06:02:11,760 --> 06:02:14,798
products right both of them were false
8828
06:02:14,798 --> 06:02:16,718
both of them were undefined
8829
06:02:16,718 --> 06:02:20,000
and as a side note there is a error here
8830
06:02:20,000 --> 06:02:22,400
cannot send headers and i'll talk about
8831
06:02:22,400 --> 06:02:24,718
it actually in a second that is one of
8832
06:02:24,718 --> 06:02:25,520
my
8833
06:02:25,520 --> 06:02:28,718
next topics so let me just deal with
8834
06:02:28,718 --> 06:02:31,600
this hello world by deleting it and
8835
06:02:31,600 --> 06:02:33,280
we're going to be in good shape but i'll
8836
06:02:33,280 --> 06:02:36,320
cover why we got this error and what
8837
06:02:36,320 --> 06:02:38,320
gotchas you should be aware of so let me
8838
06:02:38,320 --> 06:02:39,520
go back again
8839
06:02:39,520 --> 06:02:41,600
let's see again we're getting all the
8840
06:02:41,600 --> 06:02:44,638
products because the user did not
8841
06:02:44,638 --> 06:02:45,760
provide
8842
06:02:45,760 --> 06:02:48,480
that specific query string parameter so
8843
06:02:48,480 --> 06:02:51,120
if we right now navigate to the url
8844
06:02:51,120 --> 06:02:53,920
and i'm gonna start with a limit and if
8845
06:02:53,920 --> 06:02:55,520
in the limit i'm gonna say that i'm only
8846
06:02:55,520 --> 06:02:57,120
interested in two products
8847
06:02:57,120 --> 06:02:59,840
check it out now of course my limit is
8848
06:02:59,840 --> 06:03:02,400
two so i only have two products and if
8849
06:03:02,400 --> 06:03:04,558
it's three then it's gonna be three and
8850
06:03:04,558 --> 06:03:07,360
then four and hopefully you get the gist
8851
06:03:07,360 --> 06:03:11,200
where whatever value i provide here in a
8852
06:03:11,200 --> 06:03:13,600
limit well i have the functionality for
8853
06:03:13,600 --> 06:03:16,240
it now keep in mind again name needs to
8854
06:03:16,240 --> 06:03:18,400
match if this is going to be limi
8855
06:03:18,400 --> 06:03:20,878
instead of limit again i'm just going to
8856
06:03:20,878 --> 06:03:23,440
get back all the products regardless of
8857
06:03:23,440 --> 06:03:26,480
what is my value and the same goes for
8858
06:03:26,480 --> 06:03:29,200
the value of the query string parameter
8859
06:03:29,200 --> 06:03:31,680
if this is limit but for example i
8860
06:03:31,680 --> 06:03:34,558
decide to pass in abc
8861
06:03:34,558 --> 06:03:37,280
nothing i get empty array why because my
8862
06:03:37,280 --> 06:03:40,638
value wasn't what my functionality was
8863
06:03:40,638 --> 06:03:43,440
looking for and similarly of course we
8864
06:03:43,440 --> 06:03:45,600
can add the search option as well and
8865
06:03:45,600 --> 06:03:47,600
i'm purposely showing them one by one so
8866
06:03:47,600 --> 06:03:50,400
you don't get confused so let's say
8867
06:03:50,400 --> 06:03:51,440
search
8868
06:03:51,440 --> 06:03:53,840
and again the functionality is set up
8869
06:03:53,840 --> 06:03:56,400
where you need to provide a starting
8870
06:03:56,400 --> 06:03:58,320
character so in my case i'm looking only
8871
06:03:58,320 --> 06:04:01,520
for the products that start with a and
8872
06:04:01,520 --> 06:04:04,160
there it is i have two of them now if i
8873
06:04:04,160 --> 06:04:07,440
want a limit i can just add and here
8874
06:04:07,440 --> 06:04:09,840
and let's just type limit and i'm only
8875
06:04:09,840 --> 06:04:11,520
going to be looking for one product and
8876
06:04:11,520 --> 06:04:13,520
if i add this limit one
8877
06:04:13,520 --> 06:04:17,120
there it is now i only get one product
8878
06:04:17,120 --> 06:04:18,480
now there's also obviously going to be
8879
06:04:18,480 --> 06:04:22,240
the case where we return empty array
8880
06:04:22,240 --> 06:04:24,000
why well because
8881
06:04:24,000 --> 06:04:27,040
i could go with search and look for the
8882
06:04:27,040 --> 06:04:28,558
products of b
8883
06:04:28,558 --> 06:04:31,360
and unfortunately when it comes to my
8884
06:04:31,360 --> 06:04:34,320
product data i don't have products that
8885
06:04:34,320 --> 06:04:37,520
start with b so we can handle that
8886
06:04:37,520 --> 06:04:39,920
instead of sending back empty array i
8887
06:04:39,920 --> 06:04:42,798
could check what is the length of my
8888
06:04:42,798 --> 06:04:45,280
array and if for example it is less than
8889
06:04:45,280 --> 06:04:48,160
one then i explicitly send back the
8890
06:04:48,160 --> 06:04:51,920
response where i say yes the request was
8891
06:04:51,920 --> 06:04:53,120
successful
8892
06:04:53,120 --> 06:04:55,920
but i couldn't return any product so we
8893
06:04:55,920 --> 06:04:59,280
can go with if and then sorted product
8894
06:04:59,280 --> 06:05:00,798
if the length
8895
06:05:00,798 --> 06:05:02,558
of this array
8896
06:05:02,558 --> 06:05:04,878
is less than one
8897
06:05:04,878 --> 06:05:07,200
if it is less than one then of course i
8898
06:05:07,200 --> 06:05:11,040
can just go with res dot status now this
8899
06:05:11,040 --> 06:05:12,718
is one gotcha where
8900
06:05:12,718 --> 06:05:15,200
you're not sending back the 404 you're
8901
06:05:15,200 --> 06:05:17,920
not saying the url doesn't exist or the
8902
06:05:17,920 --> 06:05:19,520
resource doesn't exist
8903
06:05:19,520 --> 06:05:22,160
in this case you're trying to
8904
06:05:22,160 --> 06:05:24,958
filter the product but
8905
06:05:24,958 --> 06:05:27,200
nothing came back so
8906
06:05:27,200 --> 06:05:29,360
whatever query string parameters were
8907
06:05:29,360 --> 06:05:32,000
provided they didn't yield any results
8908
06:05:32,000 --> 06:05:34,878
so you're simply saying status and then
8909
06:05:34,878 --> 06:05:37,520
you can go with send and we can go with
8910
06:05:37,520 --> 06:05:39,440
no products
8911
06:05:39,440 --> 06:05:41,440
products matched
8912
06:05:41,440 --> 06:05:42,718
your
8913
06:05:42,718 --> 06:05:45,760
search like so so we can go here and
8914
06:05:45,760 --> 06:05:47,520
then if we refresh again with the same
8915
06:05:47,520 --> 06:05:50,000
ones we have no product match your
8916
06:05:50,000 --> 06:05:51,920
search so that is one option you can
8917
06:05:51,920 --> 06:05:54,480
send back the string but
8918
06:05:54,480 --> 06:05:57,760
a more common one is this one where i'm
8919
06:05:57,760 --> 06:05:58,878
going to comment this out for your
8920
06:05:58,878 --> 06:06:01,280
reference again and instead you go with
8921
06:06:01,280 --> 06:06:02,798
return
8922
06:06:02,798 --> 06:06:05,200
and you're going to go with res dot
8923
06:06:05,200 --> 06:06:08,878
status and again we have this error in
8924
06:06:08,878 --> 06:06:09,760
the
8925
06:06:09,760 --> 06:06:12,718
server and again i'll talk about it in
8926
06:06:12,718 --> 06:06:14,878
the next video why we have that one so
8927
06:06:14,878 --> 06:06:17,200
we're going to go with res dot status
8928
06:06:17,200 --> 06:06:20,240
and we pass in the 200 and instead we
8929
06:06:20,240 --> 06:06:23,280
send back the json one and in that json
8930
06:06:23,280 --> 06:06:25,200
again you can pass the string if you
8931
06:06:25,200 --> 06:06:28,400
want but a more common approach is
8932
06:06:28,400 --> 06:06:30,400
setting up the object where you
8933
06:06:30,400 --> 06:06:33,520
explicitly say that the request was
8934
06:06:33,520 --> 06:06:36,638
successful or a failure so you go with
8935
06:06:36,638 --> 06:06:39,440
success and that is equal to true and
8936
06:06:39,440 --> 06:06:41,760
then again you come up with whatever
8937
06:06:41,760 --> 06:06:44,000
name you would want a generic one is
8938
06:06:44,000 --> 06:06:47,200
data and then you send back the array
8939
06:06:47,200 --> 06:06:49,600
again you are in charge here you can
8940
06:06:49,600 --> 06:06:51,360
really do whatever you want i'm just
8941
06:06:51,360 --> 06:06:52,320
showing you
8942
06:06:52,320 --> 06:06:54,480
a pretty common
8943
06:06:54,480 --> 06:06:57,280
approaches to the situation so again i'm
8944
06:06:57,280 --> 06:06:59,360
looking for some kind of product
8945
06:06:59,360 --> 06:07:01,760
using my query string parameter now
8946
06:07:01,760 --> 06:07:04,558
unfortunately server can return
8947
06:07:04,558 --> 06:07:06,638
any data meaning
8948
06:07:06,638 --> 06:07:09,280
any product and then of course i just
8949
06:07:09,280 --> 06:07:11,440
get this success true because the
8950
06:07:11,440 --> 06:07:13,760
request was successful there was nothing
8951
06:07:13,760 --> 06:07:16,638
wrong with my url however
8952
06:07:16,638 --> 06:07:20,000
i'm just yielding a empty data so that's
8953
06:07:20,000 --> 06:07:23,120
it i can just delete it and if i don't
8954
06:07:23,120 --> 06:07:25,200
provide anything then i'm going to get
8955
06:07:25,200 --> 06:07:27,920
all the products hopefully that is clear
8956
06:07:27,920 --> 06:07:30,718
how the query string parameters work and
8957
06:07:30,718 --> 06:07:33,520
now we can cover a few gotchas all right
8958
06:07:33,520 --> 06:07:35,120
and two things that i would like to
8959
06:07:35,120 --> 06:07:37,360
emphasize are following first
8960
06:07:37,360 --> 06:07:39,840
remember those errors that we're getting
8961
06:07:39,840 --> 06:07:42,400
in a server when we're setting up if
8962
06:07:42,400 --> 06:07:43,840
conditions
8963
06:07:43,840 --> 06:07:47,920
in javascript if we don't explicitly
8964
06:07:47,920 --> 06:07:49,040
return
8965
06:07:49,040 --> 06:07:50,958
then of course javascript just keeps
8966
06:07:50,958 --> 06:07:54,000
reading the code correct so if i'm going
8967
06:07:54,000 --> 06:07:56,878
to omit that return i'm actually going
8968
06:07:56,878 --> 06:07:59,360
to get the server error where i send
8969
06:07:59,360 --> 06:08:02,160
back one response and then javascript
8970
06:08:02,160 --> 06:08:04,480
just keeps reading the code and then
8971
06:08:04,480 --> 06:08:06,718
express is confused express is like hey
8972
06:08:06,718 --> 06:08:08,558
wait a minute i already sent back the
8973
06:08:08,558 --> 06:08:10,878
response so while you're sending another
8974
06:08:10,878 --> 06:08:13,440
now keep in mind that it is happening in
8975
06:08:13,440 --> 06:08:15,680
the same request so you cannot send
8976
06:08:15,680 --> 06:08:18,400
basically two responses in the same
8977
06:08:18,400 --> 06:08:21,440
request one after the another yeah of
8978
06:08:21,440 --> 06:08:23,840
course you can send one based on the
8979
06:08:23,840 --> 06:08:26,000
condition so for example if there are no
8980
06:08:26,000 --> 06:08:28,480
products you send back one response
8981
06:08:28,480 --> 06:08:30,718
and if you can yield some product then
8982
06:08:30,718 --> 06:08:32,798
great then you send the second one but
8983
06:08:32,798 --> 06:08:35,200
you cannot send both of them
8984
06:08:35,200 --> 06:08:38,400
one after another in the same request
8985
06:08:38,400 --> 06:08:40,878
and if you want to see that error again
8986
06:08:40,878 --> 06:08:44,240
in action go to the query you'll add a
8987
06:08:44,240 --> 06:08:45,760
question mark here
8988
06:08:45,760 --> 06:08:48,160
and we're going to go with search and
8989
06:08:48,160 --> 06:08:51,120
again it is equal to b for example that
8990
06:08:51,120 --> 06:08:53,280
is my value that's the starting value
8991
06:08:53,280 --> 06:08:55,520
that i'm looking for and once i do that
8992
06:08:55,520 --> 06:08:58,320
notice yeah i'm getting back the success
8993
06:08:58,320 --> 06:09:00,958
true and data but again i have this big
8994
06:09:00,958 --> 06:09:04,240
fat error in my server and it says
8995
06:09:04,240 --> 06:09:06,638
cannot set headers after they are sent
8996
06:09:06,638 --> 06:09:08,080
to the client
8997
06:09:08,080 --> 06:09:11,200
so we can have only one response per
8998
06:09:11,200 --> 06:09:12,400
request
8999
06:09:12,400 --> 06:09:14,958
and in order to avoid that we just go
9000
06:09:14,958 --> 06:09:16,958
with return so always always when you're
9001
06:09:16,958 --> 06:09:18,958
setting up the condition make sure that
9002
06:09:18,958 --> 06:09:20,878
you go with return so that way we are
9003
06:09:20,878 --> 06:09:23,520
returning from our callback function and
9004
06:09:23,520 --> 06:09:25,920
one we set up over here and that way
9005
06:09:25,920 --> 06:09:28,798
you'll avoid those errors now of course
9006
06:09:28,798 --> 06:09:30,718
in this case there's no more code to
9007
06:09:30,718 --> 06:09:32,320
read so
9008
06:09:32,320 --> 06:09:34,160
yeah it is a better practice if you just
9009
06:09:34,160 --> 06:09:36,320
put this return here but you won't get
9010
06:09:36,320 --> 06:09:38,400
the error since again there's nothing
9011
06:09:38,400 --> 06:09:40,480
after that and one more thing that i
9012
06:09:40,480 --> 06:09:42,320
would like to mention
9013
06:09:42,320 --> 06:09:45,200
normally you're not going to set up a
9014
06:09:45,200 --> 06:09:48,080
separate one just for the query yeah
9015
06:09:48,080 --> 06:09:50,480
there might be some cases maybe there's
9016
06:09:50,480 --> 06:09:53,360
some apis who do that but normally again
9017
06:09:53,360 --> 06:09:55,360
you can just add it to where you're
9018
06:09:55,360 --> 06:09:56,958
getting a list
9019
06:09:56,958 --> 06:09:59,280
so basically if
9020
06:09:59,280 --> 06:10:01,920
there's a query beautiful you're maybe
9021
06:10:01,920 --> 06:10:04,480
gonna sort that data you're gonna filter
9022
06:10:04,480 --> 06:10:06,558
it you're gonna i don't know set up some
9023
06:10:06,558 --> 06:10:09,680
pages or whatever and if
9024
06:10:09,680 --> 06:10:12,240
no query string parameters are there
9025
06:10:12,240 --> 06:10:15,200
then you send back the whole product and
9026
06:10:15,200 --> 06:10:16,878
if you take a look
9027
06:10:16,878 --> 06:10:19,120
pretty much nothing stops me here from
9028
06:10:19,120 --> 06:10:22,160
just changing this to api and products
9029
06:10:22,160 --> 06:10:24,798
and the functionality is going to work
9030
06:10:24,798 --> 06:10:26,160
where if
9031
06:10:26,160 --> 06:10:28,480
there are some query string parameters
9032
06:10:28,480 --> 06:10:30,400
present beautiful
9033
06:10:30,400 --> 06:10:32,400
notice how we're filtering
9034
06:10:32,400 --> 06:10:35,040
our product if not then i always send
9035
06:10:35,040 --> 06:10:38,718
back these products anyway and as you
9036
06:10:38,718 --> 06:10:41,520
can see those are just copies of the
9037
06:10:41,520 --> 06:10:44,958
products that are coming from my data js
9038
06:10:44,958 --> 06:10:46,798
hopefully that is clear and now let's
9039
06:10:46,798 --> 06:10:48,878
move on to our next topic
9040
06:10:48,878 --> 06:10:51,280
nice and once we're familiar with route
9041
06:10:51,280 --> 06:10:54,320
params and query string let's really
9042
06:10:54,320 --> 06:10:56,240
kick things into gear and talk about
9043
06:10:56,240 --> 06:10:58,958
middleware in express.js
9044
06:10:58,958 --> 06:11:01,600
express middleware are functions that
9045
06:11:01,600 --> 06:11:05,040
execute during the request to the server
9046
06:11:05,040 --> 06:11:07,680
each middleware function has access to
9047
06:11:07,680 --> 06:11:10,320
request and response objects
9048
06:11:10,320 --> 06:11:12,320
and when it comes to functionality
9049
06:11:12,320 --> 06:11:15,360
literally sky is the limit in order to
9050
06:11:15,360 --> 06:11:17,680
hammer this home i have prepared quite a
9051
06:11:17,680 --> 06:11:19,840
few examples where we cover middleware
9052
06:11:19,840 --> 06:11:22,480
step by step since in my opinion actual
9053
06:11:22,480 --> 06:11:24,958
code examples are far more helpful than
9054
06:11:24,958 --> 06:11:27,600
text based explanations before we
9055
06:11:27,600 --> 06:11:28,958
continue though let me just stress
9056
06:11:28,958 --> 06:11:31,360
something middleware is literally
9057
06:11:31,360 --> 06:11:33,360
everywhere in express
9058
06:11:33,360 --> 06:11:34,878
you can even make an argument that
9059
06:11:34,878 --> 06:11:37,680
express apps are nothing but a bunch of
9060
06:11:37,680 --> 06:11:40,240
middleware functions stuffed together to
9061
06:11:40,240 --> 06:11:44,000
make one nice express cake or dessert if
9062
06:11:44,000 --> 06:11:46,240
you are in that sort of thing
9063
06:11:46,240 --> 06:11:48,480
and since that is the case middleware is
9064
06:11:48,480 --> 06:11:50,558
not one of those topics you can just
9065
06:11:50,558 --> 06:11:52,480
skip or avoid
9066
06:11:52,480 --> 06:11:54,958
it is at the heart and soul of express
9067
06:11:54,958 --> 06:11:57,440
so please don't dismiss it with that
9068
06:11:57,440 --> 06:11:59,360
said since you'll encounter it more than
9069
06:11:59,360 --> 06:12:01,200
once if you struggle with it in the
9070
06:12:01,200 --> 06:12:03,520
beginning don't panic the more examples
9071
06:12:03,520 --> 06:12:06,080
you'll do the better you will understand
9072
06:12:06,080 --> 06:12:09,360
it now let me start by cleaning out my
9073
06:12:09,360 --> 06:12:12,558
app.js and this is going to be the case
9074
06:12:12,558 --> 06:12:14,400
where i will leave
9075
06:12:14,400 --> 06:12:17,360
the code at least some of the code from
9076
06:12:17,360 --> 06:12:20,638
the previous lecture so let me delete
9077
06:12:20,638 --> 06:12:22,480
all the middle part and i'm just going
9078
06:12:22,480 --> 06:12:25,280
to leave express we are instantiating
9079
06:12:25,280 --> 06:12:29,840
our app and we're listening on port 5000
9080
06:12:29,840 --> 06:12:32,000
and let's just start by adding a comment
9081
06:12:32,000 --> 06:12:33,200
here
9082
06:12:33,200 --> 06:12:35,280
for your reference so there is a
9083
06:12:35,280 --> 06:12:36,958
incoming request
9084
06:12:36,958 --> 06:12:39,440
and so far we have been just sending
9085
06:12:39,440 --> 06:12:40,798
responses right
9086
06:12:40,798 --> 06:12:43,520
so what middleware does
9087
06:12:43,520 --> 06:12:45,440
it sits in between
9088
06:12:45,440 --> 06:12:48,798
hence the name so middle where over here
9089
06:12:48,798 --> 06:12:52,558
and then we pass the response so the
9090
06:12:52,558 --> 06:12:56,080
request comes in we'll do something so
9091
06:12:56,080 --> 06:12:57,600
we'll have access to the both to the
9092
06:12:57,600 --> 06:12:59,920
request and response we'll do some kind
9093
06:12:59,920 --> 06:13:02,558
of functionality again the most basic
9094
06:13:02,558 --> 06:13:04,798
you can just cancel log something
9095
06:13:04,798 --> 06:13:07,280
and then we'll send out the response and
9096
06:13:07,280 --> 06:13:10,240
again i know it probably looks confusing
9097
06:13:10,240 --> 06:13:12,480
at the moment but trust me the more
9098
06:13:12,480 --> 06:13:14,718
examples you'll do the better you'll
9099
06:13:14,718 --> 06:13:15,760
understand
9100
06:13:15,760 --> 06:13:18,878
and let's start by simple scenario where
9101
06:13:18,878 --> 06:13:21,120
i have two routes i have the home route
9102
06:13:21,120 --> 06:13:23,360
and i have the about route
9103
06:13:23,360 --> 06:13:26,000
and in those routes i would just want to
9104
06:13:26,000 --> 06:13:27,440
log
9105
06:13:27,440 --> 06:13:30,000
the method that the user is using the
9106
06:13:30,000 --> 06:13:33,200
url that the user is trying to access
9107
06:13:33,200 --> 06:13:35,600
and for example a date
9108
06:13:35,600 --> 06:13:37,120
and if you think that's silly there's
9109
06:13:37,120 --> 06:13:38,878
actually npm packages that do that
9110
06:13:38,878 --> 06:13:40,400
because
9111
06:13:40,400 --> 06:13:43,440
as your express apps grow bigger and
9112
06:13:43,440 --> 06:13:46,878
bigger it is very useful to see those
9113
06:13:46,878 --> 06:13:50,320
incoming requests in that manner so
9114
06:13:50,320 --> 06:13:52,958
let's start here by setting up add.get
9115
06:13:52,958 --> 06:13:55,360
again this is going to be my home page
9116
06:13:55,360 --> 06:13:57,760
and for time being i have my callback
9117
06:13:57,760 --> 06:14:00,718
function i pass it in and i just go with
9118
06:14:00,718 --> 06:14:02,798
the res dot send
9119
06:14:02,798 --> 06:14:05,840
and i send back the home and i'll do the
9120
06:14:05,840 --> 06:14:08,638
same thing here with my about
9121
06:14:08,638 --> 06:14:10,480
so there's going to be two routes at the
9122
06:14:10,480 --> 06:14:13,280
moment home and about unless it's just
9123
06:14:13,280 --> 06:14:16,638
going to say about so i'll save it and
9124
06:14:16,638 --> 06:14:18,320
it's not going to be surprised if i
9125
06:14:18,320 --> 06:14:21,760
navigate to localhost 5000 if i refresh
9126
06:14:21,760 --> 06:14:23,360
this is my home
9127
06:14:23,360 --> 06:14:25,920
and this is my about again something we
9128
06:14:25,920 --> 06:14:29,040
have covered already before now here's
9129
06:14:29,040 --> 06:14:30,638
the kicker
9130
06:14:30,638 --> 06:14:32,718
if i go with logger
9131
06:14:32,718 --> 06:14:36,080
and if i set up that functionality in
9132
06:14:36,080 --> 06:14:38,958
the route forward homepage i'll also
9133
06:14:38,958 --> 06:14:41,440
have to do that in the about so let me
9134
06:14:41,440 --> 06:14:44,000
showcase that so i have request object
9135
06:14:44,000 --> 06:14:47,120
and in there i have method i have url
9136
06:14:47,120 --> 06:14:48,798
and i'll simply set up the year because
9137
06:14:48,798 --> 06:14:50,400
i don't want to deal with javascript
9138
06:14:50,400 --> 06:14:52,240
types so let's go with method now that
9139
06:14:52,240 --> 06:14:54,400
one was on request
9140
06:14:54,400 --> 06:14:57,200
dot method property then we also have
9141
06:14:57,200 --> 06:15:00,160
the url so url here and that is going to
9142
06:15:00,160 --> 06:15:03,040
be a request url property
9143
06:15:03,040 --> 06:15:05,280
like so and then like i said i'm going
9144
06:15:05,280 --> 06:15:08,080
to go with const and i'll name this time
9145
06:15:08,080 --> 06:15:10,080
but in order to make it easier i'm
9146
06:15:10,080 --> 06:15:12,480
actually going to get a full year so i'm
9147
06:15:12,480 --> 06:15:14,400
going to go with new date and i'll
9148
06:15:14,400 --> 06:15:17,520
invoke it and i'll say get full year and
9149
06:15:17,520 --> 06:15:19,200
invoke it as well
9150
06:15:19,200 --> 06:15:22,240
and of course i'm sending back home yeah
9151
06:15:22,240 --> 06:15:25,200
that is nice but before i do anything i
9152
06:15:25,200 --> 06:15:26,718
would also want to
9153
06:15:26,718 --> 06:15:28,798
cancel console.log all three of them so
9154
06:15:28,798 --> 06:15:30,638
method url
9155
06:15:30,638 --> 06:15:34,000
as well as the time like so so i save it
9156
06:15:34,000 --> 06:15:36,160
and now every time
9157
06:15:36,160 --> 06:15:38,558
user is gonna hit this resource of
9158
06:15:38,558 --> 06:15:41,200
course i'll see that in a console log so
9159
06:15:41,200 --> 06:15:45,760
there it is if i go back to my home page
9160
06:15:45,760 --> 06:15:48,480
like so and if i refresh quite a few
9161
06:15:48,480 --> 06:15:49,440
times
9162
06:15:49,440 --> 06:15:51,760
there it is now in a console i can see
9163
06:15:51,760 --> 06:15:54,638
that the method was get so the user was
9164
06:15:54,638 --> 06:15:57,600
trying to get the resource and then the
9165
06:15:57,600 --> 06:16:00,558
path was the homepage that's my url and
9166
06:16:00,558 --> 06:16:04,878
then the year is 2021. okay awesome
9167
06:16:04,878 --> 06:16:07,200
but here's the problem if i want to have
9168
06:16:07,200 --> 06:16:09,200
the same functionality
9169
06:16:09,200 --> 06:16:12,000
in about what do i need to do right now
9170
06:16:12,000 --> 06:16:14,400
well again i need to copy and paste
9171
06:16:14,400 --> 06:16:18,400
now if i have 15 routes does that sound
9172
06:16:18,400 --> 06:16:20,798
like a reasonable approach of course the
9173
06:16:20,798 --> 06:16:22,160
answer is no
9174
06:16:22,160 --> 06:16:24,958
a better solution would be if we set up
9175
06:16:24,958 --> 06:16:26,718
a function
9176
06:16:26,718 --> 06:16:28,878
and in that function we have all this
9177
06:16:28,878 --> 06:16:31,600
logic and then i can just attach it
9178
06:16:31,600 --> 06:16:33,600
really nearly to all my routes and when
9179
06:16:33,600 --> 06:16:35,120
i say well in italy it just just means
9180
06:16:35,120 --> 06:16:37,440
that for some routes i maybe want to
9181
06:16:37,440 --> 06:16:40,798
attach it and for some maybe i don't
9182
06:16:40,798 --> 06:16:44,320
so here's the deal i can go above both
9183
06:16:44,320 --> 06:16:47,360
of my routes and i can just simply say
9184
06:16:47,360 --> 06:16:49,600
that there's going to be a function by
9185
06:16:49,600 --> 06:16:51,520
the name of
9186
06:16:51,520 --> 06:16:53,600
and for time being we're not going to
9187
06:16:53,600 --> 06:16:55,840
look for any parameters but yes there
9188
06:16:55,840 --> 06:16:57,040
will be there
9189
06:16:57,040 --> 06:16:59,280
and then we just take
9190
06:16:59,280 --> 06:17:01,440
all our i believe four lines of code
9191
06:17:01,440 --> 06:17:05,200
right and we just cut it out and pass it
9192
06:17:05,200 --> 06:17:08,718
here okay awesome and now of course
9193
06:17:08,718 --> 06:17:11,280
where do we attach this function so we
9194
06:17:11,280 --> 06:17:13,360
don't have to duplicate our code
9195
06:17:13,360 --> 06:17:15,680
and the place is following where we have
9196
06:17:15,680 --> 06:17:17,920
the path and then we have the callback
9197
06:17:17,920 --> 06:17:20,400
function now in between them we can
9198
06:17:20,400 --> 06:17:23,200
stuck a middleware so in this case as
9199
06:17:23,200 --> 06:17:26,080
you can see i'm referencing the function
9200
06:17:26,080 --> 06:17:27,760
please keep that in mind so i'm going to
9201
06:17:27,760 --> 06:17:30,558
go with logger that's my middleware
9202
06:17:30,558 --> 06:17:31,440
function
9203
06:17:31,440 --> 06:17:34,878
but now there's the second question well
9204
06:17:34,878 --> 06:17:38,958
in the logger i'm accessing request
9205
06:17:38,958 --> 06:17:41,120
object right so
9206
06:17:41,120 --> 06:17:42,798
how can i do that
9207
06:17:42,798 --> 06:17:44,718
because at the moment i'm not passing it
9208
06:17:44,718 --> 06:17:48,558
in well the good news is that express
9209
06:17:48,558 --> 06:17:51,600
passes it in to our middleware function
9210
06:17:51,600 --> 06:17:53,680
so in here i just set up the reference
9211
06:17:53,680 --> 06:17:56,558
for my function and express will do that
9212
06:17:56,558 --> 06:18:00,160
behind the scenes it will supply the rec
9213
06:18:00,160 --> 06:18:03,920
res and also a next and you'll see in a
9214
06:18:03,920 --> 06:18:07,520
second why we need this next function as
9215
06:18:07,520 --> 06:18:09,280
well so again
9216
06:18:09,280 --> 06:18:10,878
we don't have to do anything we just
9217
06:18:10,878 --> 06:18:12,958
pass here the middleware express
9218
06:18:12,958 --> 06:18:15,920
supplies them but of course it's our job
9219
06:18:15,920 --> 06:18:18,878
to access them as parameters and then
9220
06:18:18,878 --> 06:18:21,200
set up our logic so for them being i'm
9221
06:18:21,200 --> 06:18:23,280
not going to do anything with next i
9222
06:18:23,280 --> 06:18:24,638
will save
9223
06:18:24,638 --> 06:18:27,120
and we should see something where in the
9224
06:18:27,120 --> 06:18:30,958
console i'm still going to get my log
9225
06:18:30,958 --> 06:18:32,798
but the problem is going to be in the
9226
06:18:32,798 --> 06:18:35,520
browser so if i navigate back and if i
9227
06:18:35,520 --> 06:18:36,878
refresh
9228
06:18:36,878 --> 06:18:38,878
notice something where i have this
9229
06:18:38,878 --> 06:18:40,080
spinner
9230
06:18:40,080 --> 06:18:41,040
okay
9231
06:18:41,040 --> 06:18:43,280
so what's happening here well i
9232
06:18:43,280 --> 06:18:45,600
successfully logged
9233
06:18:45,600 --> 06:18:48,240
but i didn't pass it on to the next
9234
06:18:48,240 --> 06:18:50,400
middleware so here's the deal when you
9235
06:18:50,400 --> 06:18:53,440
work with middleware you must must must
9236
06:18:53,440 --> 06:18:56,320
must must pass it on to a next
9237
06:18:56,320 --> 06:18:59,040
middleware unless you're terminating the
9238
06:18:59,040 --> 06:19:01,440
whole cycle by sending back the response
9239
06:19:01,440 --> 06:19:02,478
and don't worry there's going to be
9240
06:19:02,478 --> 06:19:05,360
examples where we do that as well so for
9241
06:19:05,360 --> 06:19:07,680
now just keep in mind that when you have
9242
06:19:07,680 --> 06:19:09,120
a middleware
9243
06:19:09,120 --> 06:19:11,520
where you set up some kind of logic
9244
06:19:11,520 --> 06:19:13,600
unless you're sending back to response
9245
06:19:13,600 --> 06:19:16,798
yourself for example since i have access
9246
06:19:16,798 --> 06:19:20,558
to response i simply can go with res dot
9247
06:19:20,558 --> 06:19:23,120
and send and again i'm going to come up
9248
06:19:23,120 --> 06:19:24,958
with testing or whatever it doesn't
9249
06:19:24,958 --> 06:19:27,280
really matter what we send back if we
9250
06:19:27,280 --> 06:19:29,680
refresh notice it doesn't really matter
9251
06:19:29,680 --> 06:19:32,718
if i try to access the home page since i
9252
06:19:32,718 --> 06:19:34,798
have my middleware i'm actually sending
9253
06:19:34,798 --> 06:19:36,478
back the testing and this is why the
9254
06:19:36,478 --> 06:19:38,400
middleware is so so
9255
06:19:38,400 --> 06:19:40,320
powerful because you can literally do
9256
06:19:40,320 --> 06:19:42,400
whatever you want over here you can set
9257
06:19:42,400 --> 06:19:44,878
up all kinds of cool logic
9258
06:19:44,878 --> 06:19:47,760
and you have two options either you pass
9259
06:19:47,760 --> 06:19:49,040
it on to the
9260
06:19:49,040 --> 06:19:51,200
next middleware which in our case of
9261
06:19:51,200 --> 06:19:52,000
course
9262
06:19:52,000 --> 06:19:54,558
are going to be our methods our get
9263
06:19:54,558 --> 06:19:56,320
methods or
9264
06:19:56,320 --> 06:19:58,400
you simply terminate the whole cycle and
9265
06:19:58,400 --> 06:20:00,558
you just say res dot send and i'm
9266
06:20:00,558 --> 06:20:03,920
sending back my own data so let's not be
9267
06:20:03,920 --> 06:20:07,200
brutal over here i will actually remove
9268
06:20:07,200 --> 06:20:08,400
this line of code
9269
06:20:08,400 --> 06:20:09,680
and if
9270
06:20:09,680 --> 06:20:13,040
i want to pass it on to the next
9271
06:20:13,040 --> 06:20:15,120
function meaning in our case that is
9272
06:20:15,120 --> 06:20:18,638
going to be my method app.get i simply
9273
06:20:18,638 --> 06:20:21,200
go with next and we have to invoke it
9274
06:20:21,200 --> 06:20:23,280
again please keep that in mind there's
9275
06:20:23,280 --> 06:20:25,760
going to be more functionality later in
9276
06:20:25,760 --> 06:20:27,280
these middleware functions but you
9277
06:20:27,280 --> 06:20:28,798
always always
9278
06:20:28,798 --> 06:20:30,558
either you terminate
9279
06:20:30,558 --> 06:20:32,400
so either you send back your own
9280
06:20:32,400 --> 06:20:35,440
response or you pass it on to the next
9281
06:20:35,440 --> 06:20:38,478
middleware that is very crucial so now
9282
06:20:38,478 --> 06:20:40,958
if i go again to my homepage
9283
06:20:40,958 --> 06:20:43,680
there it is i successfully navigate to
9284
06:20:43,680 --> 06:20:45,920
my homepage i can clearly see that that
9285
06:20:45,920 --> 06:20:48,718
is my response and i also successfully
9286
06:20:48,718 --> 06:20:50,718
logged in my console
9287
06:20:50,718 --> 06:20:53,520
the method the url as well as the full
9288
06:20:53,520 --> 06:20:56,478
year and now of course instead of adding
9289
06:20:56,478 --> 06:21:00,878
this logic line by line to every request
9290
06:21:00,878 --> 06:21:04,240
simply can go and i say yep i would like
9291
06:21:04,240 --> 06:21:08,160
to invoke the logger here as well so if
9292
06:21:08,160 --> 06:21:11,760
we go to logo host 5000 and if we're
9293
06:21:11,760 --> 06:21:14,478
brave enough and we navigate to about
9294
06:21:14,478 --> 06:21:18,478
there it is now i have get request about
9295
06:21:18,478 --> 06:21:22,400
url as well as the full year hopefully
9296
06:21:22,400 --> 06:21:24,638
that gives you a good initial
9297
06:21:24,638 --> 06:21:27,360
understanding how the middle works
9298
06:21:27,360 --> 06:21:29,280
and now i can talk about more
9299
06:21:29,280 --> 06:21:32,320
complicated topics beautiful we are
9300
06:21:32,320 --> 06:21:35,840
familiar with the middleware we have our
9301
06:21:35,840 --> 06:21:38,400
first middleware function
9302
06:21:38,400 --> 06:21:40,000
but there are two issues with this
9303
06:21:40,000 --> 06:21:42,160
current setup first
9304
06:21:42,160 --> 06:21:45,520
our appdress is getting somewhat clunky
9305
06:21:45,520 --> 06:21:46,558
because
9306
06:21:46,558 --> 06:21:48,320
i mean we have this logger then we have
9307
06:21:48,320 --> 06:21:49,600
the methods
9308
06:21:49,600 --> 06:21:52,400
it's definitely nicer if we have this
9309
06:21:52,400 --> 06:21:55,840
logger function in a separate file
9310
06:21:55,840 --> 06:21:58,320
it's just going to keep our app.js lean
9311
06:21:58,320 --> 06:22:00,478
and in turn it's just going to make it
9312
06:22:00,478 --> 06:22:03,760
easier for us to navigate and
9313
06:22:03,760 --> 06:22:06,558
essentially work with our application
9314
06:22:06,558 --> 06:22:09,920
and the second issue what if i have 50
9315
06:22:09,920 --> 06:22:11,200
more routes
9316
06:22:11,200 --> 06:22:13,760
and i don't want to add this function
9317
06:22:13,760 --> 06:22:15,680
manually to all of them
9318
06:22:15,680 --> 06:22:18,478
wouldn't be nicer if there would be a
9319
06:22:18,478 --> 06:22:20,638
method that essentially just adds my
9320
06:22:20,638 --> 06:22:23,680
middleware function to any route and of
9321
06:22:23,680 --> 06:22:26,240
course the answer is yes there is such a
9322
06:22:26,240 --> 06:22:28,958
function in fact we used it a few videos
9323
06:22:28,958 --> 06:22:31,680
ago now let's start though by moving
9324
06:22:31,680 --> 06:22:35,120
this sucker into a separate file
9325
06:22:35,120 --> 06:22:38,638
so i'm gonna go to my not navbar app
9326
06:22:38,638 --> 06:22:39,600
sorry
9327
06:22:39,600 --> 06:22:41,440
this is what happens when you have a
9328
06:22:41,440 --> 06:22:43,120
bunch of
9329
06:22:43,120 --> 06:22:45,600
projects open meaning a bunch of folders
9330
06:22:45,600 --> 06:22:47,920
open let me go here where i have my
9331
06:22:47,920 --> 06:22:49,600
app.js
9332
06:22:49,600 --> 06:22:51,360
and i'm just going to
9333
06:22:51,360 --> 06:22:53,120
create a new file
9334
06:22:53,120 --> 06:22:54,718
and this is still going to create it in
9335
06:22:54,718 --> 06:22:57,520
the final sorry so let me go here and
9336
06:22:57,520 --> 06:22:59,680
i'll create a new file and i'll call
9337
06:22:59,680 --> 06:23:01,440
this logger
9338
06:23:01,440 --> 06:23:03,680
js now if you really want you can add
9339
06:23:03,680 --> 06:23:06,320
the middleware there in name as well but
9340
06:23:06,320 --> 06:23:08,958
in my case i'm just going to add
9341
06:23:08,958 --> 06:23:10,878
the name of my function
9342
06:23:10,878 --> 06:23:12,958
then i'll cut it out
9343
06:23:12,958 --> 06:23:14,718
and what's really cool that we know how
9344
06:23:14,718 --> 06:23:17,040
to export this right so we have cons
9345
06:23:17,040 --> 06:23:19,840
logger and then we go with module
9346
06:23:19,840 --> 06:23:22,958
exports and we'll just set it equal to
9347
06:23:22,958 --> 06:23:25,760
our logger so now we have default export
9348
06:23:25,760 --> 06:23:28,080
that's our logger so of course in the
9349
06:23:28,080 --> 06:23:30,638
app js i simply need to go with const
9350
06:23:30,638 --> 06:23:32,878
logger is equal
9351
06:23:32,878 --> 06:23:37,040
and then i require it i go with logger
9352
06:23:37,040 --> 06:23:41,920
and if i navigate to localhost 5000
9353
06:23:41,920 --> 06:23:44,798
i still should see in a console my log
9354
06:23:44,798 --> 06:23:48,638
and if you do then everything is correct
9355
06:23:48,638 --> 06:23:50,240
so that's the first part
9356
06:23:50,240 --> 06:23:52,320
now the second how can i apply this
9357
06:23:52,320 --> 06:23:53,440
logger
9358
06:23:53,440 --> 06:23:56,638
to all my routes
9359
06:23:56,638 --> 06:23:58,320
so for example
9360
06:23:58,320 --> 06:24:01,200
let me just copy and paste these ones
9361
06:24:01,200 --> 06:24:03,360
and i'll say api
9362
06:24:03,360 --> 06:24:04,958
and then
9363
06:24:04,958 --> 06:24:06,400
product
9364
06:24:06,400 --> 06:24:08,638
and then api
9365
06:24:08,638 --> 06:24:12,240
and i don't know not about maybe items
9366
06:24:12,240 --> 06:24:13,840
again doesn't really matter what you
9367
06:24:13,840 --> 06:24:16,718
place here i'm gonna go with products
9368
06:24:16,718 --> 06:24:19,200
as well as items
9369
06:24:19,200 --> 06:24:22,558
again i can add them manually but the
9370
06:24:22,558 --> 06:24:24,638
more routes i'm going to have
9371
06:24:24,638 --> 06:24:27,600
well the bigger issue this is going to
9372
06:24:27,600 --> 06:24:28,798
become right
9373
06:24:28,798 --> 06:24:30,878
because for every route i need to
9374
06:24:30,878 --> 06:24:33,600
manually add this logger so a better
9375
06:24:33,600 --> 06:24:35,600
solution is this one
9376
06:24:35,600 --> 06:24:38,080
where i select all of them i just remove
9377
06:24:38,080 --> 06:24:41,040
them like so now of course if i go to
9378
06:24:41,040 --> 06:24:42,878
any of these routes i'm not going to
9379
06:24:42,878 --> 06:24:46,000
have anything in a console but
9380
06:24:46,000 --> 06:24:48,958
there is a method by the name of app
9381
06:24:48,958 --> 06:24:52,798
dot use and in that app.use
9382
06:24:52,798 --> 06:24:54,958
this is what we do we pass in the
9383
06:24:54,958 --> 06:24:58,558
middleware so we simply go with logger
9384
06:24:58,558 --> 06:25:00,558
and once i save
9385
06:25:00,558 --> 06:25:04,240
check it out now if i go to about for
9386
06:25:04,240 --> 06:25:07,200
example or if i go to home
9387
06:25:07,200 --> 06:25:09,120
or api
9388
06:25:09,120 --> 06:25:11,680
and then products and hopefully you get
9389
06:25:11,680 --> 06:25:13,280
an idea
9390
06:25:13,280 --> 06:25:16,718
all the time you'll get this log in
9391
06:25:16,718 --> 06:25:19,920
console why well because app.use
9392
06:25:19,920 --> 06:25:23,360
will invoke this for any route now
9393
06:25:23,360 --> 06:25:26,000
please do keep in mind two things first
9394
06:25:26,000 --> 06:25:27,760
order matters here
9395
06:25:27,760 --> 06:25:30,478
if i'm gonna place this below
9396
06:25:30,478 --> 06:25:31,920
app.get
9397
06:25:31,920 --> 06:25:34,478
and if i'm going to try to do that in a
9398
06:25:34,478 --> 06:25:36,638
home page i'm not going to see anything
9399
06:25:36,638 --> 06:25:40,558
in the console why well because i invoke
9400
06:25:40,558 --> 06:25:44,798
my use only after get and express
9401
06:25:44,798 --> 06:25:48,798
everything goes in order so if app get
9402
06:25:48,798 --> 06:25:51,600
is before the used one the one that
9403
06:25:51,600 --> 06:25:54,320
applies to all the routes then yes
9404
06:25:54,320 --> 06:25:56,400
well first we'll hit the home route and
9405
06:25:56,400 --> 06:25:58,400
then we'll send back home so there is no
9406
06:25:58,400 --> 06:26:01,120
logger so that's why you'll see all the
9407
06:26:01,120 --> 06:26:04,320
middleware functions all the app.uses at
9408
06:26:04,320 --> 06:26:06,478
the top of the document
9409
06:26:06,478 --> 06:26:08,878
so you'll have your middleware functions
9410
06:26:08,878 --> 06:26:12,240
first and only then you'll have all your
9411
06:26:12,240 --> 06:26:15,440
roth methods whether that's get post and
9412
06:26:15,440 --> 06:26:17,680
you get an idea so that's the first
9413
06:26:17,680 --> 06:26:19,280
thing that i would like to mention now
9414
06:26:19,280 --> 06:26:20,558
the second thing that i would like to
9415
06:26:20,558 --> 06:26:21,520
mention
9416
06:26:21,520 --> 06:26:25,040
is the fact that we can add here a path
9417
06:26:25,040 --> 06:26:28,160
so if i go to app.use that's my method
9418
06:26:28,160 --> 06:26:29,920
and instead of providing only one
9419
06:26:29,920 --> 06:26:33,200
argument which in my case is the logger
9420
06:26:33,200 --> 06:26:35,680
i can set up a first one and that is
9421
06:26:35,680 --> 06:26:37,760
going to be path and i just need to come
9422
06:26:37,760 --> 06:26:39,920
up with a value now in my case i'm going
9423
06:26:39,920 --> 06:26:41,520
to type api
9424
06:26:41,520 --> 06:26:44,320
and you'll see e in a second why so if
9425
06:26:44,320 --> 06:26:46,878
you save with an api you'll notice
9426
06:26:46,878 --> 06:26:48,878
something interesting where this is
9427
06:26:48,878 --> 06:26:52,000
going to be apply to both of them to the
9428
06:26:52,000 --> 06:26:56,160
products as well as the items so here's
9429
06:26:56,160 --> 06:26:58,718
the deal once you apply this path over
9430
06:26:58,718 --> 06:27:02,478
here basically it's gonna apply to any
9431
06:27:02,478 --> 06:27:05,760
route after this api so for example if i
9432
06:27:05,760 --> 06:27:08,878
go with api and then some crazy one home
9433
06:27:08,878 --> 06:27:12,240
about and then products all the time is
9434
06:27:12,240 --> 06:27:13,760
going to keep on applying this
9435
06:27:13,760 --> 06:27:15,680
middleware so that's something new right
9436
06:27:15,680 --> 06:27:18,798
because previously we worked only with a
9437
06:27:18,798 --> 06:27:21,920
specific route so forward slash or about
9438
06:27:21,920 --> 06:27:23,920
or products here in this case so of
9439
06:27:23,920 --> 06:27:26,160
course these are different now when we
9440
06:27:26,160 --> 06:27:29,760
add this path to use then of course it's
9441
06:27:29,760 --> 06:27:31,840
going to be applied pretty much to
9442
06:27:31,840 --> 06:27:35,040
anything that comes after the path that
9443
06:27:35,040 --> 06:27:36,718
you provide over here so in my case
9444
06:27:36,718 --> 06:27:39,120
since i provided api and of course it's
9445
06:27:39,120 --> 06:27:42,638
going to go for any path that's after
9446
06:27:42,638 --> 06:27:44,638
that now if you want to find out more
9447
06:27:44,638 --> 06:27:48,160
info about app.use i suggest navigating
9448
06:27:48,160 --> 06:27:50,400
to docs again we're looking for api
9449
06:27:50,400 --> 06:27:52,798
reference in our case that is four and
9450
06:27:52,798 --> 06:27:55,280
if you take a look at the app.use
9451
06:27:55,280 --> 06:27:57,680
mounts specified middleware function or
9452
06:27:57,680 --> 06:28:00,160
functions and we're gonna cover that
9453
06:28:00,160 --> 06:28:04,240
later as well at the specified path and
9454
06:28:04,240 --> 06:28:05,920
middleware function is executed when the
9455
06:28:05,920 --> 06:28:09,520
base of the request path matches so this
9456
06:28:09,520 --> 06:28:12,000
is going to be our base
9457
06:28:12,000 --> 06:28:14,798
and then whatever comes after will still
9458
06:28:14,798 --> 06:28:19,360
invoke that middleware function and if
9459
06:28:19,360 --> 06:28:21,600
you omit the path then it's just going
9460
06:28:21,600 --> 06:28:26,080
to be applied to all of your requests so
9461
06:28:26,080 --> 06:28:29,040
if i were to remove the path
9462
06:28:29,040 --> 06:28:31,040
now it's going to be applied to home
9463
06:28:31,040 --> 06:28:33,040
about api product
9464
06:28:33,040 --> 06:28:37,040
as well as api items nice we're familiar
9465
06:28:37,040 --> 06:28:38,798
with app.use
9466
06:28:38,798 --> 06:28:41,520
and now i want to make our example even
9467
06:28:41,520 --> 06:28:43,760
more interesting by adding another
9468
06:28:43,760 --> 06:28:46,080
middleware function and in the process
9469
06:28:46,080 --> 06:28:48,320
we'll take a look at how we can execute
9470
06:28:48,320 --> 06:28:51,440
them in that use what is the syntax in
9471
06:28:51,440 --> 06:28:54,160
order to add it as well as what is the
9472
06:28:54,160 --> 06:28:56,000
execution order
9473
06:28:56,000 --> 06:28:58,878
and let's start simply by creating a new
9474
06:28:58,878 --> 06:29:01,440
file and i'm going to call this
9475
06:29:01,440 --> 06:29:02,958
authorize
9476
06:29:02,958 --> 06:29:06,478
authorize dot js again
9477
06:29:06,478 --> 06:29:09,040
i'll zoom in just so you can see that is
9478
06:29:09,040 --> 06:29:11,520
the file name and in here i'll just set
9479
06:29:11,520 --> 06:29:14,400
up a function similarly to how we worked
9480
06:29:14,400 --> 06:29:16,320
in the logger one so i'm going to go
9481
06:29:16,320 --> 06:29:19,280
with const authorized
9482
06:29:19,280 --> 06:29:21,440
or authorized it doesn't really matter
9483
06:29:21,440 --> 06:29:22,798
now it's going to be a middleware
9484
06:29:22,798 --> 06:29:24,718
function so of course i know that i'll
9485
06:29:24,718 --> 06:29:27,280
have access to rec res
9486
06:29:27,280 --> 06:29:30,478
as well as next and then in function
9487
06:29:30,478 --> 06:29:33,440
body for time being i'll just invoke
9488
06:29:33,440 --> 06:29:36,878
next and maybe i'll cancel log i'll say
9489
06:29:36,878 --> 06:29:39,440
log and authorize
9490
06:29:39,440 --> 06:29:41,440
like so okay let's save that one
9491
06:29:41,440 --> 06:29:43,680
remember that we need to export it so
9492
06:29:43,680 --> 06:29:44,798
module
9493
06:29:44,798 --> 06:29:47,680
exports and that's equal to
9494
06:29:47,680 --> 06:29:50,400
authorized awesome and then in the
9495
06:29:50,400 --> 06:29:53,280
app.js i'll just copy and paste
9496
06:29:53,280 --> 06:29:54,718
and i'll say
9497
06:29:54,718 --> 06:29:56,798
authorize like so
9498
06:29:56,798 --> 06:29:58,638
and of course the file name is also
9499
06:29:58,638 --> 06:30:01,200
different it's not a logger so we can go
9500
06:30:01,200 --> 06:30:03,520
with authorize
9501
06:30:03,520 --> 06:30:05,920
and once i have this setup in place
9502
06:30:05,920 --> 06:30:07,680
the way we execute
9503
06:30:07,680 --> 06:30:11,040
multiple middleware functions in app.use
9504
06:30:11,040 --> 06:30:14,400
we simply place them in the array so i'm
9505
06:30:14,400 --> 06:30:18,478
gonna go with my logger first so logger
9506
06:30:18,478 --> 06:30:21,120
and then we're gonna go with authorize
9507
06:30:21,120 --> 06:30:24,798
and now once you navigate to a localhost
9508
06:30:24,798 --> 06:30:27,120
5000 again any of them in this case
9509
06:30:27,120 --> 06:30:30,320
because notice there is no path
9510
06:30:30,320 --> 06:30:33,600
first in a console i'll see this get and
9511
06:30:33,600 --> 06:30:36,160
then authorize and that's something to
9512
06:30:36,160 --> 06:30:39,360
keep in mind where they will be executed
9513
06:30:39,360 --> 06:30:42,638
in the order so if we flip this one
9514
06:30:42,638 --> 06:30:45,280
if i go with authorized first
9515
06:30:45,280 --> 06:30:48,160
and then i go with logger then of course
9516
06:30:48,160 --> 06:30:51,280
in the console i'll have the opposite
9517
06:30:51,280 --> 06:30:53,440
order where i'm going to have authorized
9518
06:30:53,440 --> 06:30:56,320
first and only then get so that's
9519
06:30:56,320 --> 06:30:58,400
something to keep in mind now i'm going
9520
06:30:58,400 --> 06:31:00,320
to go back to my previous setup the
9521
06:31:00,320 --> 06:31:03,280
logger and then authorized and now let's
9522
06:31:03,280 --> 06:31:05,840
take a look at how we can have the if
9523
06:31:05,840 --> 06:31:08,638
condition in our middleware function now
9524
06:31:08,638 --> 06:31:10,320
before we do anything let me just stress
9525
06:31:10,320 --> 06:31:13,120
something very very important where this
9526
06:31:13,120 --> 06:31:15,440
is just for demonstration purposes this
9527
06:31:15,440 --> 06:31:18,638
is just an example and it's not how
9528
06:31:18,638 --> 06:31:20,878
we're going to authorize
9529
06:31:20,878 --> 06:31:24,000
users in our express applications i just
9530
06:31:24,000 --> 06:31:25,760
don't want to overwhelm you from the get
9531
06:31:25,760 --> 06:31:28,160
go so i'm just going to show you simple
9532
06:31:28,160 --> 06:31:31,200
example using the query string but again
9533
06:31:31,200 --> 06:31:33,200
this is not this is not how we
9534
06:31:33,200 --> 06:31:36,160
authorized users in our express
9535
06:31:36,160 --> 06:31:38,638
applications and with that said i'm
9536
06:31:38,638 --> 06:31:40,638
going to navigate back to my
9537
06:31:40,638 --> 06:31:44,718
authorized middleware and in here
9538
06:31:44,718 --> 06:31:47,840
i'm just going to set up a query string
9539
06:31:47,840 --> 06:31:50,160
so i'm going to say if the user
9540
06:31:50,160 --> 06:31:52,718
provides a query string
9541
06:31:52,718 --> 06:31:57,280
in my url then i'm good to go then i'll
9542
06:31:57,280 --> 06:32:00,478
send back the resource that the user is
9543
06:32:00,478 --> 06:32:03,280
requesting however if the user doesn't
9544
06:32:03,280 --> 06:32:06,400
provide the user query so query string
9545
06:32:06,400 --> 06:32:09,920
parameter in the url then i'll just send
9546
06:32:09,920 --> 06:32:12,478
back 401 which just stands for
9547
06:32:12,478 --> 06:32:15,120
unauthorized so let's start i'm going to
9548
06:32:15,120 --> 06:32:16,638
go with const and i'm going to be
9549
06:32:16,638 --> 06:32:18,718
looking for specific query string i'm
9550
06:32:18,718 --> 06:32:21,680
going to be looking for the user my url
9551
06:32:21,680 --> 06:32:24,240
parameter and of course we know that it
9552
06:32:24,240 --> 06:32:28,320
is available in rec dot query like so
9553
06:32:28,320 --> 06:32:31,760
and i'm going to say if the user exists
9554
06:32:31,760 --> 06:32:33,760
so if it's there with any value it
9555
06:32:33,760 --> 06:32:36,240
doesn't really matter which one or i
9556
06:32:36,240 --> 06:32:37,600
don't know maybe if you want to make it
9557
06:32:37,600 --> 06:32:40,160
more interesting let's go if the user
9558
06:32:40,160 --> 06:32:43,280
equals john okay if that is the case
9559
06:32:43,280 --> 06:32:45,920
then i'm going to go with rec.user
9560
06:32:45,920 --> 06:32:48,080
and notice what i'm doing here
9561
06:32:48,080 --> 06:32:51,040
i'm actually adding a property of user
9562
06:32:51,040 --> 06:32:53,280
onto the rack object
9563
06:32:53,280 --> 06:32:55,360
and i'll show you why it's so powerful
9564
06:32:55,360 --> 06:32:58,080
so i'm going to go with recuser that one
9565
06:32:58,080 --> 06:33:00,080
is equal to
9566
06:33:00,080 --> 06:33:02,558
whatever i mean that could be a object
9567
06:33:02,558 --> 06:33:05,680
for example and i'll say here name john
9568
06:33:05,680 --> 06:33:07,280
again this is just for demonstration
9569
06:33:07,280 --> 06:33:10,400
purposes and then id i don't know four
9570
06:33:10,400 --> 06:33:13,200
or three or whatever so that's my user
9571
06:33:13,200 --> 06:33:16,080
now i still need to call next if i won't
9572
06:33:16,080 --> 06:33:19,040
do that then the whole setup is gonna go
9573
06:33:19,040 --> 06:33:21,600
bananas so i'm gonna go with next here
9574
06:33:21,600 --> 06:33:24,558
and now of course i just need to set up
9575
06:33:24,558 --> 06:33:26,638
a response if
9576
06:33:26,638 --> 06:33:27,520
the
9577
06:33:27,520 --> 06:33:29,760
user or whoever is visiting
9578
06:33:29,760 --> 06:33:33,360
doesn't provide the query string with a
9579
06:33:33,360 --> 06:33:36,958
key of user and then value on john and
9580
06:33:36,958 --> 06:33:38,400
in that case i'm going to go with my
9581
06:33:38,400 --> 06:33:40,718
else and i'll say
9582
06:33:40,718 --> 06:33:41,680
res
9583
06:33:41,680 --> 06:33:45,200
and then let's add a status and the 401
9584
06:33:45,200 --> 06:33:47,520
is going to be for unauthorized and
9585
06:33:47,520 --> 06:33:51,120
let's just say send and simply let's
9586
06:33:51,120 --> 06:33:53,718
try to spell this sucker
9587
06:33:53,718 --> 06:33:56,638
unauthorized let's save that one
9588
06:33:56,638 --> 06:33:58,558
and now notice something interesting
9589
06:33:58,558 --> 06:34:00,478
if i'm going to go for example to my
9590
06:34:00,478 --> 06:34:02,798
home page or product again any of the
9591
06:34:02,798 --> 06:34:05,040
routes because there is no path
9592
06:34:05,040 --> 06:34:08,240
in my app.used check it out i have
9593
06:34:08,240 --> 06:34:11,920
unauthorized and if i inspect and in the
9594
06:34:11,920 --> 06:34:15,520
network tab the same deal i have 401
9595
06:34:15,520 --> 06:34:17,840
because i'm not authorized to access
9596
06:34:17,840 --> 06:34:20,798
this resource i have my authorized
9597
06:34:20,798 --> 06:34:23,200
middleware whereas say you didn't
9598
06:34:23,200 --> 06:34:26,080
provide the query stream parameter with
9599
06:34:26,080 --> 06:34:27,040
a key
9600
06:34:27,040 --> 06:34:29,360
of user and more specifically you didn't
9601
06:34:29,360 --> 06:34:32,320
provide here a value of john
9602
06:34:32,320 --> 06:34:34,558
and since you didn't do that you cannot
9603
06:34:34,558 --> 06:34:36,798
access the resource so now of course if
9604
06:34:36,798 --> 06:34:40,080
we change things around and if i go
9605
06:34:40,080 --> 06:34:42,958
with my question mark and if i say user
9606
06:34:42,958 --> 06:34:44,400
equals john
9607
06:34:44,400 --> 06:34:47,840
what do you know i have my home why well
9608
06:34:47,840 --> 06:34:50,798
because now the condition is met and now
9609
06:34:50,798 --> 06:34:53,360
we just go to next now why it is so
9610
06:34:53,360 --> 06:34:56,160
powerful because in the authorized we
9611
06:34:56,160 --> 06:34:59,360
add the user right so now of course not
9612
06:34:59,360 --> 06:35:02,080
only i can check for that query string
9613
06:35:02,080 --> 06:35:04,638
parameter but and also access this info
9614
06:35:04,638 --> 06:35:06,160
again
9615
06:35:06,160 --> 06:35:08,558
this is just demonstration normally what
9616
06:35:08,558 --> 06:35:11,680
you're going to do you'll check for the
9617
06:35:11,680 --> 06:35:14,718
json web token and then if the token
9618
06:35:14,718 --> 06:35:17,440
exists then we communicate with database
9619
06:35:17,440 --> 06:35:19,920
and actually get the user again this is
9620
06:35:19,920 --> 06:35:22,958
coming up for time being we're simply
9621
06:35:22,958 --> 06:35:25,760
hard coding these values so back in the
9622
06:35:25,760 --> 06:35:28,558
app.js in any of the routes now i'm not
9623
06:35:28,558 --> 06:35:30,638
going to add in all of them but just to
9624
06:35:30,638 --> 06:35:31,680
showcase
9625
06:35:31,680 --> 06:35:34,958
i'm gonna add for example in app items
9626
06:35:34,958 --> 06:35:37,600
i'm gonna set up here a log and i'll
9627
06:35:37,600 --> 06:35:38,798
look for
9628
06:35:38,798 --> 06:35:41,440
rec and user and you'll notice something
9629
06:35:41,440 --> 06:35:44,478
really cool where if i leave this
9630
06:35:44,478 --> 06:35:47,120
query string parameter and if i go to
9631
06:35:47,120 --> 06:35:48,240
api
9632
06:35:48,240 --> 06:35:49,760
and items
9633
06:35:49,760 --> 06:35:51,520
check it out first of all i have the
9634
06:35:51,520 --> 06:35:54,558
items and second in console what do i
9635
06:35:54,558 --> 06:35:57,120
see over here i see that user and that's
9636
06:35:57,120 --> 06:35:59,360
why it's so powerful because we can add
9637
06:35:59,360 --> 06:36:00,878
our middleware
9638
06:36:00,878 --> 06:36:03,120
we can do some kind of functionality and
9639
06:36:03,120 --> 06:36:05,840
now basically i'm attaching this to my
9640
06:36:05,840 --> 06:36:08,400
request object so i have the request
9641
06:36:08,400 --> 06:36:10,638
object and now i'm attaching this
9642
06:36:10,638 --> 06:36:12,878
property here and now in any of my
9643
06:36:12,878 --> 06:36:14,958
routes now of course i went with items
9644
06:36:14,958 --> 06:36:18,080
but in any of my routes i'll have access
9645
06:36:18,080 --> 06:36:21,600
to that user that's why middleware is so
9646
06:36:21,600 --> 06:36:24,798
so crucial and that's why it is a big
9647
06:36:24,798 --> 06:36:27,200
part of express applications because it
9648
06:36:27,200 --> 06:36:29,280
truly allows us to structure our
9649
06:36:29,280 --> 06:36:31,920
applications as lego blocks so we have
9650
06:36:31,920 --> 06:36:33,680
this piece of functionality we have that
9651
06:36:33,680 --> 06:36:35,840
piece of functionality and when we
9652
06:36:35,840 --> 06:36:39,680
combine them we have this nice working
9653
06:36:39,680 --> 06:36:42,000
express server now like i already
9654
06:36:42,000 --> 06:36:43,600
previously mentioned
9655
06:36:43,600 --> 06:36:46,798
middleware is all over express
9656
06:36:46,798 --> 06:36:49,680
so just because we're done with our
9657
06:36:49,680 --> 06:36:51,840
initial introduction doesn't mean that
9658
06:36:51,840 --> 06:36:53,280
you're not going to see middleware
9659
06:36:53,280 --> 06:36:56,160
you'll see it all over the place but
9660
06:36:56,160 --> 06:36:58,080
just to complete our initial
9661
06:36:58,080 --> 06:37:02,000
introduction i want to cover two common
9662
06:37:02,000 --> 06:37:05,440
questions first well if we have access
9663
06:37:05,440 --> 06:37:08,400
right now to app.use since we know how
9664
06:37:08,400 --> 06:37:09,600
to use it
9665
06:37:09,600 --> 06:37:12,558
are we going to add the middleware in a
9666
06:37:12,558 --> 06:37:14,878
route and actually the answer is yes
9667
06:37:14,878 --> 06:37:17,040
because imagine the scenario where
9668
06:37:17,040 --> 06:37:20,400
i don't want to apply this app.use to
9669
06:37:20,400 --> 06:37:21,760
all my routes
9670
06:37:21,760 --> 06:37:25,200
for example i only want to check for
9671
06:37:25,200 --> 06:37:27,920
authorized users in the api forward
9672
06:37:27,920 --> 06:37:31,200
slash items what do i do well i simply
9673
06:37:31,200 --> 06:37:34,558
add my authorized middleware correct so
9674
06:37:34,558 --> 06:37:36,878
now i'll be able to access all these
9675
06:37:36,878 --> 06:37:39,520
routes without any issues but the items
9676
06:37:39,520 --> 06:37:40,878
want yes
9677
06:37:40,878 --> 06:37:44,000
i'll check for that silly query string
9678
06:37:44,000 --> 06:37:46,240
parameter and if you're wondering well
9679
06:37:46,240 --> 06:37:48,478
how we can pass two middlewares if you
9680
06:37:48,478 --> 06:37:51,360
would want in a single route
9681
06:37:51,360 --> 06:37:55,520
same we just simply grab this array so
9682
06:37:55,520 --> 06:37:58,478
we copy and paste and now of course once
9683
06:37:58,478 --> 06:38:01,920
i hit the items not only i will log
9684
06:38:01,920 --> 06:38:04,400
but i'll also authorize
9685
06:38:04,400 --> 06:38:06,400
so let's go back
9686
06:38:06,400 --> 06:38:08,638
and i'm going to take a look where in
9687
06:38:08,638 --> 06:38:10,638
this case everything is fine
9688
06:38:10,638 --> 06:38:14,080
i was able to access the resource since
9689
06:38:14,080 --> 06:38:16,160
i provided the john so notice here name
9690
06:38:16,160 --> 06:38:18,718
john id number three i also have my
9691
06:38:18,718 --> 06:38:21,920
console log but if i were to go
9692
06:38:21,920 --> 06:38:25,120
to just my home page like so i have the
9693
06:38:25,120 --> 06:38:27,760
home and as you can see i don't get
9694
06:38:27,760 --> 06:38:29,680
anything in a console i don't get my
9695
06:38:29,680 --> 06:38:30,958
logger
9696
06:38:30,958 --> 06:38:34,478
and i'm also not looking for any kind of
9697
06:38:34,478 --> 06:38:37,280
authorized user so that's the first
9698
06:38:37,280 --> 06:38:39,600
thing that i would like to cover second
9699
06:38:39,600 --> 06:38:43,120
what are our options when it comes to
9700
06:38:43,120 --> 06:38:46,080
middleware so far we have covered the
9701
06:38:46,080 --> 06:38:48,958
first one our own so we can always set
9702
06:38:48,958 --> 06:38:52,240
up our own middleware now our other two
9703
06:38:52,240 --> 06:38:55,760
options are express so express provides
9704
06:38:55,760 --> 06:38:56,958
quite a few
9705
06:38:56,958 --> 06:38:59,840
built in middleware functions again in
9706
06:38:59,840 --> 06:39:02,080
this case we don't have to worry about
9707
06:39:02,080 --> 06:39:04,080
setting up the functionality we just
9708
06:39:04,080 --> 06:39:06,878
need to reference the docs to see what
9709
06:39:06,878 --> 06:39:09,920
options are provided and then of course
9710
06:39:09,920 --> 06:39:14,320
supply those values and if you remember
9711
06:39:14,320 --> 06:39:17,040
the express app example
9712
06:39:17,040 --> 06:39:20,400
we used app.use and i said put the pin
9713
06:39:20,400 --> 06:39:22,718
on this i'll explain this a little bit
9714
06:39:22,718 --> 06:39:24,638
later if you want you can retype this
9715
06:39:24,638 --> 06:39:26,478
but in my case i'm just going to copy
9716
06:39:26,478 --> 06:39:30,638
this from the for express app.js
9717
06:39:30,638 --> 06:39:34,080
and if we copy and paste and if i place
9718
06:39:34,080 --> 06:39:37,520
it here notice so this is a app.use
9719
06:39:37,520 --> 06:39:40,798
method now what argument the app.user is
9720
06:39:40,798 --> 06:39:43,680
expecting it is expecting a middleware
9721
06:39:43,680 --> 06:39:45,280
so in express
9722
06:39:45,280 --> 06:39:48,798
we have a built in middleware by the
9723
06:39:48,798 --> 06:39:52,160
name of static again somewhere in the
9724
06:39:52,160 --> 06:39:55,760
express source code there is a code
9725
06:39:55,760 --> 06:39:57,520
similar to this now of course this is
9726
06:39:57,520 --> 06:40:00,000
silly they have way more complicated
9727
06:40:00,000 --> 06:40:02,160
setup but hopefully you get the gist so
9728
06:40:02,160 --> 06:40:04,320
there is a method and the only thing
9729
06:40:04,320 --> 06:40:06,320
this method is looking for our public
9730
06:40:06,320 --> 06:40:09,360
folder and then i already told you that
9731
06:40:09,360 --> 06:40:12,478
it basically places all the contents of
9732
06:40:12,478 --> 06:40:14,080
public folder
9733
06:40:14,080 --> 06:40:16,958
as our static assets so they're publicly
9734
06:40:16,958 --> 06:40:19,360
available so we already covered that one
9735
06:40:19,360 --> 06:40:21,680
and now let me show you a simple example
9736
06:40:21,680 --> 06:40:24,958
of third-party middleware now for a
9737
06:40:24,958 --> 06:40:26,638
third-party malware we'll have to
9738
06:40:26,638 --> 06:40:30,000
install it and one of the most popular
9739
06:40:30,000 --> 06:40:32,320
ones i guess for the login is this
9740
06:40:32,320 --> 06:40:35,120
morgan npm so again just go to your
9741
06:40:35,120 --> 06:40:37,920
search engine and type morgan npm that
9742
06:40:37,920 --> 06:40:40,798
is going to direct you to the
9743
06:40:40,798 --> 06:40:43,600
docs i guess for the morgan and as you
9744
06:40:43,600 --> 06:40:47,040
can see in order to install we just run
9745
06:40:47,040 --> 06:40:50,160
npm i morgan so that's how we install
9746
06:40:50,160 --> 06:40:52,718
the package and the syntax is following
9747
06:40:52,718 --> 06:40:55,440
where we just go with morgan we required
9748
06:40:55,440 --> 06:40:58,638
a morgan and then we just pass in morgan
9749
06:40:58,638 --> 06:41:00,240
tiny now of course
9750
06:41:00,240 --> 06:41:03,040
this one we need to set it up in our
9751
06:41:03,040 --> 06:41:06,240
app.use so this is really optional
9752
06:41:06,240 --> 06:41:07,680
you don't have to install it you don't
9753
06:41:07,680 --> 06:41:10,080
have to run it but in order for me to
9754
06:41:10,080 --> 06:41:12,798
hammer this home i will do that so let's
9755
06:41:12,798 --> 06:41:15,280
go back i'll stop my server and remember
9756
06:41:15,280 --> 06:41:17,840
the command was npm
9757
06:41:17,840 --> 06:41:21,600
i and morgan okay i installed this one
9758
06:41:21,600 --> 06:41:23,840
so now of course this package
9759
06:41:23,840 --> 06:41:26,080
is going to be in my dependencies like
9760
06:41:26,080 --> 06:41:28,558
so so not only i have expressed i also
9761
06:41:28,558 --> 06:41:30,000
have the morgan
9762
06:41:30,000 --> 06:41:32,878
and then in the app.js of course i'm
9763
06:41:32,878 --> 06:41:34,958
going to have to require it so i'm going
9764
06:41:34,958 --> 06:41:37,680
to go with const morgan and that is
9765
06:41:37,680 --> 06:41:39,200
equal to
9766
06:41:39,200 --> 06:41:42,320
require and then again i don't have to
9767
06:41:42,320 --> 06:41:43,680
provide the path
9768
06:41:43,680 --> 06:41:45,440
that's the module that i installed so i
9769
06:41:45,440 --> 06:41:47,920
go with morgan and then where i have
9770
06:41:47,920 --> 06:41:49,360
app.use
9771
06:41:49,360 --> 06:41:51,360
and go with app.use since i would want
9772
06:41:51,360 --> 06:41:54,320
to add to all my rows again if you want
9773
06:41:54,320 --> 06:41:56,240
to add for specific one there's a
9774
06:41:56,240 --> 06:41:58,240
different scenario but if you want to
9775
06:41:58,240 --> 06:42:00,558
add for all the routes you just go with
9776
06:42:00,558 --> 06:42:02,000
morgan
9777
06:42:02,000 --> 06:42:03,920
and then they basically give you
9778
06:42:03,920 --> 06:42:05,920
multiple options and the tiny one is
9779
06:42:05,920 --> 06:42:08,080
just going to provide you the most
9780
06:42:08,080 --> 06:42:10,400
essential data and again if you want to
9781
06:42:10,400 --> 06:42:13,120
reference the docs please do so so i'm
9782
06:42:13,120 --> 06:42:15,600
going to go with morgan and now i just
9783
06:42:15,600 --> 06:42:18,718
need to spin up my app and i go with npm
9784
06:42:18,718 --> 06:42:21,120
start now i will remove logger and
9785
06:42:21,120 --> 06:42:24,160
authorized from my api items
9786
06:42:24,160 --> 06:42:26,958
just because i think the terminal is
9787
06:42:26,958 --> 06:42:28,958
getting quite busy
9788
06:42:28,958 --> 06:42:31,600
and now of course once i navigate for
9789
06:42:31,600 --> 06:42:33,760
example home page
9790
06:42:33,760 --> 06:42:36,478
in a console i have get i have forward
9791
06:42:36,478 --> 06:42:38,798
slash now this is a status code and then
9792
06:42:38,798 --> 06:42:41,200
i have 2.8 milliseconds and that
9793
06:42:41,200 --> 06:42:44,000
essentially just signals how long it
9794
06:42:44,000 --> 06:42:48,478
took for the server to respond and if i
9795
06:42:48,478 --> 06:42:51,520
were to go to localhost and forward
9796
06:42:51,520 --> 06:42:54,320
slash about now of course i'll see that
9797
06:42:54,320 --> 06:42:58,160
i have get request i have about the 200
9798
06:42:58,160 --> 06:43:00,320
and now of course time is a bit
9799
06:43:00,320 --> 06:43:02,958
different as well so hopefully this
9800
06:43:02,958 --> 06:43:05,360
gives you a general idea
9801
06:43:05,360 --> 06:43:08,878
why middleware functions are so crucial
9802
06:43:08,878 --> 06:43:12,160
and how powerful they are and yes the
9803
06:43:12,160 --> 06:43:14,558
bigger your express applications will
9804
06:43:14,558 --> 06:43:15,840
get
9805
06:43:15,840 --> 06:43:17,920
most likely the more middleware
9806
06:43:17,920 --> 06:43:20,400
functions you'll have
9807
06:43:20,400 --> 06:43:21,200
now
9808
06:43:21,200 --> 06:43:23,280
they could be your own ones they could
9809
06:43:23,280 --> 06:43:26,320
be the ones that provided by the express
9810
06:43:26,320 --> 06:43:27,200
or
9811
06:43:27,200 --> 06:43:29,760
there's also going to be tons of
9812
06:43:29,760 --> 06:43:32,558
third-party middleware functions
9813
06:43:32,558 --> 06:43:34,718
not bad not bad we have general
9814
06:43:34,718 --> 06:43:37,280
understanding how middleware works in
9815
06:43:37,280 --> 06:43:40,160
express and now i want to cover
9816
06:43:40,160 --> 06:43:43,440
rest of the http methods
9817
06:43:43,440 --> 06:43:44,240
so
9818
06:43:44,240 --> 06:43:46,400
if you remember which seems like an
9819
06:43:46,400 --> 06:43:49,920
eternity ago we covered the cycle the
9820
06:43:49,920 --> 06:43:52,958
request message as well as response and
9821
06:43:52,958 --> 06:43:54,400
in request
9822
06:43:54,400 --> 06:43:55,280
well
9823
06:43:55,280 --> 06:43:57,920
get was by default performed by browser
9824
06:43:57,920 --> 06:44:00,638
but of course we have other methods as
9825
06:44:00,638 --> 06:44:04,160
well and i also provided a second slide
9826
06:44:04,160 --> 06:44:06,638
just for these methods and here you can
9827
06:44:06,638 --> 06:44:10,000
see rest of the methods so get is again
9828
06:44:10,000 --> 06:44:13,040
by default set up in a browser and this
9829
06:44:13,040 --> 06:44:17,360
allows us to read data then we have post
9830
06:44:17,360 --> 06:44:19,760
that is for inserting data so for
9831
06:44:19,760 --> 06:44:23,280
example if we have some kind of api
9832
06:44:23,280 --> 06:44:26,478
orders route we can add an order so user
9833
06:44:26,478 --> 06:44:28,878
makes an order and we just place that
9834
06:44:28,878 --> 06:44:31,600
order on the server so of course
9835
06:44:31,600 --> 06:44:33,600
eventually we would say that in a
9836
06:44:33,600 --> 06:44:36,878
database then we have put if we want to
9837
06:44:36,878 --> 06:44:39,200
update data and of course we also have
9838
06:44:39,200 --> 06:44:42,558
one for deleting the data and before we
9839
06:44:42,558 --> 06:44:45,040
continue let me just mention something
9840
06:44:45,040 --> 06:44:47,680
we're not going to persist this data so
9841
06:44:47,680 --> 06:44:50,320
yes we'll have working examples of post
9842
06:44:50,320 --> 06:44:53,040
put and delete and hopefully i'll be
9843
06:44:53,040 --> 06:44:55,200
able to convey my message
9844
06:44:55,200 --> 06:44:58,160
in a manner where you understand the
9845
06:44:58,160 --> 06:45:00,958
general setup for rest of the methods
9846
06:45:00,958 --> 06:45:02,638
but since we haven't covered how to
9847
06:45:02,638 --> 06:45:05,600
connect to database we won't persist
9848
06:45:05,600 --> 06:45:08,080
this data now i could have went the file
9849
06:45:08,080 --> 06:45:09,440
system route
9850
06:45:09,440 --> 06:45:11,200
where essentially we are persisting this
9851
06:45:11,200 --> 06:45:14,000
data by writing in a file but i thought
9852
06:45:14,000 --> 06:45:16,240
it's a waste of your time because most
9853
06:45:16,240 --> 06:45:17,840
likely you're going to work with
9854
06:45:17,840 --> 06:45:20,478
database anyway so we might as well skip
9855
06:45:20,478 --> 06:45:22,400
that part so once we understand how we
9856
06:45:22,400 --> 06:45:24,798
can connect to the database what are the
9857
06:45:24,798 --> 06:45:27,920
methods by working with data in our
9858
06:45:27,920 --> 06:45:30,320
database then we'll have a proper
9859
06:45:30,320 --> 06:45:33,760
example where we're persisting in
9860
06:45:33,760 --> 06:45:34,958
as well
9861
06:45:34,958 --> 06:45:36,400
and in order to kick things off i'm
9862
06:45:36,400 --> 06:45:38,958
going to navigate back to my address
9863
06:45:38,958 --> 06:45:40,958
again the previous example is going to
9864
06:45:40,958 --> 06:45:43,600
be in the file number 10 middleware
9865
06:45:43,600 --> 06:45:44,718
options
9866
06:45:44,718 --> 06:45:46,478
and again we're just removing pretty
9867
06:45:46,478 --> 06:45:50,240
much everything starting with morgan so
9868
06:45:50,240 --> 06:45:51,920
right after app
9869
06:45:51,920 --> 06:45:54,478
and all the way to
9870
06:45:54,478 --> 06:45:56,080
app.listen
9871
06:45:56,080 --> 06:45:57,200
like so
9872
06:45:57,200 --> 06:45:59,120
now there's not going to be a home route
9873
06:45:59,120 --> 06:46:02,080
however i'm going to go with api and
9874
06:46:02,080 --> 06:46:04,958
people now why do i go with api people
9875
06:46:04,958 --> 06:46:08,320
because in the data file in the data js
9876
06:46:08,320 --> 06:46:11,120
where we had the product
9877
06:46:11,120 --> 06:46:13,680
there's also this one people people is
9878
06:46:13,680 --> 06:46:16,160
an array each person is an object
9879
06:46:16,160 --> 06:46:18,958
there's an id as well as the name as you
9880
06:46:18,958 --> 06:46:21,680
can see very simple example
9881
06:46:21,680 --> 06:46:22,718
so
9882
06:46:22,718 --> 06:46:26,160
with this data array i want to showcase
9883
06:46:26,160 --> 06:46:29,360
how we can read this data how we can add
9884
06:46:29,360 --> 06:46:31,600
for example an extra person
9885
06:46:31,600 --> 06:46:34,160
using the post how we can modify the
9886
06:46:34,160 --> 06:46:35,200
person
9887
06:46:35,200 --> 06:46:38,000
as well as deleting one and now of
9888
06:46:38,000 --> 06:46:40,240
course i want to navigate to app.js and
9889
06:46:40,240 --> 06:46:43,520
first i want to get my people so we
9890
06:46:43,520 --> 06:46:44,798
simply go
9891
06:46:44,798 --> 06:46:47,680
with import i'll structure it right away
9892
06:46:47,680 --> 06:46:50,160
and as a side note since i might make
9893
06:46:50,160 --> 06:46:53,120
some modifications i'll use let in this
9894
06:46:53,120 --> 06:46:54,000
case
9895
06:46:54,000 --> 06:46:56,878
and i'll set it equal to require
9896
06:46:56,878 --> 06:46:59,600
and then data hopefully this part is
9897
06:46:59,600 --> 06:47:02,558
clear since we have covered it quite a
9898
06:47:02,558 --> 06:47:05,600
few times and let's start with the one
9899
06:47:05,600 --> 06:47:07,520
that we already know and that is going
9900
06:47:07,520 --> 06:47:09,920
to be a get method so i'm going to go
9901
06:47:09,920 --> 06:47:12,478
with app dot get like i said i'll
9902
06:47:12,478 --> 06:47:15,600
purposely set up my route with a value
9903
06:47:15,600 --> 06:47:18,558
of api and people so that's going to be
9904
06:47:18,558 --> 06:47:20,718
my path i'll say api
9905
06:47:20,718 --> 06:47:24,400
and then people now this is a get method
9906
06:47:24,400 --> 06:47:26,400
and simply will return
9907
06:47:26,400 --> 06:47:30,400
this array so rec and res and again i'm
9908
06:47:30,400 --> 06:47:33,360
just going to go with res status let's
9909
06:47:33,360 --> 06:47:36,320
hard code the value of 200 then let's
9910
06:47:36,320 --> 06:47:38,878
set up our json and since i want to be
9911
06:47:38,878 --> 06:47:40,958
kind to my users i'll send back the
9912
06:47:40,958 --> 06:47:43,440
object i'll say that the success
9913
06:47:43,440 --> 06:47:46,478
property is true so the request was
9914
06:47:46,478 --> 06:47:49,920
successful and as far as data property
9915
06:47:49,920 --> 06:47:52,718
well i'll set it equal to people so
9916
06:47:52,718 --> 06:47:56,478
let's navigate back to our browser and
9917
06:47:56,478 --> 06:47:59,280
we're going to go with 5000 and of
9918
06:47:59,280 --> 06:48:01,200
course cannot get
9919
06:48:01,200 --> 06:48:03,920
so this is going to be 404 if i just go
9920
06:48:03,920 --> 06:48:05,440
to the homepage
9921
06:48:05,440 --> 06:48:06,878
and instead
9922
06:48:06,878 --> 06:48:09,280
i have a route for api
9923
06:48:09,280 --> 06:48:12,160
and i believe the whole path was people
9924
06:48:12,160 --> 06:48:14,958
like so so we run that
9925
06:48:14,958 --> 06:48:16,478
and now of course i have zoomed in
9926
06:48:16,478 --> 06:48:19,040
already massively here so let me zoom
9927
06:48:19,040 --> 06:48:19,920
out
9928
06:48:19,920 --> 06:48:23,680
in my browser and we should see
9929
06:48:23,680 --> 06:48:26,400
that we're getting our object the
9930
06:48:26,400 --> 06:48:29,120
success property is true beautiful and
9931
06:48:29,120 --> 06:48:31,680
then of course we have the data property
9932
06:48:31,680 --> 06:48:35,360
and in there we have the people's array
9933
06:48:35,360 --> 06:48:37,520
so a bunch of people in here so of
9934
06:48:37,520 --> 06:48:41,280
course that is our first http
9935
06:48:41,280 --> 06:48:42,240
method
9936
06:48:42,240 --> 06:48:45,200
that is the get one where effectively we
9937
06:48:45,200 --> 06:48:47,680
read the data and like i already have
9938
06:48:47,680 --> 06:48:48,558
mentioned
9939
06:48:48,558 --> 06:48:50,400
probably 20 000 times
9940
06:48:50,400 --> 06:48:52,160
that's the default
9941
06:48:52,160 --> 06:48:55,600
method that the browser performs so
9942
06:48:55,600 --> 06:48:58,080
when you set up a request with your
9943
06:48:58,080 --> 06:49:01,600
browser you right away use the http verb
9944
06:49:01,600 --> 06:49:04,638
by the name of get and that's why you
9945
06:49:04,638 --> 06:49:06,958
can read the data
9946
06:49:06,958 --> 06:49:10,478
all right so we're clear with get method
9947
06:49:10,478 --> 06:49:13,200
that's how we can read there however how
9948
06:49:13,200 --> 06:49:16,878
we can add data onto the server so how
9949
06:49:16,878 --> 06:49:17,760
we can
9950
06:49:17,760 --> 06:49:19,120
insert data
9951
06:49:19,120 --> 06:49:22,000
and in order to do that we need to use a
9952
06:49:22,000 --> 06:49:23,280
post method
9953
06:49:23,280 --> 06:49:25,200
but it's not like i can somehow
9954
06:49:25,200 --> 06:49:28,320
configure browser and start making post
9955
06:49:28,320 --> 06:49:31,280
requests unfortunately that's not how it
9956
06:49:31,280 --> 06:49:34,160
works that's why i provided a little bit
9957
06:49:34,160 --> 06:49:37,760
of help now keep in mind one thing where
9958
06:49:37,760 --> 06:49:40,160
after we set up a post method
9959
06:49:40,160 --> 06:49:42,320
effectively once we take a look at two
9960
06:49:42,320 --> 06:49:44,558
flavors of how we can set up post
9961
06:49:44,558 --> 06:49:48,160
request we will install one more tool
9962
06:49:48,160 --> 06:49:49,920
which is going to be crucial
9963
06:49:49,920 --> 06:49:54,240
in our express server development which
9964
06:49:54,240 --> 06:49:56,478
is just going to allow us to
9965
06:49:56,478 --> 06:49:59,360
test everything much faster
9966
06:49:59,360 --> 06:50:02,400
but since for the post i do want to show
9967
06:50:02,400 --> 06:50:04,958
you two flavors that we can have
9968
06:50:04,958 --> 06:50:07,680
we'll use a little bit of static data
9969
06:50:07,680 --> 06:50:09,600
meaning there's an app that i prepared
9970
06:50:09,600 --> 06:50:10,958
but don't worry
9971
06:50:10,958 --> 06:50:12,320
it's not like you'll have to do that
9972
06:50:12,320 --> 06:50:15,440
each and every time to test any of these
9973
06:50:15,440 --> 06:50:16,878
methods and you'll see what i'm talking
9974
06:50:16,878 --> 06:50:19,600
about in the following videos so first
9975
06:50:19,600 --> 06:50:22,878
let's start by jogging our memory a bit
9976
06:50:22,878 --> 06:50:25,920
we have app.use now what
9977
06:50:25,920 --> 06:50:29,280
app.use is doing is just applies the
9978
06:50:29,280 --> 06:50:30,718
middleware
9979
06:50:30,718 --> 06:50:33,440
to all our routes correct and i also
9980
06:50:33,440 --> 06:50:35,760
mentioned that we have this option
9981
06:50:35,760 --> 06:50:38,798
of setting up a public folder
9982
06:50:38,798 --> 06:50:40,798
now we can call whatever we would want
9983
06:50:40,798 --> 06:50:45,280
but for this methods example i prepared
9984
06:50:45,280 --> 06:50:46,200
a
9985
06:50:46,200 --> 06:50:47,840
methods.public
9986
06:50:47,840 --> 06:50:50,638
as you can see it is just a folder with
9987
06:50:50,638 --> 06:50:53,600
index.html with javascript html
9988
06:50:53,600 --> 06:50:55,120
some css
9989
06:50:55,120 --> 06:50:57,680
and once we navigate there of course
9990
06:50:57,680 --> 06:50:59,600
i'll show you how the functionality
9991
06:50:59,600 --> 06:51:02,400
works so this is just prepared so we can
9992
06:51:02,400 --> 06:51:05,360
test those other methods meaning more
9993
06:51:05,360 --> 06:51:07,600
specifically the post method because we
9994
06:51:07,600 --> 06:51:10,080
cannot simply just perform a post
9995
06:51:10,080 --> 06:51:13,360
request from the browser so first let's
9996
06:51:13,360 --> 06:51:14,320
set up
9997
06:51:14,320 --> 06:51:18,080
that methods public as our public folder
9998
06:51:18,080 --> 06:51:19,920
that's step number one so let's set it
9999
06:51:19,920 --> 06:51:22,798
up as our static assets and in order to
10000
06:51:22,798 --> 06:51:24,958
do that i'm just going to add first a
10001
06:51:24,958 --> 06:51:28,240
comment and i'll say assets and then i'm
10002
06:51:28,240 --> 06:51:31,360
going to go with app.use again remember
10003
06:51:31,360 --> 06:51:33,920
that is our method and this is going to
10004
06:51:33,920 --> 06:51:36,320
be a built in middleware
10005
06:51:36,320 --> 06:51:39,520
and the name is express static so i just
10006
06:51:39,520 --> 06:51:42,478
grab my express instance i look for the
10007
06:51:42,478 --> 06:51:45,520
method of static and if you remember we
10008
06:51:45,520 --> 06:51:48,080
just need to provide here a path
10009
06:51:48,080 --> 06:51:50,878
and instead of going public or static
10010
06:51:50,878 --> 06:51:53,120
like i showed you previously i'm just
10011
06:51:53,120 --> 06:51:55,840
going to go with forward slash and then
10012
06:51:55,840 --> 06:51:58,240
methods and then public
10013
06:51:58,240 --> 06:52:02,080
because this is where i set up my static
10014
06:52:02,080 --> 06:52:06,558
files for these examples we invoke it
10015
06:52:06,558 --> 06:52:08,958
and now if we go to the home page we
10016
06:52:08,958 --> 06:52:11,200
shouldn't get this error so once you
10017
06:52:11,200 --> 06:52:12,160
invoke
10018
06:52:12,160 --> 06:52:14,718
there it is we have a
10019
06:52:14,718 --> 06:52:18,240
somewhat decent looking html file with
10020
06:52:18,240 --> 06:52:21,200
two options we have this javascript
10021
06:52:21,200 --> 06:52:23,920
option which we're going to cover second
10022
06:52:23,920 --> 06:52:26,878
and first one is going to be a
10023
06:52:26,878 --> 06:52:30,320
traditional form example and again the
10024
06:52:30,320 --> 06:52:32,958
reason why we do that
10025
06:52:32,958 --> 06:52:34,958
is because you cannot simply just
10026
06:52:34,958 --> 06:52:36,160
configure
10027
06:52:36,160 --> 06:52:38,000
your browser
10028
06:52:38,000 --> 06:52:39,120
to perform
10029
06:52:39,120 --> 06:52:41,920
a post request you either need to use a
10030
06:52:41,920 --> 06:52:44,000
tool which we're going to install by the
10031
06:52:44,000 --> 06:52:46,840
name of postman or
10032
06:52:46,840 --> 06:52:49,680
insomnia here's another popular one
10033
06:52:49,680 --> 06:52:52,958
or you need to set up basically a
10034
06:52:52,958 --> 06:52:54,638
working application
10035
06:52:54,638 --> 06:52:57,440
and if you take a look in the methods
10036
06:52:57,440 --> 06:52:59,840
public we have this working application
10037
06:52:59,840 --> 06:53:01,520
all right and once we have the basic
10038
06:53:01,520 --> 06:53:04,320
setup in place now let's take a look at
10039
06:53:04,320 --> 06:53:07,920
our two flavors when it comes to the
10040
06:53:07,920 --> 06:53:09,360
post method
10041
06:53:09,360 --> 06:53:13,920
and i want you to navigate to index html
10042
06:53:13,920 --> 06:53:17,040
and in there you'll find pretty typical
10043
06:53:17,040 --> 06:53:20,160
index.html file with some styling
10044
06:53:20,160 --> 06:53:23,360
with nav as well as deform
10045
06:53:23,360 --> 06:53:26,400
and in form we have two attributes we
10046
06:53:26,400 --> 06:53:28,478
have action one
10047
06:53:28,478 --> 06:53:30,638
as well as the method and as you can
10048
06:53:30,638 --> 06:53:33,360
clearly see the value for the method is
10049
06:53:33,360 --> 06:53:34,240
post
10050
06:53:34,240 --> 06:53:36,478
and this action one just says
10051
06:53:36,478 --> 06:53:39,200
where we're going to send it and notice
10052
06:53:39,200 --> 06:53:41,680
now we have forward slash login so that
10053
06:53:41,680 --> 06:53:42,718
means that
10054
06:53:42,718 --> 06:53:45,920
this url this path is on our server but
10055
06:53:45,920 --> 06:53:48,718
of course we know that in the app.js
10056
06:53:48,718 --> 06:53:50,320
we're not handling that
10057
06:53:50,320 --> 06:53:52,240
so for the form there's going to be one
10058
06:53:52,240 --> 06:53:54,558
path and then for the second example
10059
06:53:54,558 --> 06:53:56,798
when we used straight up javascript then
10060
06:53:56,798 --> 06:53:58,638
of course there's going to be a
10061
06:53:58,638 --> 06:54:01,600
different path for that so this is going
10062
06:54:01,600 --> 06:54:03,280
to be the url or path where we're
10063
06:54:03,280 --> 06:54:05,360
submitting the form and inside of the
10064
06:54:05,360 --> 06:54:08,080
form we have label we have input
10065
06:54:08,080 --> 06:54:09,360
so basically
10066
06:54:09,360 --> 06:54:11,760
we have name attribute for this one we
10067
06:54:11,760 --> 06:54:13,280
have the id
10068
06:54:13,280 --> 06:54:16,240
as well as the autocomplete so we'll
10069
06:54:16,240 --> 06:54:18,878
type in some kind of value and then
10070
06:54:18,878 --> 06:54:21,040
we'll send this to
10071
06:54:21,040 --> 06:54:23,600
forward slash login so localhost and
10072
06:54:23,600 --> 06:54:26,080
then forward slash login and of course
10073
06:54:26,080 --> 06:54:29,680
we also have a submit button so now
10074
06:54:29,680 --> 06:54:32,958
let's start by taking a look what
10075
06:54:32,958 --> 06:54:35,840
happens when we simply submit this form
10076
06:54:35,840 --> 06:54:38,400
so go here type some kind of gibberish
10077
06:54:38,400 --> 06:54:40,878
in my case i'm going to go with john
10078
06:54:40,878 --> 06:54:44,160
bravely press enter and of course we do
10079
06:54:44,160 --> 06:54:47,920
have kind of post login okay that's fine
10080
06:54:47,920 --> 06:54:49,520
we're not expecting anything anyway
10081
06:54:49,520 --> 06:54:52,240
right we know that there is no route
10082
06:54:52,240 --> 06:54:55,120
that handles the post
10083
06:54:55,120 --> 06:54:56,798
for the login
10084
06:54:56,798 --> 06:54:59,360
but what's really interesting here
10085
06:54:59,360 --> 06:55:02,878
is in the network tab of course i can
10086
06:55:02,878 --> 06:55:03,680
see
10087
06:55:03,680 --> 06:55:05,200
my request
10088
06:55:05,200 --> 06:55:08,080
in this case this is login now this is
10089
06:55:08,080 --> 06:55:10,798
404 and again i'm going to repeat this
10090
06:55:10,798 --> 06:55:13,920
20 000 times but check out the method so
10091
06:55:13,920 --> 06:55:15,120
method is not
10092
06:55:15,120 --> 06:55:17,760
get method is post and then we're going
10093
06:55:17,760 --> 06:55:21,200
to localhost 5000 and then forward slash
10094
06:55:21,200 --> 06:55:24,400
login now what's also really interesting
10095
06:55:24,400 --> 06:55:26,638
all the way in the bottom
10096
06:55:26,638 --> 06:55:29,120
what do you see here you see this
10097
06:55:29,120 --> 06:55:30,638
forum data
10098
06:55:30,638 --> 06:55:33,520
and then we have key value pair we have
10099
06:55:33,520 --> 06:55:37,600
name and john and if you remember
10100
06:55:37,600 --> 06:55:40,638
when we talked about http messages i
10101
06:55:40,638 --> 06:55:42,798
said that the body is optional so for
10102
06:55:42,798 --> 06:55:44,478
example when we're sending a get request
10103
06:55:44,478 --> 06:55:46,080
we're not sending a body
10104
06:55:46,080 --> 06:55:48,558
but when we're sending a post request
10105
06:55:48,558 --> 06:55:51,680
it's very crucial so if i want to add
10106
06:55:51,680 --> 06:55:54,160
something onto the server
10107
06:55:54,160 --> 06:55:56,878
of course i want to get that data
10108
06:55:56,878 --> 06:55:58,958
i mean otherwise how i'm going to know
10109
06:55:58,958 --> 06:56:01,760
where to add to the server doesn't that
10110
06:56:01,760 --> 06:56:04,718
make sense i hope it does so let's go
10111
06:56:04,718 --> 06:56:08,080
back okay i see this name and i see john
10112
06:56:08,080 --> 06:56:10,240
and if you're not familiar basically the
10113
06:56:10,240 --> 06:56:12,718
way it works is whatever you provide
10114
06:56:12,718 --> 06:56:13,600
here
10115
06:56:13,600 --> 06:56:16,878
for the name is going to be a key
10116
06:56:16,878 --> 06:56:19,120
so for example if i'm going to set this
10117
06:56:19,120 --> 06:56:20,240
up as
10118
06:56:20,240 --> 06:56:23,200
testing you'll see that now when we
10119
06:56:23,200 --> 06:56:25,440
perform the same request the same post
10120
06:56:25,440 --> 06:56:27,680
request again with the same value
10121
06:56:27,680 --> 06:56:29,680
different value it doesn't really matter
10122
06:56:29,680 --> 06:56:31,600
now of course that key
10123
06:56:31,600 --> 06:56:34,478
name is going to be testing so whatever
10124
06:56:34,478 --> 06:56:36,160
value you provide over here that is
10125
06:56:36,160 --> 06:56:38,160
going to be the key and of course the
10126
06:56:38,160 --> 06:56:41,600
value is whatever you're submitting so
10127
06:56:41,600 --> 06:56:42,798
in my case
10128
06:56:42,798 --> 06:56:46,958
it's either peter or john okay
10129
06:56:46,958 --> 06:56:48,478
that's out of the way so i'm going to
10130
06:56:48,478 --> 06:56:51,520
change it back to name and now we need
10131
06:56:51,520 --> 06:56:53,200
to understand something
10132
06:56:53,200 --> 06:56:56,240
where the request is coming in
10133
06:56:56,240 --> 06:56:59,200
but a we're not handling that
10134
06:56:59,200 --> 06:57:02,320
and b we don't have the middleware that
10135
06:57:02,320 --> 06:57:06,558
actually adds this data that the form in
10136
06:57:06,558 --> 06:57:10,718
this case is sending to our request so
10137
06:57:10,718 --> 06:57:12,558
let's see how we can fix the first one
10138
06:57:12,558 --> 06:57:14,878
and we simply need to come up with a
10139
06:57:14,878 --> 06:57:17,200
round now in this case remember we're
10140
06:57:17,200 --> 06:57:19,920
dealing with post this is not get
10141
06:57:19,920 --> 06:57:22,558
and we're going to go with forward slash
10142
06:57:22,558 --> 06:57:25,280
and then log in here because of course
10143
06:57:25,280 --> 06:57:27,600
that's what i have in my form again we
10144
06:57:27,600 --> 06:57:29,920
go with rec and res
10145
06:57:29,920 --> 06:57:32,798
and technically we can come up with some
10146
06:57:32,798 --> 06:57:35,360
kind of info i can say send
10147
06:57:35,360 --> 06:57:36,160
and
10148
06:57:36,160 --> 06:57:38,320
i'm simply going to say post
10149
06:57:38,320 --> 06:57:41,120
and that's it just so we can save time
10150
06:57:41,120 --> 06:57:44,000
on typing again let's navigate back and
10151
06:57:44,000 --> 06:57:47,360
in this case let's try susan let's send
10152
06:57:47,360 --> 06:57:50,878
it yeah i see the post so i don't have
10153
06:57:50,878 --> 06:57:54,958
the 400 anymore or 404 so that's cool
10154
06:57:54,958 --> 06:57:58,080
but unfortunately i have no access at
10155
06:57:58,080 --> 06:58:00,798
this point to whatever
10156
06:58:00,798 --> 06:58:02,400
i'm being sent
10157
06:58:02,400 --> 06:58:04,240
so unfortunately i cannot access that
10158
06:58:04,240 --> 06:58:07,360
susan or john or whatever value so
10159
06:58:07,360 --> 06:58:08,798
unfortunately
10160
06:58:08,798 --> 06:58:12,718
i cannot add for example to my list if i
10161
06:58:12,718 --> 06:58:15,600
wanted to now how do we add it well this
10162
06:58:15,600 --> 06:58:18,000
is where the middleware comes in so
10163
06:58:18,000 --> 06:58:20,558
previously we covered static assets with
10164
06:58:20,558 --> 06:58:22,160
express static
10165
06:58:22,160 --> 06:58:25,520
in order to get the form data
10166
06:58:25,520 --> 06:58:29,600
we need to go with url and coded
10167
06:58:29,600 --> 06:58:32,240
middleware so i'll add here
10168
06:58:32,240 --> 06:58:33,760
parse
10169
06:58:33,760 --> 06:58:36,478
form data and essentially this is just
10170
06:58:36,478 --> 06:58:38,638
going to parse that data and add the
10171
06:58:38,638 --> 06:58:40,440
values to
10172
06:58:40,440 --> 06:58:45,040
rec.body so where we have a post request
10173
06:58:45,040 --> 06:58:48,878
in the rec.body property we'll find all
10174
06:58:48,878 --> 06:58:50,160
the info
10175
06:58:50,160 --> 06:58:51,680
and in order to use this middleware
10176
06:58:51,680 --> 06:58:53,280
surprise surprise we're going to use
10177
06:58:53,280 --> 06:58:55,440
app.use
10178
06:58:55,440 --> 06:58:58,558
and then we'll pass in express
10179
06:58:58,558 --> 06:59:02,240
and then url encoded and then you also
10180
06:59:02,240 --> 06:59:05,920
pass in flag where you go with extended
10181
06:59:05,920 --> 06:59:08,878
and you set up false and if you're
10182
06:59:08,878 --> 06:59:10,718
wondering why are we adding this
10183
06:59:10,718 --> 06:59:13,120
extended false flag or you just want to
10184
06:59:13,120 --> 06:59:17,600
get more info about this url in coded
10185
06:59:17,600 --> 06:59:20,718
middleware simply let's head back to the
10186
06:59:20,718 --> 06:59:22,798
documentation in this case we're looking
10187
06:59:22,798 --> 06:59:25,680
for the express and notice here i have
10188
06:59:25,680 --> 06:59:30,240
express url encoded this is a built-in
10189
06:59:30,240 --> 06:59:32,878
middleware function in express it parses
10190
06:59:32,878 --> 06:59:36,240
incoming requests with url and coded
10191
06:59:36,240 --> 06:59:39,280
payloads and it is based on the body
10192
06:59:39,280 --> 06:59:42,958
parser so in the previous versions of
10193
06:59:42,958 --> 06:59:45,920
express you have to actually install the
10194
06:59:45,920 --> 06:59:48,160
body parser now in this case you don't
10195
06:59:48,160 --> 06:59:51,040
it already comes built in and as far as
10196
06:59:51,040 --> 06:59:53,760
this extended this option allows to
10197
06:59:53,760 --> 06:59:56,240
choose between parsing url encoded data
10198
06:59:56,240 --> 06:59:59,280
with query string library when false
10199
06:59:59,280 --> 07:00:00,160
meaning
10200
07:00:00,160 --> 07:00:05,280
this is our case or the qs library when
10201
07:00:05,280 --> 07:00:07,920
true again i wouldn't lose my sleep over
10202
07:00:07,920 --> 07:00:10,638
this one then the common approach is
10203
07:00:10,638 --> 07:00:13,920
using this extended and false and once
10204
07:00:13,920 --> 07:00:16,160
we can access it then we can do some
10205
07:00:16,160 --> 07:00:19,040
cool functionality so first let's just
10206
07:00:19,040 --> 07:00:21,120
scroll down a bit where we have the
10207
07:00:21,120 --> 07:00:23,040
login and first i'm just going to start
10208
07:00:23,040 --> 07:00:25,200
by console logging rec
10209
07:00:25,200 --> 07:00:27,920
and body let's save that one
10210
07:00:27,920 --> 07:00:31,840
and again back to our browser
10211
07:00:31,840 --> 07:00:34,240
again i'm going to go with susan this
10212
07:00:34,240 --> 07:00:38,000
was 200 so everything was successful and
10213
07:00:38,000 --> 07:00:41,040
now of course in the right dot body i
10214
07:00:41,040 --> 07:00:45,200
also have a name and the value is susan
10215
07:00:45,200 --> 07:00:46,080
so
10216
07:00:46,080 --> 07:00:48,160
now we can start doing something with
10217
07:00:48,160 --> 07:00:50,718
this data now i purposely set up two
10218
07:00:50,718 --> 07:00:52,160
examples
10219
07:00:52,160 --> 07:00:54,080
one is going to be for form which of
10220
07:00:54,080 --> 07:00:56,558
course is this one and then one is for
10221
07:00:56,558 --> 07:00:59,200
javascript now with the javascript
10222
07:00:59,200 --> 07:01:01,840
example this is where we'll actually
10223
07:01:01,840 --> 07:01:04,718
manipulate this people array fourth time
10224
07:01:04,718 --> 07:01:07,120
being with login this is a separate
10225
07:01:07,120 --> 07:01:09,040
issue we're just dealing with the form
10226
07:01:09,040 --> 07:01:09,840
so
10227
07:01:09,840 --> 07:01:11,280
we'll just try to make things
10228
07:01:11,280 --> 07:01:14,320
interesting by checking for the name
10229
07:01:14,320 --> 07:01:16,718
if the name is provided then we'll send
10230
07:01:16,718 --> 07:01:19,600
back welcome and then pass in that name
10231
07:01:19,600 --> 07:01:21,280
if the name is not provided so for
10232
07:01:21,280 --> 07:01:23,680
example if the user
10233
07:01:23,680 --> 07:01:26,558
is trying to submit the form empty for
10234
07:01:26,558 --> 07:01:30,160
example like so we'll just send back 401
10235
07:01:30,160 --> 07:01:32,400
please provide credentials again
10236
07:01:32,400 --> 07:01:34,558
please keep in mind this is just a
10237
07:01:34,558 --> 07:01:37,040
example and i'm fully aware that we can
10238
07:01:37,040 --> 07:01:39,680
check for empty values here on the front
10239
07:01:39,680 --> 07:01:42,638
end again the goal of this example is to
10240
07:01:42,638 --> 07:01:44,958
show you how to handle this on the
10241
07:01:44,958 --> 07:01:47,840
server so i have wrecked that body okay
10242
07:01:47,840 --> 07:01:50,718
i can see that in this case i have
10243
07:01:50,718 --> 07:01:53,440
name but it is empty so now of course
10244
07:01:53,440 --> 07:01:56,080
let's set up our logic first i'm going
10245
07:01:56,080 --> 07:01:58,400
to go with const name and that one is
10246
07:01:58,400 --> 07:02:00,558
equal to rec
10247
07:02:00,558 --> 07:02:03,360
that body so i'm destructuring my name
10248
07:02:03,360 --> 07:02:05,920
property and i'll just check if the name
10249
07:02:05,920 --> 07:02:09,440
exists whatever the value that could be
10250
07:02:09,440 --> 07:02:10,080
b
10251
07:02:10,080 --> 07:02:12,638
that could be bobby lee not could be
10252
07:02:12,638 --> 07:02:15,600
again taco burrito whatever you want and
10253
07:02:15,600 --> 07:02:17,680
as you can see i'm getting quite hungry
10254
07:02:17,680 --> 07:02:20,478
so let's go with return res
10255
07:02:20,478 --> 07:02:22,718
dot status
10256
07:02:22,718 --> 07:02:26,400
then let's set up a 200
10257
07:02:26,400 --> 07:02:29,280
which just says that we were successful
10258
07:02:29,280 --> 07:02:31,520
and we're gonna go with send
10259
07:02:31,520 --> 07:02:34,400
and we'll set up a template string
10260
07:02:34,400 --> 07:02:37,360
welcome and i'm just going to access my
10261
07:02:37,360 --> 07:02:38,400
name
10262
07:02:38,400 --> 07:02:40,638
so if there's some value i'll send back
10263
07:02:40,638 --> 07:02:44,000
200 and i'll say welcome whatever is the
10264
07:02:44,000 --> 07:02:45,600
value if not
10265
07:02:45,600 --> 07:02:48,638
well here we're going to go with res
10266
07:02:48,638 --> 07:02:51,680
and let's just add status here and again
10267
07:02:51,680 --> 07:02:54,320
just for kicks i'm going to go with 401
10268
07:02:54,320 --> 07:02:56,878
and we're going to go with send and of
10269
07:02:56,878 --> 07:02:59,280
course let's say please
10270
07:02:59,280 --> 07:03:00,638
provide
10271
07:03:00,638 --> 07:03:02,638
and credentials
10272
07:03:02,638 --> 07:03:03,760
like so
10273
07:03:03,760 --> 07:03:07,040
let's save this one and now we should
10274
07:03:07,040 --> 07:03:10,080
truly see how it works in the action
10275
07:03:10,080 --> 07:03:14,718
now i'll leave i guess the tab for now
10276
07:03:14,718 --> 07:03:17,120
and let's just try to submit so for
10277
07:03:17,120 --> 07:03:21,120
example if i submit with empty values
10278
07:03:21,120 --> 07:03:22,558
i'm going to say please provide
10279
07:03:22,558 --> 07:03:25,120
credentials and as far as my login it is
10280
07:03:25,120 --> 07:03:26,478
401
10281
07:03:26,478 --> 07:03:28,958
that's what the server is sending back
10282
07:03:28,958 --> 07:03:31,520
however if i type in some kind of value
10283
07:03:31,520 --> 07:03:34,478
for example anna comes to mind i have
10284
07:03:34,478 --> 07:03:37,440
welcome and anna so that's our first
10285
07:03:37,440 --> 07:03:40,638
flavor when it comes to post request
10286
07:03:40,638 --> 07:03:42,878
where we use the form and again this is
10287
07:03:42,878 --> 07:03:45,360
just front end where we come up with an
10288
07:03:45,360 --> 07:03:48,080
action and since this index.html is on
10289
07:03:48,080 --> 07:03:50,478
the same server we simply go with
10290
07:03:50,478 --> 07:03:54,400
forward slash and login of course if
10291
07:03:54,400 --> 07:03:57,200
your front-end application is separate
10292
07:03:57,200 --> 07:03:59,440
from your server then you're going to
10293
07:03:59,440 --> 07:04:03,280
provide a full path where the server is
10294
07:04:03,280 --> 07:04:06,878
hosted so basically a full domain and
10295
07:04:06,878 --> 07:04:09,360
then we go with method post now this is
10296
07:04:09,360 --> 07:04:11,280
technically a front-end part but it is
10297
07:04:11,280 --> 07:04:14,320
crucial that we understand it so then we
10298
07:04:14,320 --> 07:04:18,320
perform of course a post request we hit
10299
07:04:18,320 --> 07:04:20,240
our url
10300
07:04:20,240 --> 07:04:23,440
so in this case we hit our forward slash
10301
07:04:23,440 --> 07:04:25,680
login and this is the part where we
10302
07:04:25,680 --> 07:04:27,600
handle that on a server
10303
07:04:27,600 --> 07:04:30,478
in order to get that data
10304
07:04:30,478 --> 07:04:33,200
whatever we are being sent we need to
10305
07:04:33,200 --> 07:04:36,718
use a middleware this one is built into
10306
07:04:36,718 --> 07:04:38,878
express so again we simply just need to
10307
07:04:38,878 --> 07:04:39,600
use
10308
07:04:39,600 --> 07:04:42,798
app.use which is going to apply this
10309
07:04:42,798 --> 07:04:46,718
middleware to all our incoming requests
10310
07:04:46,718 --> 07:04:49,360
and then we just pass an extended false
10311
07:04:49,360 --> 07:04:52,080
flag and the moment we do that
10312
07:04:52,080 --> 07:04:53,680
indirect.body
10313
07:04:53,680 --> 07:04:56,958
will have access to our form values so
10314
07:04:56,958 --> 07:04:58,000
whatever
10315
07:04:58,000 --> 07:05:00,878
is set up here as a value that is going
10316
07:05:00,878 --> 07:05:03,760
to be our key and whatever is passed
10317
07:05:03,760 --> 07:05:06,320
into our form well of course that is
10318
07:05:06,320 --> 07:05:08,080
going to be the actual value that we're
10319
07:05:08,080 --> 07:05:10,878
getting so if i go here with name i'm
10320
07:05:10,878 --> 07:05:14,080
gonna see either susan anna or whatever
10321
07:05:14,080 --> 07:05:16,000
user provides now like i already
10322
07:05:16,000 --> 07:05:17,760
previously mentioned this is just for
10323
07:05:17,760 --> 07:05:20,878
demonstration this is just so we have a
10324
07:05:20,878 --> 07:05:23,920
bit more interesting example e instead
10325
07:05:23,920 --> 07:05:27,120
of just sending back the name
10326
07:05:27,120 --> 07:05:30,000
so hopefully this part is clear
10327
07:05:30,000 --> 07:05:32,718
and now we can focus on our second
10328
07:05:32,718 --> 07:05:36,638
flavor where we use javascript to
10329
07:05:36,638 --> 07:05:40,400
send the request all right and once we
10330
07:05:40,400 --> 07:05:43,280
have the form set up out of the way now
10331
07:05:43,280 --> 07:05:45,040
let's take a look at the javascript
10332
07:05:45,040 --> 07:05:47,600
option before we do though there's one
10333
07:05:47,600 --> 07:05:50,080
more thing that i would like to mention
10334
07:05:50,080 --> 07:05:52,878
if you take a look at the headers
10335
07:05:52,878 --> 07:05:55,440
you'll notice something interesting
10336
07:05:55,440 --> 07:05:58,718
where we have a content type for the
10337
07:05:58,718 --> 07:06:01,280
request header and of course this is
10338
07:06:01,280 --> 07:06:03,680
going to be application
10339
07:06:03,680 --> 07:06:07,600
and then we have this form url encoded
10340
07:06:07,600 --> 07:06:08,878
and the reason why i'm showing that
10341
07:06:08,878 --> 07:06:11,520
because of course for javascript it is
10342
07:06:11,520 --> 07:06:14,478
going to be a bit different so just keep
10343
07:06:14,478 --> 07:06:18,000
this one for your reference now
10344
07:06:18,000 --> 07:06:20,400
let's navigate back to our
10345
07:06:20,400 --> 07:06:23,840
static files basically to our front end
10346
07:06:23,840 --> 07:06:25,600
and now let's take a look at the
10347
07:06:25,600 --> 07:06:27,120
javascript approach
10348
07:06:27,120 --> 07:06:29,040
and interestingly here
10349
07:06:29,040 --> 07:06:30,798
where i have the javascript i do have
10350
07:06:30,798 --> 07:06:32,000
the form
10351
07:06:32,000 --> 07:06:33,600
keep in mind we'll still handle this
10352
07:06:33,600 --> 07:06:35,840
form but in this case it's going to be
10353
07:06:35,840 --> 07:06:38,240
done strictly using javascript and we'll
10354
07:06:38,240 --> 07:06:42,638
use javascript to send our http requests
10355
07:06:42,638 --> 07:06:44,400
and that is very very crucial that is
10356
07:06:44,400 --> 07:06:46,240
going to be the difference where instead
10357
07:06:46,240 --> 07:06:49,040
of form now i'll be sending that request
10358
07:06:49,040 --> 07:06:50,798
using javascript
10359
07:06:50,798 --> 07:06:52,400
and of course the content type will be
10360
07:06:52,400 --> 07:06:54,798
different that's why i showed you the
10361
07:06:54,798 --> 07:06:57,440
form example but what you'll also notice
10362
07:06:57,440 --> 07:07:00,718
that right away we have our list and if
10363
07:07:00,718 --> 07:07:01,920
you don't believe me you can double
10364
07:07:01,920 --> 07:07:05,040
check in data we have john peter susan
10365
07:07:05,040 --> 07:07:08,478
anna and emma so what's happening well
10366
07:07:08,478 --> 07:07:11,200
let's go to javascript html
10367
07:07:11,200 --> 07:07:13,840
this is where we have our frontend logic
10368
07:07:13,840 --> 07:07:16,000
and again let's take a look at our index
10369
07:07:16,000 --> 07:07:17,120
html
10370
07:07:17,120 --> 07:07:19,520
a little bit of css
10371
07:07:19,520 --> 07:07:20,558
nav
10372
07:07:20,558 --> 07:07:22,080
and then we have the form now in this
10373
07:07:22,080 --> 07:07:24,798
case i don't have the action and i don't
10374
07:07:24,798 --> 07:07:26,080
have the method
10375
07:07:26,080 --> 07:07:28,718
okay interesting so i have name
10376
07:07:28,718 --> 07:07:31,520
attribute it still has a value of name
10377
07:07:31,520 --> 07:07:36,000
so i should expect probably that this is
10378
07:07:36,000 --> 07:07:37,760
going to be the value that's coming in
10379
07:07:37,760 --> 07:07:40,240
with my request then i have a little bit
10380
07:07:40,240 --> 07:07:42,160
of form alert this is where we'll
10381
07:07:42,160 --> 07:07:44,638
display a little bit of functionality as
10382
07:07:44,638 --> 07:07:46,558
well as the result
10383
07:07:46,558 --> 07:07:49,280
now if you're not familiar
10384
07:07:49,280 --> 07:07:51,920
there's a package called axios which
10385
07:07:51,920 --> 07:07:54,320
essentially just makes it easier to set
10386
07:07:54,320 --> 07:07:56,718
up http requests
10387
07:07:56,718 --> 07:07:57,600
so
10388
07:07:57,600 --> 07:08:00,080
instead of using the built-in fetch
10389
07:08:00,080 --> 07:08:02,320
which i could have used i think axios
10390
07:08:02,320 --> 07:08:05,840
provides a cleaner api and better error
10391
07:08:05,840 --> 07:08:06,958
messages
10392
07:08:06,958 --> 07:08:09,360
and since i don't want to spend three
10393
07:08:09,360 --> 07:08:13,120
hours on explaining all this code
10394
07:08:13,120 --> 07:08:17,440
i simply went with axios and the way i
10395
07:08:17,440 --> 07:08:21,360
set it up i just set up the cdn link so
10396
07:08:21,360 --> 07:08:23,680
this just gets me the axios library and
10397
07:08:23,680 --> 07:08:25,920
then the moment i install it of course i
10398
07:08:25,920 --> 07:08:29,920
have axios in my front-end project
10399
07:08:29,920 --> 07:08:31,520
please keep that in mind those are two
10400
07:08:31,520 --> 07:08:34,320
separate things this is a front-end
10401
07:08:34,320 --> 07:08:35,520
project and
10402
07:08:35,520 --> 07:08:37,840
in order not to confuse you more i just
10403
07:08:37,840 --> 07:08:40,638
set up a few script tags instead of
10404
07:08:40,638 --> 07:08:44,400
going with a separate javascript font so
10405
07:08:44,400 --> 07:08:46,320
as you can see here i'm selecting the
10406
07:08:46,320 --> 07:08:47,360
result
10407
07:08:47,360 --> 07:08:49,360
so that is my div
10408
07:08:49,360 --> 07:08:52,638
and i have this fetch people
10409
07:08:52,638 --> 07:08:55,920
so that is the function that fetches
10410
07:08:55,920 --> 07:08:59,280
people from my server now notice the
10411
07:08:59,280 --> 07:09:00,400
path
10412
07:09:00,400 --> 07:09:02,718
it is api and people
10413
07:09:02,718 --> 07:09:05,040
why well because
10414
07:09:05,040 --> 07:09:08,798
in my server i have also api people so
10415
07:09:08,798 --> 07:09:10,798
this is for get requests
10416
07:09:10,798 --> 07:09:12,718
that's the default one however in this
10417
07:09:12,718 --> 07:09:15,280
case we're not doing that with a browser
10418
07:09:15,280 --> 07:09:18,798
we're doing with javascript so axios
10419
07:09:18,798 --> 07:09:21,040
has the method by the name of surprise
10420
07:09:21,040 --> 07:09:23,840
surprise get that just means that on the
10421
07:09:23,840 --> 07:09:25,920
front end we're performing a
10422
07:09:25,920 --> 07:09:27,440
get request
10423
07:09:27,440 --> 07:09:29,520
to the same server that's why we go
10424
07:09:29,520 --> 07:09:32,718
forward slash api and then people so
10425
07:09:32,718 --> 07:09:35,280
make sure these urls match
10426
07:09:35,280 --> 07:09:36,878
so if these paths won't match then
10427
07:09:36,878 --> 07:09:39,440
you'll have an error and to showcase
10428
07:09:39,440 --> 07:09:42,000
that if i go with p poles
10429
07:09:42,000 --> 07:09:43,920
then if you navigate to the front end
10430
07:09:43,920 --> 07:09:45,200
and if you refresh
10431
07:09:45,200 --> 07:09:46,798
can't fetch data
10432
07:09:46,798 --> 07:09:49,920
so we try to fetch people from our
10433
07:09:49,920 --> 07:09:52,080
server then we do a little bit of logic
10434
07:09:52,080 --> 07:09:54,320
and this is just vanilla.js not going to
10435
07:09:54,320 --> 07:09:56,718
cover it basically we tear it over and
10436
07:09:56,718 --> 07:09:59,920
then we just set up a nice heading 5s
10437
07:09:59,920 --> 07:10:02,958
and all that and then we invoke the
10438
07:10:02,958 --> 07:10:04,400
function but
10439
07:10:04,400 --> 07:10:07,120
if there is an error we'll look for the
10440
07:10:07,120 --> 07:10:08,958
error property now in this case i'm not
10441
07:10:08,958 --> 07:10:10,878
the structuring it i'm actually not
10442
07:10:10,878 --> 07:10:14,718
using it i just say can't fetch data so
10443
07:10:14,718 --> 07:10:16,878
that's step number one now the second
10444
07:10:16,878 --> 07:10:19,760
one is of course using the form
10445
07:10:19,760 --> 07:10:22,798
so then i select the submit button i
10446
07:10:22,798 --> 07:10:26,000
select the form i also select the alert
10447
07:10:26,000 --> 07:10:29,360
because we will display an alert if the
10448
07:10:29,360 --> 07:10:31,600
value is empty and again let me repeat
10449
07:10:31,600 --> 07:10:33,280
i'm fully aware that we can check for
10450
07:10:33,280 --> 07:10:36,160
that in the front end i just wanted to
10451
07:10:36,160 --> 07:10:39,200
showcase the server setup and then
10452
07:10:39,200 --> 07:10:41,280
notice something interesting where i
10453
07:10:41,280 --> 07:10:44,320
have this try and catch block in my
10454
07:10:44,320 --> 07:10:47,760
functionality so the moment the user is
10455
07:10:47,760 --> 07:10:49,600
submitting the form of course i invoke
10456
07:10:49,600 --> 07:10:52,160
my callback function it is a sync
10457
07:10:52,160 --> 07:10:53,120
because
10458
07:10:53,120 --> 07:10:54,718
for the actuators i can wait for
10459
07:10:54,718 --> 07:10:57,440
response and notice this one so i have
10460
07:10:57,440 --> 07:11:00,400
axios and again surprise surprise the
10461
07:11:00,400 --> 07:11:03,280
method name is post why because we are
10462
07:11:03,280 --> 07:11:05,360
sending a post request
10463
07:11:05,360 --> 07:11:07,120
and this is just an axiorys thing but
10464
07:11:07,120 --> 07:11:10,478
again we provide the value in this case
10465
07:11:10,478 --> 07:11:13,760
the url value is api and people so
10466
07:11:13,760 --> 07:11:16,080
something that we're not handling yet on
10467
07:11:16,080 --> 07:11:19,200
our server and this is the value
10468
07:11:19,200 --> 07:11:22,240
so again this is just axio syntax where
10469
07:11:22,240 --> 07:11:25,200
in order to set up whatever data you're
10470
07:11:25,200 --> 07:11:27,360
sending you just pass it as a second
10471
07:11:27,360 --> 07:11:29,280
argument and you set it up as a key
10472
07:11:29,280 --> 07:11:31,520
value pair now what's really cool that
10473
07:11:31,520 --> 07:11:33,840
would axios they set up all the content
10474
07:11:33,840 --> 07:11:36,080
types and all that good stuff for you so
10475
07:11:36,080 --> 07:11:38,718
again you just save a little bit of time
10476
07:11:38,718 --> 07:11:40,718
now if there is an error in our case
10477
07:11:40,718 --> 07:11:43,120
that is going to be if we're trying to
10478
07:11:43,120 --> 07:11:46,080
submit something and in fact there's no
10479
07:11:46,080 --> 07:11:48,798
value meaning we just try to submit
10480
07:11:48,798 --> 07:11:51,920
empty form value then we will display
10481
07:11:51,920 --> 07:11:53,760
the error message and again this is
10482
07:11:53,760 --> 07:11:55,520
really cool because axiorys just
10483
07:11:55,520 --> 07:11:56,400
provides
10484
07:11:56,400 --> 07:11:58,320
a very useful
10485
07:11:58,320 --> 07:12:00,798
error api unlike the fetch where you
10486
07:12:00,798 --> 07:12:02,000
really need to
10487
07:12:02,000 --> 07:12:04,718
jump through the hoops so let me save it
10488
07:12:04,718 --> 07:12:08,080
back to people so we can see of course
10489
07:12:08,080 --> 07:12:09,600
all the people
10490
07:12:09,600 --> 07:12:10,478
that
10491
07:12:10,478 --> 07:12:13,760
we have in our array currently and then
10492
07:12:13,760 --> 07:12:17,440
of course we'll focus on this post one
10493
07:12:17,440 --> 07:12:20,320
so as you can see i get the input value
10494
07:12:20,320 --> 07:12:23,360
and i just passed it in as a
10495
07:12:23,360 --> 07:12:27,120
name property awesome so now back in our
10496
07:12:27,120 --> 07:12:29,120
app js of course we need to handle it
10497
07:12:29,120 --> 07:12:31,360
and please keep in mind one thing where
10498
07:12:31,360 --> 07:12:33,440
app dot post
10499
07:12:33,440 --> 07:12:35,600
and then api
10500
07:12:35,600 --> 07:12:36,878
and people
10501
07:12:36,878 --> 07:12:40,240
is not the same as api get and api
10502
07:12:40,240 --> 07:12:41,680
people
10503
07:12:41,680 --> 07:12:43,760
even though the url is the same
10504
07:12:43,760 --> 07:12:46,160
methods are different and that goes for
10505
07:12:46,160 --> 07:12:47,600
all the methods we're going to use
10506
07:12:47,600 --> 07:12:49,360
whether you're talking about put delete
10507
07:12:49,360 --> 07:12:52,400
or whatever so just because the urls are
10508
07:12:52,400 --> 07:12:53,360
the same
10509
07:12:53,360 --> 07:12:55,920
doesn't mean that they mean the same
10510
07:12:55,920 --> 07:12:57,200
thing no
10511
07:12:57,200 --> 07:12:59,760
the method is different here i'm just
10512
07:12:59,760 --> 07:13:02,240
reading the data i'm reading data from
10513
07:13:02,240 --> 07:13:05,280
api people here i'm actually trying to
10514
07:13:05,280 --> 07:13:06,478
add data
10515
07:13:06,478 --> 07:13:09,120
and again we're going to go with wreck
10516
07:13:09,120 --> 07:13:10,638
and res
10517
07:13:10,638 --> 07:13:13,680
and let's just go with some simple
10518
07:13:13,680 --> 07:13:15,120
response
10519
07:13:15,120 --> 07:13:17,680
now for time being i'm just going to say
10520
07:13:17,680 --> 07:13:20,718
that my response is going to be 201 now
10521
07:13:20,718 --> 07:13:22,638
that's the response that we send back if
10522
07:13:22,638 --> 07:13:25,600
we are performing a post request and we
10523
07:13:25,600 --> 07:13:29,120
are successful so i'll just say res that
10524
07:13:29,120 --> 07:13:31,520
status 201
10525
07:13:31,520 --> 07:13:34,478
and for time being let's just say send
10526
07:13:34,478 --> 07:13:36,798
and i'm just gonna say success and of
10527
07:13:36,798 --> 07:13:38,638
course now what i would want
10528
07:13:38,638 --> 07:13:42,240
is to navigate back to my javascript one
10529
07:13:42,240 --> 07:13:45,600
hour fresh i'm getting all my people and
10530
07:13:45,600 --> 07:13:48,240
now let's try it out so i'm gonna send
10531
07:13:48,240 --> 07:13:49,360
this one
10532
07:13:49,360 --> 07:13:52,478
so i sent again here nothing changed but
10533
07:13:52,478 --> 07:13:56,160
if i take a look at my network tab
10534
07:13:56,160 --> 07:13:58,798
i should see and by the way sorry i'll
10535
07:13:58,798 --> 07:14:01,120
have to do that one more time let's go
10536
07:14:01,120 --> 07:14:04,160
with i don't know susan here let's try
10537
07:14:04,160 --> 07:14:06,718
to submit and there it is now of course
10538
07:14:06,718 --> 07:14:11,040
i have api people and 201
10539
07:14:11,040 --> 07:14:13,200
so that's the response that i'm getting
10540
07:14:13,200 --> 07:14:16,320
on my front end so let's bravely click
10541
07:14:16,320 --> 07:14:19,600
on this one and we can see that the
10542
07:14:19,600 --> 07:14:23,440
request url is api people okay
10543
07:14:23,440 --> 07:14:26,798
the method was post now the response was
10544
07:14:26,798 --> 07:14:30,478
201 so it was successfully created and
10545
07:14:30,478 --> 07:14:33,520
now check it out when we look at the
10546
07:14:33,520 --> 07:14:35,840
request headers of course we have
10547
07:14:35,840 --> 07:14:39,440
application json so on our front end we
10548
07:14:39,440 --> 07:14:41,760
do need to add this content type
10549
07:14:41,760 --> 07:14:44,160
when we are sending the data now again
10550
07:14:44,160 --> 07:14:47,040
what's really cool about axios it adds
10551
07:14:47,040 --> 07:14:48,000
for us
10552
07:14:48,000 --> 07:14:50,240
but in general you need to keep that in
10553
07:14:50,240 --> 07:14:52,798
mind and there it is again we have this
10554
07:14:52,798 --> 07:14:54,718
request payload
10555
07:14:54,718 --> 07:14:57,440
and the name is equal to susan so
10556
07:14:57,440 --> 07:14:59,360
whatever i'm going to pass it in if i'm
10557
07:14:59,360 --> 07:15:01,120
going to send it empty then i'm going to
10558
07:15:01,120 --> 07:15:02,958
still have the key name
10559
07:15:02,958 --> 07:15:05,760
and the value will be empty now the
10560
07:15:05,760 --> 07:15:07,680
gotcha here is even though we're
10561
07:15:07,680 --> 07:15:09,120
handling a
10562
07:15:09,120 --> 07:15:11,760
form submissions we're not handling the
10563
07:15:11,760 --> 07:15:14,798
json data so yes we know how we can send
10564
07:15:14,798 --> 07:15:17,120
back the json data but we're not
10565
07:15:17,120 --> 07:15:20,160
handling the incoming json data and this
10566
07:15:20,160 --> 07:15:22,798
is where another middleware comes into
10567
07:15:22,798 --> 07:15:28,080
play so i'm going to say parse json here
10568
07:15:28,080 --> 07:15:30,160
and we simply need to go again with
10569
07:15:30,160 --> 07:15:31,680
app.use
10570
07:15:31,680 --> 07:15:33,840
and we go with express
10571
07:15:33,840 --> 07:15:35,600
and json now in this case i'm not going
10572
07:15:35,600 --> 07:15:37,360
to go to documentation
10573
07:15:37,360 --> 07:15:40,320
if you want you can do that in my case i
10574
07:15:40,320 --> 07:15:42,718
will try to save us a little bit of time
10575
07:15:42,718 --> 07:15:46,478
and we'll skip that and here we have api
10576
07:15:46,478 --> 07:15:51,200
people and now similarly to our login
10577
07:15:51,200 --> 07:15:54,718
we have access to the form data that is
10578
07:15:54,718 --> 07:15:57,360
being sent with javascript in the rec
10579
07:15:57,360 --> 07:15:58,558
dot body
10580
07:15:58,558 --> 07:15:59,760
previously
10581
07:15:59,760 --> 07:16:02,320
before this was added no we have no
10582
07:16:02,320 --> 07:16:05,200
access yes when it comes to form that
10583
07:16:05,200 --> 07:16:08,000
was one setup now when it comes to
10584
07:16:08,000 --> 07:16:11,200
straight up http requests for example in
10585
07:16:11,200 --> 07:16:13,360
this case sent with javascript no we had
10586
07:16:13,360 --> 07:16:16,400
no access so we were getting a json
10587
07:16:16,400 --> 07:16:19,520
and now we added a specific middleware
10588
07:16:19,520 --> 07:16:21,920
that handles that so of course in this
10589
07:16:21,920 --> 07:16:24,718
case now we can set up more logic
10590
07:16:24,718 --> 07:16:26,638
correct we can say that we're looking
10591
07:16:26,638 --> 07:16:30,558
for the name and if the name is provided
10592
07:16:30,558 --> 07:16:33,120
then awesome then i'm going to send back
10593
07:16:33,120 --> 07:16:36,000
that new person that was created so we
10594
07:16:36,000 --> 07:16:38,240
can add it to our front end and again
10595
07:16:38,240 --> 07:16:40,400
this is the case where we're not
10596
07:16:40,400 --> 07:16:42,798
persisting this data anywhere later
10597
07:16:42,798 --> 07:16:45,040
we'll learn how to do that for now i
10598
07:16:45,040 --> 07:16:47,360
already have the code that adds it to
10599
07:16:47,360 --> 07:16:50,160
our front end however if the user tries
10600
07:16:50,160 --> 07:16:53,280
to submit without any values well then
10601
07:16:53,280 --> 07:16:56,320
there's going to be a error message so
10602
07:16:56,320 --> 07:16:58,240
in the api people
10603
07:16:58,240 --> 07:17:00,958
in the post method let's set up our
10604
07:17:00,958 --> 07:17:02,160
logic
10605
07:17:02,160 --> 07:17:04,398
where i'm going to go with const name is
10606
07:17:04,398 --> 07:17:05,478
equal to
10607
07:17:05,478 --> 07:17:07,360
rec.body
10608
07:17:07,360 --> 07:17:10,320
the middleware makes it possible now
10609
07:17:10,320 --> 07:17:12,478
and then i'm going to say if the name is
10610
07:17:12,478 --> 07:17:14,398
not provided well then i'm going to send
10611
07:17:14,398 --> 07:17:17,120
back the 400 then i'm going to say
10612
07:17:17,120 --> 07:17:19,920
please provide name value so there's
10613
07:17:19,920 --> 07:17:22,160
going to be a error message so i'll say
10614
07:17:22,160 --> 07:17:24,240
here if and
10615
07:17:24,240 --> 07:17:26,958
exclamation point and if the name is not
10616
07:17:26,958 --> 07:17:29,280
provided then let's go to return
10617
07:17:29,280 --> 07:17:30,240
res
10618
07:17:30,240 --> 07:17:34,080
dot status and for the request error we
10619
07:17:34,080 --> 07:17:37,520
go with 400 that's the status code and
10620
07:17:37,520 --> 07:17:40,160
then let's go again with json and in
10621
07:17:40,160 --> 07:17:42,878
this case we're gonna go with success
10622
07:17:42,878 --> 07:17:45,760
now this success will be false and then
10623
07:17:45,760 --> 07:17:48,240
we're going to go with message again i
10624
07:17:48,240 --> 07:17:49,840
would suggest using the same properties
10625
07:17:49,840 --> 07:17:52,000
because of course i'm handling them on
10626
07:17:52,000 --> 07:17:54,398
the front end as well so in here i'll
10627
07:17:54,398 --> 07:17:56,558
say please provide
10628
07:17:56,558 --> 07:18:00,798
name value like so now if the value is
10629
07:18:00,798 --> 07:18:03,200
provided whatever it is then of course
10630
07:18:03,200 --> 07:18:06,958
we can send back res.status 201. hey
10631
07:18:06,958 --> 07:18:09,760
guys it's john from the future with a
10632
07:18:09,760 --> 07:18:11,040
small update
10633
07:18:11,040 --> 07:18:12,878
as you'll see in a second while i was
10634
07:18:12,878 --> 07:18:16,080
recording i forgot to change the methods
10635
07:18:16,080 --> 07:18:18,160
so instead of writing json i just left
10636
07:18:18,160 --> 07:18:19,600
it as send
10637
07:18:19,600 --> 07:18:21,520
and while we can get away with it in
10638
07:18:21,520 --> 07:18:24,240
this example normally you always want to
10639
07:18:24,240 --> 07:18:27,760
use json instead so make sure you change
10640
07:18:27,760 --> 07:18:31,520
your method from send to json and in
10641
07:18:31,520 --> 07:18:32,478
this case
10642
07:18:32,478 --> 07:18:35,200
let's just set up a proper object we'll
10643
07:18:35,200 --> 07:18:38,000
say success is true so now we're being
10644
07:18:38,000 --> 07:18:40,798
successful and i'm just gonna send back
10645
07:18:40,798 --> 07:18:42,958
that person now i'm going to set it up
10646
07:18:42,958 --> 07:18:45,840
with person key and the value will be
10647
07:18:45,840 --> 07:18:48,160
named whatever i'm getting again please
10648
07:18:48,160 --> 07:18:50,558
use the same ones if you want to have a
10649
07:18:50,558 --> 07:18:52,798
same result now
10650
07:18:52,798 --> 07:18:55,600
let's navigate back we can refresh just
10651
07:18:55,600 --> 07:18:59,360
so we are all on the same page and if we
10652
07:18:59,360 --> 07:19:02,000
type susan now we should see another
10653
07:19:02,000 --> 07:19:04,240
susan added to our list
10654
07:19:04,240 --> 07:19:07,440
and then again we can go with john and
10655
07:19:07,440 --> 07:19:09,600
i'm just being lazy on coming up with
10656
07:19:09,600 --> 07:19:12,320
original names and as you can see every
10657
07:19:12,320 --> 07:19:14,080
time we add
10658
07:19:14,080 --> 07:19:17,200
value in our form we're being successful
10659
07:19:17,200 --> 07:19:19,680
we're actually getting back this value
10660
07:19:19,680 --> 07:19:23,120
from our server and then we dynamically
10661
07:19:23,120 --> 07:19:24,878
added to our front end and again the
10662
07:19:24,878 --> 07:19:27,040
reason why we're doing this way is
10663
07:19:27,040 --> 07:19:29,120
because we're not persisting this data
10664
07:19:29,120 --> 07:19:31,520
so that's why the setup is following
10665
07:19:31,520 --> 07:19:32,798
later of course we'll learn how to
10666
07:19:32,798 --> 07:19:35,840
persist this in a database as well and
10667
07:19:35,840 --> 07:19:39,360
if i try to submit with empty value
10668
07:19:39,360 --> 07:19:42,240
check it out now of course i have people
10669
07:19:42,240 --> 07:19:45,040
and this one is what 400 right
10670
07:19:45,040 --> 07:19:48,320
notice here and it says please provide
10671
07:19:48,320 --> 07:19:51,280
name value now if you want to do a bit
10672
07:19:51,280 --> 07:19:53,440
of console logging you're welcome to do
10673
07:19:53,440 --> 07:19:56,558
that on the front end if for example you
10674
07:19:56,558 --> 07:19:59,920
are iffy about the syntax but just to
10675
07:19:59,920 --> 07:20:02,638
let you know when we talk about axios
10676
07:20:02,638 --> 07:20:03,520
here
10677
07:20:03,520 --> 07:20:05,440
effectively we're getting back the whole
10678
07:20:05,440 --> 07:20:09,920
thing so when we perform a request http
10679
07:20:09,920 --> 07:20:12,798
request with axiorys we're actually
10680
07:20:12,798 --> 07:20:14,240
getting back a
10681
07:20:14,240 --> 07:20:15,760
giant object
10682
07:20:15,760 --> 07:20:18,798
now what we're interested in that object
10683
07:20:18,798 --> 07:20:20,478
is a data property
10684
07:20:20,478 --> 07:20:22,478
so that's the response that is coming
10685
07:20:22,478 --> 07:20:23,280
back
10686
07:20:23,280 --> 07:20:24,718
from the server
10687
07:20:24,718 --> 07:20:27,520
and then in here as you can see i set up
10688
07:20:27,520 --> 07:20:30,878
a heading 5 with data.person so that's
10689
07:20:30,878 --> 07:20:33,760
the person that we're sending back from
10690
07:20:33,760 --> 07:20:36,000
our server now
10691
07:20:36,000 --> 07:20:38,240
if for example there is an error we go
10692
07:20:38,240 --> 07:20:41,920
with error response again data property
10693
07:20:41,920 --> 07:20:43,680
because that's where the useful
10694
07:20:43,680 --> 07:20:46,160
information sits because the object of
10695
07:20:46,160 --> 07:20:48,000
course is giant with lots of useful
10696
07:20:48,000 --> 07:20:49,760
information but as far as what we're
10697
07:20:49,760 --> 07:20:52,160
sending back it is sending in data and
10698
07:20:52,160 --> 07:20:54,558
then message now i'm looking for the
10699
07:20:54,558 --> 07:20:57,040
message property of course here because
10700
07:20:57,040 --> 07:20:59,680
i set up a message property
10701
07:20:59,680 --> 07:21:02,558
on the server hopefully it is clear this
10702
07:21:02,558 --> 07:21:05,040
is our second flavor
10703
07:21:05,040 --> 07:21:07,920
where effectively we perform a post
10704
07:21:07,920 --> 07:21:10,558
request from the application
10705
07:21:10,558 --> 07:21:12,718
and of course in that case we do need to
10706
07:21:12,718 --> 07:21:15,440
provide content type as well as the data
10707
07:21:15,440 --> 07:21:17,840
and next video we'll install
10708
07:21:17,840 --> 07:21:20,638
and cover some basic setup for a very
10709
07:21:20,638 --> 07:21:21,840
useful tool
10710
07:21:21,840 --> 07:21:24,160
that is going to save us a lot of
10711
07:21:24,160 --> 07:21:27,520
trouble when we work with post
10712
07:21:27,520 --> 07:21:31,200
delete as well as put methods
10713
07:21:31,200 --> 07:21:33,440
all right so at this point we're
10714
07:21:33,440 --> 07:21:37,360
familiar with two ways how we can send a
10715
07:21:37,360 --> 07:21:38,718
post request
10716
07:21:38,718 --> 07:21:40,718
so we covered how we can do that using
10717
07:21:40,718 --> 07:21:41,680
the form
10718
07:21:41,680 --> 07:21:45,280
as well as straight up http request in
10719
07:21:45,280 --> 07:21:47,920
our case using javascript
10720
07:21:47,920 --> 07:21:50,958
however there's a major problem in our
10721
07:21:50,958 --> 07:21:52,240
current setup
10722
07:21:52,240 --> 07:21:55,120
and the problem here is following
10723
07:21:55,120 --> 07:21:55,920
if
10724
07:21:55,920 --> 07:21:58,160
we need to create a frontend to test
10725
07:21:58,160 --> 07:22:02,000
something for every route as you can see
10726
07:22:02,000 --> 07:22:03,600
i mean our development is going to be
10727
07:22:03,600 --> 07:22:05,440
extremely slow
10728
07:22:05,440 --> 07:22:07,680
because we can clearly see that it's
10729
07:22:07,680 --> 07:22:10,000
probably going to take us way longer
10730
07:22:10,000 --> 07:22:13,760
just to set up a front end than setting
10731
07:22:13,760 --> 07:22:16,558
up just one simple route so every time i
10732
07:22:16,558 --> 07:22:18,080
create a route i'm probably going to
10733
07:22:18,080 --> 07:22:21,520
spend i don't know five six times more
10734
07:22:21,520 --> 07:22:23,920
on setting up the front end where i can
10735
07:22:23,920 --> 07:22:26,638
test this route and of course there has
10736
07:22:26,638 --> 07:22:28,558
to be a better option and that better
10737
07:22:28,558 --> 07:22:29,520
option
10738
07:22:29,520 --> 07:22:32,398
is a tool called postman
10739
07:22:32,398 --> 07:22:36,000
and postman is just awesome for quickly
10740
07:22:36,000 --> 07:22:39,040
testing our apis so at this point i
10741
07:22:39,040 --> 07:22:41,958
strongly suggest that you navigate to
10742
07:22:41,958 --> 07:22:45,040
postman.com again it's a free tool so
10743
07:22:45,040 --> 07:22:46,718
you're not going to have to pay anything
10744
07:22:46,718 --> 07:22:49,280
but for mainer of the course yes we'll
10745
07:22:49,280 --> 07:22:52,398
use this so if you're kind of iffy about
10746
07:22:52,398 --> 07:22:54,080
installing something new this is
10747
07:22:54,080 --> 07:22:57,360
something that is required so navigate
10748
07:22:57,360 --> 07:22:58,280
to
10749
07:22:58,280 --> 07:22:59,840
postman.com
10750
07:22:59,840 --> 07:23:02,638
and then they'll try to sign you up you
10751
07:23:02,638 --> 07:23:04,398
don't have to do that just keep
10752
07:23:04,398 --> 07:23:06,878
scrolling down keep scrolling down and
10753
07:23:06,878 --> 07:23:09,120
all the way in the bottom you'll see
10754
07:23:09,120 --> 07:23:12,160
this download app option go here and
10755
07:23:12,160 --> 07:23:13,680
they'll right away detect which
10756
07:23:13,680 --> 07:23:15,280
operating system you're using of course
10757
07:23:15,280 --> 07:23:18,320
in my case that is mac so download the
10758
07:23:18,320 --> 07:23:20,558
application once it's on my computer i'm
10759
07:23:20,558 --> 07:23:22,240
going to navigate back to the desktop
10760
07:23:22,240 --> 07:23:24,320
i'm going to go to downloads i'll crack
10761
07:23:24,320 --> 07:23:25,360
it open
10762
07:23:25,360 --> 07:23:27,760
then of course i'll just move it to my
10763
07:23:27,760 --> 07:23:30,000
applications like so and i already had
10764
07:23:30,000 --> 07:23:33,280
one on my computer but i removed it so
10765
07:23:33,280 --> 07:23:36,000
we can do these steps together then this
10766
07:23:36,000 --> 07:23:38,798
is the application let me zoom in that's
10767
07:23:38,798 --> 07:23:41,520
the postman so just click on it
10768
07:23:41,520 --> 07:23:45,280
and then it's going to spin up the app
10769
07:23:45,280 --> 07:23:47,040
and eventually
10770
07:23:47,040 --> 07:23:49,600
you'll hit the dashboard and i'm not
10771
07:23:49,600 --> 07:23:52,160
going to spend half an hour on this
10772
07:23:52,160 --> 07:23:54,878
setup but of course as we're progressing
10773
07:23:54,878 --> 07:23:58,000
we the course as we're progressing with
10774
07:23:58,000 --> 07:24:00,558
our examples we will cover
10775
07:24:00,558 --> 07:24:02,398
more features
10776
07:24:02,398 --> 07:24:04,478
that this app provides
10777
07:24:04,478 --> 07:24:07,280
and basically here you'll have all your
10778
07:24:07,280 --> 07:24:10,160
requests that you can actually group in
10779
07:24:10,160 --> 07:24:12,240
collections and again that's something
10780
07:24:12,240 --> 07:24:14,798
that we'll cover a bit later and this is
10781
07:24:14,798 --> 07:24:17,440
where we can make those requests and in
10782
07:24:17,440 --> 07:24:19,200
order to create a new request we just
10783
07:24:19,200 --> 07:24:22,000
click on this plus sign and check it out
10784
07:24:22,000 --> 07:24:24,160
so at the moment i have untitled a
10785
07:24:24,160 --> 07:24:25,360
request
10786
07:24:25,360 --> 07:24:28,718
in here i need to enter the url and i
10787
07:24:28,718 --> 07:24:31,600
can send my request not orders here on
10788
07:24:31,600 --> 07:24:35,040
left hand side what do i have i have get
10789
07:24:35,040 --> 07:24:38,240
but what's really cool i have all these
10790
07:24:38,240 --> 07:24:41,280
methods available as well so first let's
10791
07:24:41,280 --> 07:24:43,920
test out the get route and then we'll
10792
07:24:43,920 --> 07:24:46,398
see how we can deal with post route as
10793
07:24:46,398 --> 07:24:49,600
well now as far as our url what it is
10794
07:24:49,600 --> 07:24:52,798
well it is localhost 5000
10795
07:24:52,798 --> 07:24:57,360
and of course the full url is api and
10796
07:24:57,360 --> 07:25:00,320
people now this is for the post as well
10797
07:25:00,320 --> 07:25:02,718
as the get and since we're testing it of
10798
07:25:02,718 --> 07:25:05,840
course that's why we have a method with
10799
07:25:05,840 --> 07:25:09,120
the value of get so let's go here local
10800
07:25:09,120 --> 07:25:12,558
host and then five thousand
10801
07:25:12,558 --> 07:25:15,120
then forward slash api
10802
07:25:15,120 --> 07:25:18,320
and people and the moment i click send
10803
07:25:18,320 --> 07:25:20,638
check it out what do i see here in the
10804
07:25:20,638 --> 07:25:23,600
bottom i see my object the success value
10805
07:25:23,600 --> 07:25:27,120
is true and there i have my data so as
10806
07:25:27,120 --> 07:25:30,798
you can see the moment i create my route
10807
07:25:30,798 --> 07:25:33,600
for example post route or put route or
10808
07:25:33,600 --> 07:25:37,520
whatever i can test it immediately i can
10809
07:25:37,520 --> 07:25:40,080
test it within a few seconds instead of
10810
07:25:40,080 --> 07:25:42,558
building the whole front end just so i
10811
07:25:42,558 --> 07:25:46,000
can test one route and of course once we
10812
07:25:46,000 --> 07:25:47,760
have covered get route now let's see how
10813
07:25:47,760 --> 07:25:50,718
we can deal with a post route so we need
10814
07:25:50,718 --> 07:25:53,040
to change the method of course
10815
07:25:53,040 --> 07:25:56,080
now the url still stays the same because
10816
07:25:56,080 --> 07:25:58,718
i do have a route for the post as you
10817
07:25:58,718 --> 07:26:01,520
can see and in here what i'm looking for
10818
07:26:01,520 --> 07:26:03,520
i'm looking for the name
10819
07:26:03,520 --> 07:26:05,040
in the body
10820
07:26:05,040 --> 07:26:07,840
and how do we construct a post request
10821
07:26:07,840 --> 07:26:10,160
in a postman well we do that in a
10822
07:26:10,160 --> 07:26:13,040
following way first we go to the body
10823
07:26:13,040 --> 07:26:15,120
and we look for raw
10824
07:26:15,120 --> 07:26:19,440
and instead of text i go with json
10825
07:26:19,440 --> 07:26:21,600
because i'm going to be sending a json
10826
07:26:21,600 --> 07:26:24,240
data so this is not the case where we're
10827
07:26:24,240 --> 07:26:26,798
working with the forms in this case
10828
07:26:26,798 --> 07:26:29,760
we're sending a json data and what
10829
07:26:29,760 --> 07:26:31,520
you'll notice something really
10830
07:26:31,520 --> 07:26:34,398
interesting in headers right away
10831
07:26:34,398 --> 07:26:37,280
there's going to be a content type
10832
07:26:37,280 --> 07:26:39,520
so let's see let's set up first the body
10833
07:26:39,520 --> 07:26:41,520
and you'll see how it gets added so
10834
07:26:41,520 --> 07:26:44,718
we're gonna go here with a name again
10835
07:26:44,718 --> 07:26:46,478
this is what i'm looking for that's why
10836
07:26:46,478 --> 07:26:49,120
i need to pass it and then i'm gonna set
10837
07:26:49,120 --> 07:26:50,718
up some kind of value now since i'm
10838
07:26:50,718 --> 07:26:52,320
setting up the json please remember that
10839
07:26:52,320 --> 07:26:55,200
we need to use double quotation marks so
10840
07:26:55,200 --> 07:26:57,120
i'm going to go here with john and if
10841
07:26:57,120 --> 07:26:59,840
you paid attention previously we had
10842
07:26:59,840 --> 07:27:02,638
eight headers and now we have nine so
10843
07:27:02,638 --> 07:27:05,120
i'm gonna go back and there it is now i
10844
07:27:05,120 --> 07:27:09,040
have content type application json so
10845
07:27:09,040 --> 07:27:10,558
that's really cool we don't have to do
10846
07:27:10,558 --> 07:27:13,760
anything the header gets added for us
10847
07:27:13,760 --> 07:27:16,320
and of course the moment i click send
10848
07:27:16,320 --> 07:27:20,080
check it out now i have success true and
10849
07:27:20,080 --> 07:27:22,558
person john why do i have that well
10850
07:27:22,558 --> 07:27:24,398
because that is my response i have
10851
07:27:24,398 --> 07:27:28,160
success true and of course whatever name
10852
07:27:28,160 --> 07:27:30,478
i received i just sent it back so now of
10853
07:27:30,478 --> 07:27:33,360
course let's quickly test out our 400 as
10854
07:27:33,360 --> 07:27:36,478
well so i'm going to navigate back and
10855
07:27:36,478 --> 07:27:39,360
then in the body i'm just going to send
10856
07:27:39,360 --> 07:27:42,160
a empty string or you can remove name
10857
07:27:42,160 --> 07:27:44,638
altogether that's really up to you and
10858
07:27:44,638 --> 07:27:48,240
now of course i have 400 bad requests
10859
07:27:48,240 --> 07:27:51,120
and success is false and of course i can
10860
07:27:51,120 --> 07:27:53,200
see the message so as you can see we can
10861
07:27:53,200 --> 07:27:56,000
test it within seconds and to really
10862
07:27:56,000 --> 07:27:58,478
hammer this home why don't we create
10863
07:27:58,478 --> 07:28:01,520
another post route and quickly set up
10864
07:28:01,520 --> 07:28:04,000
some kind of response and let's test it
10865
07:28:04,000 --> 07:28:07,200
out in our postman as well so i'm going
10866
07:28:07,200 --> 07:28:10,718
to go in between the post and login i'm
10867
07:28:10,718 --> 07:28:13,200
just going to go with app.post
10868
07:28:13,200 --> 07:28:15,760
and the route is going to be following
10869
07:28:15,760 --> 07:28:18,638
i'm going to go with path of api
10870
07:28:18,638 --> 07:28:19,760
people
10871
07:28:19,760 --> 07:28:20,958
people
10872
07:28:20,958 --> 07:28:23,920
and i'm going to add a postman here
10873
07:28:23,920 --> 07:28:25,520
or you know what let's do it this way
10874
07:28:25,520 --> 07:28:27,280
let's go postman
10875
07:28:27,280 --> 07:28:28,718
and then
10876
07:28:28,718 --> 07:28:30,478
people like so
10877
07:28:30,478 --> 07:28:32,878
and then as far as the callback function
10878
07:28:32,878 --> 07:28:33,760
again
10879
07:28:33,760 --> 07:28:36,240
we get rec res
10880
07:28:36,240 --> 07:28:37,440
and then
10881
07:28:37,440 --> 07:28:39,360
in the body again i'm going to be
10882
07:28:39,360 --> 07:28:42,798
looking for the name property so i want
10883
07:28:42,798 --> 07:28:44,878
to get that name so i'm going to say
10884
07:28:44,878 --> 07:28:46,558
that i'm expecting it
10885
07:28:46,558 --> 07:28:49,360
we'll go with name rec
10886
07:28:49,360 --> 07:28:51,440
dot body again if you want to cancel log
10887
07:28:51,440 --> 07:28:53,680
the body you can do so but i think we
10888
07:28:53,680 --> 07:28:55,680
have done that quite a few times so i'm
10889
07:28:55,680 --> 07:28:58,398
going to omit that then i'll copy and
10890
07:28:58,398 --> 07:28:59,360
paste
10891
07:28:59,360 --> 07:29:02,958
my response so again if the name is not
10892
07:29:02,958 --> 07:29:05,360
provided meaning if the property is not
10893
07:29:05,360 --> 07:29:07,840
there or if it's just empty i'm sending
10894
07:29:07,840 --> 07:29:11,440
back to 400 however if i'm successful
10895
07:29:11,440 --> 07:29:14,240
then i'm just going to take my array
10896
07:29:14,240 --> 07:29:16,798
i'll add that extra person and i'll send
10897
07:29:16,798 --> 07:29:19,600
it back now i'm not going to add the id
10898
07:29:19,600 --> 07:29:21,200
i'll leave that one blank because
10899
07:29:21,200 --> 07:29:23,120
eventually database will do that for us
10900
07:29:23,120 --> 07:29:26,160
so i don't really see the point of doing
10901
07:29:26,160 --> 07:29:28,320
that manually so i'm going to go back to
10902
07:29:28,320 --> 07:29:31,440
my api postman people and then again
10903
07:29:31,440 --> 07:29:34,160
this is my error response and now let's
10904
07:29:34,160 --> 07:29:37,360
copy and paste our status and then
10905
07:29:37,360 --> 07:29:40,000
instead of sending that one person we
10906
07:29:40,000 --> 07:29:42,398
created why don't we do this why don't
10907
07:29:42,398 --> 07:29:44,160
we go with data
10908
07:29:44,160 --> 07:29:46,478
and then of course the array name is
10909
07:29:46,478 --> 07:29:48,878
people and now as you can see i'm using
10910
07:29:48,878 --> 07:29:51,120
the spread operator and i'm just going
10911
07:29:51,120 --> 07:29:54,398
to add that one single person so i'll
10912
07:29:54,398 --> 07:29:56,718
save now of course we do need to change
10913
07:29:56,718 --> 07:29:59,520
the route so it's not going to be api
10914
07:29:59,520 --> 07:30:01,600
it's going to be postman
10915
07:30:01,600 --> 07:30:05,200
and then people like so now let's add
10916
07:30:05,200 --> 07:30:08,478
for example peter and the moment i click
10917
07:30:08,478 --> 07:30:11,920
send there it is i have my success true
10918
07:30:11,920 --> 07:30:13,680
and as far as data
10919
07:30:13,680 --> 07:30:16,638
all the way somewhere should be peter as
10920
07:30:16,638 --> 07:30:19,920
well so again we just grabbed that peter
10921
07:30:19,920 --> 07:30:23,280
and then we added to our array hopefully
10922
07:30:23,280 --> 07:30:24,718
this makes sense and now of course we
10923
07:30:24,718 --> 07:30:28,718
can work on other http methods as well
10924
07:30:28,718 --> 07:30:31,040
all right and once we're familiar with
10925
07:30:31,040 --> 07:30:34,080
postman now let's work on the other two
10926
07:30:34,080 --> 07:30:35,040
methods
10927
07:30:35,040 --> 07:30:37,600
so now we're gonna be working on put
10928
07:30:37,600 --> 07:30:40,878
method as well as the delete
10929
07:30:40,878 --> 07:30:43,760
so i'm gonna scroll down and below the
10930
07:30:43,760 --> 07:30:47,120
post login route i'm gonna go with app
10931
07:30:47,120 --> 07:30:50,718
and then put now again this method
10932
07:30:50,718 --> 07:30:55,040
is for editing the data so put method is
10933
07:30:55,040 --> 07:30:56,320
for update
10934
07:30:56,320 --> 07:30:59,840
and again the convention is following
10935
07:30:59,840 --> 07:31:02,320
where if we have a list for example in
10936
07:31:02,320 --> 07:31:05,040
this case that would be api and orders
10937
07:31:05,040 --> 07:31:07,200
if we want to edit
10938
07:31:07,200 --> 07:31:11,360
or delete we go with our route parameter
10939
07:31:11,360 --> 07:31:14,638
where effectively we set up a route for
10940
07:31:14,638 --> 07:31:18,558
api orders and then we go with colon id
10941
07:31:18,558 --> 07:31:20,478
and this is going to get me that one
10942
07:31:20,478 --> 07:31:23,280
specific item now please keep in mind
10943
07:31:23,280 --> 07:31:26,958
one thing where this is a convention so
10944
07:31:26,958 --> 07:31:28,878
technically there are multiple other
10945
07:31:28,878 --> 07:31:30,798
ways how you can set it up but there's a
10946
07:31:30,798 --> 07:31:32,958
reason why that convention exists and
10947
07:31:32,958 --> 07:31:34,878
that's why throughout the course we'll
10948
07:31:34,878 --> 07:31:37,360
stick with this convention i'll cover
10949
07:31:37,360 --> 07:31:39,600
the official name of this convention a
10950
07:31:39,600 --> 07:31:42,000
little bit later as well as a bit more
10951
07:31:42,000 --> 07:31:44,080
details about it for now i just quickly
10952
07:31:44,080 --> 07:31:47,440
want to cover both of the methods so i'm
10953
07:31:47,440 --> 07:31:49,440
going to navigate back i have my put
10954
07:31:49,440 --> 07:31:52,478
method that is for modifying the data
10955
07:31:52,478 --> 07:31:56,160
and in our case what is the url for the
10956
07:31:56,160 --> 07:31:59,120
whole list because that is api people so
10957
07:31:59,120 --> 07:32:02,320
this was just for testing correct and
10958
07:32:02,320 --> 07:32:05,520
then we go with api forward slash
10959
07:32:05,520 --> 07:32:08,000
and then we go with people
10960
07:32:08,000 --> 07:32:10,240
and then of course i also want to add
10961
07:32:10,240 --> 07:32:13,280
that id so this is going to get me that
10962
07:32:13,280 --> 07:32:15,760
one specific item and again the reason
10963
07:32:15,760 --> 07:32:18,000
why we're doing that because when we'll
10964
07:32:18,000 --> 07:32:19,360
send our
10965
07:32:19,360 --> 07:32:20,160
put
10966
07:32:20,160 --> 07:32:23,600
or delete requests we'll send to a
10967
07:32:23,600 --> 07:32:24,958
specific path
10968
07:32:24,958 --> 07:32:28,160
so this is going to be that one item and
10969
07:32:28,160 --> 07:32:29,920
if that item exists
10970
07:32:29,920 --> 07:32:31,440
then in the body there's going to be
10971
07:32:31,440 --> 07:32:33,040
data to update
10972
07:32:33,040 --> 07:32:35,600
or in the case of delete there's going
10973
07:32:35,600 --> 07:32:37,440
to be nothing in the body we'll just
10974
07:32:37,440 --> 07:32:39,600
remove that item and you'll see how it
10975
07:32:39,600 --> 07:32:42,320
works in a second so please be patient
10976
07:32:42,320 --> 07:32:43,920
so we're going to go with people and
10977
07:32:43,920 --> 07:32:46,798
then remember again we have route params
10978
07:32:46,798 --> 07:32:48,638
and in this case i'm not going to call
10979
07:32:48,638 --> 07:32:51,040
this person id i'm just going to say id
10980
07:32:51,040 --> 07:32:53,200
just remember that we can call it
10981
07:32:53,200 --> 07:32:55,280
technically whatever we would want and
10982
07:32:55,280 --> 07:32:57,040
now in this case there's two things that
10983
07:32:57,040 --> 07:33:00,000
i'm looking for first i want to get the
10984
07:33:00,000 --> 07:33:02,798
value here in the params and remember we
10985
07:33:02,798 --> 07:33:05,520
access that using the rec params and the
10986
07:33:05,520 --> 07:33:08,240
second thing when it comes to update
10987
07:33:08,240 --> 07:33:10,958
when it comes to the put request
10988
07:33:10,958 --> 07:33:13,040
i'm also going to send something in the
10989
07:33:13,040 --> 07:33:15,440
body why well because if i'm updating
10990
07:33:15,440 --> 07:33:17,760
for example if i'm changing the name
10991
07:33:17,760 --> 07:33:20,398
from peter to susan or whatever of
10992
07:33:20,398 --> 07:33:22,718
course i also need to supply this value
10993
07:33:22,718 --> 07:33:24,558
so you need to understand that there's
10994
07:33:24,558 --> 07:33:27,360
two sides of this request
10995
07:33:27,360 --> 07:33:29,760
not only we're looking for that specific
10996
07:33:29,760 --> 07:33:32,160
item that's why we use the params that's
10997
07:33:32,160 --> 07:33:34,718
why i specifically say hey i would like
10998
07:33:34,718 --> 07:33:37,280
to get the item number one or item
10999
07:33:37,280 --> 07:33:39,760
number two i also need to supply this
11000
07:33:39,760 --> 07:33:41,920
value because otherwise what's the point
11001
07:33:41,920 --> 07:33:44,958
of updating something so let's access
11002
07:33:44,958 --> 07:33:47,200
both of those things let's say that i'm
11003
07:33:47,200 --> 07:33:51,280
gonna get the id from rec params so this
11004
07:33:51,280 --> 07:33:53,760
is going to tell me which item and then
11005
07:33:53,760 --> 07:33:55,680
the second thing since of course i want
11006
07:33:55,680 --> 07:33:58,080
to update that item i need that value as
11007
07:33:58,080 --> 07:34:00,638
well correct so we're going to go here
11008
07:34:00,638 --> 07:34:02,638
with const
11009
07:34:02,638 --> 07:34:04,240
and i'm going to be again looking for
11010
07:34:04,240 --> 07:34:07,840
the name and we'll say reg dot body
11011
07:34:07,840 --> 07:34:10,558
and that's it that should do it so these
11012
07:34:10,558 --> 07:34:12,958
are the two values that we're gonna get
11013
07:34:12,958 --> 07:34:15,600
from our question and just to make
11014
07:34:15,600 --> 07:34:17,520
things interesting i will console log
11015
07:34:17,520 --> 07:34:20,718
them right now so i'm gonna go back to
11016
07:34:20,718 --> 07:34:23,600
the postman and remember we need to
11017
07:34:23,600 --> 07:34:26,240
change the url it's not gonna be api
11018
07:34:26,240 --> 07:34:28,240
postman in this case it is going to be
11019
07:34:28,240 --> 07:34:31,680
people and now you can select any of the
11020
07:34:31,680 --> 07:34:35,040
ids whether that's one all the way up to
11021
07:34:35,040 --> 07:34:38,638
the five so we go here with one
11022
07:34:38,638 --> 07:34:40,878
and then as far as the value what is the
11023
07:34:40,878 --> 07:34:43,440
value for the first one it is john so
11024
07:34:43,440 --> 07:34:45,520
kind of makes sense if i'm updating that
11025
07:34:45,520 --> 07:34:48,080
i go with peter correct so we have put
11026
07:34:48,080 --> 07:34:51,280
request we have api and then people and
11027
07:34:51,280 --> 07:34:53,840
then check it out the moment we send it
11028
07:34:53,840 --> 07:34:56,000
now of course the request is hanging
11029
07:34:56,000 --> 07:34:59,200
don't worry about that but in a console
11030
07:34:59,200 --> 07:35:03,840
we should get api people and then the id
11031
07:35:03,840 --> 07:35:05,520
and of course the reason why i don't see
11032
07:35:05,520 --> 07:35:07,840
anything because i was smart enough and
11033
07:35:07,840 --> 07:35:09,600
i didn't console log anything so i'm
11034
07:35:09,600 --> 07:35:12,320
gonna go back with id name and you know
11035
07:35:12,320 --> 07:35:14,478
what for timing let me just send back
11036
07:35:14,478 --> 07:35:17,920
hello world okay let's go here
11037
07:35:17,920 --> 07:35:20,478
let's actually send it one more time as
11038
07:35:20,478 --> 07:35:22,320
you can see could not get the response
11039
07:35:22,320 --> 07:35:24,240
that was the error this time in this
11040
07:35:24,240 --> 07:35:26,958
case i am getting hello world so that
11041
07:35:26,958 --> 07:35:29,040
worked beautifully and as you can see
11042
07:35:29,040 --> 07:35:32,080
the status code is 200 as well and then
11043
07:35:32,080 --> 07:35:34,798
in the console i have one
11044
07:35:34,798 --> 07:35:37,280
and i have the repeater so this is going
11045
07:35:37,280 --> 07:35:38,638
to be the id
11046
07:35:38,638 --> 07:35:40,398
and of course this is going to be the
11047
07:35:40,398 --> 07:35:42,958
value now if i'll change it of course if
11048
07:35:42,958 --> 07:35:45,600
i'll say number two and if i'll send it
11049
07:35:45,600 --> 07:35:48,398
again i get back my response and now the
11050
07:35:48,398 --> 07:35:52,160
value for the id is due hopefully this
11051
07:35:52,160 --> 07:35:54,160
makes sense so now of course at this
11052
07:35:54,160 --> 07:35:56,558
point we need to set up the logic again
11053
07:35:56,558 --> 07:35:59,280
we are going to set up the logic with
11054
07:35:59,280 --> 07:36:02,240
our local data with our people array
11055
07:36:02,240 --> 07:36:05,440
eventually in next examples we'll work
11056
07:36:05,440 --> 07:36:07,760
actually with database but the main idea
11057
07:36:07,760 --> 07:36:09,360
is exactly the same
11058
07:36:09,360 --> 07:36:11,360
where from the postman while we're
11059
07:36:11,360 --> 07:36:14,958
testing we're sending the id
11060
07:36:14,958 --> 07:36:17,920
in the url basically we add that url
11061
07:36:17,920 --> 07:36:20,080
parameter and then in the body we'll
11062
07:36:20,080 --> 07:36:23,440
send the value so that part won't change
11063
07:36:23,440 --> 07:36:25,920
just the functionality in here and as
11064
07:36:25,920 --> 07:36:28,080
far as this functionality well since we
11065
07:36:28,080 --> 07:36:31,840
have a simple javascript array first i
11066
07:36:31,840 --> 07:36:34,080
would just want to get the person with
11067
07:36:34,080 --> 07:36:35,280
that id
11068
07:36:35,280 --> 07:36:37,600
if the person doesn't exist
11069
07:36:37,600 --> 07:36:41,040
then i'll send back the error response
11070
07:36:41,040 --> 07:36:44,160
and if the id does exist then i'll just
11071
07:36:44,160 --> 07:36:46,240
change the value now i'm not going to be
11072
07:36:46,240 --> 07:36:48,398
looking for the error message if the
11073
07:36:48,398 --> 07:36:50,080
value is not provided we're going to
11074
07:36:50,080 --> 07:36:52,320
cover that once actually we work with
11075
07:36:52,320 --> 07:36:55,120
database i think is just going to be at
11076
07:36:55,120 --> 07:36:58,558
the moment waste of our time so let's do
11077
07:36:58,558 --> 07:36:59,680
it people
11078
07:36:59,680 --> 07:37:00,958
i'm going to remove both of these
11079
07:37:00,958 --> 07:37:02,878
console logs hopefully you understand
11080
07:37:02,878 --> 07:37:06,638
how we supply both values in the postman
11081
07:37:06,638 --> 07:37:08,478
and i'm going to start by
11082
07:37:08,478 --> 07:37:10,320
creating a person and this is going to
11083
07:37:10,320 --> 07:37:12,558
be equal to people
11084
07:37:12,558 --> 07:37:15,120
dot find again i have the array so i
11085
07:37:15,120 --> 07:37:17,840
have access to all the array methods and
11086
07:37:17,840 --> 07:37:18,718
then
11087
07:37:18,718 --> 07:37:21,280
if the person id matches
11088
07:37:21,280 --> 07:37:24,160
to my params id then of course i'm going
11089
07:37:24,160 --> 07:37:26,958
to get the person now let me check in
11090
07:37:26,958 --> 07:37:30,080
the data notice i still have it as a
11091
07:37:30,080 --> 07:37:32,398
number so i'll have to change the type
11092
07:37:32,398 --> 07:37:35,360
as well so i'll say if person id is
11093
07:37:35,360 --> 07:37:38,878
equal to a number and i'll pass in the
11094
07:37:38,878 --> 07:37:41,440
id that i'm getting from my params
11095
07:37:41,440 --> 07:37:43,520
whether that's one five or maybe it's
11096
07:37:43,520 --> 07:37:45,680
going to be id that doesn't make sense
11097
07:37:45,680 --> 07:37:48,638
all right good and if the person doesn't
11098
07:37:48,638 --> 07:37:52,000
exist well then i'll send back the
11099
07:37:52,000 --> 07:37:54,718
response now again in order to save us a
11100
07:37:54,718 --> 07:37:56,878
little bit of time i'll scroll up and
11101
07:37:56,878 --> 07:37:59,840
i'll look for any of my error messages
11102
07:37:59,840 --> 07:38:02,000
since i just want to copy and paste and
11103
07:38:02,000 --> 07:38:03,920
change some values around so in this
11104
07:38:03,920 --> 07:38:06,240
case i'm going to look for this name if
11105
07:38:06,240 --> 07:38:07,600
it doesn't exist
11106
07:38:07,600 --> 07:38:11,040
then of course we have the error and in
11107
07:38:11,040 --> 07:38:12,320
our case of course we just need to
11108
07:38:12,320 --> 07:38:14,558
change it around if the person doesn't
11109
07:38:14,558 --> 07:38:17,280
exist so if we cannot find that in the
11110
07:38:17,280 --> 07:38:21,040
array then we're sending back 404
11111
07:38:21,040 --> 07:38:24,080
remember that was the status code if we
11112
07:38:24,080 --> 07:38:27,120
cannot find the resource and as far as
11113
07:38:27,120 --> 07:38:29,360
the response we have success and of
11114
07:38:29,360 --> 07:38:31,280
course it's going to be false and now
11115
07:38:31,280 --> 07:38:34,398
let's go with message and in the message
11116
07:38:34,398 --> 07:38:35,840
we're going to use a template string
11117
07:38:35,840 --> 07:38:37,920
we'll say no person
11118
07:38:37,920 --> 07:38:39,680
with id
11119
07:38:39,680 --> 07:38:41,520
and then i'm just going to pass in the
11120
07:38:41,520 --> 07:38:42,398
id
11121
07:38:42,398 --> 07:38:43,840
so that's the first
11122
07:38:43,840 --> 07:38:45,600
response if
11123
07:38:45,600 --> 07:38:48,080
of course the person doesn't exist so if
11124
07:38:48,080 --> 07:38:50,718
in the primes we enter for example value
11125
07:38:50,718 --> 07:38:51,520
six
11126
07:38:51,520 --> 07:38:54,398
we know that we have only one to five or
11127
07:38:54,398 --> 07:38:57,600
abc now the second thing if the person
11128
07:38:57,600 --> 07:38:59,120
doesn't exist now of course i just want
11129
07:38:59,120 --> 07:39:01,920
to iterate over the array
11130
07:39:01,920 --> 07:39:05,200
and for that specific person for the
11131
07:39:05,200 --> 07:39:09,120
person whose id matches my params value
11132
07:39:09,120 --> 07:39:12,718
i'll change the name since i'm expecting
11133
07:39:12,718 --> 07:39:14,798
that one to be in the body again we're
11134
07:39:14,798 --> 07:39:16,160
not going to deal with this error for
11135
07:39:16,160 --> 07:39:18,398
now we'll do that once we work with
11136
07:39:18,398 --> 07:39:20,878
database and we're going to go with new
11137
07:39:20,878 --> 07:39:22,398
people
11138
07:39:22,398 --> 07:39:25,680
new people here and we'll say people
11139
07:39:25,680 --> 07:39:29,440
map so i'm iterating over my array again
11140
07:39:29,440 --> 07:39:32,160
i'll reference each and every person as
11141
07:39:32,160 --> 07:39:36,398
a person and i'll say if person id
11142
07:39:36,398 --> 07:39:40,558
matches to that one i have in params so
11143
07:39:40,558 --> 07:39:42,398
number id again
11144
07:39:42,398 --> 07:39:45,280
then i'm going to say person name since
11145
07:39:45,280 --> 07:39:48,240
that is the only other property i have
11146
07:39:48,240 --> 07:39:51,200
is equal to my name now where i'm
11147
07:39:51,200 --> 07:39:53,440
getting the name because i'm getting it
11148
07:39:53,440 --> 07:39:55,120
from the body
11149
07:39:55,120 --> 07:39:58,160
so if the id of the person matches to
11150
07:39:58,160 --> 07:40:00,478
that one in the params then please
11151
07:40:00,478 --> 07:40:02,718
change the name that i can find in the
11152
07:40:02,718 --> 07:40:04,558
body if not
11153
07:40:04,558 --> 07:40:07,680
then simply return a person return
11154
07:40:07,680 --> 07:40:10,320
a person and now of course all the way
11155
07:40:10,320 --> 07:40:12,320
in the bottom i'm going to go with res
11156
07:40:12,320 --> 07:40:13,200
dot
11157
07:40:13,200 --> 07:40:17,120
status and then we're sending back 200
11158
07:40:17,120 --> 07:40:19,840
since the request was successful as far
11159
07:40:19,840 --> 07:40:23,200
as the json we go with success or sorry
11160
07:40:23,200 --> 07:40:26,878
not super we're gonna go with success
11161
07:40:26,878 --> 07:40:28,240
true
11162
07:40:28,240 --> 07:40:30,320
and then as far as data well now i'm
11163
07:40:30,320 --> 07:40:33,520
sending back the new people so let's go
11164
07:40:33,520 --> 07:40:34,478
back
11165
07:40:34,478 --> 07:40:36,638
at this point i have two
11166
07:40:36,638 --> 07:40:38,718
so let me change it back to 1 because i
11167
07:40:38,718 --> 07:40:40,718
know for sure the
11168
07:40:40,718 --> 07:40:43,280
first value was john so again
11169
07:40:43,280 --> 07:40:45,840
the path is api
11170
07:40:45,840 --> 07:40:46,798
people
11171
07:40:46,798 --> 07:40:49,520
and then id of 1 and i just want to
11172
07:40:49,520 --> 07:40:51,680
change it from john
11173
07:40:51,680 --> 07:40:52,398
to
11174
07:40:52,398 --> 07:40:55,040
peter so let me double check in data
11175
07:40:55,040 --> 07:40:57,360
this should be john uh it's a good thing
11176
07:40:57,360 --> 07:40:58,478
i didn't go
11177
07:40:58,478 --> 07:41:01,600
with the idf2 since that one is already
11178
07:41:01,600 --> 07:41:04,878
peter so now let me send it and check it
11179
07:41:04,878 --> 07:41:05,680
out
11180
07:41:05,680 --> 07:41:07,920
now i have success true
11181
07:41:07,920 --> 07:41:09,600
and data
11182
07:41:09,600 --> 07:41:12,958
of course is my array and instead of
11183
07:41:12,958 --> 07:41:14,478
name being john
11184
07:41:14,478 --> 07:41:17,120
for the person number one now the name
11185
07:41:17,120 --> 07:41:19,760
is peter and of course if i'll do the
11186
07:41:19,760 --> 07:41:21,760
same thing for example
11187
07:41:21,760 --> 07:41:23,840
for the person number three again we
11188
07:41:23,840 --> 07:41:25,440
send it back of course now i have three
11189
07:41:25,440 --> 07:41:28,160
peters because i just keep changing
11190
07:41:28,160 --> 07:41:30,000
those values again we're not persisting
11191
07:41:30,000 --> 07:41:32,160
that data but while we're making
11192
07:41:32,160 --> 07:41:35,600
requests you can clearly see how we are
11193
07:41:35,600 --> 07:41:38,718
modifying this data and we do that using
11194
07:41:38,718 --> 07:41:41,600
http put method
11195
07:41:41,600 --> 07:41:44,240
and then of course the path
11196
07:41:44,240 --> 07:41:47,680
is api people and then whatever the id
11197
07:41:47,680 --> 07:41:49,440
and again the whole reason why is that
11198
07:41:49,440 --> 07:41:52,638
happening because on our server we are
11199
07:41:52,638 --> 07:41:54,000
handling that
11200
07:41:54,000 --> 07:41:56,080
all right and the last method i would
11201
07:41:56,080 --> 07:41:57,200
like to cover
11202
07:41:57,200 --> 07:42:00,798
is a delete method so below the put
11203
07:42:00,798 --> 07:42:04,080
method below the modify option i'm going
11204
07:42:04,080 --> 07:42:07,120
to go with app and the method name is
11205
07:42:07,120 --> 07:42:10,320
delete again the convention if you have
11206
07:42:10,320 --> 07:42:15,280
a list is going with a list url and then
11207
07:42:15,280 --> 07:42:17,840
add that specific id because the setup
11208
07:42:17,840 --> 07:42:20,478
is going to be extremely similar to a
11209
07:42:20,478 --> 07:42:22,478
put one now the only difference is going
11210
07:42:22,478 --> 07:42:24,478
to be that of course in this case when
11211
07:42:24,478 --> 07:42:26,878
we're deleting we're not expecting
11212
07:42:26,878 --> 07:42:29,840
anything in the body
11213
07:42:29,840 --> 07:42:32,958
the user hits the url for example api
11214
07:42:32,958 --> 07:42:35,920
people and then one we just remove that
11215
07:42:35,920 --> 07:42:39,280
sucker from our list that's it
11216
07:42:39,280 --> 07:42:41,840
so let's go down where we have
11217
07:42:41,840 --> 07:42:44,478
app delete and again the path is
11218
07:42:44,478 --> 07:42:46,958
following where we go with api
11219
07:42:46,958 --> 07:42:49,120
and then people
11220
07:42:49,120 --> 07:42:51,760
and again let me stress one more time
11221
07:42:51,760 --> 07:42:54,638
something where these routes are
11222
07:42:54,638 --> 07:42:56,240
different
11223
07:42:56,240 --> 07:42:59,600
when we talk about app.get and api
11224
07:42:59,600 --> 07:43:01,798
people when we talk about
11225
07:43:01,798 --> 07:43:05,760
app.post and api people or even if we
11226
07:43:05,760 --> 07:43:09,600
talk about app.put and app.delete
11227
07:43:09,600 --> 07:43:12,080
just because the path is the same since
11228
07:43:12,080 --> 07:43:13,520
the method is different
11229
07:43:13,520 --> 07:43:16,080
these are different requests that is
11230
07:43:16,080 --> 07:43:18,398
crucial please don't think that just
11231
07:43:18,398 --> 07:43:20,958
because you have a different method you
11232
07:43:20,958 --> 07:43:24,638
have to set up a whole new path no
11233
07:43:24,638 --> 07:43:26,558
you can have the same value for the path
11234
07:43:26,558 --> 07:43:28,718
if the method is different that is
11235
07:43:28,718 --> 07:43:31,520
totally different request so let's go
11236
07:43:31,520 --> 07:43:33,360
here again with our callback function we
11237
07:43:33,360 --> 07:43:34,958
should already be comfortable with this
11238
07:43:34,958 --> 07:43:38,160
one we have rack and res and in this
11239
07:43:38,160 --> 07:43:40,000
case i'm not gonna the structure out the
11240
07:43:40,000 --> 07:43:42,320
id from the prompts i just want to
11241
07:43:42,320 --> 07:43:45,280
showcase that we also have this option
11242
07:43:45,280 --> 07:43:48,478
of accessing the params directly so
11243
07:43:48,478 --> 07:43:50,638
first again i want to look for the
11244
07:43:50,638 --> 07:43:52,718
person then we'll send back this
11245
07:43:52,718 --> 07:43:55,920
response and yes oftentimes put and
11246
07:43:55,920 --> 07:43:58,798
delete are going to be extremely similar
11247
07:43:58,798 --> 07:44:01,280
and then if the person does exist then
11248
07:44:01,280 --> 07:44:03,600
of course i'll just filter out the array
11249
07:44:03,600 --> 07:44:06,320
and remove that person from the array
11250
07:44:06,320 --> 07:44:08,240
since again the whole point of this
11251
07:44:08,240 --> 07:44:11,920
request is to remove that person from
11252
07:44:11,920 --> 07:44:14,798
the array so in this case
11253
07:44:14,798 --> 07:44:17,280
you can actually copy and paste and i
11254
07:44:17,280 --> 07:44:19,920
know that some of you might find that
11255
07:44:19,920 --> 07:44:22,558
annoying but at the same time we do need
11256
07:44:22,558 --> 07:44:25,280
to speed this up and start working with
11257
07:44:25,280 --> 07:44:27,760
a database so hopefully you can forgive
11258
07:44:27,760 --> 07:44:29,520
me i'm just gonna grab everything
11259
07:44:29,520 --> 07:44:32,718
starting with the person all the way
11260
07:44:32,718 --> 07:44:36,718
until the end of our error response d404
11261
07:44:36,718 --> 07:44:39,360
let's scroll down copy and paste and
11262
07:44:39,360 --> 07:44:41,600
this is what we're trying to do so i'm
11263
07:44:41,600 --> 07:44:45,040
trying to find a person whose id
11264
07:44:45,040 --> 07:44:47,920
matches that in the params and of course
11265
07:44:47,920 --> 07:44:50,878
in this case i haven't destructured out
11266
07:44:50,878 --> 07:44:52,958
the id right so i just need to change
11267
07:44:52,958 --> 07:44:55,120
this around where i go with rack
11268
07:44:55,120 --> 07:44:58,558
and then params and the id again the
11269
07:44:58,558 --> 07:45:00,638
setup is exactly the same the difference
11270
07:45:00,638 --> 07:45:03,840
is that i'm directly accessing the
11271
07:45:03,840 --> 07:45:06,878
params object and if the person doesn't
11272
07:45:06,878 --> 07:45:07,840
exist
11273
07:45:07,840 --> 07:45:11,760
then we send back d404 so let's try this
11274
07:45:11,760 --> 07:45:13,200
response
11275
07:45:13,200 --> 07:45:16,558
i'm gonna go back to my postman again
11276
07:45:16,558 --> 07:45:20,240
the api is people but we need to change
11277
07:45:20,240 --> 07:45:22,478
the method right so now i would want to
11278
07:45:22,478 --> 07:45:24,798
delete and since i want to test out the
11279
07:45:24,798 --> 07:45:27,520
error and as a sign up you can send the
11280
07:45:27,520 --> 07:45:29,600
body doesn't really matter we're not
11281
07:45:29,600 --> 07:45:32,558
handling that on server anyway so it's
11282
07:45:32,558 --> 07:45:34,478
really irrelevant but since i want to
11283
07:45:34,478 --> 07:45:36,558
test out the error i'm going to go with
11284
07:45:36,558 --> 07:45:40,878
abc so again the path is localhost api
11285
07:45:40,878 --> 07:45:43,840
people and then abc and if everything is
11286
07:45:43,840 --> 07:45:46,320
correct i should see the error and of
11287
07:45:46,320 --> 07:45:48,718
course there is but something tells me
11288
07:45:48,718 --> 07:45:51,280
that this is a different error so let's
11289
07:45:51,280 --> 07:45:53,760
go back and check it out and of course
11290
07:45:53,760 --> 07:45:56,718
the issue is following notice here in
11291
07:45:56,718 --> 07:45:59,360
the json how i'm accessing the id now of
11292
07:45:59,360 --> 07:46:01,280
course i haven't destructured it right
11293
07:46:01,280 --> 07:46:04,320
in this case i went with rec params and
11294
07:46:04,320 --> 07:46:06,638
then the id so i'm going to go with rack
11295
07:46:06,638 --> 07:46:10,160
params and the id let's save it and
11296
07:46:10,160 --> 07:46:12,240
let's try one more time again i'm
11297
07:46:12,240 --> 07:46:15,120
looking for a person whose id doesn't
11298
07:46:15,120 --> 07:46:18,080
exist so i'm gonna get the error and
11299
07:46:18,080 --> 07:46:21,040
there it is i have success
11300
07:46:21,040 --> 07:46:22,080
false
11301
07:46:22,080 --> 07:46:24,718
and as far as the message no person with
11302
07:46:24,718 --> 07:46:28,558
id of abc so i was looking for the
11303
07:46:28,558 --> 07:46:30,080
person
11304
07:46:30,080 --> 07:46:31,360
with id
11305
07:46:31,360 --> 07:46:34,638
that's not in my array so that's why of
11306
07:46:34,638 --> 07:46:37,760
course i got back the error response so
11307
07:46:37,760 --> 07:46:41,200
now i'm gonna go back to our app delete
11308
07:46:41,200 --> 07:46:43,520
request and here i just want to filter
11309
07:46:43,520 --> 07:46:45,360
out the array and the way we're going to
11310
07:46:45,360 --> 07:46:48,398
do that i'm going to go with const new
11311
07:46:48,398 --> 07:46:49,520
people
11312
07:46:49,520 --> 07:46:51,200
is equal to
11313
07:46:51,200 --> 07:46:52,798
people.filter
11314
07:46:52,798 --> 07:46:54,878
that's my array and i'm just filtering
11315
07:46:54,878 --> 07:46:58,160
it i'll pass in my callback function
11316
07:46:58,160 --> 07:46:59,040
and
11317
07:46:59,040 --> 07:47:00,718
i'll reference each
11318
07:47:00,718 --> 07:47:03,600
object with the person parameter
11319
07:47:03,600 --> 07:47:06,240
and i'll just say if person
11320
07:47:06,240 --> 07:47:07,280
id
11321
07:47:07,280 --> 07:47:08,878
does not match
11322
07:47:08,878 --> 07:47:12,558
the id found in rec dot params then
11323
07:47:12,558 --> 07:47:14,240
return that person
11324
07:47:14,240 --> 07:47:16,320
if it does match then it's not going to
11325
07:47:16,320 --> 07:47:18,000
be included that's why we go with
11326
07:47:18,000 --> 07:47:20,638
exclamation point and remember that we
11327
07:47:20,638 --> 07:47:23,920
still get our id as a string so we must
11328
07:47:23,920 --> 07:47:26,958
do the number one first and then only we
11329
07:47:26,958 --> 07:47:30,320
go with params and the id so that's our
11330
07:47:30,320 --> 07:47:33,760
new array without that specific person
11331
07:47:33,760 --> 07:47:35,120
whose id
11332
07:47:35,120 --> 07:47:37,520
matched the one in params so now of
11333
07:47:37,520 --> 07:47:39,600
course let's just go with our return
11334
07:47:39,600 --> 07:47:40,718
return
11335
07:47:40,718 --> 07:47:41,760
res
11336
07:47:41,760 --> 07:47:43,680
dot status
11337
07:47:43,680 --> 07:47:45,920
here and this is going to be 200 of
11338
07:47:45,920 --> 07:47:46,718
course
11339
07:47:46,718 --> 07:47:49,440
and i'm going to go with json and we'll
11340
07:47:49,440 --> 07:47:53,280
set up our object success is true
11341
07:47:53,280 --> 07:47:56,638
and i'll send back the new people array
11342
07:47:56,638 --> 07:47:59,200
so we're going to go with data and then
11343
07:47:59,200 --> 07:48:02,240
new people let's save it
11344
07:48:02,240 --> 07:48:04,558
and let me test that out e in the
11345
07:48:04,558 --> 07:48:07,120
postman where of course now i'm gonna go
11346
07:48:07,120 --> 07:48:08,398
with id
11347
07:48:08,398 --> 07:48:11,120
that is in the array and that's gonna be
11348
07:48:11,120 --> 07:48:14,558
number one so if i send notice how it is
11349
07:48:14,558 --> 07:48:17,280
irrelevant if i send body
11350
07:48:17,280 --> 07:48:19,520
again because i'm not handling that on
11351
07:48:19,520 --> 07:48:21,760
the server anyway but
11352
07:48:21,760 --> 07:48:24,240
if i take a look at my data
11353
07:48:24,240 --> 07:48:27,280
well the person with id number one
11354
07:48:27,280 --> 07:48:30,478
john is missing why well because his id
11355
07:48:30,478 --> 07:48:33,120
matched to the one that i passed in the
11356
07:48:33,120 --> 07:48:36,160
primes so now of course he was removed
11357
07:48:36,160 --> 07:48:39,120
from the array so if i'll go with five
11358
07:48:39,120 --> 07:48:41,200
same deal now the john is back here
11359
07:48:41,200 --> 07:48:43,360
because again we're not persisting data
11360
07:48:43,360 --> 07:48:44,080
but
11361
07:48:44,080 --> 07:48:47,840
the person with id of five has been
11362
07:48:47,840 --> 07:48:50,958
removed from the array hopefully this
11363
07:48:50,958 --> 07:48:53,280
gives you a good understanding how we
11364
07:48:53,280 --> 07:48:56,558
use other methods as well not just get
11365
07:48:56,558 --> 07:49:00,798
and why we go with postman instead of
11366
07:49:00,798 --> 07:49:03,040
setting up the front-end but before we
11367
07:49:03,040 --> 07:49:04,000
cover
11368
07:49:04,000 --> 07:49:06,080
router in express let me just stress
11369
07:49:06,080 --> 07:49:07,120
something
11370
07:49:07,120 --> 07:49:08,160
that yes
11371
07:49:08,160 --> 07:49:10,478
while we're developing our server of
11372
07:49:10,478 --> 07:49:12,558
course we'll use postman because you can
11373
07:49:12,558 --> 07:49:15,600
clearly see how much faster it is i set
11374
07:49:15,600 --> 07:49:18,398
up the route and i quickly tested
11375
07:49:18,398 --> 07:49:20,160
otherwise if i would have to build a
11376
07:49:20,160 --> 07:49:22,558
front end for every route that i'm
11377
07:49:22,558 --> 07:49:24,718
creating i mean the process would be
11378
07:49:24,718 --> 07:49:27,920
just very very long but at the end of
11379
07:49:27,920 --> 07:49:29,280
the day
11380
07:49:29,280 --> 07:49:32,320
when you set up the api
11381
07:49:32,320 --> 07:49:34,558
the expectation is that there is going
11382
07:49:34,558 --> 07:49:37,440
to be someone who's using that data now
11383
07:49:37,440 --> 07:49:39,600
that could be on your own server so for
11384
07:49:39,600 --> 07:49:42,320
example if you're building a full stack
11385
07:49:42,320 --> 07:49:44,958
application with mern that's one of the
11386
07:49:44,958 --> 07:49:46,878
options that could be decoupled
11387
07:49:46,878 --> 07:49:49,440
application remember how i showed you
11388
07:49:49,440 --> 07:49:52,240
a example where i have the api on my
11389
07:49:52,240 --> 07:49:55,520
server but then there is a react
11390
07:49:55,520 --> 07:49:57,520
application that uses it it doesn't mean
11391
07:49:57,520 --> 07:49:58,878
that you have to build that front-end
11392
07:49:58,878 --> 07:50:00,558
application yourself
11393
07:50:00,558 --> 07:50:03,680
just be mindful that the whole idea of
11394
07:50:03,680 --> 07:50:06,240
setting up the api
11395
07:50:06,240 --> 07:50:08,718
is that someone is going to be using
11396
07:50:08,718 --> 07:50:11,600
that data that's it that's all i want to
11397
07:50:11,600 --> 07:50:14,240
say and now of course we can talk about
11398
07:50:14,240 --> 07:50:17,120
router in express
11399
07:50:17,120 --> 07:50:19,280
all right i think we're moving along
11400
07:50:19,280 --> 07:50:20,798
quite nicely
11401
07:50:20,798 --> 07:50:23,760
but as you can see the moment we're
11402
07:50:23,760 --> 07:50:25,360
starting to have
11403
07:50:25,360 --> 07:50:26,878
more routes
11404
07:50:26,878 --> 07:50:29,360
and more functionality
11405
07:50:29,360 --> 07:50:31,840
we have another issue and that issue of
11406
07:50:31,840 --> 07:50:35,200
course is the fact that our app.js
11407
07:50:35,200 --> 07:50:37,680
is getting quite busy and technically we
11408
07:50:37,680 --> 07:50:40,558
don't even have that many routes so what
11409
07:50:40,558 --> 07:50:43,360
is the solution well the solution is
11410
07:50:43,360 --> 07:50:46,958
using express router where we can group
11411
07:50:46,958 --> 07:50:48,718
those routes together
11412
07:50:48,718 --> 07:50:51,360
and then as far as the functionality and
11413
07:50:51,360 --> 07:50:53,840
actually set them up as separate
11414
07:50:53,840 --> 07:50:55,200
controllers
11415
07:50:55,200 --> 07:50:56,718
now eventually
11416
07:50:56,718 --> 07:50:58,398
in the next example when we work with
11417
07:50:58,398 --> 07:51:01,520
database i'll cover again the common
11418
07:51:01,520 --> 07:51:02,558
setup
11419
07:51:02,558 --> 07:51:05,680
which is called mvc basically it is a
11420
07:51:05,680 --> 07:51:08,398
pattern it's not a rule but it is a
11421
07:51:08,398 --> 07:51:11,120
nicely used pattern when you're setting
11422
07:51:11,120 --> 07:51:13,760
up the api but we are missing the model
11423
07:51:13,760 --> 07:51:16,240
part in the mvc pattern because we
11424
07:51:16,240 --> 07:51:18,638
haven't connected the database so i'll
11425
07:51:18,638 --> 07:51:20,558
just leave it at that just please keep
11426
07:51:20,558 --> 07:51:22,798
in mind whatever i'm about to show you
11427
07:51:22,798 --> 07:51:25,760
in this video and in the next yes it is
11428
07:51:25,760 --> 07:51:28,398
a pretty common convention and pattern
11429
07:51:28,398 --> 07:51:29,280
so
11430
07:51:29,280 --> 07:51:32,718
i also suggest sticking to that now also
11431
07:51:32,718 --> 07:51:35,280
i want to mention that in this video
11432
07:51:35,280 --> 07:51:38,478
we will do a bit of copy and pasting
11433
07:51:38,478 --> 07:51:40,718
meaning we'll have to refactor the code
11434
07:51:40,718 --> 07:51:42,240
because i think this is a perfect
11435
07:51:42,240 --> 07:51:44,718
example for us to set up the router
11436
07:51:44,718 --> 07:51:47,040
where we already have some code so you
11437
07:51:47,040 --> 07:51:49,840
can clearly see the benefits of setting
11438
07:51:49,840 --> 07:51:52,160
up the router instead of me coming up
11439
07:51:52,160 --> 07:51:54,160
with some empty routes and then saying
11440
07:51:54,160 --> 07:51:58,000
that yes that is the best pattern so
11441
07:51:58,000 --> 07:52:00,958
with that said first where we have the
11442
07:52:00,958 --> 07:52:04,240
request the post request for api postman
11443
07:52:04,240 --> 07:52:06,798
people i want you to change this around
11444
07:52:06,798 --> 07:52:08,798
a little bit so as you can see i keep
11445
07:52:08,798 --> 07:52:11,120
going back and forth in this case i want
11446
07:52:11,120 --> 07:52:13,760
to go with api people and then let's
11447
07:52:13,760 --> 07:52:15,600
just say postman again it doesn't really
11448
07:52:15,600 --> 07:52:17,040
matter we're not going to make requests
11449
07:52:17,040 --> 07:52:19,200
anymore from the postman in these
11450
07:52:19,200 --> 07:52:22,160
examples anyway this is just about our
11451
07:52:22,160 --> 07:52:24,878
code structure and then once we change
11452
07:52:24,878 --> 07:52:27,760
this around i also want to move this
11453
07:52:27,760 --> 07:52:30,240
login up just so it's a bit clearer what
11454
07:52:30,240 --> 07:52:32,718
we're doing so take this login route and
11455
07:52:32,718 --> 07:52:34,798
keep moving moving moving moving and
11456
07:52:34,798 --> 07:52:37,840
just place it on top of your routes
11457
07:52:37,840 --> 07:52:40,160
and there it is now of course i have my
11458
07:52:40,160 --> 07:52:43,200
login and now notice the pattern
11459
07:52:43,200 --> 07:52:46,558
whereas i have one route for the login
11460
07:52:46,558 --> 07:52:50,240
but then rest of them i have api people
11461
07:52:50,240 --> 07:52:53,680
api people api people postman
11462
07:52:53,680 --> 07:52:55,440
api people id
11463
07:52:55,440 --> 07:52:57,280
and hopefully you get the gist again i
11464
07:52:57,280 --> 07:53:00,240
have api people id
11465
07:53:00,240 --> 07:53:02,798
so wouldn't it be nicer
11466
07:53:02,798 --> 07:53:06,000
if i could somehow group all the api
11467
07:53:06,000 --> 07:53:10,000
people and also add one for the login
11468
07:53:10,000 --> 07:53:12,878
but basically in a separate file and the
11469
07:53:12,878 --> 07:53:15,760
way we can do that is by setting up the
11470
07:53:15,760 --> 07:53:18,478
router now again common convention
11471
07:53:18,478 --> 07:53:21,760
is just creating another folder
11472
07:53:21,760 --> 07:53:23,920
so i just want to make sure that i'm
11473
07:53:23,920 --> 07:53:25,680
actually doing that in the express
11474
07:53:25,680 --> 07:53:28,398
tutorial let's create a new folder
11475
07:53:28,398 --> 07:53:30,958
and common convention is calling this
11476
07:53:30,958 --> 07:53:33,680
route and then you just come up with
11477
07:53:33,680 --> 07:53:35,920
whatever name you want now in my case
11478
07:53:35,920 --> 07:53:37,120
i'm gonna go
11479
07:53:37,120 --> 07:53:40,160
off for the login because
11480
07:53:40,160 --> 07:53:41,440
we're just gonna imagine that maybe
11481
07:53:41,440 --> 07:53:43,680
there's going to be one for register one
11482
07:53:43,680 --> 07:53:45,840
for logout or whatever
11483
07:53:45,840 --> 07:53:48,000
and then there's going to be one
11484
07:53:48,000 --> 07:53:50,798
40 people so that's going to be for my
11485
07:53:50,798 --> 07:53:53,920
api and people so let's set up these two
11486
07:53:53,920 --> 07:53:56,638
files i will zoom in massively just so
11487
07:53:56,638 --> 07:53:58,398
you don't think that i'm cheating so
11488
07:53:58,398 --> 07:54:00,958
let's go here with alt js this is where
11489
07:54:00,958 --> 07:54:03,520
i'll place my login one and also right
11490
07:54:03,520 --> 07:54:05,360
away set up my
11491
07:54:05,360 --> 07:54:06,718
people
11492
07:54:06,718 --> 07:54:07,680
people
11493
07:54:07,680 --> 07:54:11,280
just okay awesome i'll start working in
11494
07:54:11,280 --> 07:54:13,040
people.js because it's going to be a bit
11495
07:54:13,040 --> 07:54:15,040
interesting and then we'll do the same
11496
07:54:15,040 --> 07:54:17,760
thing in the auth as well
11497
07:54:17,760 --> 07:54:19,840
and the way we set this up we simply
11498
07:54:19,840 --> 07:54:22,958
start by requiring the express so we go
11499
07:54:22,958 --> 07:54:26,160
here express require and of course the
11500
07:54:26,160 --> 07:54:28,878
module name is express and then instead
11501
07:54:28,878 --> 07:54:32,638
of setting up the app we go with router
11502
07:54:32,638 --> 07:54:35,280
and we explicitly get the router from
11503
07:54:35,280 --> 07:54:37,520
the express so this is going to be a
11504
07:54:37,520 --> 07:54:39,440
router instance
11505
07:54:39,440 --> 07:54:42,478
that takes care of the routing instead
11506
07:54:42,478 --> 07:54:43,760
of the app
11507
07:54:43,760 --> 07:54:44,558
so
11508
07:54:44,558 --> 07:54:45,680
we're not going to do the routing
11509
07:54:45,680 --> 07:54:48,558
anymore in our app.js well sort of we
11510
07:54:48,558 --> 07:54:51,520
will we'll use the middleware but as far
11511
07:54:51,520 --> 07:54:54,958
as the specific urls we'll handle that
11512
07:54:54,958 --> 07:54:57,840
in this routes people file and the way
11513
07:54:57,840 --> 07:55:00,160
we set up the router we go with router
11514
07:55:00,160 --> 07:55:02,478
and that one is equal to like i said
11515
07:55:02,478 --> 07:55:05,840
express dot and then we go with capital
11516
07:55:05,840 --> 07:55:09,520
letter router and then we invoke it
11517
07:55:09,520 --> 07:55:10,478
awesome
11518
07:55:10,478 --> 07:55:13,120
that's a great start we can save this
11519
07:55:13,120 --> 07:55:15,520
sucker and now i want you to go back to
11520
07:55:15,520 --> 07:55:16,878
app.js
11521
07:55:16,878 --> 07:55:19,520
and don't grab the login that's why i
11522
07:55:19,520 --> 07:55:21,760
set it up separately above all the
11523
07:55:21,760 --> 07:55:25,520
routes but grab all of the ones with api
11524
07:55:25,520 --> 07:55:29,200
and people so take these suckers here
11525
07:55:29,200 --> 07:55:31,040
all the way
11526
07:55:31,040 --> 07:55:32,798
up to delete
11527
07:55:32,798 --> 07:55:36,080
basically stop at listen cut it out
11528
07:55:36,080 --> 07:55:38,958
and then copy and paste in people like
11529
07:55:38,958 --> 07:55:40,718
so okay
11530
07:55:40,718 --> 07:55:43,280
great so now of course i have all my
11531
07:55:43,280 --> 07:55:44,878
routes here
11532
07:55:44,878 --> 07:55:47,920
now our next step is actually changing
11533
07:55:47,920 --> 07:55:49,600
from app
11534
07:55:49,600 --> 07:55:52,798
to router because i want the router to
11535
07:55:52,798 --> 07:55:55,040
handle the routing so in my case i'm
11536
07:55:55,040 --> 07:55:57,520
going to select all of my apps like so
11537
07:55:57,520 --> 07:55:59,840
so i'm just doing that with command d
11538
07:55:59,840 --> 07:56:03,760
in the visual studio code and
11539
07:56:03,760 --> 07:56:05,440
at this point i should have multiple
11540
07:56:05,440 --> 07:56:08,398
cursors like so and i'll set up my
11541
07:56:08,398 --> 07:56:09,440
router
11542
07:56:09,440 --> 07:56:12,718
and now all the way in the bottom
11543
07:56:12,718 --> 07:56:14,958
i want to go with module
11544
07:56:14,958 --> 07:56:18,080
not exports and of course at the moment
11545
07:56:18,080 --> 07:56:20,718
i'm exporting my router
11546
07:56:20,718 --> 07:56:23,040
and then once we export a router of
11547
07:56:23,040 --> 07:56:25,360
course in the app.js
11548
07:56:25,360 --> 07:56:28,798
i want to set up a app.use where i'll
11549
07:56:28,798 --> 07:56:31,680
say yes for the path that starts with
11550
07:56:31,680 --> 07:56:33,840
api and people
11551
07:56:33,840 --> 07:56:37,680
i want to use my people router and the
11552
07:56:37,680 --> 07:56:39,280
way it's going to look like we're going
11553
07:56:39,280 --> 07:56:40,478
to go back
11554
07:56:40,478 --> 07:56:41,360
and
11555
07:56:41,360 --> 07:56:43,200
at this point we have people here and as
11556
07:56:43,200 --> 07:56:45,920
a side note actually we need to cut it
11557
07:56:45,920 --> 07:56:48,000
out but we're not going to use it here
11558
07:56:48,000 --> 07:56:50,558
as well eventually the setup is going to
11559
07:56:50,558 --> 07:56:53,040
be different so for now yes
11560
07:56:53,040 --> 07:56:56,240
please cut it out and copy and paste in
11561
07:56:56,240 --> 07:56:58,638
people but since we'll be setting up the
11562
07:56:58,638 --> 07:57:00,798
controls we'll have to do that one more
11563
07:57:00,798 --> 07:57:02,958
time anyway so i guess i'm going to go
11564
07:57:02,958 --> 07:57:05,120
below the router i'm going to look for
11565
07:57:05,120 --> 07:57:07,280
my people otherwise my functionality
11566
07:57:07,280 --> 07:57:09,440
won't make sense and in this case
11567
07:57:09,440 --> 07:57:10,958
remember
11568
07:57:10,958 --> 07:57:13,920
we are sitting already in routes so i
11569
07:57:13,920 --> 07:57:17,120
need to go two levels up and i still
11570
07:57:17,120 --> 07:57:20,398
need to look for my data correct so now
11571
07:57:20,398 --> 07:57:23,520
of course our functionality should work
11572
07:57:23,520 --> 07:57:27,120
hopefully we don't have some weird bugs
11573
07:57:27,120 --> 07:57:30,080
and then like i said in the app jess now
11574
07:57:30,080 --> 07:57:32,040
of course i would want to use my
11575
07:57:32,040 --> 07:57:36,240
app.use so i'm going to go below express
11576
07:57:36,240 --> 07:57:39,280
json and i'm going to say app.use and
11577
07:57:39,280 --> 07:57:41,760
now i need to set up that base route now
11578
07:57:41,760 --> 07:57:44,638
what is my base route here well it is
11579
07:57:44,638 --> 07:57:46,878
api and people correct
11580
07:57:46,878 --> 07:57:47,840
because
11581
07:57:47,840 --> 07:57:50,878
this is just my route and then in some
11582
07:57:50,878 --> 07:57:54,080
cases i have the postman or i have the
11583
07:57:54,080 --> 07:57:55,120
id
11584
07:57:55,120 --> 07:57:57,200
that's it but the base stays the same
11585
07:57:57,200 --> 07:58:00,240
okay awesome so we provide this path we
11586
07:58:00,240 --> 07:58:04,080
say api and then people and now i want
11587
07:58:04,080 --> 07:58:05,840
to set up that router
11588
07:58:05,840 --> 07:58:07,440
and of course in order to set up the
11589
07:58:07,440 --> 07:58:08,638
router
11590
07:58:08,638 --> 07:58:10,240
i need to import it
11591
07:58:10,240 --> 07:58:12,718
and you can call it people router you
11592
07:58:12,718 --> 07:58:14,958
can just call it people again
11593
07:58:14,958 --> 07:58:17,200
that is really up to you i'm just going
11594
07:58:17,200 --> 07:58:19,760
to say people is equal and now we need
11595
07:58:19,760 --> 07:58:22,398
to require it and remember we have a
11596
07:58:22,398 --> 07:58:25,280
folder by the name of what routes
11597
07:58:25,280 --> 07:58:28,798
and then we also have people file okay
11598
07:58:28,798 --> 07:58:32,398
awesome we require our router
11599
07:58:32,398 --> 07:58:34,240
here correct
11600
07:58:34,240 --> 07:58:36,798
so that's our router and we're exporting
11601
07:58:36,798 --> 07:58:40,080
awesome so we get the people and like i
11602
07:58:40,080 --> 07:58:42,958
said with use we can use the path if we
11603
07:58:42,958 --> 07:58:44,958
want so this is not going to be applied
11604
07:58:44,958 --> 07:58:46,718
to all of them this is only going to be
11605
07:58:46,718 --> 07:58:48,878
applied to the ones
11606
07:58:48,878 --> 07:58:52,240
that start with api people and then we
11607
07:58:52,240 --> 07:58:54,798
pass in the people we'll save it but
11608
07:58:54,798 --> 07:58:57,040
there is a problem you see once we set
11609
07:58:57,040 --> 07:58:58,638
up this router
11610
07:58:58,638 --> 07:59:00,798
with a base path
11611
07:59:00,798 --> 07:59:02,240
in the people
11612
07:59:02,240 --> 07:59:05,200
actually we have a mess right now
11613
07:59:05,200 --> 07:59:07,680
because we already have the base
11614
07:59:07,680 --> 07:59:10,878
for our router in app js where we have
11615
07:59:10,878 --> 07:59:13,600
api people and now in the people.js if
11616
07:59:13,600 --> 07:59:15,600
you check it out we actually have api
11617
07:59:15,600 --> 07:59:18,878
people forward slash api people so i
11618
07:59:18,878 --> 07:59:21,600
fully understand that this is going to
11619
07:59:21,600 --> 07:59:23,600
look a bit drastic
11620
07:59:23,600 --> 07:59:26,320
but in order for this to work we
11621
07:59:26,320 --> 07:59:28,320
actually remove everything we remove
11622
07:59:28,320 --> 07:59:30,878
that base so we go here with just a
11623
07:59:30,878 --> 07:59:32,398
forward slash why well because we
11624
07:59:32,398 --> 07:59:35,360
already have the base here we have api
11625
07:59:35,360 --> 07:59:36,320
people
11626
07:59:36,320 --> 07:59:38,958
so that's how the route is going to
11627
07:59:38,958 --> 07:59:40,240
start
11628
07:59:40,240 --> 07:59:41,280
and then
11629
07:59:41,280 --> 07:59:43,440
we just have the forward slash so this
11630
07:59:43,440 --> 07:59:46,160
will match whatever i have in the
11631
07:59:46,160 --> 07:59:47,280
address
11632
07:59:47,280 --> 07:59:50,240
and since in the get and in a post both
11633
07:59:50,240 --> 07:59:52,718
of them just have api people well i
11634
07:59:52,718 --> 07:59:54,958
simply go with forward slash again i
11635
07:59:54,958 --> 07:59:56,878
fully understand that this might look a
11636
07:59:56,878 --> 08:00:00,160
bit confusing in the beginning but again
11637
08:00:00,160 --> 08:00:02,718
the more routers you'll set up the
11638
08:00:02,718 --> 08:00:05,360
easier it's going to get so this will
11639
08:00:05,360 --> 08:00:06,638
just match
11640
08:00:06,638 --> 08:00:09,920
whatever path i have here whatever base
11641
08:00:09,920 --> 08:00:13,200
path i set up in my app.js so i'm just
11642
08:00:13,200 --> 08:00:16,398
saying yes i'm using this people router
11643
08:00:16,398 --> 08:00:18,798
and in the people since i already have
11644
08:00:18,798 --> 08:00:21,600
base path in place i can simply
11645
08:00:21,600 --> 08:00:23,920
reference it using the forward slash so
11646
08:00:23,920 --> 08:00:26,478
this is going to match whatever i have
11647
08:00:26,478 --> 08:00:27,760
in app
11648
08:00:27,760 --> 08:00:30,558
and once i have spent what seems like an
11649
08:00:30,558 --> 08:00:33,600
eternity on the base path i can keep on
11650
08:00:33,600 --> 08:00:36,398
scrolling and again notice i have api
11651
08:00:36,398 --> 08:00:38,878
people and then postman so what's the
11652
08:00:38,878 --> 08:00:41,520
only thing that i need here it's just a
11653
08:00:41,520 --> 08:00:43,360
postman because i already have api
11654
08:00:43,360 --> 08:00:46,240
people so i remove this sucker here
11655
08:00:46,240 --> 08:00:47,920
keep on scrolling
11656
08:00:47,920 --> 08:00:50,878
then the same with id right we only care
11657
08:00:50,878 --> 08:00:52,398
about this one that's the whole point of
11658
08:00:52,398 --> 08:00:54,718
setting up the router so we can start
11659
08:00:54,718 --> 08:00:57,280
splitting up our functionality in
11660
08:00:57,280 --> 08:01:00,958
multiple files same goes with this last
11661
08:01:00,958 --> 08:01:01,840
id
11662
08:01:01,840 --> 08:01:03,920
and even though i think i said that
11663
08:01:03,920 --> 08:01:05,680
we're not going to use the postman i do
11664
08:01:05,680 --> 08:01:07,360
want to quickly check
11665
08:01:07,360 --> 08:01:09,520
and i'm going to go with get simple if
11666
08:01:09,520 --> 08:01:11,600
the get works trust me the other ones
11667
08:01:11,600 --> 08:01:14,240
work as well so we go here with api
11668
08:01:14,240 --> 08:01:17,600
people we send but unfortunately i get
11669
08:01:17,600 --> 08:01:20,638
the error hmm that's interesting so let
11670
08:01:20,638 --> 08:01:23,520
me check i think the problem here is in
11671
08:01:23,520 --> 08:01:24,638
the app.js
11672
08:01:24,638 --> 08:01:27,360
i don't have the forward slash so let me
11673
08:01:27,360 --> 08:01:28,798
try this one
11674
08:01:28,798 --> 08:01:31,680
let me send it one more time and there
11675
08:01:31,680 --> 08:01:34,000
it is now of course everything works so
11676
08:01:34,000 --> 08:01:36,398
my apologies i forgot to add here
11677
08:01:36,398 --> 08:01:39,680
forward slash that's why we were getting
11678
08:01:39,680 --> 08:01:42,718
the error and of course everything works
11679
08:01:42,718 --> 08:01:46,398
i can go with get i can get my list
11680
08:01:46,398 --> 08:01:49,280
if i want i can also maybe delete a
11681
08:01:49,280 --> 08:01:51,840
person so i'm going to go delete and
11682
08:01:51,840 --> 08:01:54,160
we're gonna test three so let's see we
11683
08:01:54,160 --> 08:01:56,240
send and as you can see
11684
08:01:56,240 --> 08:01:59,120
the person with id of three is missing
11685
08:01:59,120 --> 08:02:02,080
so as you can see our route works so
11686
08:02:02,080 --> 08:02:04,320
that's the first step so now here's the
11687
08:02:04,320 --> 08:02:05,280
challenge
11688
08:02:05,280 --> 08:02:08,718
try to repeat the same thing with login
11689
08:02:08,718 --> 08:02:10,240
now with login
11690
08:02:10,240 --> 08:02:12,878
it is going to be a bit simpler because
11691
08:02:12,878 --> 08:02:15,280
we don't have that long of a path but
11692
08:02:15,280 --> 08:02:18,240
again stop the video and try to set up
11693
08:02:18,240 --> 08:02:20,398
the login yourself and then once you're
11694
08:02:20,398 --> 08:02:22,958
ready resume it and we're going to do it
11695
08:02:22,958 --> 08:02:24,398
together
11696
08:02:24,398 --> 08:02:27,360
how was it hopefully you were successful
11697
08:02:27,360 --> 08:02:30,798
and i'm going to give it a shot so first
11698
08:02:30,798 --> 08:02:33,680
remember we needed a file so in this
11699
08:02:33,680 --> 08:02:37,120
case i do have auth js awesome
11700
08:02:37,120 --> 08:02:40,798
so i'm just gonna cut this sucker out
11701
08:02:40,798 --> 08:02:43,200
from my app.js
11702
08:02:43,200 --> 08:02:46,320
and place it in off then of course i do
11703
08:02:46,320 --> 08:02:48,638
need to have the boilerplate for the
11704
08:02:48,638 --> 08:02:51,680
router so again let's go with express
11705
08:02:51,680 --> 08:02:52,878
require
11706
08:02:52,878 --> 08:02:55,280
we require the module by the name of
11707
08:02:55,280 --> 08:02:57,680
express of course then we set up our
11708
08:02:57,680 --> 08:02:59,040
router
11709
08:02:59,040 --> 08:03:02,160
router and that one is equal to express
11710
08:03:02,160 --> 08:03:06,160
dot router we invoke it then we change
11711
08:03:06,160 --> 08:03:07,440
this value
11712
08:03:07,440 --> 08:03:09,760
around like so
11713
08:03:09,760 --> 08:03:14,000
and then we're just gonna go with module
11714
08:03:14,000 --> 08:03:17,680
and then export and that one is equal to
11715
08:03:17,680 --> 08:03:20,240
our router we do this
11716
08:03:20,240 --> 08:03:23,440
then we navigate back to app.js and
11717
08:03:23,440 --> 08:03:26,080
we'll have to import it as well so we go
11718
08:03:26,080 --> 08:03:29,120
with auth i guess let's call it that
11719
08:03:29,120 --> 08:03:32,240
and we're looking for auth file
11720
08:03:32,240 --> 08:03:34,638
that's where our router is coming in and
11721
08:03:34,638 --> 08:03:37,040
then of course i just need to set up my
11722
08:03:37,040 --> 08:03:40,080
middleware myapp.use where i provide a
11723
08:03:40,080 --> 08:03:43,520
path of what well of login and in this
11724
08:03:43,520 --> 08:03:45,360
case i'm going to be more careful and i
11725
08:03:45,360 --> 08:03:47,760
will add the forward slash and of course
11726
08:03:47,760 --> 08:03:51,360
i'll set it up as auth so let's save
11727
08:03:51,360 --> 08:03:53,040
this one and then the only thing we need
11728
08:03:53,040 --> 08:03:55,280
to do in the login
11729
08:03:55,280 --> 08:03:58,718
is simply change it around where i'm
11730
08:03:58,718 --> 08:04:01,200
already handling the login in the app.js
11731
08:04:01,200 --> 08:04:03,760
so forward slash login so again my route
11732
08:04:03,760 --> 08:04:07,120
now is forward slash login so i simply
11733
08:04:07,120 --> 08:04:09,360
go with the forward slash i already
11734
08:04:09,360 --> 08:04:12,398
covered the route in the objects so
11735
08:04:12,398 --> 08:04:15,200
i don't need to do that anymore in here
11736
08:04:15,200 --> 08:04:17,360
and you know for this sucker just to
11737
08:04:17,360 --> 08:04:19,360
make things a bit more interesting
11738
08:04:19,360 --> 08:04:21,360
why don't we use the browser now keep in
11739
08:04:21,360 --> 08:04:23,200
mind you can still use the postman if
11740
08:04:23,200 --> 08:04:26,320
you want but i'm gonna go the browser
11741
08:04:26,320 --> 08:04:28,878
out so we're gonna go with new window
11742
08:04:28,878 --> 08:04:31,360
i'm gonna go with 5
11743
08:04:31,360 --> 08:04:32,798
000 here
11744
08:04:32,798 --> 08:04:34,320
and of course this is going to be the
11745
08:04:34,320 --> 08:04:37,360
small screen i have traditional form so
11746
08:04:37,360 --> 08:04:39,440
let's go with some kind of name peter
11747
08:04:39,440 --> 08:04:42,798
and i send and i have welcome peter and
11748
08:04:42,798 --> 08:04:44,638
of course if that is the case i know
11749
08:04:44,638 --> 08:04:47,920
that my functionality works so we are in
11750
08:04:47,920 --> 08:04:50,878
good shape again probably the most
11751
08:04:50,878 --> 08:04:52,320
confusing thing
11752
08:04:52,320 --> 08:04:56,240
is this one where we use app.use to set
11753
08:04:56,240 --> 08:04:59,520
up our route so all the routes
11754
08:04:59,520 --> 08:05:02,320
that will start with this api people are
11755
08:05:02,320 --> 08:05:05,680
going to be located in the people.js in
11756
08:05:05,680 --> 08:05:08,558
the routes folder and in there instead
11757
08:05:08,558 --> 08:05:11,440
of using app we use the router and then
11758
08:05:11,440 --> 08:05:13,520
since we already have the base
11759
08:05:13,520 --> 08:05:16,000
setup in the app.js
11760
08:05:16,000 --> 08:05:18,718
when we have a route that exactly
11761
08:05:18,718 --> 08:05:20,798
matches the base we just go with forward
11762
08:05:20,798 --> 08:05:23,200
slash now if there is something else
11763
08:05:23,200 --> 08:05:26,478
after the base route so some additional
11764
08:05:26,478 --> 08:05:29,200
info of course then we go with forward
11765
08:05:29,200 --> 08:05:31,360
slash which just means that we match the
11766
08:05:31,360 --> 08:05:35,280
base path and then we add whatever value
11767
08:05:35,280 --> 08:05:36,398
we want
11768
08:05:36,398 --> 08:05:38,798
and hopefully you can see how much
11769
08:05:38,798 --> 08:05:42,160
cleaner is our app.js
11770
08:05:42,160 --> 08:05:43,840
where now we're splitting up the
11771
08:05:43,840 --> 08:05:46,878
functionality in separate files
11772
08:05:46,878 --> 08:05:50,320
for our routes beautiful i think we
11773
08:05:50,320 --> 08:05:53,280
really clean up our app js
11774
08:05:53,280 --> 08:05:56,638
but we're not out of the woods yet
11775
08:05:56,638 --> 08:05:59,760
if we take a look at our auth js yeah
11776
08:05:59,760 --> 08:06:01,760
this one looks somewhat clean but we
11777
08:06:01,760 --> 08:06:04,558
need to keep in mind that we only have
11778
08:06:04,558 --> 08:06:05,920
one request
11779
08:06:05,920 --> 08:06:08,638
we only have one post request to our
11780
08:06:08,638 --> 08:06:11,920
login however if we take a look at our
11781
08:06:11,920 --> 08:06:13,520
people.js
11782
08:06:13,520 --> 08:06:16,638
it is still somewhat of a mess because
11783
08:06:16,638 --> 08:06:19,680
we have a bunch of functionality since
11784
08:06:19,680 --> 08:06:22,240
of course we have more routes
11785
08:06:22,240 --> 08:06:25,680
and of course a better setup is if we
11786
08:06:25,680 --> 08:06:29,760
would be able to split this up into a
11787
08:06:29,760 --> 08:06:32,398
separate file and i'm talking about the
11788
08:06:32,398 --> 08:06:35,520
functions so imagine this scenario where
11789
08:06:35,520 --> 08:06:38,240
we only would have a router
11790
08:06:38,240 --> 08:06:41,360
then i'm gonna set up my method whether
11791
08:06:41,360 --> 08:06:44,240
that's get post or whatever then i'm
11792
08:06:44,240 --> 08:06:46,398
still gonna have the route but the
11793
08:06:46,398 --> 08:06:49,520
functionality this one the controller
11794
08:06:49,520 --> 08:06:51,200
is going to be in a separate file so
11795
08:06:51,200 --> 08:06:53,120
it's going to be a separate function and
11796
08:06:53,120 --> 08:06:56,638
you'll see how much cleaner our file is
11797
08:06:56,638 --> 08:06:59,600
going to look like just in a second so
11798
08:06:59,600 --> 08:07:02,558
again we need to head back to our
11799
08:07:02,558 --> 08:07:04,638
express tutorial
11800
08:07:04,638 --> 08:07:06,398
now i'll zoom in
11801
08:07:06,398 --> 08:07:08,638
i'll create a new folder
11802
08:07:08,638 --> 08:07:11,760
and again common convention is calling
11803
08:07:11,760 --> 08:07:13,760
this controllers
11804
08:07:13,760 --> 08:07:16,718
for the reason that there is a pattern
11805
08:07:16,718 --> 08:07:20,478
called mvc so model view controller and
11806
08:07:20,478 --> 08:07:22,478
then again we come up with the name in
11807
08:07:22,478 --> 08:07:24,878
my case i'm going to go with jobs or i'm
11808
08:07:24,878 --> 08:07:26,558
sorry not jobs i'm going to go with
11809
08:07:26,558 --> 08:07:29,840
people and i'm not gonna set up one for
11810
08:07:29,840 --> 08:07:32,160
the other one for the auth since there's
11811
08:07:32,160 --> 08:07:35,200
only one but for people i will set up a
11812
08:07:35,200 --> 08:07:37,680
controllers file by the name of
11813
08:07:37,680 --> 08:07:40,240
people.js and here
11814
08:07:40,240 --> 08:07:44,320
i just to set up first my functions
11815
08:07:44,320 --> 08:07:45,920
and the way it's going to work we're
11816
08:07:45,920 --> 08:07:47,920
going to go with const and then again
11817
08:07:47,920 --> 08:07:49,520
you just need to come up with a name so
11818
08:07:49,520 --> 08:07:51,600
for example for get request where i'm
11819
08:07:51,600 --> 08:07:53,520
getting people i'm just going to call
11820
08:07:53,520 --> 08:07:55,840
this get people keep in mind this is
11821
08:07:55,840 --> 08:07:58,718
just a function so the name is really up
11822
08:07:58,718 --> 08:08:00,398
to you and since i'm going to have the
11823
08:08:00,398 --> 08:08:01,920
same functionality of course i need to
11824
08:08:01,920 --> 08:08:04,240
keep in mind that i do need to have
11825
08:08:04,240 --> 08:08:07,600
access to rack and res now it's really
11826
08:08:07,600 --> 08:08:10,160
cool that once i pass this function as a
11827
08:08:10,160 --> 08:08:12,398
reference it's still going to work so i
11828
08:08:12,398 --> 08:08:15,040
simply go here with this rack res and
11829
08:08:15,040 --> 08:08:17,040
set up the functionality now probably
11830
08:08:17,040 --> 08:08:19,920
faster is just going to be doing like
11831
08:08:19,920 --> 08:08:22,160
this where i cut out
11832
08:08:22,160 --> 08:08:24,718
and then back into controllers i copy
11833
08:08:24,718 --> 08:08:26,798
and paste now i know that some people
11834
08:08:26,798 --> 08:08:28,878
might find this annoying but yes i'm
11835
08:08:28,878 --> 08:08:31,040
purposely refactoring the code so you
11836
08:08:31,040 --> 08:08:34,320
can see the benefits right away and i'm
11837
08:08:34,320 --> 08:08:36,398
going to do that for all the functions
11838
08:08:36,398 --> 08:08:37,520
and
11839
08:08:37,520 --> 08:08:39,040
for a little bit it's gonna probably
11840
08:08:39,040 --> 08:08:40,878
look messy
11841
08:08:40,878 --> 08:08:42,958
where i'm gonna do the same thing for
11842
08:08:42,958 --> 08:08:45,360
the post so i guess first i'm just gonna
11843
08:08:45,360 --> 08:08:47,520
cut it out and then back in the
11844
08:08:47,520 --> 08:08:49,280
controllers nothing routes in the
11845
08:08:49,280 --> 08:08:51,760
controllers let's come up with that
11846
08:08:51,760 --> 08:08:54,160
function as well so const and in this
11847
08:08:54,160 --> 08:08:55,840
case i'm going to call this create
11848
08:08:55,840 --> 08:08:56,798
person
11849
08:08:56,798 --> 08:08:59,120
i'll copy and paste the code
11850
08:08:59,120 --> 08:09:00,558
okay awesome
11851
08:09:00,558 --> 08:09:03,200
back in the routes let's get the postman
11852
08:09:03,200 --> 08:09:05,840
one so in a controller let's also call
11853
08:09:05,840 --> 08:09:08,478
this const and create
11854
08:09:08,478 --> 08:09:09,840
person
11855
08:09:09,840 --> 08:09:13,760
and postman like so copy and paste
11856
08:09:13,760 --> 08:09:16,958
that's my function then back in routes i
11857
08:09:16,958 --> 08:09:18,478
have one for put
11858
08:09:18,478 --> 08:09:22,240
so i think it would make sense if i call
11859
08:09:22,240 --> 08:09:25,760
this for example update person again cut
11860
08:09:25,760 --> 08:09:27,200
out everything
11861
08:09:27,200 --> 08:09:30,558
starting with a function and then all
11862
08:09:30,558 --> 08:09:33,600
the way to the closing parenthesis
11863
08:09:33,600 --> 08:09:34,958
cut it out
11864
08:09:34,958 --> 08:09:37,280
back in controllers we're going to call
11865
08:09:37,280 --> 08:09:38,478
it const
11866
08:09:38,478 --> 08:09:40,798
update person
11867
08:09:40,798 --> 08:09:44,798
like so copy and paste and lastly let's
11868
08:09:44,798 --> 08:09:48,718
deal with a delete one as well
11869
08:09:48,718 --> 08:09:50,638
so let's keep scrolling
11870
08:09:50,638 --> 08:09:52,878
again we cut everything out
11871
08:09:52,878 --> 08:09:54,000
like so
11872
08:09:54,000 --> 08:09:56,558
and then back in the controller let's
11873
08:09:56,558 --> 08:09:58,798
create one for deleting the person so
11874
08:09:58,798 --> 08:10:00,080
const
11875
08:10:00,080 --> 08:10:03,360
cons delete person
11876
08:10:03,360 --> 08:10:06,558
and copy and paste and if you remember
11877
08:10:06,558 --> 08:10:09,280
which seems like an eternity go
11878
08:10:09,280 --> 08:10:12,558
when we covered the exports i said that
11879
08:10:12,558 --> 08:10:14,958
of course we can also use right away
11880
08:10:14,958 --> 08:10:19,200
module exports and then whatever so for
11881
08:10:19,200 --> 08:10:20,718
example in this case that could be get
11882
08:10:20,718 --> 08:10:23,680
people or create person or
11883
08:10:23,680 --> 08:10:26,320
create person postman now in my case i
11884
08:10:26,320 --> 08:10:28,320
always prefer to do my exports all the
11885
08:10:28,320 --> 08:10:30,478
way on the bottom where i say
11886
08:10:30,478 --> 08:10:33,040
module exports now this is going to be
11887
08:10:33,040 --> 08:10:35,840
the object and now i just want to access
11888
08:10:35,840 --> 08:10:37,760
all the functions keep in mind
11889
08:10:37,760 --> 08:10:39,200
it doesn't change the functionality
11890
08:10:39,200 --> 08:10:40,878
functionality is the same where i'm
11891
08:10:40,878 --> 08:10:43,200
basically exporting an object
11892
08:10:43,200 --> 08:10:45,040
with bunch of functions so here i just
11893
08:10:45,040 --> 08:10:47,120
need to reference their names so it's
11894
08:10:47,120 --> 08:10:50,398
get people it is create person
11895
08:10:50,398 --> 08:10:52,638
create person postman
11896
08:10:52,638 --> 08:10:56,558
then update person and lastly it is also
11897
08:10:56,558 --> 08:10:58,718
for deleting the person now one thing
11898
08:10:58,718 --> 08:11:01,200
that is missing of course is the people
11899
08:11:01,200 --> 08:11:02,398
like i said this is just going to be
11900
08:11:02,398 --> 08:11:03,520
temporary
11901
08:11:03,520 --> 08:11:05,680
so i'm going to go to the controller
11902
08:11:05,680 --> 08:11:06,718
here
11903
08:11:06,718 --> 08:11:09,120
and i'll copy and paste now in this case
11904
08:11:09,120 --> 08:11:11,200
the path is correct because
11905
08:11:11,200 --> 08:11:12,878
the route was already in a separate
11906
08:11:12,878 --> 08:11:14,558
folder so
11907
08:11:14,558 --> 08:11:15,600
that
11908
08:11:15,600 --> 08:11:18,320
should still work and of course now i
11909
08:11:18,320 --> 08:11:20,558
can save my controllers and in the
11910
08:11:20,558 --> 08:11:23,120
routes what i need to do is of course
11911
08:11:23,120 --> 08:11:24,478
import all of them
11912
08:11:24,478 --> 08:11:27,600
so in this case we go with const and
11913
08:11:27,600 --> 08:11:30,000
again i'll write away the structure all
11914
08:11:30,000 --> 08:11:32,798
of them so let me just copy and paste i
11915
08:11:32,798 --> 08:11:34,558
think it's going to be faster
11916
08:11:34,558 --> 08:11:36,798
like so since the names of course are
11917
08:11:36,798 --> 08:11:38,558
going to be exactly the same i
11918
08:11:38,558 --> 08:11:41,040
destructure them and i'd structure them
11919
08:11:41,040 --> 08:11:41,920
from
11920
08:11:41,920 --> 08:11:44,240
require and then of course we're looking
11921
08:11:44,240 --> 08:11:46,558
for controllers and then more
11922
08:11:46,558 --> 08:11:49,200
specifically i'm looking for the people
11923
08:11:49,200 --> 08:11:51,600
like so and once we have required them
11924
08:11:51,600 --> 08:11:54,320
of course now we pass in the function
11925
08:11:54,320 --> 08:11:56,958
reference so for example here i can say
11926
08:11:56,958 --> 08:12:00,240
get people now i do need to add a comma
11927
08:12:00,240 --> 08:12:02,080
and if everything is correct our
11928
08:12:02,080 --> 08:12:05,120
functionality is still going to work
11929
08:12:05,120 --> 08:12:07,360
so in the controllers we're still
11930
08:12:07,360 --> 08:12:10,000
accessing rack and arrays
11931
08:12:10,000 --> 08:12:11,600
and we're still sending back the
11932
08:12:11,600 --> 08:12:14,080
response but of course the benefit of
11933
08:12:14,080 --> 08:12:16,160
all of this work
11934
08:12:16,160 --> 08:12:18,958
is the fact that our routes file is
11935
08:12:18,958 --> 08:12:21,760
gonna get much cleaner so in here
11936
08:12:21,760 --> 08:12:24,878
remember the name was create person
11937
08:12:24,878 --> 08:12:26,080
like so
11938
08:12:26,080 --> 08:12:28,878
and then we have one for create person
11939
08:12:28,878 --> 08:12:30,240
postman
11940
08:12:30,240 --> 08:12:32,958
and let's set it up like so then we have
11941
08:12:32,958 --> 08:12:34,840
also one for update
11942
08:12:34,840 --> 08:12:37,760
person and lastly we have one for the
11943
08:12:37,760 --> 08:12:40,398
leading person right so we go here with
11944
08:12:40,398 --> 08:12:43,040
delete person i save again i'm not gonna
11945
08:12:43,040 --> 08:12:45,920
test all of them out but i'm gonna go
11946
08:12:45,920 --> 08:12:48,320
with for example delete
11947
08:12:48,320 --> 08:12:50,798
and i'm gonna look for four this still
11948
08:12:50,798 --> 08:12:51,680
works
11949
08:12:51,680 --> 08:12:54,000
i remove the person number four
11950
08:12:54,000 --> 08:12:57,120
and also if i check out the get one
11951
08:12:57,120 --> 08:13:00,320
for the people this also should get me
11952
08:13:00,320 --> 08:13:02,240
back the array so i know that my
11953
08:13:02,240 --> 08:13:04,878
functionality still works but i nicely
11954
08:13:04,878 --> 08:13:08,478
cleaned out my file now lastly before we
11955
08:13:08,478 --> 08:13:11,440
talk about the databases i just want to
11956
08:13:11,440 --> 08:13:13,840
mention that when it comes to setting up
11957
08:13:13,840 --> 08:13:15,200
the routes
11958
08:13:15,200 --> 08:13:18,558
we actually have two flavors so i'm
11959
08:13:18,558 --> 08:13:20,718
gonna comment these ones out and i'll
11960
08:13:20,718 --> 08:13:23,040
show you another way how we can set up
11961
08:13:23,040 --> 08:13:25,280
the routes and effectively the benefit
11962
08:13:25,280 --> 08:13:28,558
is that we can set it up in fewer lines
11963
08:13:28,558 --> 08:13:31,360
of code keep in mind the functionality
11964
08:13:31,360 --> 08:13:33,600
is going to be exactly the same so again
11965
08:13:33,600 --> 08:13:36,000
this is going to be the case where it
11966
08:13:36,000 --> 08:13:37,920
comes down to your preference if you
11967
08:13:37,920 --> 08:13:40,080
prefer setting up your routes this way
11968
08:13:40,080 --> 08:13:42,000
you can definitely do so there's no rule
11969
08:13:42,000 --> 08:13:45,200
against it if you prefer the method that
11970
08:13:45,200 --> 08:13:47,520
i'm about to show you then of course you
11971
08:13:47,520 --> 08:13:51,600
can use that one instead so we go with a
11972
08:13:51,600 --> 08:13:54,638
router and then we go with route and
11973
08:13:54,638 --> 08:13:57,840
then in the route i set up the path
11974
08:13:57,840 --> 08:14:01,440
and again since there's get and post
11975
08:14:01,440 --> 08:14:04,000
that match exactly what i have in base
11976
08:14:04,000 --> 08:14:06,160
same situation i just set up the forward
11977
08:14:06,160 --> 08:14:08,718
slash and instead of setting up them
11978
08:14:08,718 --> 08:14:12,160
separately for example get and post i
11979
08:14:12,160 --> 08:14:14,958
can just chain them so chain one after
11980
08:14:14,958 --> 08:14:17,600
another so i go with get that is of
11981
08:14:17,600 --> 08:14:19,440
course my method
11982
08:14:19,440 --> 08:14:21,760
that is not changing and then i need to
11983
08:14:21,760 --> 08:14:25,760
pass in my controller function okay as
11984
08:14:25,760 --> 08:14:27,200
you can see
11985
08:14:27,200 --> 08:14:30,080
almost the same in this case however i
11986
08:14:30,080 --> 08:14:32,398
can change it now the method names did
11987
08:14:32,398 --> 08:14:35,200
not change the paths did not change
11988
08:14:35,200 --> 08:14:36,320
they're still
11989
08:14:36,320 --> 08:14:38,558
actually referencing the base path in
11990
08:14:38,558 --> 08:14:41,200
here what we have in app.js but since we
11991
08:14:41,200 --> 08:14:43,680
can chain it we can simply write it in
11992
08:14:43,680 --> 08:14:45,920
one line of code so of course the
11993
08:14:45,920 --> 08:14:49,440
callback function is gonna be create and
11994
08:14:49,440 --> 08:14:52,798
i believe it was person so create and
11995
08:14:52,798 --> 08:14:54,240
person like so
11996
08:14:54,240 --> 08:14:56,478
then we have of course one for the
11997
08:14:56,478 --> 08:14:58,558
postman so in this case of course it's
11998
08:14:58,558 --> 08:15:00,398
not going to change it's pretty much
11999
08:15:00,398 --> 08:15:03,120
going to be the same logic where we go
12000
08:15:03,120 --> 08:15:05,680
the router route
12001
08:15:05,680 --> 08:15:07,680
like so and then we go with forward
12002
08:15:07,680 --> 08:15:10,240
slash postman and then of course i want
12003
08:15:10,240 --> 08:15:11,200
to chain
12004
08:15:11,200 --> 08:15:13,840
dot and i believe it was post right and
12005
08:15:13,840 --> 08:15:17,280
then we go with create person postman
12006
08:15:17,280 --> 08:15:20,000
and then we can again set up one line of
12007
08:15:20,000 --> 08:15:23,840
code for the put and delete so in this
12008
08:15:23,840 --> 08:15:25,680
case i think i'm going to just copy
12009
08:15:25,680 --> 08:15:28,478
where i have router dot route then we
12010
08:15:28,478 --> 08:15:31,360
set up our route and in this case it is
12011
08:15:31,360 --> 08:15:34,240
going to be forward slash and then id
12012
08:15:34,240 --> 08:15:37,360
and then we have two we have one for put
12013
08:15:37,360 --> 08:15:40,718
and we have one for delete so we go with
12014
08:15:40,718 --> 08:15:42,798
delete and then as far as the callback
12015
08:15:42,798 --> 08:15:46,638
functions we go with update person
12016
08:15:46,638 --> 08:15:50,080
and also we have delete person and
12017
08:15:50,080 --> 08:15:52,080
finally let me just stress it one more
12018
08:15:52,080 --> 08:15:55,840
time where whichever setup you choose is
12019
08:15:55,840 --> 08:15:57,680
really up to your preference
12020
08:15:57,680 --> 08:16:00,318
functionality is exactly the same we go
12021
08:16:00,318 --> 08:16:03,360
with router route and then we can just
12022
08:16:03,360 --> 08:16:06,398
chain our methods and add the callback
12023
08:16:06,398 --> 08:16:07,680
functions
12024
08:16:07,680 --> 08:16:11,120
now in this case we set up each route
12025
08:16:11,120 --> 08:16:13,520
separately so for example if you have
12026
08:16:13,520 --> 08:16:16,558
multiple urls that match yeah you're in
12027
08:16:16,558 --> 08:16:18,798
good shape you can just set up router
12028
08:16:18,798 --> 08:16:22,878
route pass in the url and then chain all
12029
08:16:22,878 --> 08:16:24,958
the methods and lastly let me just
12030
08:16:24,958 --> 08:16:26,878
quickly test it one more time since i
12031
08:16:26,878 --> 08:16:29,440
made some changes i send it and there it
12032
08:16:29,440 --> 08:16:33,600
is i successfully get back my people so
12033
08:16:33,600 --> 08:16:35,440
with this in place
12034
08:16:35,440 --> 08:16:37,520
with our router in place
12035
08:16:37,520 --> 08:16:39,840
with the fact that we have covered all
12036
08:16:39,840 --> 08:16:40,958
the methods
12037
08:16:40,958 --> 08:16:44,240
the get post put and delete i think
12038
08:16:44,240 --> 08:16:46,240
we're ready for the next level where we
12039
08:16:46,240 --> 08:16:49,200
connect our server
835435
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.