All language subtitles for [English (auto-generated)] Node.js Full Course for Beginners _ Complete All-in-One Tutorial _ 7 Hours [DownSub.com]

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,910 --> 00:00:04,960 [Music] 2 00:00:04,960 --> 00:00:07,359 hello and welcome to seven hours of 3 00:00:07,359 --> 00:00:10,080 node.js tutorials and instruction this 4 00:00:10,080 --> 00:00:12,320 video is made up of 15 tutorials for 5 00:00:12,320 --> 00:00:14,320 learning node.js that build upon each 6 00:00:14,320 --> 00:00:16,400 other much like the chapters of a book 7 00:00:16,400 --> 00:00:18,160 you will build projects and learn the 8 00:00:18,160 --> 00:00:20,960 fundamentals of node.js the express js 9 00:00:20,960 --> 00:00:23,119 framework and mongodb 10 00:00:23,119 --> 00:00:25,199 before beginning i recommend that you 11 00:00:25,199 --> 00:00:27,359 already know the basics of javascript 12 00:00:27,359 --> 00:00:29,840 this tutorial is for node.js beginners 13 00:00:29,840 --> 00:00:32,719 but not absolute beginners in javascript 14 00:00:32,719 --> 00:00:34,559 throughout the lessons in this video i 15 00:00:34,559 --> 00:00:36,239 will mention links being available in 16 00:00:36,239 --> 00:00:38,719 the description below i've compiled all 17 00:00:38,719 --> 00:00:41,120 of these links into one github resource 18 00:00:41,120 --> 00:00:43,520 that you will find in the description hi 19 00:00:43,520 --> 00:00:45,360 i'm dave gray and i'm the creator of 20 00:00:45,360 --> 00:00:47,440 these node.js tutorials you can 21 00:00:47,440 --> 00:00:49,360 subscribe to my youtube channel for more 22 00:00:49,360 --> 00:00:51,520 tutorials like this one you can also 23 00:00:51,520 --> 00:00:53,199 follow me on twitter and if you're 24 00:00:53,199 --> 00:00:55,199 feeling generous you can even buy me a 25 00:00:55,199 --> 00:00:57,440 cup of coffee let's get started learning 26 00:00:57,440 --> 00:00:59,760 node.js with chapter one 27 00:00:59,760 --> 00:01:02,800 here we can see node.js is a javascript 28 00:01:02,800 --> 00:01:05,519 runtime it's not a new language it's not 29 00:01:05,519 --> 00:01:08,799 a different language it's a javascript 30 00:01:08,799 --> 00:01:11,760 runtime and it's built on chrome's v8 31 00:01:11,760 --> 00:01:14,400 javascript engine it's not a framework 32 00:01:14,400 --> 00:01:17,360 or a library either but it does run on 33 00:01:17,360 --> 00:01:19,439 the server and that's what makes it 34 00:01:19,439 --> 00:01:21,439 different we're used to javascript 35 00:01:21,439 --> 00:01:24,000 running in the browser where the html 36 00:01:24,000 --> 00:01:26,000 and css is and that's all on the front 37 00:01:26,000 --> 00:01:29,600 end but no js is on the back end 38 00:01:29,600 --> 00:01:31,079 so you'll want to go to 39 00:01:31,079 --> 00:01:33,200 nodejs.org if you haven't already 40 00:01:33,200 --> 00:01:36,560 download note downloaded node.js 41 00:01:36,560 --> 00:01:38,079 and you'll want to download the 42 00:01:38,079 --> 00:01:40,079 recommended for most users version is 43 00:01:40,079 --> 00:01:41,680 fine or if you want to get the one with 44 00:01:41,680 --> 00:01:44,000 the latest features either way 45 00:01:44,000 --> 00:01:45,759 download node.js 46 00:01:45,759 --> 00:01:48,240 and you probably already have it or 47 00:01:48,240 --> 00:01:50,320 familiar with it but if you're not i 48 00:01:50,320 --> 00:01:52,399 just want to mention i will be using 49 00:01:52,399 --> 00:01:55,439 visual studio code and you can download 50 00:01:55,439 --> 00:01:57,840 that code editor you can also use 51 00:01:57,840 --> 00:01:59,840 others that you're familiar with however 52 00:01:59,840 --> 00:02:02,159 my examples will be with visual studio 53 00:02:02,159 --> 00:02:04,880 code and you can get that for free at 54 00:02:04,880 --> 00:02:07,880 code.visualstudio.com 55 00:02:08,239 --> 00:02:10,959 okay with nodejs and visual studio code 56 00:02:10,959 --> 00:02:13,440 both downloaded and installed let's get 57 00:02:13,440 --> 00:02:16,319 started okay i've got visual studio code 58 00:02:16,319 --> 00:02:19,040 open and you can see i have created a 59 00:02:19,040 --> 00:02:21,599 new folder just for this tutorial called 60 00:02:21,599 --> 00:02:23,920 zero one tut you can create a folder and 61 00:02:23,920 --> 00:02:25,920 name it whatever you want to but open 62 00:02:25,920 --> 00:02:28,319 that folder up in visual studio code and 63 00:02:28,319 --> 00:02:30,239 then go ahead and create a new file 64 00:02:30,239 --> 00:02:32,560 named server.js 65 00:02:32,560 --> 00:02:35,519 now with that said i want to mention you 66 00:02:35,519 --> 00:02:38,720 should already know some html css and 67 00:02:38,720 --> 00:02:41,519 javascript before this tutorial series 68 00:02:41,519 --> 00:02:43,360 possibly maybe some experience with 69 00:02:43,360 --> 00:02:45,120 other libraries and frameworks like 70 00:02:45,120 --> 00:02:48,480 react or vue but that's not necessary 71 00:02:48,480 --> 00:02:51,599 but you will need the basics of html css 72 00:02:51,599 --> 00:02:54,680 and javascript so while it is a 73 00:02:54,680 --> 00:02:57,680 nodejs beginners tutorial series it's 74 00:02:57,680 --> 00:03:00,560 not an absolute beginners for web devs 75 00:03:00,560 --> 00:03:03,519 if you haven't already learned html css 76 00:03:03,519 --> 00:03:06,319 and javascript you should probably start 77 00:03:06,319 --> 00:03:09,200 there before attempting this series 78 00:03:09,200 --> 00:03:11,760 now with that said we need to focus on 79 00:03:11,760 --> 00:03:15,599 how no js differs from vanilla js just 80 00:03:15,599 --> 00:03:18,319 plain old javascript first of all and 81 00:03:18,319 --> 00:03:19,840 what will be the running theme 82 00:03:19,840 --> 00:03:22,879 throughout the differences is that node 83 00:03:22,879 --> 00:03:24,720 runs on a server 84 00:03:24,720 --> 00:03:27,519 not in the browser and that is a big 85 00:03:27,519 --> 00:03:29,519 difference so you're working on the back 86 00:03:29,519 --> 00:03:31,840 end not the front end we're not relating 87 00:03:31,840 --> 00:03:34,560 to the browser anymore 88 00:03:34,560 --> 00:03:37,280 and because of that the console is now 89 00:03:37,280 --> 00:03:40,080 in the terminal window it's not in 90 00:03:40,080 --> 00:03:42,319 the console window of devtools in the 91 00:03:42,319 --> 00:03:43,840 browser because we're not using the 92 00:03:43,840 --> 00:03:46,159 browser at all let's open a terminal 93 00:03:46,159 --> 00:03:48,640 window by going to the terminal menu and 94 00:03:48,640 --> 00:03:51,360 choosing new terminal in vs code and 95 00:03:51,360 --> 00:03:54,640 with the new terminal window open 96 00:03:54,640 --> 00:03:56,959 we will be able to run node right inside 97 00:03:56,959 --> 00:04:00,879 the terminal so if i type node we're now 98 00:04:00,879 --> 00:04:03,200 running the console 99 00:04:03,200 --> 00:04:05,439 directly in the terminal so if we can 100 00:04:05,439 --> 00:04:07,840 add in any actual javascript that would 101 00:04:07,840 --> 00:04:08,720 be 102 00:04:08,720 --> 00:04:11,360 an expression such as two plus two and 103 00:04:11,360 --> 00:04:13,200 we get four in return 104 00:04:13,200 --> 00:04:16,399 so this is just like the dev tools 105 00:04:16,399 --> 00:04:17,600 console 106 00:04:17,600 --> 00:04:19,918 except we're now using the terminal for 107 00:04:19,918 --> 00:04:22,560 node and with that said we can also exit 108 00:04:22,560 --> 00:04:24,960 by pressing ctrl c and then it will tell 109 00:04:24,960 --> 00:04:28,160 us to press ctrl c one more time and now 110 00:04:28,160 --> 00:04:30,560 we've exited node but what if we put a 111 00:04:30,560 --> 00:04:32,800 console statement in our file so if i 112 00:04:32,800 --> 00:04:34,720 just put in 113 00:04:34,720 --> 00:04:38,240 condyle console there we go console.log 114 00:04:38,240 --> 00:04:40,240 and i put in your standard 115 00:04:40,240 --> 00:04:43,040 hello world and save 116 00:04:43,040 --> 00:04:44,880 we can also run 117 00:04:44,880 --> 00:04:47,440 any javascript file from the terminal 118 00:04:47,440 --> 00:04:50,160 with node we'll just type node and then 119 00:04:50,160 --> 00:04:51,919 the name of the file we don't need to 120 00:04:51,919 --> 00:04:54,479 put the js extension after i'll press 121 00:04:54,479 --> 00:04:55,360 enter 122 00:04:55,360 --> 00:04:57,120 and now we get hello world in the 123 00:04:57,120 --> 00:04:59,840 terminal another difference between node 124 00:04:59,840 --> 00:05:02,400 and vanilla javascript is that there is 125 00:05:02,400 --> 00:05:04,639 a global object instead of a window 126 00:05:04,639 --> 00:05:07,039 object the window object once again 127 00:05:07,039 --> 00:05:08,800 referred to the browser where we could 128 00:05:08,800 --> 00:05:11,120 do window dot inner height 129 00:05:11,120 --> 00:05:13,360 and different properties like that the 130 00:05:13,360 --> 00:05:15,520 global object is much smaller but it 131 00:05:15,520 --> 00:05:18,080 does have some of the same properties we 132 00:05:18,080 --> 00:05:20,320 were used to seeing in the window object 133 00:05:20,320 --> 00:05:22,000 so what i'm going to do 134 00:05:22,000 --> 00:05:24,479 is just say console.log 135 00:05:24,479 --> 00:05:26,000 and i'll put 136 00:05:26,000 --> 00:05:28,160 global in here because global is the 137 00:05:28,160 --> 00:05:30,320 keyword for the global object 138 00:05:30,320 --> 00:05:31,759 and i'll save 139 00:05:31,759 --> 00:05:34,160 and now we can run our file once again 140 00:05:34,160 --> 00:05:37,360 by typing node server in the terminal 141 00:05:37,360 --> 00:05:39,360 and it logged the full global object 142 00:05:39,360 --> 00:05:41,520 i'll expand the terminal window up and 143 00:05:41,520 --> 00:05:43,759 scroll just so we can see this object 144 00:05:43,759 --> 00:05:45,919 here so we have our hello world again 145 00:05:45,919 --> 00:05:47,680 then here is the global object and you 146 00:05:47,680 --> 00:05:50,000 can see there's a clear interval clear 147 00:05:50,000 --> 00:05:52,800 timeout set interval set timeout those 148 00:05:52,800 --> 00:05:54,560 are things that we already had in 149 00:05:54,560 --> 00:05:57,039 vanilla javascript that we're used to 150 00:05:57,039 --> 00:05:58,960 and it's a much smaller object than the 151 00:05:58,960 --> 00:06:00,880 window object if you were to go into a 152 00:06:00,880 --> 00:06:03,440 browser and to log that but that is 153 00:06:03,440 --> 00:06:06,800 another difference in node.js a fourth 154 00:06:06,800 --> 00:06:09,039 difference in node.js 155 00:06:09,039 --> 00:06:10,960 is that there are common core modules 156 00:06:10,960 --> 00:06:12,880 that we will explore and vanilla 157 00:06:12,880 --> 00:06:14,720 javascript doesn't really have those 158 00:06:14,720 --> 00:06:16,800 these are modules that relate to the 159 00:06:16,800 --> 00:06:19,600 operating system the file system 160 00:06:19,600 --> 00:06:22,319 and other things that we can do on the 161 00:06:22,319 --> 00:06:24,800 server and to import those common core 162 00:06:24,800 --> 00:06:27,600 modules as well as any other modules 163 00:06:27,600 --> 00:06:31,039 we use common js modules common js 164 00:06:31,039 --> 00:06:34,960 imports instead of es6 imports and now 165 00:06:34,960 --> 00:06:37,039 there is some work being done 166 00:06:37,039 --> 00:06:39,680 to use the same syntax that we typically 167 00:06:39,680 --> 00:06:41,919 use in vanilla javascript that is such 168 00:06:41,919 --> 00:06:45,840 as import whatever name from whatever 169 00:06:45,840 --> 00:06:46,800 file 170 00:06:46,800 --> 00:06:49,759 and that is how we do it in es6 however 171 00:06:49,759 --> 00:06:53,360 common js uses a require statement so 172 00:06:53,360 --> 00:06:55,199 let's go ahead and 173 00:06:55,199 --> 00:06:57,360 import something and we can learn how to 174 00:06:57,360 --> 00:06:58,720 do that with 175 00:06:58,720 --> 00:07:01,120 let's call this os and set 176 00:07:01,120 --> 00:07:02,639 this equal to 177 00:07:02,639 --> 00:07:04,639 require dot 178 00:07:04,639 --> 00:07:05,840 os 179 00:07:05,840 --> 00:07:07,840 and that's pretty much all there is to 180 00:07:07,840 --> 00:07:09,680 it it's just a different syntax than 181 00:07:09,680 --> 00:07:11,599 we're used to seeing with the import and 182 00:07:11,599 --> 00:07:13,759 now that we've imported the os let's go 183 00:07:13,759 --> 00:07:16,000 ahead and get some information about the 184 00:07:16,000 --> 00:07:19,120 os from it so we can say os dot 185 00:07:19,120 --> 00:07:21,520 type and we'll go ahead and log that and 186 00:07:21,520 --> 00:07:23,360 i'm going to copy this down a couple of 187 00:07:23,360 --> 00:07:26,400 times and change type to a couple of 188 00:07:26,400 --> 00:07:28,880 other values we can get about the os 189 00:07:28,880 --> 00:07:30,400 such as 190 00:07:30,400 --> 00:07:31,759 version 191 00:07:31,759 --> 00:07:34,319 and home directory and that is 192 00:07:34,319 --> 00:07:37,039 abbreviated with home dir you can see 193 00:07:37,039 --> 00:07:40,479 that i'll comment out the global object 194 00:07:40,479 --> 00:07:42,240 log because it takes up a little more 195 00:07:42,240 --> 00:07:44,800 space let's go ahead and save this and 196 00:07:44,800 --> 00:07:46,479 once again 197 00:07:46,479 --> 00:07:49,120 call node server in the terminal 198 00:07:49,120 --> 00:07:51,039 and here we get our hello world and then 199 00:07:51,039 --> 00:07:54,080 you can see os type delivers 200 00:07:54,080 --> 00:07:56,240 windows nt 201 00:07:56,240 --> 00:07:59,599 os version is windows 10 pro 202 00:07:59,599 --> 00:08:02,160 and then os home directory shows my home 203 00:08:02,160 --> 00:08:04,080 directory in users 204 00:08:04,080 --> 00:08:06,240 dave gray there are a couple of other 205 00:08:06,240 --> 00:08:08,879 values that we always have access to in 206 00:08:08,879 --> 00:08:10,000 node 207 00:08:10,000 --> 00:08:13,039 and let me type the first one out it's 208 00:08:13,039 --> 00:08:15,280 der name for directory name and it 209 00:08:15,280 --> 00:08:17,039 starts with two underscores and that 210 00:08:17,039 --> 00:08:19,360 will always give us the directory name 211 00:08:19,360 --> 00:08:20,479 in node 212 00:08:20,479 --> 00:08:23,120 and then the other one 213 00:08:23,120 --> 00:08:25,199 is two underscores 214 00:08:25,199 --> 00:08:27,360 and file name for the file name we're 215 00:08:27,360 --> 00:08:28,319 using 216 00:08:28,319 --> 00:08:30,720 here i can see i'm being inconsistent on 217 00:08:30,720 --> 00:08:32,880 the semicolon so if anything i'll just 218 00:08:32,880 --> 00:08:35,360 remove the one i typed let's go ahead 219 00:08:35,360 --> 00:08:38,479 and save that and once again call node 220 00:08:38,479 --> 00:08:40,958 server in the terminal and here's what 221 00:08:40,958 --> 00:08:43,039 we get after the home directory that was 222 00:08:43,039 --> 00:08:45,600 delivered from the os common core module 223 00:08:45,600 --> 00:08:48,399 in our console log statement right here 224 00:08:48,399 --> 00:08:51,120 you can see directory name 225 00:08:51,120 --> 00:08:52,800 the value that we have we don't even 226 00:08:52,800 --> 00:08:54,800 have to import a module to get that is 227 00:08:54,800 --> 00:08:58,640 this full length here to my 01 228 00:08:58,640 --> 00:09:01,760 directory and then you can see file name 229 00:09:01,760 --> 00:09:04,880 gives that same full path but also 230 00:09:04,880 --> 00:09:08,000 includes the file name at the end 231 00:09:08,000 --> 00:09:09,839 there's another common core module that 232 00:09:09,839 --> 00:09:12,080 we can import and i'll do that right now 233 00:09:12,080 --> 00:09:14,560 and we'll call it path because 234 00:09:14,560 --> 00:09:17,680 it is called path as well so 235 00:09:17,680 --> 00:09:21,600 there is path with the common js import 236 00:09:21,600 --> 00:09:23,440 and now we can use 237 00:09:23,440 --> 00:09:25,920 file name within path so let me give 238 00:09:25,920 --> 00:09:28,720 some examples of that to get more values 239 00:09:28,720 --> 00:09:30,720 here we'll log the path 240 00:09:30,720 --> 00:09:33,440 directory name and pass in 241 00:09:33,440 --> 00:09:35,839 the file name and we'll see that it is 242 00:09:35,839 --> 00:09:38,800 very similar to what we just got by 243 00:09:38,800 --> 00:09:42,640 using the directory name here however if 244 00:09:42,640 --> 00:09:44,399 we're using path that might come in 245 00:09:44,399 --> 00:09:45,680 handy as well 246 00:09:45,680 --> 00:09:49,360 there's also path dot base name 247 00:09:49,360 --> 00:09:51,279 and we'll also pass in the file name 248 00:09:51,279 --> 00:09:52,080 there 249 00:09:52,080 --> 00:09:54,959 and i'll copy this down one more time 250 00:09:54,959 --> 00:09:57,519 and there we'll just change base name 251 00:09:57,519 --> 00:09:58,959 and this will be 252 00:09:58,959 --> 00:10:01,839 extension name and we'll go ahead and 253 00:10:01,839 --> 00:10:03,040 save that 254 00:10:03,040 --> 00:10:05,200 now let's run the file one more time 255 00:10:05,200 --> 00:10:07,519 with node in the terminal 256 00:10:07,519 --> 00:10:10,959 and see what we got in return so after 257 00:10:10,959 --> 00:10:14,320 our directory name and file name here if 258 00:10:14,320 --> 00:10:16,560 i drag this up just a little we can see 259 00:10:16,560 --> 00:10:18,480 it all 260 00:10:18,480 --> 00:10:20,720 so we have the directory 261 00:10:20,720 --> 00:10:23,440 that's the value before we use the path 262 00:10:23,440 --> 00:10:26,480 import and here is the server 263 00:10:26,480 --> 00:10:28,720 file name before we use the path 264 00:10:28,720 --> 00:10:30,959 now the final three that we used with 265 00:10:30,959 --> 00:10:33,200 the path import 266 00:10:33,200 --> 00:10:35,760 we got the same as we did above for the 267 00:10:35,760 --> 00:10:39,200 directory so using the path directory 268 00:10:39,200 --> 00:10:41,920 name is the same as using this value in 269 00:10:41,920 --> 00:10:43,440 this instance 270 00:10:43,440 --> 00:10:46,880 and then the base name was just 271 00:10:46,880 --> 00:10:50,000 server js so that allowed us to just 272 00:10:50,000 --> 00:10:52,560 pull the file name out instead of having 273 00:10:52,560 --> 00:10:54,320 everything included 274 00:10:54,320 --> 00:10:57,279 as the file name value does up here on 275 00:10:57,279 --> 00:10:59,200 line 22 276 00:10:59,200 --> 00:11:02,640 and then the extension name gave us just 277 00:11:02,640 --> 00:11:06,079 the extension of the file the dot js 278 00:11:06,079 --> 00:11:08,720 what i've found to be even more useful 279 00:11:08,720 --> 00:11:11,360 than any one of those individual values 280 00:11:11,360 --> 00:11:13,760 is to oh i had my fingers on the wrong 281 00:11:13,760 --> 00:11:16,079 keys console.log 282 00:11:16,079 --> 00:11:17,120 path 283 00:11:17,120 --> 00:11:18,000 dot 284 00:11:18,000 --> 00:11:19,040 parse 285 00:11:19,040 --> 00:11:22,399 and then pass in the file name 286 00:11:22,399 --> 00:11:24,880 and we'll see what we get in return 287 00:11:24,880 --> 00:11:27,040 once we call this one more time in the 288 00:11:27,040 --> 00:11:30,320 terminal node server and here we get an 289 00:11:30,320 --> 00:11:33,040 object with all of these values so we 290 00:11:33,040 --> 00:11:33,839 have 291 00:11:33,839 --> 00:11:35,200 the root 292 00:11:35,200 --> 00:11:37,760 the directory 293 00:11:37,760 --> 00:11:38,959 the base 294 00:11:38,959 --> 00:11:40,560 the extension 295 00:11:40,560 --> 00:11:43,600 and the file name itself so we can 296 00:11:43,600 --> 00:11:45,839 really pull all of that together in any 297 00:11:45,839 --> 00:11:48,560 way we want to if we just go ahead and 298 00:11:48,560 --> 00:11:52,000 parse and then get each individual value 299 00:11:52,000 --> 00:11:54,399 as well now besides the common core 300 00:11:54,399 --> 00:11:56,720 modules we can also pull in packages 301 00:11:56,720 --> 00:11:58,720 that other developers have created and 302 00:11:58,720 --> 00:12:00,800 some of you may be familiar with node 303 00:12:00,800 --> 00:12:03,040 package manager already and i will cover 304 00:12:03,040 --> 00:12:05,680 that in some detail in the near future 305 00:12:05,680 --> 00:12:08,399 but we can also create our own modules 306 00:12:08,399 --> 00:12:10,399 and so we have a couple of imports here 307 00:12:10,399 --> 00:12:12,800 we'll make room to import one more but 308 00:12:12,800 --> 00:12:15,120 first in the file tree let's create 309 00:12:15,120 --> 00:12:17,720 another file and i'll just call this 310 00:12:17,720 --> 00:12:20,959 math.js inside of mathjs i'm going to 311 00:12:20,959 --> 00:12:22,480 create some functions i'll call the 312 00:12:22,480 --> 00:12:25,440 first one add and set it equal to 313 00:12:25,440 --> 00:12:26,240 a 314 00:12:26,240 --> 00:12:27,920 anonymous function it's an arrow 315 00:12:27,920 --> 00:12:29,120 function really 316 00:12:29,120 --> 00:12:32,000 and from there it'll just be a plus b 317 00:12:32,000 --> 00:12:34,000 which would return the sum of the two 318 00:12:34,000 --> 00:12:36,720 parameters a and b now i'm going to copy 319 00:12:36,720 --> 00:12:39,120 this down three times and we can just 320 00:12:39,120 --> 00:12:40,720 change these so 321 00:12:40,720 --> 00:12:42,639 here we'll have 322 00:12:42,639 --> 00:12:45,200 subtract they'll all have the two 323 00:12:45,200 --> 00:12:48,240 parameters a and b we can also have 324 00:12:48,240 --> 00:12:49,920 multiply 325 00:12:49,920 --> 00:12:52,560 and we can have divide 326 00:12:52,560 --> 00:12:55,040 so instead of a plus b of course it 327 00:12:55,040 --> 00:12:57,279 would be a minus b 328 00:12:57,279 --> 00:12:59,279 a times b 329 00:12:59,279 --> 00:13:02,560 and a divided by b 330 00:13:02,560 --> 00:13:04,399 right now though while we have these 331 00:13:04,399 --> 00:13:06,560 four functions we have not exported them 332 00:13:06,560 --> 00:13:08,800 in any way so we can have a statement at 333 00:13:08,800 --> 00:13:10,480 the bottom that says 334 00:13:10,480 --> 00:13:12,880 module dot exports 335 00:13:12,880 --> 00:13:15,440 and then we can set that equal to an 336 00:13:15,440 --> 00:13:18,480 object and we can pass in the names of 337 00:13:18,480 --> 00:13:21,120 all the functions 338 00:13:21,120 --> 00:13:22,720 there is another way to do this that 339 00:13:22,720 --> 00:13:25,839 i'll show you right afterwards 340 00:13:25,839 --> 00:13:27,839 but first let's attempt this way which 341 00:13:27,839 --> 00:13:29,680 is a common way to see this so we're 342 00:13:29,680 --> 00:13:31,600 exporting all of the functions that we 343 00:13:31,600 --> 00:13:34,240 defined to import these back in the 344 00:13:34,240 --> 00:13:36,959 server we need to come over here 345 00:13:36,959 --> 00:13:39,519 and let's just define math and we'll set 346 00:13:39,519 --> 00:13:41,199 this equal to 347 00:13:41,199 --> 00:13:42,480 require 348 00:13:42,480 --> 00:13:44,240 and now since it's not a common core 349 00:13:44,240 --> 00:13:46,720 module we can't just say math we have to 350 00:13:46,720 --> 00:13:49,120 say dot slash 351 00:13:49,120 --> 00:13:51,279 and now math is available to us there 352 00:13:51,279 --> 00:13:54,399 but we don't need the js extension so we 353 00:13:54,399 --> 00:13:56,959 can save that and then let's go ahead 354 00:13:56,959 --> 00:13:58,880 and comment out all of these console 355 00:13:58,880 --> 00:14:00,160 logs 356 00:14:00,160 --> 00:14:02,560 and we'll add the console log for the 357 00:14:02,560 --> 00:14:05,600 math underneath here so console.log and 358 00:14:05,600 --> 00:14:07,040 then we could say 359 00:14:07,040 --> 00:14:09,839 math.ad and we could pass in two 360 00:14:09,839 --> 00:14:12,399 variables two and three 361 00:14:12,399 --> 00:14:15,519 and let's go ahead and call that below 362 00:14:15,519 --> 00:14:18,480 with our node server 363 00:14:18,480 --> 00:14:20,399 and we got hello world of course it's 364 00:14:20,399 --> 00:14:22,320 still in there and then we got the total 365 00:14:22,320 --> 00:14:23,360 five 366 00:14:23,360 --> 00:14:27,120 so there is math being called however 367 00:14:27,120 --> 00:14:29,519 we don't need to call it as an object 368 00:14:29,519 --> 00:14:31,680 where we use dot notation we could 369 00:14:31,680 --> 00:14:34,160 destructure right here so instead of 370 00:14:34,160 --> 00:14:36,480 math we could destructure 371 00:14:36,480 --> 00:14:39,760 and just say add 372 00:14:40,480 --> 00:14:43,600 and remove the math dot and save and 373 00:14:43,600 --> 00:14:45,839 this should work in the same way so if 374 00:14:45,839 --> 00:14:48,560 we go ahead and call the server again 375 00:14:48,560 --> 00:14:50,399 and we got five once again with our 376 00:14:50,399 --> 00:14:52,399 hello world now we could go ahead and 377 00:14:52,399 --> 00:14:55,279 destructure and pull in the rest 378 00:14:55,279 --> 00:14:57,600 of the functions as well 379 00:14:57,600 --> 00:14:59,519 subtract 380 00:14:59,519 --> 00:15:01,440 multiply 381 00:15:01,440 --> 00:15:03,120 and divide 382 00:15:03,120 --> 00:15:04,880 we have all those functions available to 383 00:15:04,880 --> 00:15:09,600 us now so if we go ahead and copy down 384 00:15:10,560 --> 00:15:12,639 and we'll change each one to 385 00:15:12,639 --> 00:15:15,440 subtract 386 00:15:16,880 --> 00:15:19,600 multiply 387 00:15:20,000 --> 00:15:22,639 and divide 388 00:15:23,440 --> 00:15:24,839 we can 389 00:15:24,839 --> 00:15:27,360 save go ahead and call the file once 390 00:15:27,360 --> 00:15:29,600 again and here are the values looks like 391 00:15:29,600 --> 00:15:31,199 i'll need to move this just a little bit 392 00:15:31,199 --> 00:15:33,600 to see all the values but we've got five 393 00:15:33,600 --> 00:15:36,240 and then two minus three is minus one 394 00:15:36,240 --> 00:15:38,959 two times three is six and two divided 395 00:15:38,959 --> 00:15:41,079 by 3 is essentially 2 396 00:15:41,079 --> 00:15:42,800 3.66 397 00:15:42,800 --> 00:15:45,120 and on now before we're finished with 398 00:15:45,120 --> 00:15:48,079 the custom module let's go back to math 399 00:15:48,079 --> 00:15:49,519 and i'll show you one other way this 400 00:15:49,519 --> 00:15:52,240 could be done instead of combining all 401 00:15:52,240 --> 00:15:55,759 of these at the end 402 00:15:55,759 --> 00:15:57,600 there we go if i'll comment out this 403 00:15:57,600 --> 00:15:58,959 export here 404 00:15:58,959 --> 00:16:01,920 instead of the const here with each 405 00:16:01,920 --> 00:16:03,440 definition 406 00:16:03,440 --> 00:16:05,519 i'll just select all of those 407 00:16:05,519 --> 00:16:07,680 we could change this to 408 00:16:07,680 --> 00:16:09,600 exports dot 409 00:16:09,600 --> 00:16:11,920 and then it's essentially adding each of 410 00:16:11,920 --> 00:16:14,759 these functions to the export so 411 00:16:14,759 --> 00:16:17,519 exports.add is equal to this anonymous 412 00:16:17,519 --> 00:16:19,000 function and 413 00:16:19,000 --> 00:16:21,040 exports.subtract is equal to this 414 00:16:21,040 --> 00:16:23,279 anonymous function and so on but this 415 00:16:23,279 --> 00:16:26,880 will work in the same way so once again 416 00:16:26,880 --> 00:16:29,120 if we go ahead and call 417 00:16:29,120 --> 00:16:31,839 the server file from node we've still 418 00:16:31,839 --> 00:16:34,240 exported all of those functions 419 00:16:34,240 --> 00:16:37,199 and we actually deconstructed them over 420 00:16:37,199 --> 00:16:39,759 here to pull them in so you can also 421 00:16:39,759 --> 00:16:41,040 export 422 00:16:41,040 --> 00:16:43,279 in that way instead of using module 423 00:16:43,279 --> 00:16:46,000 exports at the bottom okay with that 424 00:16:46,000 --> 00:16:48,000 said we have covered quite a bit but 425 00:16:48,000 --> 00:16:49,920 there's one more difference to point out 426 00:16:49,920 --> 00:16:51,759 that we will see down the road and i'll 427 00:16:51,759 --> 00:16:54,160 just put it here under number five and 428 00:16:54,160 --> 00:16:57,440 that is nodejs is missing some of the 429 00:16:57,440 --> 00:16:59,279 apis that are available to vanilla 430 00:16:59,279 --> 00:17:02,639 javascript and one notable one is fetch 431 00:17:02,639 --> 00:17:04,880 but of course we can always pull in 432 00:17:04,880 --> 00:17:08,240 packages into node and there is a large 433 00:17:08,240 --> 00:17:10,240 amount of packages available through 434 00:17:10,240 --> 00:17:14,000 npm4 node so we won't miss it that much 435 00:17:14,000 --> 00:17:15,520 in the next tutorial we'll be working 436 00:17:15,520 --> 00:17:18,319 with files as far as reading writing 437 00:17:18,319 --> 00:17:20,880 creating updating deleting all of the 438 00:17:20,880 --> 00:17:22,880 things that we can do with files and 439 00:17:22,880 --> 00:17:24,799 we'll be using the file system common 440 00:17:24,799 --> 00:17:27,520 core module for that 441 00:17:27,520 --> 00:17:29,440 today we're going to look at the file 442 00:17:29,440 --> 00:17:32,480 system common core module for node.js it 443 00:17:32,480 --> 00:17:35,120 allows us to create read update and 444 00:17:35,120 --> 00:17:37,919 delete files and work with directories 445 00:17:37,919 --> 00:17:40,559 on the server and that's because node.js 446 00:17:40,559 --> 00:17:43,520 is a javascript runtime that actually 447 00:17:43,520 --> 00:17:45,600 runs on the server instead of in the 448 00:17:45,600 --> 00:17:48,960 browser now i'm going to click docs at 449 00:17:48,960 --> 00:17:50,640 nodejs.org 450 00:17:50,640 --> 00:17:53,440 because nodejs.org is the source of 451 00:17:53,440 --> 00:17:55,600 truth for information about node much 452 00:17:55,600 --> 00:17:57,840 like mdn would be the source of truth 453 00:17:57,840 --> 00:17:59,200 for information 454 00:17:59,200 --> 00:18:02,080 about vanilla javascript as well as html 455 00:18:02,080 --> 00:18:04,880 and css currently the stable version 456 00:18:04,880 --> 00:18:08,480 that i have installed is 14.17.5 457 00:18:08,480 --> 00:18:10,720 as you view this tutorial the versions 458 00:18:10,720 --> 00:18:12,799 that are stable may change so go ahead 459 00:18:12,799 --> 00:18:15,360 and click on whichever version you have 460 00:18:15,360 --> 00:18:17,360 and now you can see we have node.js 461 00:18:17,360 --> 00:18:19,600 documentation today we're looking at the 462 00:18:19,600 --> 00:18:21,760 file system module so i can click that 463 00:18:21,760 --> 00:18:22,880 right here 464 00:18:22,880 --> 00:18:25,200 and there are many things you can do 465 00:18:25,200 --> 00:18:27,360 with the file system with directories 466 00:18:27,360 --> 00:18:29,760 and different files so amongst all of 467 00:18:29,760 --> 00:18:31,360 those things what i usually do is press 468 00:18:31,360 --> 00:18:34,720 control f i'm on windows and then i type 469 00:18:34,720 --> 00:18:36,400 what i'm looking for and we're going to 470 00:18:36,400 --> 00:18:37,840 start out by 471 00:18:37,840 --> 00:18:40,559 reading files so when i type read file i 472 00:18:40,559 --> 00:18:44,000 can see the fs dot read file and it 473 00:18:44,000 --> 00:18:46,320 shows the structure here for what i want 474 00:18:46,320 --> 00:18:47,440 to call 475 00:18:47,440 --> 00:18:49,200 and then you can click it and get 476 00:18:49,200 --> 00:18:51,280 details about that i just wanted to show 477 00:18:51,280 --> 00:18:53,120 you how to find the different 478 00:18:53,120 --> 00:18:56,160 documentation on node.js and now let's 479 00:18:56,160 --> 00:18:58,480 get started in visual studio code as we 480 00:18:58,480 --> 00:19:01,360 work with the fs the file system common 481 00:19:01,360 --> 00:19:04,320 core module i've got visual studio code 482 00:19:04,320 --> 00:19:07,360 open with an empty index.js file i've 483 00:19:07,360 --> 00:19:09,840 also got a files directory here in the 484 00:19:09,840 --> 00:19:12,080 file tree and you can see a couple of 485 00:19:12,080 --> 00:19:14,480 starter text files a lorem text file 486 00:19:14,480 --> 00:19:17,360 that has a lot of text in it and a 487 00:19:17,360 --> 00:19:19,440 starter text file that just says hi my 488 00:19:19,440 --> 00:19:22,000 name is dave we're going to start by 489 00:19:22,000 --> 00:19:25,440 importing the fs common core module here 490 00:19:25,440 --> 00:19:28,480 and we use common js imports for that 491 00:19:28,480 --> 00:19:31,600 and so it's just require fs 492 00:19:31,600 --> 00:19:34,559 after that let's go ahead and read 493 00:19:34,559 --> 00:19:37,520 that starter file so we need to specify 494 00:19:37,520 --> 00:19:38,720 the file 495 00:19:38,720 --> 00:19:40,640 and it's in files and then it's at 496 00:19:40,640 --> 00:19:42,320 starter 497 00:19:42,320 --> 00:19:44,240 dot text 498 00:19:44,240 --> 00:19:45,600 from there 499 00:19:45,600 --> 00:19:48,080 we have a callback function 500 00:19:48,080 --> 00:19:51,360 that has error and data that we read 501 00:19:51,360 --> 00:19:54,480 and so we'll say if air we need to throw 502 00:19:54,480 --> 00:19:56,000 the error 503 00:19:56,000 --> 00:19:59,120 and otherwise let's log the data to the 504 00:19:59,120 --> 00:20:00,559 console 505 00:20:00,559 --> 00:20:03,120 now if you remember the console in 506 00:20:03,120 --> 00:20:04,799 node.js 507 00:20:04,799 --> 00:20:06,640 is in the terminal so i'm going to press 508 00:20:06,640 --> 00:20:08,720 control and backtick to open a terminal 509 00:20:08,720 --> 00:20:10,400 window you could also do that from the 510 00:20:10,400 --> 00:20:11,760 terminal menu 511 00:20:11,760 --> 00:20:15,440 now here to run our little node file in 512 00:20:15,440 --> 00:20:18,559 index we just type node and then index 513 00:20:18,559 --> 00:20:20,960 we don't need the js and let's go ahead 514 00:20:20,960 --> 00:20:22,799 and take a look at this data from 515 00:20:22,799 --> 00:20:24,400 starter 516 00:20:24,400 --> 00:20:26,960 and notice after we read the data 517 00:20:26,960 --> 00:20:29,679 it's presented as buffer data so that's 518 00:20:29,679 --> 00:20:31,679 what we get right here if we want to be 519 00:20:31,679 --> 00:20:34,720 able to read the data we can put a to 520 00:20:34,720 --> 00:20:38,240 string method afterwards i'll save 521 00:20:38,240 --> 00:20:40,480 and i'll go ahead and call this again 522 00:20:40,480 --> 00:20:43,200 and now we can see hi my name is dave in 523 00:20:43,200 --> 00:20:44,559 the console 524 00:20:44,559 --> 00:20:47,679 but instead of two string we can also 525 00:20:47,679 --> 00:20:50,799 put in a parameter here that says 526 00:20:50,799 --> 00:20:52,480 the encoding 527 00:20:52,480 --> 00:20:54,640 or defines the encoding so we'll say 528 00:20:54,640 --> 00:20:56,480 utf-8 529 00:20:56,480 --> 00:20:59,520 before the callback as a perimeter and 530 00:20:59,520 --> 00:21:02,000 now let's go ahead and run it again node 531 00:21:02,000 --> 00:21:04,240 index in the terminal and once again we 532 00:21:04,240 --> 00:21:07,440 get hi my name is dave now notice the 533 00:21:07,440 --> 00:21:09,840 throw error here according to the node 534 00:21:09,840 --> 00:21:12,559 documentation if we get an uncaught 535 00:21:12,559 --> 00:21:15,120 exception we need to go ahead and catch 536 00:21:15,120 --> 00:21:17,679 that and i'm just going to paste this in 537 00:21:17,679 --> 00:21:20,080 if we have an uncaught error we should 538 00:21:20,080 --> 00:21:21,039 exit 539 00:21:21,039 --> 00:21:21,760 so 540 00:21:21,760 --> 00:21:23,760 we listen for this uncaught exception 541 00:21:23,760 --> 00:21:26,080 using process now process is one of 542 00:21:26,080 --> 00:21:28,320 those values that node has available to 543 00:21:28,320 --> 00:21:30,080 us we don't need to import it it's 544 00:21:30,080 --> 00:21:32,000 already there so we're listing for an 545 00:21:32,000 --> 00:21:34,320 uncaught exception then we pass in the 546 00:21:34,320 --> 00:21:36,320 error to the callback here 547 00:21:36,320 --> 00:21:38,960 and we're just logging the console 548 00:21:38,960 --> 00:21:40,880 error there was an uncaught error and 549 00:21:40,880 --> 00:21:42,960 then we put the error there and then we 550 00:21:42,960 --> 00:21:46,159 exit the application and this is direct 551 00:21:46,159 --> 00:21:48,400 from the node documentation so let's go 552 00:21:48,400 --> 00:21:51,200 ahead and throw an error on purpose i'll 553 00:21:51,200 --> 00:21:53,120 just look for a file to read that 554 00:21:53,120 --> 00:21:55,520 doesn't exist named hello 555 00:21:55,520 --> 00:21:58,240 and once again we'll call node index and 556 00:21:58,240 --> 00:22:01,600 you can see we get an uncaught error 557 00:22:01,600 --> 00:22:04,159 and here it is there is no such file or 558 00:22:04,159 --> 00:22:06,559 directory now let's change this back to 559 00:22:06,559 --> 00:22:08,720 starter so we can keep reading the data 560 00:22:08,720 --> 00:22:10,080 that exists 561 00:22:10,080 --> 00:22:12,159 and i'm going to put a console log 562 00:22:12,159 --> 00:22:14,000 statement underneath 563 00:22:14,000 --> 00:22:16,240 and this is to demonstrate 564 00:22:16,240 --> 00:22:18,000 that 565 00:22:18,000 --> 00:22:22,000 read file and node in general the 566 00:22:22,000 --> 00:22:24,000 functions or methods you'll find from 567 00:22:24,000 --> 00:22:27,280 node will be asynchronous and so we're 568 00:22:27,280 --> 00:22:29,600 logging the data here but we're also 569 00:22:29,600 --> 00:22:31,679 logging hello here 570 00:22:31,679 --> 00:22:33,840 and node has the ability to say i'm 571 00:22:33,840 --> 00:22:36,320 going to process this but let's go ahead 572 00:22:36,320 --> 00:22:38,240 and tackle the rest 573 00:22:38,240 --> 00:22:40,960 of the tasks in the program and when i 574 00:22:40,960 --> 00:22:42,960 finish reading the file i'll get that 575 00:22:42,960 --> 00:22:44,640 data to you if you're familiar with 576 00:22:44,640 --> 00:22:47,840 async a weight this should be a familiar 577 00:22:47,840 --> 00:22:50,640 concept but let's go ahead and save this 578 00:22:50,640 --> 00:22:52,640 and let's see which we get in the 579 00:22:52,640 --> 00:22:56,559 console first hello or the data 580 00:22:56,559 --> 00:22:58,799 and we get hello and then we get hi my 581 00:22:58,799 --> 00:23:01,840 name is dave so node said it would read 582 00:23:01,840 --> 00:23:03,840 the file but it went ahead and processed 583 00:23:03,840 --> 00:23:06,000 this console log statement and it 584 00:23:06,000 --> 00:23:08,400 actually was logged to the console first 585 00:23:08,400 --> 00:23:10,559 and then when node completed reading the 586 00:23:10,559 --> 00:23:13,600 file it logged the data to the console 587 00:23:13,600 --> 00:23:15,840 instead of hard coding the path like we 588 00:23:15,840 --> 00:23:18,159 see here in read file there is a better 589 00:23:18,159 --> 00:23:19,200 way 590 00:23:19,200 --> 00:23:21,440 and let's pull in the path module to do 591 00:23:21,440 --> 00:23:23,760 that so we have path and then we use 592 00:23:23,760 --> 00:23:27,919 require and we're requiring path 593 00:23:27,919 --> 00:23:31,039 and the reason is the slashes sometimes 594 00:23:31,039 --> 00:23:32,320 if you're familiar with different 595 00:23:32,320 --> 00:23:34,559 operating systems sometimes backwards 596 00:23:34,559 --> 00:23:36,720 sometimes forward there can be some 597 00:23:36,720 --> 00:23:39,280 problems not always but there can be 598 00:23:39,280 --> 00:23:41,919 when you hard code file paths like that 599 00:23:41,919 --> 00:23:44,320 but if we use the path module we can 600 00:23:44,320 --> 00:23:47,679 eliminate this problem so let's use path 601 00:23:47,679 --> 00:23:50,960 dot join i typed json there we go 602 00:23:50,960 --> 00:23:52,000 join 603 00:23:52,000 --> 00:23:54,640 and then we need to specify 604 00:23:54,640 --> 00:23:56,799 first the directory name and that's two 605 00:23:56,799 --> 00:23:58,640 underscores first and remember that's a 606 00:23:58,640 --> 00:24:00,640 value that's always available to us in 607 00:24:00,640 --> 00:24:01,520 node 608 00:24:01,520 --> 00:24:03,600 after the directory name 609 00:24:03,600 --> 00:24:06,559 we'll say the files directory to attach 610 00:24:06,559 --> 00:24:09,919 on to concatenate to that directory name 611 00:24:09,919 --> 00:24:13,440 and then we need to say the actual name 612 00:24:13,440 --> 00:24:16,080 of the file itself so now we've got 613 00:24:16,080 --> 00:24:19,919 starter text so if we supply this 614 00:24:19,919 --> 00:24:22,480 instead of hard coding a file name it's 615 00:24:22,480 --> 00:24:24,320 a much better approach let's go ahead 616 00:24:24,320 --> 00:24:27,120 and save and run this one more time 617 00:24:27,120 --> 00:24:29,840 and everything still works as it did now 618 00:24:29,840 --> 00:24:32,240 that we know how to read files and catch 619 00:24:32,240 --> 00:24:34,400 an error let's go ahead and copy the 620 00:24:34,400 --> 00:24:36,480 read file and underneath this console 621 00:24:36,480 --> 00:24:39,200 log statement i'll paste this in and we 622 00:24:39,200 --> 00:24:41,919 will write a file but it's just a little 623 00:24:41,919 --> 00:24:44,240 different i can change 624 00:24:44,240 --> 00:24:46,320 just a little bit of the copy and paste 625 00:24:46,320 --> 00:24:48,320 so instead of read file we've got write 626 00:24:48,320 --> 00:24:49,200 file 627 00:24:49,200 --> 00:24:51,760 and this path here will change the file 628 00:24:51,760 --> 00:24:53,360 name because we're going to write a new 629 00:24:53,360 --> 00:24:56,480 file this will be reply dot text now we 630 00:24:56,480 --> 00:24:58,559 don't have to specify 631 00:24:58,559 --> 00:25:02,720 the utf-8 that is by default now 632 00:25:02,720 --> 00:25:05,360 we will have a callback and the callback 633 00:25:05,360 --> 00:25:07,120 will only have an error we're not 634 00:25:07,120 --> 00:25:09,279 reading data we're writing it 635 00:25:09,279 --> 00:25:11,120 so if we have an error we'll throw the 636 00:25:11,120 --> 00:25:13,360 error in here instead of data we can 637 00:25:13,360 --> 00:25:15,200 just say 638 00:25:15,200 --> 00:25:17,120 operation 639 00:25:17,120 --> 00:25:19,919 complete or more specifically 640 00:25:19,919 --> 00:25:21,600 let's say 641 00:25:21,600 --> 00:25:23,520 write complete 642 00:25:23,520 --> 00:25:25,840 and we can save this and now let's go 643 00:25:25,840 --> 00:25:28,559 ahead and run this code 644 00:25:28,559 --> 00:25:30,799 and we got an error oh and that's 645 00:25:30,799 --> 00:25:33,279 because i forgot to specify exactly what 646 00:25:33,279 --> 00:25:36,000 we're writing to the file i'll just say 647 00:25:36,000 --> 00:25:36,980 nice to meet you 648 00:25:36,980 --> 00:25:39,679 [Music] 649 00:25:39,679 --> 00:25:42,080 and then we have our callback had things 650 00:25:42,080 --> 00:25:44,880 out of order so again path name 651 00:25:44,880 --> 00:25:46,880 content we're going to put in the file 652 00:25:46,880 --> 00:25:48,799 and then the callback 653 00:25:48,799 --> 00:25:51,200 with that saved let's run it 654 00:25:51,200 --> 00:25:53,600 and there we go we've got hello hi my 655 00:25:53,600 --> 00:25:55,200 name is dave and then the right is 656 00:25:55,200 --> 00:25:58,080 complete let's look at the reply file we 657 00:25:58,080 --> 00:26:00,880 created and it says nice to meet you i'm 658 00:26:00,880 --> 00:26:03,200 going to copy all of this right file 659 00:26:03,200 --> 00:26:05,679 operation down one 660 00:26:05,679 --> 00:26:06,480 and 661 00:26:06,480 --> 00:26:08,640 then we can change it to append file 662 00:26:08,640 --> 00:26:10,400 which is updating a file 663 00:26:10,400 --> 00:26:13,520 adding more content to it 664 00:26:13,520 --> 00:26:16,000 we'll go ahead and create a different 665 00:26:16,000 --> 00:26:17,200 file here 666 00:26:17,200 --> 00:26:19,440 and that is to show you that a pin file 667 00:26:19,440 --> 00:26:21,200 will go ahead and create a file as well 668 00:26:21,200 --> 00:26:23,760 if it doesn't exist so let's just call 669 00:26:23,760 --> 00:26:25,440 this test 670 00:26:25,440 --> 00:26:26,799 and we'll say 671 00:26:26,799 --> 00:26:30,960 testing text or something like that here 672 00:26:31,840 --> 00:26:34,559 and other than that it is basically 673 00:26:34,559 --> 00:26:36,840 the same syntax as 674 00:26:36,840 --> 00:26:39,760 right we'll save this and go ahead and 675 00:26:39,760 --> 00:26:42,720 run the code again 676 00:26:42,880 --> 00:26:44,880 and we got write complete and append 677 00:26:44,880 --> 00:26:46,720 complete and then notice due to the 678 00:26:46,720 --> 00:26:48,080 asynchronous 679 00:26:48,080 --> 00:26:50,640 fashion of node.js 680 00:26:50,640 --> 00:26:53,200 the read completed last this time and we 681 00:26:53,200 --> 00:26:55,440 have hi my name is dave after the other 682 00:26:55,440 --> 00:26:57,840 operation is completed in the file tree 683 00:26:57,840 --> 00:27:00,320 we now have our test file as well it has 684 00:27:00,320 --> 00:27:03,039 the testing text in it so append file 685 00:27:03,039 --> 00:27:05,200 worth noting that it will modify an 686 00:27:05,200 --> 00:27:06,400 existing file 687 00:27:06,400 --> 00:27:08,960 and can append content to it but will 688 00:27:08,960 --> 00:27:10,080 also 689 00:27:10,080 --> 00:27:12,559 it will create a file if it doesn't 690 00:27:12,559 --> 00:27:13,679 exist 691 00:27:13,679 --> 00:27:16,080 so due to the asynchronous nature of 692 00:27:16,080 --> 00:27:18,480 nodejs and these different methods we're 693 00:27:18,480 --> 00:27:20,720 calling here from read file write file 694 00:27:20,720 --> 00:27:24,080 and append file if we wanted to modify 695 00:27:24,080 --> 00:27:27,440 the file that we created such as reply 696 00:27:27,440 --> 00:27:28,720 it would be 697 00:27:28,720 --> 00:27:31,360 better to put a pen file i'm going to 698 00:27:31,360 --> 00:27:33,520 cut this and put it inside of the 699 00:27:33,520 --> 00:27:35,200 callback 700 00:27:35,200 --> 00:27:38,799 of the write file operation that way it 701 00:27:38,799 --> 00:27:41,279 would definitely create the file 702 00:27:41,279 --> 00:27:43,360 and then we would be ready to append to 703 00:27:43,360 --> 00:27:44,399 it 704 00:27:44,399 --> 00:27:46,799 instead of wondering if we would 705 00:27:46,799 --> 00:27:48,720 possibly create the file first with 706 00:27:48,720 --> 00:27:49,919 append 707 00:27:49,919 --> 00:27:53,039 and then write over it with write file 708 00:27:53,039 --> 00:27:56,080 so here let's change this to 709 00:27:56,080 --> 00:27:59,279 reply.text and it says nice to meet you 710 00:27:59,279 --> 00:28:02,720 let's give a couple of line breaks 711 00:28:02,720 --> 00:28:05,039 and then say 712 00:28:05,039 --> 00:28:07,279 yes it is 713 00:28:07,279 --> 00:28:09,840 and if we save that we'll go ahead and 714 00:28:09,840 --> 00:28:11,600 delete the test that we're no longer 715 00:28:11,600 --> 00:28:13,600 using 716 00:28:13,600 --> 00:28:16,559 and our reply because we will create it 717 00:28:16,559 --> 00:28:19,360 once again so we've got reply and then 718 00:28:19,360 --> 00:28:22,399 we will modify reply with append and 719 00:28:22,399 --> 00:28:24,640 it's inside of the callback 720 00:28:24,640 --> 00:28:27,600 of write file 721 00:28:28,080 --> 00:28:30,640 call node index in the terminal 722 00:28:30,640 --> 00:28:31,919 hello 723 00:28:31,919 --> 00:28:33,520 write complete 724 00:28:33,520 --> 00:28:36,159 then we got the read operation completed 725 00:28:36,159 --> 00:28:38,159 and then the append completed 726 00:28:38,159 --> 00:28:40,799 so if we look at reply now it says nice 727 00:28:40,799 --> 00:28:43,039 to meet you and yes it is in the order 728 00:28:43,039 --> 00:28:45,919 we expected now as you might imagine if 729 00:28:45,919 --> 00:28:48,240 we wanted to do something to this file 730 00:28:48,240 --> 00:28:50,720 after we added more content and we 731 00:28:50,720 --> 00:28:52,880 wanted to make sure it happened in the 732 00:28:52,880 --> 00:28:55,039 order it needed to such as renaming the 733 00:28:55,039 --> 00:28:57,679 file then that would need to be in the 734 00:28:57,679 --> 00:29:00,799 callback of the append file i'm going to 735 00:29:00,799 --> 00:29:04,080 go ahead and copy a pen file 736 00:29:04,080 --> 00:29:06,080 and then inside of the callback i'm 737 00:29:06,080 --> 00:29:07,600 going to paste it 738 00:29:07,600 --> 00:29:10,000 and change this 739 00:29:10,000 --> 00:29:11,360 append here 740 00:29:11,360 --> 00:29:12,640 to 741 00:29:12,640 --> 00:29:15,440 rename so we can rename the file and 742 00:29:15,440 --> 00:29:17,039 we're grabbing 743 00:29:17,039 --> 00:29:19,760 the reply file 744 00:29:19,760 --> 00:29:21,760 and then we're going to replace this 745 00:29:21,760 --> 00:29:23,760 parameter with 746 00:29:23,760 --> 00:29:25,520 what we're going to name it 747 00:29:25,520 --> 00:29:28,159 and we'll just call this 748 00:29:28,159 --> 00:29:31,039 new reply 749 00:29:31,520 --> 00:29:33,679 and then of course it has a callback 750 00:29:33,679 --> 00:29:35,600 with an error as well and then we can 751 00:29:35,600 --> 00:29:36,960 put 752 00:29:36,960 --> 00:29:39,440 rename complete 753 00:29:39,440 --> 00:29:42,000 now let's go ahead and call all of this 754 00:29:42,000 --> 00:29:44,640 in the terminal with node index and it 755 00:29:44,640 --> 00:29:46,320 does happen in the order we expect we 756 00:29:46,320 --> 00:29:48,480 got write append and rename and you can 757 00:29:48,480 --> 00:29:50,320 see this time the read operation 758 00:29:50,320 --> 00:29:53,039 completed before the others once again 759 00:29:53,039 --> 00:29:55,120 asynchronous so we don't know which will 760 00:29:55,120 --> 00:29:57,840 happen before the next but in this way 761 00:29:57,840 --> 00:29:59,440 we're controlling it because we're 762 00:29:59,440 --> 00:30:01,919 putting the append inside the callback 763 00:30:01,919 --> 00:30:03,760 of the right and we're putting the 764 00:30:03,760 --> 00:30:06,960 rename inside the callback of the append 765 00:30:06,960 --> 00:30:09,120 let's go ahead and look at our new reply 766 00:30:09,120 --> 00:30:11,840 and it does have the content we expected 767 00:30:11,840 --> 00:30:13,760 now if you've worked with javascript for 768 00:30:13,760 --> 00:30:15,279 a while at this point you would be 769 00:30:15,279 --> 00:30:16,240 saying 770 00:30:16,240 --> 00:30:18,240 yes this is nice dave but this is 771 00:30:18,240 --> 00:30:19,919 starting to look like what is called 772 00:30:19,919 --> 00:30:23,279 callback hell and yes it is because 773 00:30:23,279 --> 00:30:24,080 we're 774 00:30:24,080 --> 00:30:26,480 putting one inside the next inside the 775 00:30:26,480 --> 00:30:28,640 next and they're all inside the 776 00:30:28,640 --> 00:30:31,520 callbacks of the other so 777 00:30:31,520 --> 00:30:33,600 we are controlling the flow but at the 778 00:30:33,600 --> 00:30:36,399 same time we're able to avoid this in 779 00:30:36,399 --> 00:30:40,000 vanilla javascript using async await and 780 00:30:40,000 --> 00:30:42,159 now we can look at how to avoid it in 781 00:30:42,159 --> 00:30:45,039 node by doing the same i'm going to 782 00:30:45,039 --> 00:30:48,720 comment all of this code out 783 00:30:49,440 --> 00:30:51,520 and up here we can get rid of the 784 00:30:51,520 --> 00:30:54,080 console log hello as well 785 00:30:54,080 --> 00:30:57,279 and then at the top where we have fs 786 00:30:57,279 --> 00:31:00,720 we'll switch this to fs 787 00:31:00,720 --> 00:31:02,240 promises 788 00:31:02,240 --> 00:31:04,640 and let's attach 789 00:31:04,640 --> 00:31:05,760 dot 790 00:31:05,760 --> 00:31:08,559 promises to our import here so we are 791 00:31:08,559 --> 00:31:11,919 now importing the file system promises 792 00:31:11,919 --> 00:31:14,799 instead of just fs and now i'm going to 793 00:31:14,799 --> 00:31:17,120 create a function and i'm going to call 794 00:31:17,120 --> 00:31:18,480 it file 795 00:31:18,480 --> 00:31:21,440 ops for file operations and this will be 796 00:31:21,440 --> 00:31:24,720 an async function 797 00:31:25,519 --> 00:31:28,720 and then inside this function we can use 798 00:31:28,720 --> 00:31:32,120 try catch 799 00:31:32,320 --> 00:31:34,720 so here we'll have our catch for any 800 00:31:34,720 --> 00:31:37,039 errors 801 00:31:37,039 --> 00:31:38,880 and now inside here i'll just say 802 00:31:38,880 --> 00:31:40,640 console.error 803 00:31:40,640 --> 00:31:42,720 and pass in the air 804 00:31:42,720 --> 00:31:44,960 or we could throw the air but that's 805 00:31:44,960 --> 00:31:46,559 actually what's happening here and we're 806 00:31:46,559 --> 00:31:48,159 catching them so it will not be an 807 00:31:48,159 --> 00:31:50,640 uncaught error and then inside the try 808 00:31:50,640 --> 00:31:53,919 block i'm going to define data 809 00:31:53,919 --> 00:31:55,760 and set that equal to 810 00:31:55,760 --> 00:31:59,360 fs or sorry before that a weight 811 00:31:59,360 --> 00:32:01,039 fs 812 00:32:01,039 --> 00:32:03,760 promises 813 00:32:03,840 --> 00:32:05,519 dot 814 00:32:05,519 --> 00:32:06,880 read file 815 00:32:06,880 --> 00:32:09,360 and now we need to specify our file path 816 00:32:09,360 --> 00:32:11,760 and that's what we have here part of why 817 00:32:11,760 --> 00:32:15,279 i saved that so i could just copy it up 818 00:32:15,279 --> 00:32:16,720 and i'm going to hide the file tree 819 00:32:16,720 --> 00:32:19,440 momentarily so we can see a little more 820 00:32:19,440 --> 00:32:22,480 after that we can still specify 821 00:32:22,480 --> 00:32:24,640 utf-8 822 00:32:24,640 --> 00:32:27,919 and then we don't need the callback here 823 00:32:27,919 --> 00:32:29,760 that has the error and the data because 824 00:32:29,760 --> 00:32:32,159 we're using a weight and we're catching 825 00:32:32,159 --> 00:32:33,919 the error down here 826 00:32:33,919 --> 00:32:36,640 so this should be just fine now now once 827 00:32:36,640 --> 00:32:38,880 we have the data 828 00:32:38,880 --> 00:32:41,120 let's go ahead and log the data once 829 00:32:41,120 --> 00:32:42,720 again 830 00:32:42,720 --> 00:32:45,039 and i didn't remember my semicolon after 831 00:32:45,039 --> 00:32:47,360 that being a little inconsistent okay so 832 00:32:47,360 --> 00:32:49,919 we have file ops i'll delete the read 833 00:32:49,919 --> 00:32:51,519 file operation 834 00:32:51,519 --> 00:32:53,039 and now here 835 00:32:53,039 --> 00:32:55,519 we're just going to call file ops into 836 00:32:55,519 --> 00:32:57,840 action 837 00:32:58,000 --> 00:33:00,640 we'll have node index in the terminal 838 00:33:00,640 --> 00:33:04,240 oh and i didn't save the file yet 839 00:33:04,880 --> 00:33:06,880 there i'll save the file 840 00:33:06,880 --> 00:33:08,240 hopefully we don't have unexpected 841 00:33:08,240 --> 00:33:10,159 results now and node index in the 842 00:33:10,159 --> 00:33:12,640 terminal and we got hi my name is dave 843 00:33:12,640 --> 00:33:15,039 the data that we expected to get let's 844 00:33:15,039 --> 00:33:17,760 add another await in this process of 845 00:33:17,760 --> 00:33:19,360 file operations 846 00:33:19,360 --> 00:33:22,960 we'll say await fs promises and now we 847 00:33:22,960 --> 00:33:25,440 will write a file 848 00:33:25,440 --> 00:33:28,080 and this file is going to be promise 849 00:33:28,080 --> 00:33:31,039 right so we'll have the same type of 850 00:33:31,039 --> 00:33:33,200 path once again which i can just copy 851 00:33:33,200 --> 00:33:35,279 and paste 852 00:33:35,279 --> 00:33:38,720 but we'll just call this 853 00:33:38,720 --> 00:33:41,039 promisewrite and after that we want to 854 00:33:41,039 --> 00:33:42,880 pass in the data 855 00:33:42,880 --> 00:33:45,840 that we just read 856 00:33:46,880 --> 00:33:48,720 and now i'm going to copy down once 857 00:33:48,720 --> 00:33:49,840 again 858 00:33:49,840 --> 00:33:52,640 and after we write a file let's go ahead 859 00:33:52,640 --> 00:33:55,679 and append to the file 860 00:33:55,679 --> 00:33:56,880 and now 861 00:33:56,880 --> 00:33:58,880 in the append area 862 00:33:58,880 --> 00:34:00,399 we'll just say 863 00:34:00,399 --> 00:34:01,840 nice to meet you 864 00:34:01,840 --> 00:34:03,679 oh let's put a couple of line breaks 865 00:34:03,679 --> 00:34:05,600 here 866 00:34:05,600 --> 00:34:07,760 with backslash 867 00:34:07,760 --> 00:34:10,879 lowercase in so two line breaks 868 00:34:10,879 --> 00:34:13,440 and one more copy down 869 00:34:13,440 --> 00:34:15,870 and now let's rename 870 00:34:15,870 --> 00:34:17,280 [Music] 871 00:34:17,280 --> 00:34:19,679 and we're renaming promise right so 872 00:34:19,679 --> 00:34:23,440 we'll just copy this once again 873 00:34:23,440 --> 00:34:26,159 and instead of content now we need to 874 00:34:26,159 --> 00:34:27,359 put 875 00:34:27,359 --> 00:34:29,520 the new name of the file and this will 876 00:34:29,520 --> 00:34:30,560 be 877 00:34:30,560 --> 00:34:32,320 promise 878 00:34:32,320 --> 00:34:34,079 complete 879 00:34:34,079 --> 00:34:36,000 and we can save that so you can see 880 00:34:36,000 --> 00:34:38,560 we're taking the data from the starter 881 00:34:38,560 --> 00:34:40,399 then we're writing a new file called 882 00:34:40,399 --> 00:34:42,639 promise write then we're appending to 883 00:34:42,639 --> 00:34:43,918 the file 884 00:34:43,918 --> 00:34:47,359 and then we are renaming the file and 885 00:34:47,359 --> 00:34:49,599 finally let's go ahead 886 00:34:49,599 --> 00:34:53,440 and copy these two lines once again 887 00:34:53,440 --> 00:34:55,440 and put them right here 888 00:34:55,440 --> 00:34:58,480 and now we'll call this 889 00:34:58,480 --> 00:35:01,720 new data 890 00:35:03,839 --> 00:35:05,760 and here we want to read the new file 891 00:35:05,760 --> 00:35:07,440 which is 892 00:35:07,440 --> 00:35:08,800 promise 893 00:35:08,800 --> 00:35:11,280 complete and then we'll go ahead and log 894 00:35:11,280 --> 00:35:13,520 that data as well so you can see there's 895 00:35:13,520 --> 00:35:15,520 several different operations happening 896 00:35:15,520 --> 00:35:18,320 here we are reading data from a file 897 00:35:18,320 --> 00:35:20,800 then we're writing it to a new file 898 00:35:20,800 --> 00:35:23,119 then we're appending to that new file 899 00:35:23,119 --> 00:35:25,280 then we're renaming that new file then 900 00:35:25,280 --> 00:35:27,920 we're reading the new file and logging 901 00:35:27,920 --> 00:35:30,880 the data from that file so i hope that's 902 00:35:30,880 --> 00:35:32,880 not confusing i just wanted to show all 903 00:35:32,880 --> 00:35:35,760 the operations that we have gone over 904 00:35:35,760 --> 00:35:37,440 and then use them 905 00:35:37,440 --> 00:35:40,480 in an async await fashion so here is 906 00:35:40,480 --> 00:35:42,560 node index 907 00:35:42,560 --> 00:35:45,839 and here was the original content 908 00:35:45,839 --> 00:35:48,720 hi my name is dave but then the new file 909 00:35:48,720 --> 00:35:51,280 has hi my name is dave and nice to meet 910 00:35:51,280 --> 00:35:53,440 you and let's go ahead and look at the 911 00:35:53,440 --> 00:35:56,079 file tree 912 00:35:56,400 --> 00:35:57,440 there it is 913 00:35:57,440 --> 00:35:59,280 and we've got promise complete right 914 00:35:59,280 --> 00:36:01,680 here that has hi my name is dave nice to 915 00:36:01,680 --> 00:36:04,240 meet you let's go back to the index and 916 00:36:04,240 --> 00:36:07,040 let's add one more thing right here i'm 917 00:36:07,040 --> 00:36:10,000 just going to copy this one line 918 00:36:10,000 --> 00:36:12,800 and above it 919 00:36:12,800 --> 00:36:15,280 i'll paste in again and here instead of 920 00:36:15,280 --> 00:36:17,839 write file i'm going to say 921 00:36:17,839 --> 00:36:19,040 unlink 922 00:36:19,040 --> 00:36:20,720 and after that 923 00:36:20,720 --> 00:36:22,480 we don't even need to pass in data 924 00:36:22,480 --> 00:36:24,000 because unlink 925 00:36:24,000 --> 00:36:26,640 is actually a delete so we can delete 926 00:36:26,640 --> 00:36:28,560 this original file 927 00:36:28,560 --> 00:36:29,920 that is called 928 00:36:29,920 --> 00:36:32,640 starter text there it is 929 00:36:32,640 --> 00:36:34,240 and now 930 00:36:34,240 --> 00:36:36,640 that we have it being deleted and i can 931 00:36:36,640 --> 00:36:38,240 get rid of the space here as well so 932 00:36:38,240 --> 00:36:40,800 we're reading the file logging the data 933 00:36:40,800 --> 00:36:42,560 to the console and then deleting that 934 00:36:42,560 --> 00:36:45,359 file then creating the new file 935 00:36:45,359 --> 00:36:47,440 and we can even get rid of promise 936 00:36:47,440 --> 00:36:49,119 complete here again 937 00:36:49,119 --> 00:36:51,280 and we'll get rid of new reply we're not 938 00:36:51,280 --> 00:36:53,040 using that anymore 939 00:36:53,040 --> 00:36:55,760 let's make sure we save the index 940 00:36:55,760 --> 00:36:59,119 and we'll call it into action 941 00:36:59,200 --> 00:37:00,640 once again we got the same in the 942 00:37:00,640 --> 00:37:02,800 console and we have promise complete 943 00:37:02,800 --> 00:37:05,359 over here but no more starter text it's 944 00:37:05,359 --> 00:37:07,359 gone because we deleted it with the 945 00:37:07,359 --> 00:37:10,240 unlink right here and this is also 946 00:37:10,240 --> 00:37:13,200 available without fs promises as the 947 00:37:13,200 --> 00:37:15,040 first examples i showed that were just 948 00:37:15,040 --> 00:37:17,760 fs and then read file write file and so 949 00:37:17,760 --> 00:37:20,000 on unlink is what you would use to 950 00:37:20,000 --> 00:37:22,720 delete a file okay now i'm going to 951 00:37:22,720 --> 00:37:24,960 create a new file over here and i'm just 952 00:37:24,960 --> 00:37:26,560 going to call this 953 00:37:26,560 --> 00:37:28,800 stream.js 954 00:37:28,800 --> 00:37:32,320 now if we have larger files sometimes it 955 00:37:32,320 --> 00:37:35,280 is good to not grab all of the data at 956 00:37:35,280 --> 00:37:37,760 once it could be too much just like 957 00:37:37,760 --> 00:37:39,200 moving a 958 00:37:39,200 --> 00:37:42,400 large pile of sand bucket by bucket or 959 00:37:42,400 --> 00:37:44,560 moving all the water in a swimming pool 960 00:37:44,560 --> 00:37:47,119 bucket by bucket rather than attempting 961 00:37:47,119 --> 00:37:49,520 to grab everything all at once so this 962 00:37:49,520 --> 00:37:51,839 could be more efficient and a little bit 963 00:37:51,839 --> 00:37:54,720 easier on the application if we do this 964 00:37:54,720 --> 00:37:57,440 so let's say cons fs and once again 965 00:37:57,440 --> 00:37:58,880 let's require 966 00:37:58,880 --> 00:38:02,079 fs not fs promises just fs 967 00:38:02,079 --> 00:38:03,200 and here 968 00:38:03,200 --> 00:38:05,520 i'm going to define rs and we're going 969 00:38:05,520 --> 00:38:08,240 to set this equal to fs dot 970 00:38:08,240 --> 00:38:09,520 create 971 00:38:09,520 --> 00:38:11,760 read stream 972 00:38:11,760 --> 00:38:13,040 from here 973 00:38:13,040 --> 00:38:15,920 i'm going to specify our files 974 00:38:15,920 --> 00:38:17,560 slash 975 00:38:17,560 --> 00:38:20,000 lorem.txt once again you could use the 976 00:38:20,000 --> 00:38:22,640 path module would probably be a better 977 00:38:22,640 --> 00:38:24,480 option i'm just doing this quickly so 978 00:38:24,480 --> 00:38:26,320 i'm hard coding that in 979 00:38:26,320 --> 00:38:27,440 and now 980 00:38:27,440 --> 00:38:30,839 putting in the encoding and this is 981 00:38:30,839 --> 00:38:34,079 utf 8 once again so we've created a 982 00:38:34,079 --> 00:38:36,560 readable stream and we've specified the 983 00:38:36,560 --> 00:38:38,960 encoding in the options now let's go 984 00:38:38,960 --> 00:38:40,000 ahead 985 00:38:40,000 --> 00:38:41,440 and specify 986 00:38:41,440 --> 00:38:43,440 a writable stream and this is going to 987 00:38:43,440 --> 00:38:46,760 be ws.com 988 00:38:47,839 --> 00:38:48,760 write stream 989 00:38:48,760 --> 00:38:50,400 [Music] 990 00:38:50,400 --> 00:38:52,800 and now what do we want to 991 00:38:52,800 --> 00:38:54,880 write this to would be the question so 992 00:38:54,880 --> 00:38:56,800 let's call this 993 00:38:56,800 --> 00:38:58,480 from 994 00:38:58,480 --> 00:39:01,680 stream ah let's call it 995 00:39:01,680 --> 00:39:04,240 new dash lorem because it is our lorem 996 00:39:04,240 --> 00:39:05,920 text still that works 997 00:39:05,920 --> 00:39:08,720 so we have new lorem 998 00:39:08,720 --> 00:39:10,560 and really we don't need to specify 999 00:39:10,560 --> 00:39:12,640 anything else here we already specified 1000 00:39:12,640 --> 00:39:15,440 what was being read is utf-8 so we're 1001 00:39:15,440 --> 00:39:18,240 good there and i did make a mistake here 1002 00:39:18,240 --> 00:39:21,280 i needed to make this fs not ws because 1003 00:39:21,280 --> 00:39:22,240 this is 1004 00:39:22,240 --> 00:39:24,160 getting this from the fs module the 1005 00:39:24,160 --> 00:39:25,599 create write stream and that's why it 1006 00:39:25,599 --> 00:39:28,000 didn't help me out when i was typing it 1007 00:39:28,000 --> 00:39:30,400 okay so once we have that we need to 1008 00:39:30,400 --> 00:39:32,720 listen for the data coming in from the 1009 00:39:32,720 --> 00:39:35,040 stream so here is our readable stream 1010 00:39:35,040 --> 00:39:36,800 and we can say on 1011 00:39:36,800 --> 00:39:38,880 now we're listening for the data that's 1012 00:39:38,880 --> 00:39:40,160 coming in 1013 00:39:40,160 --> 00:39:42,640 and here let's say this is a chunk of 1014 00:39:42,640 --> 00:39:44,640 data or a data 1015 00:39:44,640 --> 00:39:47,760 chunk if you will 1016 00:39:48,079 --> 00:39:50,720 and inside of here we could console log 1017 00:39:50,720 --> 00:39:53,280 the chunk or we could just 1018 00:39:53,280 --> 00:39:56,560 write to our writable stream and pass in 1019 00:39:56,560 --> 00:39:58,480 that data chunk 1020 00:39:58,480 --> 00:40:00,400 so let's do that 1021 00:40:00,400 --> 00:40:02,480 and i will save 1022 00:40:02,480 --> 00:40:05,359 and now let's go ahead and run this so 1023 00:40:05,359 --> 00:40:08,319 we'll type node 1024 00:40:08,839 --> 00:40:12,000 stream and notice now we have new lorem 1025 00:40:12,000 --> 00:40:15,359 as well and this will be a large 1026 00:40:15,359 --> 00:40:18,240 file of lorem probably about a thousand 1027 00:40:18,240 --> 00:40:21,359 lines yes so it did it very rapidly but 1028 00:40:21,359 --> 00:40:24,400 once again a example of a large file 1029 00:40:24,400 --> 00:40:25,440 just a 1030 00:40:25,440 --> 00:40:27,680 test example we could have much larger 1031 00:40:27,680 --> 00:40:29,119 files but in that case when you're 1032 00:40:29,119 --> 00:40:30,880 working with a large file this is much 1033 00:40:30,880 --> 00:40:32,960 more efficient now speaking of 1034 00:40:32,960 --> 00:40:35,760 efficiency i'll go ahead and comment 1035 00:40:35,760 --> 00:40:38,960 this out there is even a better way to 1036 00:40:38,960 --> 00:40:41,359 do this instead of the listener 1037 00:40:41,359 --> 00:40:42,560 just take 1038 00:40:42,560 --> 00:40:45,680 the readable stream and use pipe 1039 00:40:45,680 --> 00:40:47,760 and then you can pass to the writable 1040 00:40:47,760 --> 00:40:50,160 stream this will accomplish the same 1041 00:40:50,160 --> 00:40:52,960 thing and piping is more efficient than 1042 00:40:52,960 --> 00:40:55,119 this structure with the listener here 1043 00:40:55,119 --> 00:40:56,880 i'm going to save this i'm going to 1044 00:40:56,880 --> 00:40:59,520 delete the new lorem file 1045 00:40:59,520 --> 00:41:01,920 and now let's go ahead and run our 1046 00:41:01,920 --> 00:41:04,400 stream once again by typing node stream 1047 00:41:04,400 --> 00:41:07,200 in the terminal 1048 00:41:07,200 --> 00:41:09,680 and you can see we once again got our 1049 00:41:09,680 --> 00:41:13,040 new lorem text file and it has all 1000 1050 00:41:13,040 --> 00:41:16,640 lines of lorem ipsum text 1051 00:41:16,640 --> 00:41:18,560 okay i'm going to create one more new 1052 00:41:18,560 --> 00:41:21,240 file in the file tree and call this 1053 00:41:21,240 --> 00:41:24,800 dur.js which is short for directory 1054 00:41:24,800 --> 00:41:26,240 once again 1055 00:41:26,240 --> 00:41:28,319 let's go ahead and define 1056 00:41:28,319 --> 00:41:32,000 fs at the top and require it 1057 00:41:32,000 --> 00:41:34,800 and after we do that what i want to do 1058 00:41:34,800 --> 00:41:37,960 is create a directory with 1059 00:41:37,960 --> 00:41:41,839 fs.mkdir which stands for make directory 1060 00:41:41,839 --> 00:41:43,040 and we'll have 1061 00:41:43,040 --> 00:41:45,200 let's just call it new we're specifying 1062 00:41:45,200 --> 00:41:46,720 what we want to call it right here and 1063 00:41:46,720 --> 00:41:48,400 then there's a callback that has an 1064 00:41:48,400 --> 00:41:49,599 error 1065 00:41:49,599 --> 00:41:51,359 and this is the same as we've seen 1066 00:41:51,359 --> 00:41:54,000 previously so if error we could throw 1067 00:41:54,000 --> 00:41:56,400 error now i won't go ahead and copy and 1068 00:41:56,400 --> 00:41:58,560 paste the uncaught error handling code 1069 00:41:58,560 --> 00:42:00,160 in here that i showed you earlier just 1070 00:42:00,160 --> 00:42:01,839 to save time but you could use it in the 1071 00:42:01,839 --> 00:42:02,960 same way 1072 00:42:02,960 --> 00:42:05,040 so if we have an error throw an error 1073 00:42:05,040 --> 00:42:06,480 and if not we've created the new 1074 00:42:06,480 --> 00:42:10,000 directory so we'll just log 1075 00:42:10,880 --> 00:42:12,160 directory 1076 00:42:12,160 --> 00:42:13,599 created 1077 00:42:13,599 --> 00:42:16,480 and we can save this let's go ahead and 1078 00:42:16,480 --> 00:42:19,520 run our code so here we'll type node dur 1079 00:42:19,520 --> 00:42:22,160 which is our new file dur 1080 00:42:22,160 --> 00:42:24,800 and directory is created and now we can 1081 00:42:24,800 --> 00:42:27,280 see here is our new directory now 1082 00:42:27,280 --> 00:42:29,359 earlier in the tutorial we threw an 1083 00:42:29,359 --> 00:42:32,000 error on purpose because we attempted to 1084 00:42:32,000 --> 00:42:34,240 read a file that did not exist 1085 00:42:34,240 --> 00:42:36,800 well we can check to see if files and 1086 00:42:36,800 --> 00:42:39,359 directories exist or not so we don't get 1087 00:42:39,359 --> 00:42:41,920 those errors and we also might want to 1088 00:42:41,920 --> 00:42:44,720 check in this regard to say if the 1089 00:42:44,720 --> 00:42:47,920 folder if the directory already exists 1090 00:42:47,920 --> 00:42:49,920 let's not create it because we don't 1091 00:42:49,920 --> 00:42:52,160 want to write over what we already have 1092 00:42:52,160 --> 00:42:53,760 so we can use 1093 00:42:53,760 --> 00:42:56,880 the exists sync method and let's start 1094 00:42:56,880 --> 00:42:59,359 out with if and then we'll say if it 1095 00:42:59,359 --> 00:43:01,680 does not and not would be the 1096 00:43:01,680 --> 00:43:03,200 exclamation mark 1097 00:43:03,200 --> 00:43:05,200 fs dot 1098 00:43:05,200 --> 00:43:07,440 exists 1099 00:43:07,440 --> 00:43:11,440 is it exist yet two s's exists sync 1100 00:43:11,440 --> 00:43:14,079 and then we'll say the same 1101 00:43:14,079 --> 00:43:16,720 folder slash or the same directory dot 1102 00:43:16,720 --> 00:43:20,880 slash new so if it does not exist 1103 00:43:20,880 --> 00:43:23,760 then go ahead and do what's inside of 1104 00:43:23,760 --> 00:43:25,440 the if statement 1105 00:43:25,440 --> 00:43:27,119 and we'll put that right there let's go 1106 00:43:27,119 --> 00:43:29,839 ahead and close that out and close this 1107 00:43:29,839 --> 00:43:31,760 out 1108 00:43:31,760 --> 00:43:34,240 and save and now let's see if we create 1109 00:43:34,240 --> 00:43:36,000 the directory when we run it in the 1110 00:43:36,000 --> 00:43:37,760 terminal 1111 00:43:37,760 --> 00:43:40,720 and no no directory was created because 1112 00:43:40,720 --> 00:43:42,160 it exists 1113 00:43:42,160 --> 00:43:45,280 so this says if it does not exist create 1114 00:43:45,280 --> 00:43:47,680 it so let's go ahead and delete the 1115 00:43:47,680 --> 00:43:49,520 directory 1116 00:43:49,520 --> 00:43:52,800 and now let's run this once again 1117 00:43:52,800 --> 00:43:54,480 and now it created the directory again 1118 00:43:54,480 --> 00:43:57,280 because it did not exist you might find 1119 00:43:57,280 --> 00:44:00,160 checking for file existence fairly 1120 00:44:00,160 --> 00:44:02,160 useful before you attempt to delete a 1121 00:44:02,160 --> 00:44:05,680 file if it exists or not or before you 1122 00:44:05,680 --> 00:44:08,880 attempt to even rename a file or copy a 1123 00:44:08,880 --> 00:44:10,720 file several things that you want to 1124 00:44:10,720 --> 00:44:13,440 make sure the file exists likewise you 1125 00:44:13,440 --> 00:44:16,000 can check directories in this regard now 1126 00:44:16,000 --> 00:44:18,079 let's go ahead and write some code that 1127 00:44:18,079 --> 00:44:19,359 deletes 1128 00:44:19,359 --> 00:44:22,480 the new directory okay let's copy this 1129 00:44:22,480 --> 00:44:24,400 down 1130 00:44:24,400 --> 00:44:26,160 highlight everything 1131 00:44:26,160 --> 00:44:28,640 and bring it down here to line 10 1132 00:44:28,640 --> 00:44:31,680 and now let's check to say if it does 1133 00:44:31,680 --> 00:44:33,440 exist 1134 00:44:33,440 --> 00:44:35,280 and this is the folder we're going to 1135 00:44:35,280 --> 00:44:37,880 remove the directory and that is 1136 00:44:37,880 --> 00:44:41,119 rmdir for remove directory so this would 1137 00:44:41,119 --> 00:44:43,440 be a zero sum game here if it does not 1138 00:44:43,440 --> 00:44:45,119 exist create it 1139 00:44:45,119 --> 00:44:48,319 and if it does exist remove it so now 1140 00:44:48,319 --> 00:44:50,480 let's say directory 1141 00:44:50,480 --> 00:44:52,720 removed 1142 00:44:52,720 --> 00:44:53,599 and 1143 00:44:53,599 --> 00:44:55,599 we'll be able to tell by the console but 1144 00:44:55,599 --> 00:44:57,040 in the file tree 1145 00:44:57,040 --> 00:44:58,880 we should not have a directory when 1146 00:44:58,880 --> 00:45:02,240 these operations are complete 1147 00:45:02,240 --> 00:45:04,240 and the directory was removed it already 1148 00:45:04,240 --> 00:45:06,720 existed so it wasn't created but it was 1149 00:45:06,720 --> 00:45:10,160 removed let's go ahead and run it again 1150 00:45:10,160 --> 00:45:13,040 and now it was created and removed and 1151 00:45:13,040 --> 00:45:14,880 so it's still not in our file tree but 1152 00:45:14,880 --> 00:45:18,720 we can see both operations completed 1153 00:45:18,720 --> 00:45:20,800 today we're learning about node package 1154 00:45:20,800 --> 00:45:23,520 manager which is also commonly known as 1155 00:45:23,520 --> 00:45:26,839 npm and when you install nodejs from 1156 00:45:26,839 --> 00:45:29,680 nodejs.org which i have open right here 1157 00:45:29,680 --> 00:45:33,839 you also install npm however npm also 1158 00:45:33,839 --> 00:45:37,960 has its own site and documentation at 1159 00:45:37,960 --> 00:45:41,040 npmjs.com so let's take a look at that 1160 00:45:41,040 --> 00:45:42,520 and you can see the 1161 00:45:42,520 --> 00:45:45,119 npmjs.com site here one little quirk i 1162 00:45:45,119 --> 00:45:46,520 really like about 1163 00:45:46,520 --> 00:45:49,040 npmjs.com is they give 1164 00:45:49,040 --> 00:45:51,839 wrong answers for what npm stands for in 1165 00:45:51,839 --> 00:45:53,839 the top left so every time you come back 1166 00:45:53,839 --> 00:45:55,040 to the site you'll see something 1167 00:45:55,040 --> 00:45:56,560 different here right now it says new 1168 00:45:56,560 --> 00:45:59,359 prog mixtape it really stands for node 1169 00:45:59,359 --> 00:46:02,079 package manager and npm provides 1170 00:46:02,079 --> 00:46:04,160 thousands of packages for us to use in 1171 00:46:04,160 --> 00:46:07,119 our applications the difference between 1172 00:46:07,119 --> 00:46:09,680 a node common core module and an npm 1173 00:46:09,680 --> 00:46:12,400 package is that npm packages are node 1174 00:46:12,400 --> 00:46:14,480 modules that are created by third 1175 00:46:14,480 --> 00:46:16,640 parties in other words other developers 1176 00:46:16,640 --> 00:46:18,480 so let's search for something here like 1177 00:46:18,480 --> 00:46:21,119 axios which is a node package 1178 00:46:21,119 --> 00:46:23,680 and we see the results axios is at the 1179 00:46:23,680 --> 00:46:26,079 top with an exact match and you can see 1180 00:46:26,079 --> 00:46:28,160 they're also ranked by popularity 1181 00:46:28,160 --> 00:46:30,319 quality and maintenance and you see the 1182 00:46:30,319 --> 00:46:32,800 little chart over here to the right so 1183 00:46:32,800 --> 00:46:35,200 essentially other developers create 1184 00:46:35,200 --> 00:46:37,599 reusable modules and share them through 1185 00:46:37,599 --> 00:46:40,480 npm so instead of recreating the wheel 1186 00:46:40,480 --> 00:46:42,400 we can search for a module that already 1187 00:46:42,400 --> 00:46:44,720 exists and then we can import that 1188 00:46:44,720 --> 00:46:47,760 module into our applications now today 1189 00:46:47,760 --> 00:46:49,599 i'm going to go over what i consider to 1190 00:46:49,599 --> 00:46:52,319 be essential npm knowledge and we're 1191 00:46:52,319 --> 00:46:54,400 also going to install some packages that 1192 00:46:54,400 --> 00:46:56,240 we'll need in the near future when we 1193 00:46:56,240 --> 00:46:58,640 create an event logger i'm going to 1194 00:46:58,640 --> 00:46:59,760 start by showing you where the 1195 00:46:59,760 --> 00:47:01,640 documentation is at 1196 00:47:01,640 --> 00:47:04,319 npmjs.com click on documentation at the 1197 00:47:04,319 --> 00:47:09,000 top then it takes us to docs.npmjs.com 1198 00:47:09,359 --> 00:47:11,040 now when we're there what we're really 1199 00:47:11,040 --> 00:47:12,240 working with 1200 00:47:12,240 --> 00:47:14,160 is the cli 1201 00:47:14,160 --> 00:47:16,480 and that's what developers typically use 1202 00:47:16,480 --> 00:47:19,040 when they're integrating npm into their 1203 00:47:19,040 --> 00:47:21,359 projects and their cli commands 1204 00:47:21,359 --> 00:47:24,319 configuring npm and using npm there's 1205 00:47:24,319 --> 00:47:26,160 quite a few commands i'll just go over 1206 00:47:26,160 --> 00:47:28,160 what i consider to be essential what i 1207 00:47:28,160 --> 00:47:30,160 use the most and what you really need to 1208 00:47:30,160 --> 00:47:31,920 get started but i always want you to 1209 00:47:31,920 --> 00:47:34,559 know where to find that documentation 1210 00:47:34,559 --> 00:47:36,880 and reading the docs is definitely a big 1211 00:47:36,880 --> 00:47:39,520 part of being a developer okay let's 1212 00:47:39,520 --> 00:47:41,920 move over to visual studio code i've got 1213 00:47:41,920 --> 00:47:45,520 visual studio code open and i have a new 1214 00:47:45,520 --> 00:47:47,839 folder for the tutorial and then an 1215 00:47:47,839 --> 00:47:50,480 index.js file that is completely empty 1216 00:47:50,480 --> 00:47:52,559 right now we're going to start off by 1217 00:47:52,559 --> 00:47:54,880 just putting a console log statement and 1218 00:47:54,880 --> 00:47:56,480 saying testing 1219 00:47:56,480 --> 00:47:59,200 inside of our index.js and we can save 1220 00:47:59,200 --> 00:48:01,680 that file now i want to open a terminal 1221 00:48:01,680 --> 00:48:04,079 so we can go to the terminal menu or 1222 00:48:04,079 --> 00:48:05,760 since i'm in windows i'm just going to 1223 00:48:05,760 --> 00:48:07,920 press control and the back tick and it 1224 00:48:07,920 --> 00:48:10,240 will open up a terminal 1225 00:48:10,240 --> 00:48:11,920 now in the terminal 1226 00:48:11,920 --> 00:48:15,839 we can install a node package globally 1227 00:48:15,839 --> 00:48:17,680 that we can just call from the command 1228 00:48:17,680 --> 00:48:19,760 line without adding it to a specific 1229 00:48:19,760 --> 00:48:21,760 project so i'd like to do that first 1230 00:48:21,760 --> 00:48:23,760 i'll type npm 1231 00:48:23,760 --> 00:48:26,559 and then the word install works but you 1232 00:48:26,559 --> 00:48:29,200 can also use the alias i or there's also 1233 00:48:29,200 --> 00:48:30,800 an alias ad 1234 00:48:30,800 --> 00:48:33,040 i typically use just i 1235 00:48:33,040 --> 00:48:36,640 from there i want to install node mon 1236 00:48:36,640 --> 00:48:38,720 and this is a great development package 1237 00:48:38,720 --> 00:48:41,760 but it's also good to install globally 1238 00:48:41,760 --> 00:48:43,520 because you might just want to call it 1239 00:48:43,520 --> 00:48:45,040 from the command line 1240 00:48:45,040 --> 00:48:47,200 and with nodemon 1241 00:48:47,200 --> 00:48:50,640 we want to put dash g now what nodemon 1242 00:48:50,640 --> 00:48:53,119 does is it monitors your files and as 1243 00:48:53,119 --> 00:48:55,440 you save it automatically restarts the 1244 00:48:55,440 --> 00:48:57,760 server so we're not always typing node 1245 00:48:57,760 --> 00:48:59,839 and the file name in the terminal it 1246 00:48:59,839 --> 00:49:02,240 just kind of does it for us so i'm going 1247 00:49:02,240 --> 00:49:04,240 to go ahead and press enter to install 1248 00:49:04,240 --> 00:49:06,880 this package globally and then when it 1249 00:49:06,880 --> 00:49:09,440 finishes we'll see how it works with our 1250 00:49:09,440 --> 00:49:12,800 index.js okay and the package installed 1251 00:49:12,800 --> 00:49:15,119 we're ready to try out nodemon so let's 1252 00:49:15,119 --> 00:49:18,160 type nodemon instead of node 1253 00:49:18,160 --> 00:49:21,040 and it's going to look for the index.js 1254 00:49:21,040 --> 00:49:22,960 by default so i don't really need to 1255 00:49:22,960 --> 00:49:25,040 type that name but if i had a different 1256 00:49:25,040 --> 00:49:27,440 file name like server i would type 1257 00:49:27,440 --> 00:49:29,680 server or something like that right now 1258 00:49:29,680 --> 00:49:32,000 i'm just going to type nodemon 1259 00:49:32,000 --> 00:49:33,760 and it will know to look for the 1260 00:49:33,760 --> 00:49:36,880 index.js and it says it is watching the 1261 00:49:36,880 --> 00:49:40,880 extensions js mjs and json and it 1262 00:49:40,880 --> 00:49:43,359 started our index.js 1263 00:49:43,359 --> 00:49:45,520 and here we can see the message testing 1264 00:49:45,520 --> 00:49:47,520 so it did go ahead and log to the 1265 00:49:47,520 --> 00:49:49,440 console testing now i'm going to change 1266 00:49:49,440 --> 00:49:50,800 the file 1267 00:49:50,800 --> 00:49:53,599 add an exclamation mark and save and 1268 00:49:53,599 --> 00:49:55,440 then you can see it was restarting due 1269 00:49:55,440 --> 00:49:57,599 to changes and then it went ahead and 1270 00:49:57,599 --> 00:49:59,680 ran the file and we got testing in the 1271 00:49:59,680 --> 00:50:02,559 console so that's how nodemon works now 1272 00:50:02,559 --> 00:50:05,520 to exit nodemon you can press ctrl c 1273 00:50:05,520 --> 00:50:07,440 just like we would exit a node 1274 00:50:07,440 --> 00:50:09,119 application that we were running if we 1275 00:50:09,119 --> 00:50:11,839 tested it by typing node index now that 1276 00:50:11,839 --> 00:50:13,359 you know how to install a package 1277 00:50:13,359 --> 00:50:15,440 globally let's go ahead and add a 1278 00:50:15,440 --> 00:50:18,640 package to our project but before we do 1279 00:50:18,640 --> 00:50:21,680 that we have to initialize npm for our 1280 00:50:21,680 --> 00:50:24,640 project i'll type npm and then just like 1281 00:50:24,640 --> 00:50:26,319 when we initialize git if you're 1282 00:50:26,319 --> 00:50:28,800 familiar with working with git 1283 00:50:28,800 --> 00:50:31,280 we type init now it will ask some 1284 00:50:31,280 --> 00:50:32,880 questions at the beginning if i want to 1285 00:50:32,880 --> 00:50:34,640 skip those questions i'll just put in 1286 00:50:34,640 --> 00:50:37,200 the flag hyphen yes to answer yes to all 1287 00:50:37,200 --> 00:50:39,359 the defaults but i'll go ahead and not 1288 00:50:39,359 --> 00:50:41,040 do that just to show you the questions 1289 00:50:41,040 --> 00:50:42,400 it will ask 1290 00:50:42,400 --> 00:50:44,000 and then i'll just press enter on each 1291 00:50:44,000 --> 00:50:45,680 one to go ahead and accept the defaults 1292 00:50:45,680 --> 00:50:48,480 but first it's going to initialize npm 1293 00:50:48,480 --> 00:50:51,440 okay it's asking me the package name and 1294 00:50:51,440 --> 00:50:53,680 the default would be zero three tut what 1295 00:50:53,680 --> 00:50:55,599 i named the folder 1296 00:50:55,599 --> 00:50:59,200 and then the version by default is 1.0.0 1297 00:50:59,200 --> 00:51:02,079 description you don't have to enter one 1298 00:51:02,079 --> 00:51:04,280 entry point by default is 1299 00:51:04,280 --> 00:51:06,640 index.js but we could change that to 1300 00:51:06,640 --> 00:51:08,880 server.js or something else if we wanted 1301 00:51:08,880 --> 00:51:09,839 to 1302 00:51:09,839 --> 00:51:12,079 test command nothing to enter there we 1303 00:51:12,079 --> 00:51:14,559 don't have a git repository yet no 1304 00:51:14,559 --> 00:51:16,720 keywords you could put your name for 1305 00:51:16,720 --> 00:51:18,319 author if you want to 1306 00:51:18,319 --> 00:51:20,079 and there's the license by default and 1307 00:51:20,079 --> 00:51:22,559 then it says is all of this okay and the 1308 00:51:22,559 --> 00:51:24,559 default is yes so i'll just press enter 1309 00:51:24,559 --> 00:51:26,079 one more time 1310 00:51:26,079 --> 00:51:28,160 now if we look over 1311 00:51:28,160 --> 00:51:31,280 in the file tree we see a package json 1312 00:51:31,280 --> 00:51:33,839 file and this pretty much has all the 1313 00:51:33,839 --> 00:51:35,520 information that we answered those 1314 00:51:35,520 --> 00:51:38,480 questions to then the package json file 1315 00:51:38,480 --> 00:51:41,359 is important because this is what npm 1316 00:51:41,359 --> 00:51:43,920 reads to know what packages to install 1317 00:51:43,920 --> 00:51:46,559 for your project and that is because 1318 00:51:46,559 --> 00:51:49,280 this file will stay with your repository 1319 00:51:49,280 --> 00:51:50,960 if we send it to github 1320 00:51:50,960 --> 00:51:53,359 but we won't send the packages itself 1321 00:51:53,359 --> 00:51:55,359 and that way we don't have to transfer 1322 00:51:55,359 --> 00:51:58,240 as much data or store as much data 1323 00:51:58,240 --> 00:52:00,559 and then when somebody else clones or 1324 00:52:00,559 --> 00:52:03,359 installs this application it can just 1325 00:52:03,359 --> 00:52:05,760 read the package json or likewise if we 1326 00:52:05,760 --> 00:52:07,280 were to host it and build the 1327 00:52:07,280 --> 00:52:08,559 application 1328 00:52:08,559 --> 00:52:11,119 at a host and run a build command 1329 00:52:11,119 --> 00:52:13,359 then it would install those packages 1330 00:52:13,359 --> 00:52:15,599 there without having to transfer them 1331 00:52:15,599 --> 00:52:17,119 over from github 1332 00:52:17,119 --> 00:52:19,440 so now that we have this let's go ahead 1333 00:52:19,440 --> 00:52:22,480 and add a package to our program and i'm 1334 00:52:22,480 --> 00:52:24,800 going to say npmi 1335 00:52:24,800 --> 00:52:26,400 and then i'll type 1336 00:52:26,400 --> 00:52:29,280 date dash fns which stands for date 1337 00:52:29,280 --> 00:52:30,480 functions 1338 00:52:30,480 --> 00:52:32,640 and press enter and it will install this 1339 00:52:32,640 --> 00:52:35,119 package as a dependency which we will 1340 00:52:35,119 --> 00:52:37,839 see in our package json file okay that 1341 00:52:37,839 --> 00:52:40,160 completed installing and we now see 1342 00:52:40,160 --> 00:52:43,119 dependencies listed in our package json 1343 00:52:43,119 --> 00:52:44,720 file and these are production 1344 00:52:44,720 --> 00:52:46,720 dependencies we see listed here so when 1345 00:52:46,720 --> 00:52:48,800 the project would build with a build 1346 00:52:48,800 --> 00:52:51,920 command it would include this package 1347 00:52:51,920 --> 00:52:54,000 because we would know it would be part 1348 00:52:54,000 --> 00:52:57,119 of the overall application that needed 1349 00:52:57,119 --> 00:52:59,280 to go into production but we can also 1350 00:52:59,280 --> 00:53:01,200 have dev dependencies before we get to 1351 00:53:01,200 --> 00:53:03,280 the dev dependencies though let's go 1352 00:53:03,280 --> 00:53:05,040 ahead and look at the file tree because 1353 00:53:05,040 --> 00:53:08,319 now we have a package dash lock dot json 1354 00:53:08,319 --> 00:53:10,319 and we don't want to change anything in 1355 00:53:10,319 --> 00:53:12,480 there that's just handled by npm but you 1356 00:53:12,480 --> 00:53:14,400 will see it show up in its file tree 1357 00:53:14,400 --> 00:53:17,599 we just work with package.json we also 1358 00:53:17,599 --> 00:53:20,800 see a node modules folder and this can 1359 00:53:20,800 --> 00:53:23,839 get fairly large fairly fast because 1360 00:53:23,839 --> 00:53:26,079 there will be a lot of 1361 00:53:26,079 --> 00:53:28,559 files and folders in here and that's 1362 00:53:28,559 --> 00:53:32,240 because any dependency that we add can 1363 00:53:32,240 --> 00:53:35,200 also pull in other dependencies and if 1364 00:53:35,200 --> 00:53:37,119 this package needs all of these 1365 00:53:37,119 --> 00:53:38,400 dependencies 1366 00:53:38,400 --> 00:53:40,480 this gets very large and that's why we 1367 00:53:40,480 --> 00:53:42,720 don't want to store that in github so 1368 00:53:42,720 --> 00:53:44,480 what we should do 1369 00:53:44,480 --> 00:53:46,960 is always add a dot 1370 00:53:46,960 --> 00:53:48,319 git 1371 00:53:48,319 --> 00:53:50,720 ignore file oh and i didn't press dot i 1372 00:53:50,720 --> 00:53:52,559 got a slash there we go dot 1373 00:53:52,559 --> 00:53:53,599 get 1374 00:53:53,599 --> 00:53:55,359 ignore 1375 00:53:55,359 --> 00:53:57,359 and in this get ignore file 1376 00:53:57,359 --> 00:54:00,079 the first thing you should add 1377 00:54:00,079 --> 00:54:01,760 should be node 1378 00:54:01,760 --> 00:54:03,119 underscore 1379 00:54:03,119 --> 00:54:04,480 modules 1380 00:54:04,480 --> 00:54:06,720 now i'll go ahead and press save and now 1381 00:54:06,720 --> 00:54:09,119 if we were to initialize git and save 1382 00:54:09,119 --> 00:54:11,280 this to github the node modules folder 1383 00:54:11,280 --> 00:54:13,680 would not be included and that's 1384 00:54:13,680 --> 00:54:16,800 important because there is a lot of data 1385 00:54:16,800 --> 00:54:19,280 in node modules now i'm not going to 1386 00:54:19,280 --> 00:54:21,839 delete the node modules folder right now 1387 00:54:21,839 --> 00:54:24,000 but if you were to clone another 1388 00:54:24,000 --> 00:54:26,640 repository and it had the package json 1389 00:54:26,640 --> 00:54:28,640 but did not have the node modules you 1390 00:54:28,640 --> 00:54:31,520 would get an error if you tried to run 1391 00:54:31,520 --> 00:54:33,599 or tried to start the project so what 1392 00:54:33,599 --> 00:54:36,839 you want to do is type npm 1393 00:54:36,839 --> 00:54:39,920 install and if you just type npm install 1394 00:54:39,920 --> 00:54:42,000 and press enter it's going to read the 1395 00:54:42,000 --> 00:54:44,880 package json file and go ahead and 1396 00:54:44,880 --> 00:54:47,599 install the node modules you need and 1397 00:54:47,599 --> 00:54:49,280 that's important of course when you're 1398 00:54:49,280 --> 00:54:51,599 pulling down another repo say from 1399 00:54:51,599 --> 00:54:54,720 github maybe you cloned it and 1400 00:54:54,720 --> 00:54:56,240 then you want to install the node 1401 00:54:56,240 --> 00:54:58,240 modules that are used in that 1402 00:54:58,240 --> 00:55:00,960 application okay with the date fns 1403 00:55:00,960 --> 00:55:03,440 dependency let's go ahead and use it now 1404 00:55:03,440 --> 00:55:06,480 in our index js i'll get rid of this 1405 00:55:06,480 --> 00:55:09,280 console log statement that we have here 1406 00:55:09,280 --> 00:55:11,359 and at the top we want to go ahead and 1407 00:55:11,359 --> 00:55:13,440 import and we're going to destructure 1408 00:55:13,440 --> 00:55:16,079 because we'll just import format 1409 00:55:16,079 --> 00:55:18,880 and this is using common js again so we 1410 00:55:18,880 --> 00:55:21,680 use require and then we'll say date dash 1411 00:55:21,680 --> 00:55:23,280 fns 1412 00:55:23,280 --> 00:55:25,040 and now that we have that imported we 1413 00:55:25,040 --> 00:55:26,640 can use it in the statement so i'll just 1414 00:55:26,640 --> 00:55:29,040 put another console log here and i'm 1415 00:55:29,040 --> 00:55:31,520 going to paste in the format but then we 1416 00:55:31,520 --> 00:55:33,040 can go over it so we're calling the 1417 00:55:33,040 --> 00:55:35,520 format function from date fns and it 1418 00:55:35,520 --> 00:55:37,440 accepts a new date 1419 00:55:37,440 --> 00:55:39,280 and after it accepts a new date we have 1420 00:55:39,280 --> 00:55:41,599 to tell it how we want it to format it 1421 00:55:41,599 --> 00:55:43,520 and of course you have to read the docs 1422 00:55:43,520 --> 00:55:45,760 to know how to format it and i am 1423 00:55:45,760 --> 00:55:47,760 pulling in the year 1424 00:55:47,760 --> 00:55:49,680 the month 1425 00:55:49,680 --> 00:55:50,720 and 1426 00:55:50,720 --> 00:55:52,559 and i didn't mean to do that let's undo 1427 00:55:52,559 --> 00:55:55,280 that the month and the days 1428 00:55:55,280 --> 00:55:57,200 and then i'm putting a tab in and then 1429 00:55:57,200 --> 00:55:59,359 i'm putting in the hours minutes and 1430 00:55:59,359 --> 00:56:01,280 seconds and so this will be tab 1431 00:56:01,280 --> 00:56:03,440 delimited so we'll have two columns 1432 00:56:03,440 --> 00:56:05,920 we'll have the date and the time and we 1433 00:56:05,920 --> 00:56:10,000 could use this in a log format and that 1434 00:56:10,000 --> 00:56:12,400 will be the end goal eventually as we 1435 00:56:12,400 --> 00:56:14,079 learn about events but right now let's 1436 00:56:14,079 --> 00:56:16,720 just format this date and go ahead and 1437 00:56:16,720 --> 00:56:18,480 send it to the console 1438 00:56:18,480 --> 00:56:20,640 now remember we're not running nodemon 1439 00:56:20,640 --> 00:56:23,520 again yet so let's go ahead and add 1440 00:56:23,520 --> 00:56:24,799 nodemon 1441 00:56:24,799 --> 00:56:28,480 as a dev package to our project 1442 00:56:28,480 --> 00:56:31,440 okay back in package json we're going to 1443 00:56:31,440 --> 00:56:34,240 add a dev dependency now easily 1444 00:56:34,240 --> 00:56:36,880 i could run nodemon right here from the 1445 00:56:36,880 --> 00:56:39,200 terminal as a global dependency because 1446 00:56:39,200 --> 00:56:40,880 we'd already installed it but i just 1447 00:56:40,880 --> 00:56:43,119 want to use this as an example of 1448 00:56:43,119 --> 00:56:45,599 installing a dev dependency so here we 1449 00:56:45,599 --> 00:56:49,520 type npmi once again i'll type nodemon 1450 00:56:49,520 --> 00:56:51,440 but then i want to give the flag you 1451 00:56:51,440 --> 00:56:54,319 could do dash dash save 1452 00:56:54,319 --> 00:56:56,079 dash dev 1453 00:56:56,079 --> 00:56:58,400 but there is just a shorthand for that 1454 00:56:58,400 --> 00:57:00,960 and it's dash with a capital d and this 1455 00:57:00,960 --> 00:57:04,559 will save nodemon as a dev dependency so 1456 00:57:04,559 --> 00:57:06,160 i'll go ahead and press enter and let 1457 00:57:06,160 --> 00:57:09,200 that install all right now with nodemon 1458 00:57:09,200 --> 00:57:11,200 installed as a dev dependency you can 1459 00:57:11,200 --> 00:57:13,520 now see that in the package json as well 1460 00:57:13,520 --> 00:57:16,799 and it lists a dev dependency so let's 1461 00:57:16,799 --> 00:57:20,160 talk about scripts and how to run 1462 00:57:20,160 --> 00:57:22,400 our application using scripts because 1463 00:57:22,400 --> 00:57:24,480 that's what a server would use if we 1464 00:57:24,480 --> 00:57:26,559 were to host this somewhere and you 1465 00:57:26,559 --> 00:57:30,559 often see a start script a dev script 1466 00:57:30,559 --> 00:57:32,640 and a build script if you're familiar 1467 00:57:32,640 --> 00:57:34,640 with react or something similar you have 1468 00:57:34,640 --> 00:57:36,559 definitely seen some of those before so 1469 00:57:36,559 --> 00:57:41,040 let's go ahead and add a start script 1470 00:57:41,040 --> 00:57:43,200 and now in the start script 1471 00:57:43,200 --> 00:57:46,240 we need to just go ahead and call node 1472 00:57:46,240 --> 00:57:48,480 not nodemon but node 1473 00:57:48,480 --> 00:57:51,200 and we can say index right here because 1474 00:57:51,200 --> 00:57:53,599 that is what we want to run our project 1475 00:57:53,599 --> 00:57:54,400 with 1476 00:57:54,400 --> 00:57:56,240 but then let's go ahead and we don't 1477 00:57:56,240 --> 00:57:58,319 need a test script so let's change this 1478 00:57:58,319 --> 00:57:59,760 to dev 1479 00:57:59,760 --> 00:58:02,160 and i'll go ahead and replace 1480 00:58:02,160 --> 00:58:05,680 the test script now with nodemon 1481 00:58:05,680 --> 00:58:07,280 and we'll go ahead 1482 00:58:07,280 --> 00:58:09,839 and we can specify index here as well 1483 00:58:09,839 --> 00:58:12,640 but remember it defaults to index js 1484 00:58:12,640 --> 00:58:15,440 anyway so let's go ahead and save that 1485 00:58:15,440 --> 00:58:18,319 now if we're to start the project start 1486 00:58:18,319 --> 00:58:20,319 is one of the few words that works 1487 00:58:20,319 --> 00:58:22,160 without the word run so we could just 1488 00:58:22,160 --> 00:58:24,400 type npm start 1489 00:58:24,400 --> 00:58:26,240 and start our project but we don't need 1490 00:58:26,240 --> 00:58:28,400 to do that yet we want to use 1491 00:58:28,400 --> 00:58:31,200 dev so let's say npm 1492 00:58:31,200 --> 00:58:34,640 run dev and press enter and you can see 1493 00:58:34,640 --> 00:58:37,680 nodemon started and we've got the date 1494 00:58:37,680 --> 00:58:39,520 over here with the format i gave it 1495 00:58:39,520 --> 00:58:40,720 which is 1496 00:58:40,720 --> 00:58:43,520 year month and day and then there's a 1497 00:58:43,520 --> 00:58:46,000 tab spacing and then we have the time 1498 00:58:46,000 --> 00:58:47,760 and you can see we're using military 1499 00:58:47,760 --> 00:58:51,359 time which would be 0 through 23 hours 1500 00:58:51,359 --> 00:58:54,160 and then minutes and seconds now back in 1501 00:58:54,160 --> 00:58:56,160 the index if we were to make any other 1502 00:58:56,160 --> 00:58:58,480 changes nodemon will continue to watch 1503 00:58:58,480 --> 00:58:59,280 this 1504 00:58:59,280 --> 00:59:01,440 and go ahead and restart the server so 1505 00:59:01,440 --> 00:59:03,440 then if i just put another console log 1506 00:59:03,440 --> 00:59:04,480 and said 1507 00:59:04,480 --> 00:59:05,760 hello 1508 00:59:05,760 --> 00:59:08,240 save the file nodemon restarts and we 1509 00:59:08,240 --> 00:59:10,240 should get the new information in the 1510 00:59:10,240 --> 00:59:11,920 console as well and that's what we get 1511 00:59:11,920 --> 00:59:14,400 with the hello now i want to go ahead 1512 00:59:14,400 --> 00:59:16,480 and stop this again because we want to 1513 00:59:16,480 --> 00:59:19,359 add another dependency to our project 1514 00:59:19,359 --> 00:59:20,880 while you don't have to look at the 1515 00:59:20,880 --> 00:59:22,480 package json while you install 1516 00:59:22,480 --> 00:59:24,079 dependencies i'm going to go ahead and 1517 00:59:24,079 --> 00:59:25,920 do that so we can see the new dependency 1518 00:59:25,920 --> 00:59:28,960 added and here i'm going to type npm i 1519 00:59:28,960 --> 00:59:31,040 for install once again and this is a 1520 00:59:31,040 --> 00:59:33,520 production dependency so we don't need 1521 00:59:33,520 --> 00:59:35,680 to put a flag after it it'll go directly 1522 00:59:35,680 --> 00:59:37,680 to the dependencies and we're going to 1523 00:59:37,680 --> 00:59:42,000 install uuid which allows us to generate 1524 00:59:42,000 --> 00:59:44,960 ids that of course are different for 1525 00:59:44,960 --> 00:59:47,040 each entry so i'll go ahead and press 1526 00:59:47,040 --> 00:59:50,559 enter and now we can see uuid installed 1527 00:59:50,559 --> 00:59:52,960 here as a dependency in our package json 1528 00:59:52,960 --> 00:59:55,760 also so let's go back to index 1529 00:59:55,760 --> 00:59:58,640 and use this package as well so we need 1530 00:59:58,640 --> 01:00:00,480 to import it at the top 1531 01:00:00,480 --> 01:00:03,599 and here we'll say const and now 1532 01:00:03,599 --> 01:00:05,920 this is a little different now with es6 1533 01:00:05,920 --> 01:00:07,680 imports we would say 1534 01:00:07,680 --> 01:00:09,040 import 1535 01:00:09,040 --> 01:00:12,160 uuid as and give the name that we want 1536 01:00:12,160 --> 01:00:14,480 to give it now here we need to import a 1537 01:00:14,480 --> 01:00:16,799 version which is a specific version it's 1538 01:00:16,799 --> 01:00:18,240 v4 1539 01:00:18,240 --> 01:00:22,880 but we want to import that as uuid 1540 01:00:22,880 --> 01:00:26,240 and then we can say equals require 1541 01:00:26,240 --> 01:00:29,040 and then specify uuid here now you may 1542 01:00:29,040 --> 01:00:31,440 see this done in different ways as well 1543 01:00:31,440 --> 01:00:35,599 so instead of the v4 this way some could 1544 01:00:35,599 --> 01:00:38,559 just use it as v4 1545 01:00:38,559 --> 01:00:40,640 or you might see 1546 01:00:40,640 --> 01:00:45,760 an import and just here have uuid 1547 01:00:46,799 --> 01:00:48,559 and let me get rid of that curly brace 1548 01:00:48,559 --> 01:00:49,839 there 1549 01:00:49,839 --> 01:00:52,559 and then in the code they could possibly 1550 01:00:52,559 --> 01:00:55,760 use it as uuid dot 1551 01:00:55,760 --> 01:00:57,119 v4 1552 01:00:57,119 --> 01:00:59,680 and call it that way as well what we're 1553 01:00:59,680 --> 01:01:03,040 going to do is use it as uuid so back 1554 01:01:03,040 --> 01:01:05,520 here we want to make sure it is v4 so 1555 01:01:05,520 --> 01:01:08,799 i'm going to say import v4 as 1556 01:01:08,799 --> 01:01:10,480 uuid 1557 01:01:10,480 --> 01:01:12,559 and now we can use this and this will 1558 01:01:12,559 --> 01:01:13,520 log 1559 01:01:13,520 --> 01:01:15,440 a different 1560 01:01:15,440 --> 01:01:18,240 id or generate a different id to the 1561 01:01:18,240 --> 01:01:19,839 console and then of course our console 1562 01:01:19,839 --> 01:01:22,079 log statement is what will log that so 1563 01:01:22,079 --> 01:01:24,480 let's go ahead and save this and now 1564 01:01:24,480 --> 01:01:26,640 once again in the terminal i'm going to 1565 01:01:26,640 --> 01:01:30,799 type npm run dev to start nodemon 1566 01:01:30,799 --> 01:01:32,960 nodemon started up and you can see we've 1567 01:01:32,960 --> 01:01:35,920 got the format of the date with the date 1568 01:01:35,920 --> 01:01:37,760 and time and then on the next line the 1569 01:01:37,760 --> 01:01:39,839 second console statement we got the 1570 01:01:39,839 --> 01:01:43,359 result of calling uuid and if we just 1571 01:01:43,359 --> 01:01:46,079 make any change basically to the console 1572 01:01:46,079 --> 01:01:48,079 it's going to generate 1573 01:01:48,079 --> 01:01:49,280 another 1574 01:01:49,280 --> 01:01:51,440 uuid that's different than the one we 1575 01:01:51,440 --> 01:01:53,920 got before of course so you can see how 1576 01:01:53,920 --> 01:01:56,400 it generates a different id each time 1577 01:01:56,400 --> 01:01:59,119 and that can be very useful including 1578 01:01:59,119 --> 01:02:01,760 with something we would log such as an 1579 01:02:01,760 --> 01:02:04,480 event whether it's an error or a request 1580 01:02:04,480 --> 01:02:06,079 or anything like that that we might 1581 01:02:06,079 --> 01:02:07,680 write to a log file we might want to 1582 01:02:07,680 --> 01:02:10,400 give each entry its own id i want to 1583 01:02:10,400 --> 01:02:12,720 jump back to chrome really quickly just 1584 01:02:12,720 --> 01:02:14,559 to draw your attention to 1585 01:02:14,559 --> 01:02:16,319 how we can search for these packages 1586 01:02:16,319 --> 01:02:17,920 because you might be wondering how do i 1587 01:02:17,920 --> 01:02:19,599 know about these packages well you could 1588 01:02:19,599 --> 01:02:21,280 search for id 1589 01:02:21,280 --> 01:02:22,880 and see what would come up and remember 1590 01:02:22,880 --> 01:02:25,280 they're also ranked by popularity and 1591 01:02:25,280 --> 01:02:28,319 we've got nano id short id but we don't 1592 01:02:28,319 --> 01:02:30,960 see uuid but somewhere i learned about 1593 01:02:30,960 --> 01:02:32,799 it along the way and if you do search 1594 01:02:32,799 --> 01:02:36,240 for uuid it will show up here it is the 1595 01:02:36,240 --> 01:02:39,280 exact match popularity is huge quality 1596 01:02:39,280 --> 01:02:41,760 is good and so then when you go to the 1597 01:02:41,760 --> 01:02:44,319 page you can read about the details for 1598 01:02:44,319 --> 01:02:47,119 the package you want to use so here's 1599 01:02:47,119 --> 01:02:49,119 some good information about it it tells 1600 01:02:49,119 --> 01:02:50,720 you how to install it and of course it 1601 01:02:50,720 --> 01:02:52,640 quickly tells you how to install it here 1602 01:02:52,640 --> 01:02:55,119 shows you where the github repository is 1603 01:02:55,119 --> 01:02:58,240 weekly downloads that's a very popular 1604 01:02:58,240 --> 01:03:00,880 package right there so this is a good 1605 01:03:00,880 --> 01:03:02,960 example of course there's some docs as 1606 01:03:02,960 --> 01:03:04,880 well and you can look that up and of 1607 01:03:04,880 --> 01:03:06,480 course you can search again at the top 1608 01:03:06,480 --> 01:03:09,039 i'll look for date dash fns 1609 01:03:09,039 --> 01:03:11,119 many times a package will have its own 1610 01:03:11,119 --> 01:03:14,240 website as well that has documentation 1611 01:03:14,240 --> 01:03:16,640 and date fns is definitely one of those 1612 01:03:16,640 --> 01:03:18,480 that do of course 1613 01:03:18,480 --> 01:03:20,319 notice it changed nomadic people 1614 01:03:20,319 --> 01:03:23,520 migration at the top for npm 1615 01:03:23,520 --> 01:03:25,520 i always like the different uh 1616 01:03:25,520 --> 01:03:27,760 definitions it gives npm again node 1617 01:03:27,760 --> 01:03:30,000 package manager this says it's like 1618 01:03:30,000 --> 01:03:32,079 lowdash for dates lowdash is another 1619 01:03:32,079 --> 01:03:35,200 package you could use but this datefns 1620 01:03:35,200 --> 01:03:38,400 has its own github repository it also 1621 01:03:38,400 --> 01:03:40,400 has its own web page that i believe is 1622 01:03:40,400 --> 01:03:42,480 linked here somewhere if i scroll down 1623 01:03:42,480 --> 01:03:45,480 maybe we'll see that oh here it is see 1624 01:03:45,480 --> 01:03:48,400 datefns.org for the docs so there's all 1625 01:03:48,400 --> 01:03:50,400 kinds of information about each package 1626 01:03:50,400 --> 01:03:52,079 as you explore that i just wanted to 1627 01:03:52,079 --> 01:03:54,000 highlight that again as we work with 1628 01:03:54,000 --> 01:03:56,160 these and now let's take another look at 1629 01:03:56,160 --> 01:03:58,000 the package json because there is a 1630 01:03:58,000 --> 01:03:59,680 little more detail we need to look at 1631 01:03:59,680 --> 01:04:01,760 look at the packages we have installed 1632 01:04:01,760 --> 01:04:03,599 here under dependencies 1633 01:04:03,599 --> 01:04:05,680 or also under dev dependencies and you 1634 01:04:05,680 --> 01:04:08,240 can see we get the semantic versioning 1635 01:04:08,240 --> 01:04:09,280 numbers 1636 01:04:09,280 --> 01:04:11,520 now let's talk about these because this 1637 01:04:11,520 --> 01:04:15,200 first number means a major version the 1638 01:04:15,200 --> 01:04:18,160 second number means a minor version 1639 01:04:18,160 --> 01:04:20,160 and the third number 1640 01:04:20,160 --> 01:04:21,920 means a patch 1641 01:04:21,920 --> 01:04:24,400 now the carrot we see in front 1642 01:04:24,400 --> 01:04:27,680 means go ahead and allow an update to 1643 01:04:27,680 --> 01:04:30,240 the minor version and the patch if 1644 01:04:30,240 --> 01:04:33,200 needed but do not update a major version 1645 01:04:33,200 --> 01:04:35,359 a major version could have breaking 1646 01:04:35,359 --> 01:04:37,440 changes to your application so you don't 1647 01:04:37,440 --> 01:04:40,079 necessarily want to allow those you can 1648 01:04:40,079 --> 01:04:42,160 now if you don't put anything in front 1649 01:04:42,160 --> 01:04:43,280 of the number 1650 01:04:43,280 --> 01:04:45,760 this is saying specifically this version 1651 01:04:45,760 --> 01:04:47,839 and only this version for this project 1652 01:04:47,839 --> 01:04:50,319 will work so once again if you put the 1653 01:04:50,319 --> 01:04:52,880 carrot that is saying go ahead and 1654 01:04:52,880 --> 01:04:57,039 update minor versions or patches but not 1655 01:04:57,039 --> 01:04:58,640 a major version 1656 01:04:58,640 --> 01:05:01,440 so now if we have what's called a tilde 1657 01:05:01,440 --> 01:05:03,280 the little squiggly kind of let me put 1658 01:05:03,280 --> 01:05:04,240 it there 1659 01:05:04,240 --> 01:05:07,119 this is saying go ahead and update a 1660 01:05:07,119 --> 01:05:09,760 patch version but do not update a minor 1661 01:05:09,760 --> 01:05:12,000 version so if you see the tilde that 1662 01:05:12,000 --> 01:05:14,880 will allow patches only and not minor 1663 01:05:14,880 --> 01:05:17,119 versions or major versions 1664 01:05:17,119 --> 01:05:19,200 now if we got rid of this all together 1665 01:05:19,200 --> 01:05:20,880 let me go ahead and put the carrot back 1666 01:05:20,880 --> 01:05:22,480 because i'm going to want to 1667 01:05:22,480 --> 01:05:23,839 replace that 1668 01:05:23,839 --> 01:05:25,839 and you just saw an 1669 01:05:25,839 --> 01:05:27,760 asterisk in here 1670 01:05:27,760 --> 01:05:29,520 this would mean go ahead and update 1671 01:05:29,520 --> 01:05:31,839 everything all the time use the absolute 1672 01:05:31,839 --> 01:05:34,000 latest version every time 1673 01:05:34,000 --> 01:05:36,559 that is not too safe although you might 1674 01:05:36,559 --> 01:05:38,799 see it this is what you'll typically see 1675 01:05:38,799 --> 01:05:41,599 and what is installed by default to your 1676 01:05:41,599 --> 01:05:44,400 package json is to have the carrot here 1677 01:05:44,400 --> 01:05:46,000 and it's worth noting that when you 1678 01:05:46,000 --> 01:05:48,000 install if you specifically want to 1679 01:05:48,000 --> 01:05:50,880 install a different version or 1680 01:05:50,880 --> 01:05:52,480 any kind of version you can just have 1681 01:05:52,480 --> 01:05:54,240 npm install 1682 01:05:54,240 --> 01:05:56,960 and then say we would type uuid and then 1683 01:05:56,960 --> 01:05:58,720 put the at symbol 1684 01:05:58,720 --> 01:06:01,039 and after that say the specific version 1685 01:06:01,039 --> 01:06:02,839 you want if you wanted 1686 01:06:02,839 --> 01:06:04,799 8.3.1 1687 01:06:04,799 --> 01:06:07,119 then this would install that specific 1688 01:06:07,119 --> 01:06:09,680 version however if you just say 1689 01:06:09,680 --> 01:06:12,720 uuid you're going to probably get the 1690 01:06:12,720 --> 01:06:15,359 latest version when you install here but 1691 01:06:15,359 --> 01:06:17,599 then it's going to mark it to not update 1692 01:06:17,599 --> 01:06:20,160 any major changes because those could 1693 01:06:20,160 --> 01:06:22,000 once again cause 1694 01:06:22,000 --> 01:06:24,160 a break in your application so that 1695 01:06:24,160 --> 01:06:26,079 could be what would be considered a 1696 01:06:26,079 --> 01:06:28,240 breaking change i'm going to go ahead 1697 01:06:28,240 --> 01:06:31,119 and save the package json now and if 1698 01:06:31,119 --> 01:06:33,039 there were updates or if you wanted to 1699 01:06:33,039 --> 01:06:36,559 check for updates to say okay is uuid 1700 01:06:36,559 --> 01:06:38,559 does it have a new miner version or 1701 01:06:38,559 --> 01:06:39,920 something like that if we had it like 1702 01:06:39,920 --> 01:06:41,440 this remember it would not install the 1703 01:06:41,440 --> 01:06:43,200 major version but if there was a minor 1704 01:06:43,200 --> 01:06:46,400 version or a patch you could just type 1705 01:06:46,400 --> 01:06:47,599 npm 1706 01:06:47,599 --> 01:06:51,039 update and npm will check for any 1707 01:06:51,039 --> 01:06:52,079 updates 1708 01:06:52,079 --> 01:06:54,400 for your packages and you can see i had 1709 01:06:54,400 --> 01:06:56,400 no updates or it would say something 1710 01:06:56,400 --> 01:06:58,559 about that as well so there were no 1711 01:06:58,559 --> 01:07:00,400 updates there so the only thing that i 1712 01:07:00,400 --> 01:07:03,039 typically use or would might need to use 1713 01:07:03,039 --> 01:07:04,880 that i haven't shown you on a regular 1714 01:07:04,880 --> 01:07:07,920 basis would be uninstall so here we can 1715 01:07:07,920 --> 01:07:09,599 type npm 1716 01:07:09,599 --> 01:07:11,119 and then you could use 1717 01:07:11,119 --> 01:07:13,599 the full uninstall 1718 01:07:13,599 --> 01:07:17,440 or you can just use on or you can use rm 1719 01:07:17,440 --> 01:07:19,760 for remove so let's just go ahead and 1720 01:07:19,760 --> 01:07:22,480 use rm and then we're going to uninstall 1721 01:07:22,480 --> 01:07:25,280 nodemon but nodemon is a dev dependency 1722 01:07:25,280 --> 01:07:27,440 so you always need to add that specific 1723 01:07:27,440 --> 01:07:30,160 flag if it's a dev dependency or if we 1724 01:07:30,160 --> 01:07:32,400 wanted to install the global version i 1725 01:07:32,400 --> 01:07:34,559 would put the dash g as well now if it 1726 01:07:34,559 --> 01:07:36,559 was production you wouldn't have a flag 1727 01:07:36,559 --> 01:07:37,520 at all 1728 01:07:37,520 --> 01:07:39,520 so let's go ahead and uninstall the dev 1729 01:07:39,520 --> 01:07:41,359 dependency node mod 1730 01:07:41,359 --> 01:07:44,160 this is worth noting we did uninstall it 1731 01:07:44,160 --> 01:07:46,400 so there is no longer 1732 01:07:46,400 --> 01:07:48,319 a dev dependency listed it went ahead 1733 01:07:48,319 --> 01:07:50,079 and kept the dev dependencies in here 1734 01:07:50,079 --> 01:07:52,400 but it's just an empty object right now 1735 01:07:52,400 --> 01:07:55,839 however we still have nodemon listed in 1736 01:07:55,839 --> 01:07:57,359 our script it's not going to 1737 01:07:57,359 --> 01:08:00,160 automatically change a script for you so 1738 01:08:00,160 --> 01:08:01,920 that could be an issue if you want to 1739 01:08:01,920 --> 01:08:03,359 install something that you have in a 1740 01:08:03,359 --> 01:08:05,520 script you need to remember to go 1741 01:08:05,520 --> 01:08:07,440 and check for that as well the nodemon 1742 01:08:07,440 --> 01:08:09,280 is something i would probably have 1743 01:08:09,280 --> 01:08:11,119 installed as a dev dependency so we 1744 01:08:11,119 --> 01:08:13,760 could just re-add that but again worth 1745 01:08:13,760 --> 01:08:16,560 noting if you uninstall a package it 1746 01:08:16,560 --> 01:08:18,640 doesn't remove it from your scripts so 1747 01:08:18,640 --> 01:08:21,759 you need to check that as well 1748 01:08:21,759 --> 01:08:24,399 today we'll be learning about the events 1749 01:08:24,399 --> 01:08:26,719 common core module in node.js and more 1750 01:08:26,719 --> 01:08:29,600 specifically how to both emit custom 1751 01:08:29,600 --> 01:08:31,359 events and how to respond to those 1752 01:08:31,359 --> 01:08:33,439 events when they are admitted 1753 01:08:33,439 --> 01:08:35,120 right now we're picking up where we left 1754 01:08:35,120 --> 01:08:37,439 off in the last tutorial about npm 1755 01:08:37,439 --> 01:08:39,359 modules and if you didn't see that 1756 01:08:39,359 --> 01:08:41,600 tutorial you can just catch up by 1757 01:08:41,600 --> 01:08:43,439 installing the dependencies you see 1758 01:08:43,439 --> 01:08:45,040 right here so for production 1759 01:08:45,040 --> 01:08:48,319 dependencies we have the date fns module 1760 01:08:48,319 --> 01:08:51,120 and the uuid module you could also refer 1761 01:08:51,120 --> 01:08:53,520 to those as packages also as a dev 1762 01:08:53,520 --> 01:08:56,880 dependency we have nodemon installed and 1763 01:08:56,880 --> 01:08:59,759 then in the scripts of course the start 1764 01:08:59,759 --> 01:09:02,000 script is node index but what we would 1765 01:09:02,000 --> 01:09:05,040 use in development is the dev script and 1766 01:09:05,040 --> 01:09:08,719 that is nodemon index so if we type npm 1767 01:09:08,719 --> 01:09:11,359 run dev it will launch nodemill and 1768 01:09:11,359 --> 01:09:13,279 nodemon will listen for the changes in 1769 01:09:13,279 --> 01:09:15,198 our files and restart the server without 1770 01:09:15,198 --> 01:09:17,520 us constantly having to type that so 1771 01:09:17,520 --> 01:09:18,880 again if you didn't see the previous 1772 01:09:18,880 --> 01:09:22,238 tutorial just update your package json 1773 01:09:22,238 --> 01:09:22,960 and 1774 01:09:22,960 --> 01:09:26,158 npm installs to this and if you don't 1775 01:09:26,158 --> 01:09:27,920 know how to do that please do watch the 1776 01:09:27,920 --> 01:09:30,560 previous tutorial okay let's move over 1777 01:09:30,560 --> 01:09:33,279 to the index js and you can see we're 1778 01:09:33,279 --> 01:09:35,920 already importing format from the date 1779 01:09:35,920 --> 01:09:39,198 fns module and then we're also importing 1780 01:09:39,198 --> 01:09:41,520 uuid and it's actually 1781 01:09:41,520 --> 01:09:44,719 v4 there's different versions in the uid 1782 01:09:44,719 --> 01:09:47,359 so we're importing v4 as this is an 1783 01:09:47,359 --> 01:09:50,238 alias as uuid so we can call that right 1784 01:09:50,238 --> 01:09:52,000 here and you can test these out with 1785 01:09:52,000 --> 01:09:53,839 these console log statements that we had 1786 01:09:53,839 --> 01:09:55,760 from the previous tutorial but what i 1787 01:09:55,760 --> 01:09:58,080 want to do right now is rename this 1788 01:09:58,080 --> 01:10:00,719 index file so i'm going to right click 1789 01:10:00,719 --> 01:10:03,120 and choose rename and i'm going to name 1790 01:10:03,120 --> 01:10:03,920 this 1791 01:10:03,920 --> 01:10:04,880 log 1792 01:10:04,880 --> 01:10:07,840 events in camelcase.js so this is a 1793 01:10:07,840 --> 01:10:10,400 complete different file and i'm going to 1794 01:10:10,400 --> 01:10:13,679 make this into a module that we import 1795 01:10:13,679 --> 01:10:16,480 into an index.js that we haven't created 1796 01:10:16,480 --> 01:10:19,199 yet so we need both of these imports 1797 01:10:19,199 --> 01:10:21,120 but we're going to make a logging 1798 01:10:21,120 --> 01:10:23,120 function of course logging events is 1799 01:10:23,120 --> 01:10:25,280 something that's very useful on a server 1800 01:10:25,280 --> 01:10:28,320 let's go ahead and also import the fs 1801 01:10:28,320 --> 01:10:29,679 module so 1802 01:10:29,679 --> 01:10:31,360 we'll require 1803 01:10:31,360 --> 01:10:32,640 fs 1804 01:10:32,640 --> 01:10:34,000 and then we're going to want to use 1805 01:10:34,000 --> 01:10:35,280 promises 1806 01:10:35,280 --> 01:10:37,600 so let's say fs 1807 01:10:37,600 --> 01:10:38,960 promises 1808 01:10:38,960 --> 01:10:41,600 set this equal to 1809 01:10:41,600 --> 01:10:43,360 require 1810 01:10:43,360 --> 01:10:44,400 fs 1811 01:10:44,400 --> 01:10:45,760 and then dot 1812 01:10:45,760 --> 01:10:47,280 promises 1813 01:10:47,280 --> 01:10:51,040 and we also need the path module 1814 01:10:51,040 --> 01:10:53,760 so here is require 1815 01:10:53,760 --> 01:10:55,600 path 1816 01:10:55,600 --> 01:10:57,360 and now that we have all of the imports 1817 01:10:57,360 --> 01:10:59,440 we need and notice these are the only 1818 01:10:59,440 --> 01:11:02,880 two that we really needed to use npm for 1819 01:11:02,880 --> 01:11:04,640 because these are all common core 1820 01:11:04,640 --> 01:11:06,800 modules here that we imported 1821 01:11:06,800 --> 01:11:08,640 now that we have that 1822 01:11:08,640 --> 01:11:10,320 i'll get a couple of extra lines let's 1823 01:11:10,320 --> 01:11:13,120 go ahead and define our log events 1824 01:11:13,120 --> 01:11:16,320 function that we can export 1825 01:11:16,320 --> 01:11:19,199 and it's going to be an async function 1826 01:11:19,199 --> 01:11:23,199 and it will receive a message parameter 1827 01:11:23,199 --> 01:11:25,199 now inside the function 1828 01:11:25,199 --> 01:11:27,840 we need to define we'll call it date 1829 01:11:27,840 --> 01:11:30,960 time and let's set this equal to 1830 01:11:30,960 --> 01:11:33,199 and let's just grab this format that we 1831 01:11:33,199 --> 01:11:35,679 have right here and copy this 1832 01:11:35,679 --> 01:11:38,239 because that's what we essentially need 1833 01:11:38,239 --> 01:11:41,679 i'll use a template literal 1834 01:11:41,679 --> 01:11:44,320 and inside here i'm going to paste 1835 01:11:44,320 --> 01:11:48,080 the format the new date it's got the tab 1836 01:11:48,080 --> 01:11:50,239 and then it will just in the template 1837 01:11:50,239 --> 01:11:51,520 literal 1838 01:11:51,520 --> 01:11:53,760 and leave it at that and then we're 1839 01:11:53,760 --> 01:11:56,640 going to take another definition here 1840 01:11:56,640 --> 01:11:58,239 and call it log 1841 01:11:58,239 --> 01:11:59,840 item 1842 01:11:59,840 --> 01:12:02,320 and we'll really use the date time so 1843 01:12:02,320 --> 01:12:04,960 i'll put another template literal 1844 01:12:04,960 --> 01:12:06,400 and we could have done this all in one 1845 01:12:06,400 --> 01:12:08,239 line but this gets kind of long if we do 1846 01:12:08,239 --> 01:12:10,159 that so i wanted to break it up 1847 01:12:10,159 --> 01:12:12,080 and so we've got the date time then i'm 1848 01:12:12,080 --> 01:12:13,920 going to put another tab and remember 1849 01:12:13,920 --> 01:12:16,000 there's a tab in here too so what we're 1850 01:12:16,000 --> 01:12:18,880 doing is creating a tab delimited log 1851 01:12:18,880 --> 01:12:19,840 file 1852 01:12:19,840 --> 01:12:23,360 and now i'm going to use uuid 1853 01:12:23,360 --> 01:12:26,400 and call that to get a unique id for 1854 01:12:26,400 --> 01:12:27,920 each log event 1855 01:12:27,920 --> 01:12:30,480 and then one more tab 1856 01:12:30,480 --> 01:12:32,719 and we'll just put in 1857 01:12:32,719 --> 01:12:35,120 the message 1858 01:12:35,120 --> 01:12:37,440 there we go and now that we have our log 1859 01:12:37,440 --> 01:12:39,600 item let's go ahead and log that to the 1860 01:12:39,600 --> 01:12:41,760 console so we can see it while we're in 1861 01:12:41,760 --> 01:12:43,840 development mode here as well 1862 01:12:43,840 --> 01:12:45,760 and now we need a try catch block let's 1863 01:12:45,760 --> 01:12:48,840 get to the async await portion of this 1864 01:12:48,840 --> 01:12:50,800 function 1865 01:12:50,800 --> 01:12:52,480 and we'll catch an error if an error 1866 01:12:52,480 --> 01:12:55,199 happens and let's just 1867 01:12:55,199 --> 01:12:57,520 log the error to the console if we have 1868 01:12:57,520 --> 01:12:59,360 an error writing to the log we won't be 1869 01:12:59,360 --> 01:13:01,679 able to write the error to the log so 1870 01:13:01,679 --> 01:13:03,440 this is the best thing we could probably 1871 01:13:03,440 --> 01:13:05,040 do right there 1872 01:13:05,040 --> 01:13:07,440 but in the try block 1873 01:13:07,440 --> 01:13:08,719 we can 1874 01:13:08,719 --> 01:13:11,840 await fs 1875 01:13:11,840 --> 01:13:13,600 promises 1876 01:13:13,600 --> 01:13:15,120 and then 1877 01:13:15,120 --> 01:13:17,600 let's go ahead and append file if you 1878 01:13:17,600 --> 01:13:20,080 remember from our tutorial on working 1879 01:13:20,080 --> 01:13:22,640 with files a pin file will also create a 1880 01:13:22,640 --> 01:13:25,360 file if it doesn't exist and now we need 1881 01:13:25,360 --> 01:13:28,800 to use path and we'll use path join 1882 01:13:28,800 --> 01:13:31,199 and two underscores and then we'll use 1883 01:13:31,199 --> 01:13:33,199 the directory name value and then let's 1884 01:13:33,199 --> 01:13:36,080 put it in a logs folder and after that 1885 01:13:36,080 --> 01:13:39,280 let's call this event log 1886 01:13:39,280 --> 01:13:40,640 dot text 1887 01:13:40,640 --> 01:13:43,199 and then we still need to pass in the 1888 01:13:43,199 --> 01:13:44,640 content that we're going to put in the 1889 01:13:44,640 --> 01:13:46,560 log right after that parentheses we need 1890 01:13:46,560 --> 01:13:49,360 the comma and that would be our log item 1891 01:13:49,360 --> 01:13:51,760 that we defined okay now we can save 1892 01:13:51,760 --> 01:13:53,040 this much 1893 01:13:53,040 --> 01:13:55,199 now i am thinking of something that 1894 01:13:55,199 --> 01:13:56,719 could cause an error in the future and 1895 01:13:56,719 --> 01:13:58,159 we'll get rid of these console log 1896 01:13:58,159 --> 01:13:59,920 statements down here but i want to leave 1897 01:13:59,920 --> 01:14:01,760 it like this for now so we can actually 1898 01:14:01,760 --> 01:14:03,760 test out getting the error as well or 1899 01:14:03,760 --> 01:14:05,120 catching the air 1900 01:14:05,120 --> 01:14:08,239 so let's do module.exports 1901 01:14:08,239 --> 01:14:10,640 and set this equal to 1902 01:14:10,640 --> 01:14:12,400 log events 1903 01:14:12,400 --> 01:14:14,719 now we're exporting our log events 1904 01:14:14,719 --> 01:14:16,719 function and we'll be able to use it in 1905 01:14:16,719 --> 01:14:18,880 the index that we're going to create so 1906 01:14:18,880 --> 01:14:20,719 over in the file tree we'll create a new 1907 01:14:20,719 --> 01:14:23,199 file called index js 1908 01:14:23,199 --> 01:14:26,800 from here let's go ahead and define 1909 01:14:26,800 --> 01:14:29,520 log events and set it equal to 1910 01:14:29,520 --> 01:14:30,960 require 1911 01:14:30,960 --> 01:14:33,520 and now dot slash because this is once 1912 01:14:33,520 --> 01:14:35,840 again a custom module not a common core 1913 01:14:35,840 --> 01:14:39,600 module or an npm module and so dot slash 1914 01:14:39,600 --> 01:14:42,400 log events and now we're ready to work 1915 01:14:42,400 --> 01:14:45,600 with the events common core module and 1916 01:14:45,600 --> 01:14:48,880 what we want to do is define an event 1917 01:14:48,880 --> 01:14:52,239 emitter and let's set this equal to 1918 01:14:52,239 --> 01:14:56,080 events the common core module events 1919 01:14:56,080 --> 01:14:58,800 after that we need to define a class 1920 01:14:58,800 --> 01:15:01,920 so class my emitter 1921 01:15:01,920 --> 01:15:04,000 and it's standard to capitalize instead 1922 01:15:04,000 --> 01:15:06,239 of use lowercase on the first my there 1923 01:15:06,239 --> 01:15:07,679 for a class 1924 01:15:07,679 --> 01:15:10,960 and then we want to say extends 1925 01:15:10,960 --> 01:15:13,280 event emitter 1926 01:15:13,280 --> 01:15:15,679 and then we can just have the empty 1927 01:15:15,679 --> 01:15:17,920 curly braces right here this is i know 1928 01:15:17,920 --> 01:15:19,840 it looks strange but it's directly from 1929 01:15:19,840 --> 01:15:21,520 the docs as well 1930 01:15:21,520 --> 01:15:23,199 from here we can 1931 01:15:23,199 --> 01:15:25,520 initialize 1932 01:15:25,520 --> 01:15:27,600 the object that we're going to create so 1933 01:15:27,600 --> 01:15:31,440 we want to create my emitter but now 1934 01:15:31,440 --> 01:15:32,400 oops 1935 01:15:32,400 --> 01:15:34,960 this needs to be a lowercase m to start 1936 01:15:34,960 --> 01:15:37,600 out so don't let it change on you or or 1937 01:15:37,600 --> 01:15:40,000 select the option that would select the 1938 01:15:40,000 --> 01:15:41,679 previous my emitter 1939 01:15:41,679 --> 01:15:43,199 and now that we've got my emitter 1940 01:15:43,199 --> 01:15:45,760 defined let's set it equal to a new 1941 01:15:45,760 --> 01:15:48,080 object that is 1942 01:15:48,080 --> 01:15:49,760 my emitter right here so we've 1943 01:15:49,760 --> 01:15:51,679 initialized the object i know that's a 1944 01:15:51,679 --> 01:15:53,280 little confusing 1945 01:15:53,280 --> 01:15:54,640 but that's what it looks like in the 1946 01:15:54,640 --> 01:15:56,640 dock so i just stuck with that 1947 01:15:56,640 --> 01:15:59,040 and then we've got 1948 01:15:59,040 --> 01:16:00,480 add a listener 1949 01:16:00,480 --> 01:16:02,080 before 1950 01:16:02,080 --> 01:16:04,159 the log event 1951 01:16:04,159 --> 01:16:07,840 so then we can say my emitter 1952 01:16:08,000 --> 01:16:10,640 dot on and that is how we listen for an 1953 01:16:10,640 --> 01:16:12,400 event and i'm just going to call this a 1954 01:16:12,400 --> 01:16:13,840 log event 1955 01:16:13,840 --> 01:16:15,440 we could be listening for any event we 1956 01:16:15,440 --> 01:16:17,360 want to this is the first parameter of 1957 01:16:17,360 --> 01:16:18,960 listening for an event so we just say 1958 01:16:18,960 --> 01:16:21,440 which event we're listening for 1959 01:16:21,440 --> 01:16:24,080 and then we can call this anonymous 1960 01:16:24,080 --> 01:16:26,159 function to pass in the parameter 1961 01:16:26,159 --> 01:16:27,360 message 1962 01:16:27,360 --> 01:16:30,719 and now inside this anonymous function 1963 01:16:30,719 --> 01:16:32,719 we'll call log events 1964 01:16:32,719 --> 01:16:35,040 and once again send that message that 1965 01:16:35,040 --> 01:16:36,400 was passed in 1966 01:16:36,400 --> 01:16:38,880 so now that we are listening for the log 1967 01:16:38,880 --> 01:16:40,000 event 1968 01:16:40,000 --> 01:16:42,320 we need to go ahead and omit the event 1969 01:16:42,320 --> 01:16:44,880 to test this out now i want to set a 1970 01:16:44,880 --> 01:16:46,719 timeout 1971 01:16:46,719 --> 01:16:48,800 which will hopefully 1972 01:16:48,800 --> 01:16:50,880 let us understand how everything is 1973 01:16:50,880 --> 01:16:52,560 processed a little bit better but we're 1974 01:16:52,560 --> 01:16:53,679 just going to 1975 01:16:53,679 --> 01:16:55,280 emit the event 1976 01:16:55,280 --> 01:16:56,880 you don't have to have a timeout to do 1977 01:16:56,880 --> 01:16:58,560 this i just wanted to put a little delay 1978 01:16:58,560 --> 01:17:01,920 in there so once again we use my emitter 1979 01:17:01,920 --> 01:17:05,440 dot omit now not on on is listening so 1980 01:17:05,440 --> 01:17:07,360 emit is emitting the event now we'll 1981 01:17:07,360 --> 01:17:09,679 emit the log event 1982 01:17:09,679 --> 01:17:11,440 and then let's go ahead and send our 1983 01:17:11,440 --> 01:17:15,600 message i'll just say log event 1984 01:17:15,600 --> 01:17:18,080 emitted 1985 01:17:18,719 --> 01:17:20,480 that looks good now let's put a delay in 1986 01:17:20,480 --> 01:17:22,560 here with the timeout and i'll put 1987 01:17:22,560 --> 01:17:25,040 two seconds that may be too long it may 1988 01:17:25,040 --> 01:17:27,040 not be long enough to see the difference 1989 01:17:27,040 --> 01:17:28,960 just depends how long it takes nodemon 1990 01:17:28,960 --> 01:17:30,960 to restart the server 1991 01:17:30,960 --> 01:17:33,280 so now that we've got that saved and 1992 01:17:33,280 --> 01:17:35,280 this is our index let's go ahead and 1993 01:17:35,280 --> 01:17:36,960 open a terminal you can do that from the 1994 01:17:36,960 --> 01:17:39,920 terminal menu or i do control backtick 1995 01:17:39,920 --> 01:17:41,440 i'm in windows 1996 01:17:41,440 --> 01:17:44,800 and now i'm going to type npm run dev to 1997 01:17:44,800 --> 01:17:48,320 launch nodemon and our index js 1998 01:17:48,320 --> 01:17:50,800 and we get an error so let's see what we 1999 01:17:50,800 --> 01:17:54,480 got i did expect to get one 2000 01:17:54,480 --> 01:17:56,719 notice we did log to the console so 2001 01:17:56,719 --> 01:17:59,120 here's what we would have written 2002 01:17:59,120 --> 01:18:00,960 to a log file 2003 01:18:00,960 --> 01:18:03,360 we've got the date we've got the time 2004 01:18:03,360 --> 01:18:05,280 we've got the unique id and we've got 2005 01:18:05,280 --> 01:18:07,920 our message log event admitted so that 2006 01:18:07,920 --> 01:18:10,560 all worked out as expected but we have 2007 01:18:10,560 --> 01:18:13,600 an error and let's see what it says here 2008 01:18:13,600 --> 01:18:16,800 console error is not a function well i 2009 01:18:16,800 --> 01:18:18,560 should have typed console log here let 2010 01:18:18,560 --> 01:18:20,480 me go back and fix that 2011 01:18:20,480 --> 01:18:22,320 we definitely have another error that i 2012 01:18:22,320 --> 01:18:24,719 was expecting to see so let's drop this 2013 01:18:24,719 --> 01:18:26,640 back down and of course nodemon should 2014 01:18:26,640 --> 01:18:29,199 restart after i change this so instead 2015 01:18:29,199 --> 01:18:33,280 of console error let's put console log 2016 01:18:34,480 --> 01:18:36,239 we might have to restart nodemon let's 2017 01:18:36,239 --> 01:18:39,440 see yep now it restarted and we can see 2018 01:18:39,440 --> 01:18:41,520 the message that i was expecting to get 2019 01:18:41,520 --> 01:18:44,480 so here once again we got the log file 2020 01:18:44,480 --> 01:18:46,960 and then we got no such file or 2021 01:18:46,960 --> 01:18:48,320 directory 2022 01:18:48,320 --> 01:18:51,199 and that is because append file will 2023 01:18:51,199 --> 01:18:53,040 create the file if it doesn't exist but 2024 01:18:53,040 --> 01:18:55,120 it won't create the directory and we 2025 01:18:55,120 --> 01:18:57,760 didn't have a logs directory yet so we 2026 01:18:57,760 --> 01:19:00,320 need to modify our function because if 2027 01:19:00,320 --> 01:19:02,640 you think about this when you install 2028 01:19:02,640 --> 01:19:04,239 some software 2029 01:19:04,239 --> 01:19:06,480 there may not be all of the directories 2030 01:19:06,480 --> 01:19:08,800 created that you need so let's modify 2031 01:19:08,800 --> 01:19:12,880 this try block to account for not having 2032 01:19:12,880 --> 01:19:15,520 that logs directory to begin with if you 2033 01:19:15,520 --> 01:19:18,480 notice we haven't used the fs module yet 2034 01:19:18,480 --> 01:19:21,360 we had used fs promises so in the try 2035 01:19:21,360 --> 01:19:23,280 block let's say if 2036 01:19:23,280 --> 01:19:24,880 and then we'll say 2037 01:19:24,880 --> 01:19:27,520 not if it not exist is what we're going 2038 01:19:27,520 --> 01:19:30,000 for here so fs dot 2039 01:19:30,000 --> 01:19:31,360 exists 2040 01:19:31,360 --> 01:19:32,640 sync 2041 01:19:32,640 --> 01:19:34,880 and then we want 2042 01:19:34,880 --> 01:19:36,960 path path.join 2043 01:19:36,960 --> 01:19:39,199 two underscores and directory name and 2044 01:19:39,199 --> 01:19:40,400 then we're just looking for the 2045 01:19:40,400 --> 01:19:44,239 directory so if this does not exist 2046 01:19:44,239 --> 01:19:46,400 essentially is what we're saying 2047 01:19:46,400 --> 01:19:48,080 then as you might guess we want to 2048 01:19:48,080 --> 01:19:50,480 create that so we'll say await and now 2049 01:19:50,480 --> 01:19:53,120 use fs promises 2050 01:19:53,120 --> 01:19:55,239 oh and then we need dot 2051 01:19:55,239 --> 01:19:58,080 mkdir which is make directory 2052 01:19:58,080 --> 01:20:00,480 and we can say path.join 2053 01:20:00,480 --> 01:20:02,159 once again 2054 01:20:02,159 --> 01:20:04,000 two underscores in the directory name 2055 01:20:04,000 --> 01:20:05,440 and create 2056 01:20:05,440 --> 01:20:08,960 the logs directory please so now 2057 01:20:08,960 --> 01:20:11,679 if the logs directory does not exist it 2058 01:20:11,679 --> 01:20:14,880 will create the directory and then it 2059 01:20:14,880 --> 01:20:18,320 will either create or append to the 2060 01:20:18,320 --> 01:20:22,320 event log file so let's make this change 2061 01:20:22,320 --> 01:20:25,280 and now if we look back 2062 01:20:25,280 --> 01:20:27,679 we can see we got no error on this 2063 01:20:27,679 --> 01:20:30,239 latest execution once again everything 2064 01:20:30,239 --> 01:20:33,040 wrote to the console and now i'll bring 2065 01:20:33,040 --> 01:20:34,719 this down 2066 01:20:34,719 --> 01:20:36,960 and if we look over here we have a logs 2067 01:20:36,960 --> 01:20:38,880 folder 2068 01:20:38,880 --> 01:20:40,719 and we have an event log 2069 01:20:40,719 --> 01:20:42,800 and there it is we wrote to the event 2070 01:20:42,800 --> 01:20:44,159 log 2071 01:20:44,159 --> 01:20:47,360 now let's go ahead and run 2072 01:20:47,360 --> 01:20:49,440 the program again we'll need to make a 2073 01:20:49,440 --> 01:20:51,760 change of some sort to do that 2074 01:20:51,760 --> 01:20:54,639 and then we can check the event log so 2075 01:20:54,639 --> 01:20:58,480 let's just make a quick change here by 2076 01:20:58,480 --> 01:20:59,960 putting in a 2077 01:20:59,960 --> 01:21:02,320 console.log or no you know what we don't 2078 01:21:02,320 --> 01:21:03,600 need to do anything like that we could 2079 01:21:03,600 --> 01:21:05,760 even just put in a comment 2080 01:21:05,760 --> 01:21:07,920 say testing 2081 01:21:07,920 --> 01:21:09,520 save 2082 01:21:09,520 --> 01:21:10,800 and we should be able to see some 2083 01:21:10,800 --> 01:21:12,560 changes once again if we go back to the 2084 01:21:12,560 --> 01:21:16,000 event log oh we've got another problem 2085 01:21:16,000 --> 01:21:17,520 after our message 2086 01:21:17,520 --> 01:21:19,520 we didn't put a line break 2087 01:21:19,520 --> 01:21:24,080 and so then it just wrote the next log 2088 01:21:24,080 --> 01:21:26,480 on the same line so we need to put in a 2089 01:21:26,480 --> 01:21:28,159 line break after that as well so let's 2090 01:21:28,159 --> 01:21:31,040 go to our log events 2091 01:21:31,040 --> 01:21:33,600 and where we define our log item at the 2092 01:21:33,600 --> 01:21:35,280 end of message then 2093 01:21:35,280 --> 01:21:39,520 let's put a line break with a slash in 2094 01:21:39,520 --> 01:21:42,080 and now this should make a difference 2095 01:21:42,080 --> 01:21:45,600 let's check the event log again 2096 01:21:48,400 --> 01:21:50,719 and maybe it made a difference 2097 01:21:50,719 --> 01:21:52,320 i'll tell you what let's try this i'll 2098 01:21:52,320 --> 01:21:54,480 just delete this all together 2099 01:21:54,480 --> 01:21:57,199 now the file doesn't exist 2100 01:21:57,199 --> 01:22:00,639 i'll get rid of the testing 2101 01:22:00,800 --> 01:22:03,199 save 2102 01:22:04,719 --> 01:22:06,480 write the file let's look at the file 2103 01:22:06,480 --> 01:22:07,840 there it is 2104 01:22:07,840 --> 01:22:08,960 now 2105 01:22:08,960 --> 01:22:12,239 back here once again i will undo 2106 01:22:12,239 --> 01:22:15,280 put the testing back and save 2107 01:22:15,280 --> 01:22:17,360 and let's look at the event log there it 2108 01:22:17,360 --> 01:22:20,400 is we've got another log event emitted 2109 01:22:20,400 --> 01:22:22,639 on a new line and everything is lining 2110 01:22:22,639 --> 01:22:24,480 up as we expect 2111 01:22:24,480 --> 01:22:28,239 delete testing again and save 2112 01:22:28,880 --> 01:22:30,880 and there it wrote a third line to the 2113 01:22:30,880 --> 01:22:33,760 log so this is working as expected so 2114 01:22:33,760 --> 01:22:36,400 here is a abstracted 2115 01:22:36,400 --> 01:22:38,800 log events function that you could use 2116 01:22:38,800 --> 01:22:41,520 you could accept more than one uh 2117 01:22:41,520 --> 01:22:43,199 parameter as well 2118 01:22:43,199 --> 01:22:45,920 and if there was a second parameter here 2119 01:22:45,920 --> 01:22:47,920 when we would be in 2120 01:22:47,920 --> 01:22:50,719 the index.js when we admit this we would 2121 01:22:50,719 --> 01:22:52,880 just need to put the next parameter 2122 01:22:52,880 --> 01:22:55,520 right here 2123 01:22:55,520 --> 01:22:57,760 so there can be more than one parameter 2124 01:22:57,760 --> 01:23:00,080 as well and this is how 2125 01:23:00,080 --> 01:23:03,840 you set up an emitter to not only listen 2126 01:23:03,840 --> 01:23:04,639 for 2127 01:23:04,639 --> 01:23:06,800 but to emit events now there might be 2128 01:23:06,800 --> 01:23:08,560 all sorts of actions that you want to 2129 01:23:08,560 --> 01:23:11,600 emit events for when we create a web 2130 01:23:11,600 --> 01:23:14,080 server we're wanting to emit events to 2131 01:23:14,080 --> 01:23:16,320 show what requests came in 2132 01:23:16,320 --> 01:23:18,800 and log all of those so we have some 2133 01:23:18,800 --> 01:23:21,040 detail of the activity for our web 2134 01:23:21,040 --> 01:23:23,440 server and that's what we'll be building 2135 01:23:23,440 --> 01:23:27,760 next a web server with node.js 2136 01:23:27,760 --> 01:23:30,080 today we will be building a web server 2137 01:23:30,080 --> 01:23:32,639 with node.js no framework will be 2138 01:23:32,639 --> 01:23:34,719 utilized and this will help us learn 2139 01:23:34,719 --> 01:23:37,679 more foundational knowledge about nodejs 2140 01:23:37,679 --> 01:23:39,679 and then in future tutorials i will 2141 01:23:39,679 --> 01:23:42,080 introduce the express js framework we're 2142 01:23:42,080 --> 01:23:44,080 picking up right where we left off from 2143 01:23:44,080 --> 01:23:46,159 the last tutorial about the nodejs 2144 01:23:46,159 --> 01:23:48,239 events module so let's go over some 2145 01:23:48,239 --> 01:23:50,719 quick changes and file additions and i 2146 01:23:50,719 --> 01:23:52,560 will be providing a link to a github 2147 01:23:52,560 --> 01:23:55,360 repository in the description below okay 2148 01:23:55,360 --> 01:23:58,159 in the package json you can see i have 2149 01:23:58,159 --> 01:24:02,320 changed the name to o5 tut for the fifth 2150 01:24:02,320 --> 01:24:04,159 tutorial in this series you can change 2151 01:24:04,159 --> 01:24:06,000 that whatever you want 2152 01:24:06,000 --> 01:24:07,920 now i've also got 2153 01:24:07,920 --> 01:24:10,960 server.js instead of index.js as the 2154 01:24:10,960 --> 01:24:13,840 main file and i have changed the scripts 2155 01:24:13,840 --> 01:24:16,560 accordingly so it says node server and 2156 01:24:16,560 --> 01:24:20,159 nodemon server instead of node index and 2157 01:24:20,159 --> 01:24:22,800 nodemon index other than that we still 2158 01:24:22,800 --> 01:24:25,199 have the same dependencies as the last 2159 01:24:25,199 --> 01:24:29,360 tutorial so date fns and uuid are here 2160 01:24:29,360 --> 01:24:31,920 and the dev dependency of nodemon is 2161 01:24:31,920 --> 01:24:35,040 here and those are the few changes for 2162 01:24:35,040 --> 01:24:37,679 the package json of course i have 2163 01:24:37,679 --> 01:24:41,920 renamed the index file to server.js you 2164 01:24:41,920 --> 01:24:44,239 can see everything else is currently the 2165 01:24:44,239 --> 01:24:45,520 same here 2166 01:24:45,520 --> 01:24:48,159 in the server.js file that was 2167 01:24:48,159 --> 01:24:50,960 previously the index.js file and we will 2168 01:24:50,960 --> 01:24:54,159 come back to that i want to delete the 2169 01:24:54,159 --> 01:24:56,880 event log file that is currently in the 2170 01:24:56,880 --> 01:24:58,880 logs folder you can leave the logs 2171 01:24:58,880 --> 01:25:01,120 folder if you want to if not it will be 2172 01:25:01,120 --> 01:25:03,600 recreated and that's because we still 2173 01:25:03,600 --> 01:25:06,400 have the same log events js from the 2174 01:25:06,400 --> 01:25:08,960 last tutorial here as well 2175 01:25:08,960 --> 01:25:11,360 and we might make a change to that but i 2176 01:25:11,360 --> 01:25:13,120 won't do that now either we'll come back 2177 01:25:13,120 --> 01:25:15,280 to the whole events topic 2178 01:25:15,280 --> 01:25:17,760 what i do want to show you are the other 2179 01:25:17,760 --> 01:25:19,679 folders and files that i've added and 2180 01:25:19,679 --> 01:25:22,239 these are simply to be served by the 2181 01:25:22,239 --> 01:25:24,080 server so we can make sure it's working 2182 01:25:24,080 --> 01:25:27,920 i've got a style dot css file 2183 01:25:27,920 --> 01:25:30,960 in the data folder i've got a data.json 2184 01:25:30,960 --> 01:25:32,719 and a data.txt 2185 01:25:32,719 --> 01:25:34,639 and you can just put in whatever 2186 01:25:34,639 --> 01:25:36,639 information you really want to and style 2187 01:25:36,639 --> 01:25:39,280 the page or web pages however you want 2188 01:25:39,280 --> 01:25:42,320 to i've got one image in here again you 2189 01:25:42,320 --> 01:25:44,000 can download the repository and get 2190 01:25:44,000 --> 01:25:45,840 exactly what i have or you can use your 2191 01:25:45,840 --> 01:25:48,400 own and then in the views folder i've 2192 01:25:48,400 --> 01:25:51,040 got a 404.html 2193 01:25:51,040 --> 01:25:56,000 an index.html and a new dash page.html 2194 01:25:56,000 --> 01:25:58,719 and i also have a subdirectory named 2195 01:25:58,719 --> 01:26:00,000 subdir 2196 01:26:00,000 --> 01:26:02,679 and in this subdirectory is another 2197 01:26:02,679 --> 01:26:05,199 index.html that will be the default page 2198 01:26:05,199 --> 01:26:08,239 for this subdirectory route and so we're 2199 01:26:08,239 --> 01:26:10,800 going to serve all these different file 2200 01:26:10,800 --> 01:26:13,520 types or content types from the server 2201 01:26:13,520 --> 01:26:16,159 today now let's make some changes and 2202 01:26:16,159 --> 01:26:19,120 additions to the server.js file i'm 2203 01:26:19,120 --> 01:26:21,040 going to eliminate some of these blank 2204 01:26:21,040 --> 01:26:24,239 lines and then when we created a class 2205 01:26:24,239 --> 01:26:26,320 my emitter that extended the event 2206 01:26:26,320 --> 01:26:28,719 emitter in the last tutorial it got kind 2207 01:26:28,719 --> 01:26:31,040 of weird when we initialized our object 2208 01:26:31,040 --> 01:26:33,600 and it was also named my emitter so i 2209 01:26:33,600 --> 01:26:36,000 just want to name the class emitter and 2210 01:26:36,000 --> 01:26:37,760 this is just kind of for my peace of 2211 01:26:37,760 --> 01:26:40,080 mind after that we can get rid of that 2212 01:26:40,080 --> 01:26:42,400 extra space too we will leave all of 2213 01:26:42,400 --> 01:26:44,560 this here i'm going to get rid of the 2214 01:26:44,560 --> 01:26:45,840 comments 2215 01:26:45,840 --> 01:26:48,560 about the listener i'll also get rid of 2216 01:26:48,560 --> 01:26:50,639 the timeout that we added because that 2217 01:26:50,639 --> 01:26:52,480 will not be needed and i'm going to 2218 01:26:52,480 --> 01:26:54,000 comment out 2219 01:26:54,000 --> 01:26:56,000 both of these right now 2220 01:26:56,000 --> 01:26:58,239 and then we'll come back to those later 2221 01:26:58,239 --> 01:27:00,080 on when we need them 2222 01:27:00,080 --> 01:27:02,239 give a little space in between now let's 2223 01:27:02,239 --> 01:27:04,560 go ahead and import the common core 2224 01:27:04,560 --> 01:27:06,639 modules that we'll need for this file at 2225 01:27:06,639 --> 01:27:08,960 the top and the first one we need is 2226 01:27:08,960 --> 01:27:10,639 http 2227 01:27:10,639 --> 01:27:12,639 and so that's one we haven't covered yet 2228 01:27:12,639 --> 01:27:14,400 that we will today 2229 01:27:14,400 --> 01:27:17,360 and we just require http 2230 01:27:17,360 --> 01:27:19,679 after that we need path 2231 01:27:19,679 --> 01:27:23,040 and we'll require path 2232 01:27:23,040 --> 01:27:24,880 and we'll also need 2233 01:27:24,880 --> 01:27:28,320 fs for the file system so we'll require 2234 01:27:28,320 --> 01:27:29,199 that 2235 01:27:29,199 --> 01:27:31,360 expo require 2236 01:27:31,360 --> 01:27:35,760 and we're also going to need fs 2237 01:27:35,760 --> 01:27:38,320 promises so that equals 2238 01:27:38,320 --> 01:27:39,440 require 2239 01:27:39,440 --> 01:27:42,239 fs but then at the end dot 2240 01:27:42,239 --> 01:27:44,719 promises and those are the common core 2241 01:27:44,719 --> 01:27:46,800 modules we're going to need and now we 2242 01:27:46,800 --> 01:27:49,520 need to define a port for our web server 2243 01:27:49,520 --> 01:27:51,520 so not only will it have the address of 2244 01:27:51,520 --> 01:27:53,360 localhost because it will just be a 2245 01:27:53,360 --> 01:27:56,080 development server on our local machine 2246 01:27:56,080 --> 01:27:58,320 but we need to say what port it will be 2247 01:27:58,320 --> 01:28:01,679 on and so now we'll just define port in 2248 01:28:01,679 --> 01:28:04,800 all caps definitely a constant here 2249 01:28:04,800 --> 01:28:08,480 and we should say process.enb 2250 01:28:08,480 --> 01:28:10,960 dot port because if we were to host this 2251 01:28:10,960 --> 01:28:13,440 somewhere it would use this information 2252 01:28:13,440 --> 01:28:16,800 and then we can say or and i'll say 3500 2253 01:28:16,800 --> 01:28:19,600 today i've seen different ports used and 2254 01:28:19,600 --> 01:28:22,719 so today we'll be using port 3500 if we 2255 01:28:22,719 --> 01:28:24,880 were to host this somewhere 2256 01:28:24,880 --> 01:28:27,760 it would have a different value here now 2257 01:28:27,760 --> 01:28:30,000 that we have defined the port let's go 2258 01:28:30,000 --> 01:28:31,760 ahead and create 2259 01:28:31,760 --> 01:28:34,560 the minimal server we can do that by 2260 01:28:34,560 --> 01:28:35,760 defining 2261 01:28:35,760 --> 01:28:39,360 server and let's set it equal to http 2262 01:28:39,360 --> 01:28:40,840 dot 2263 01:28:40,840 --> 01:28:42,960 create server 2264 01:28:42,960 --> 01:28:45,840 and now that gets a request that we'll 2265 01:28:45,840 --> 01:28:47,840 just call req 2266 01:28:47,840 --> 01:28:49,840 and a response 2267 01:28:49,840 --> 01:28:52,639 in a function 2268 01:28:52,639 --> 01:28:55,199 and now we have our request and response 2269 01:28:55,199 --> 01:28:57,360 headed into the server and we won't 2270 01:28:57,360 --> 01:29:00,480 really do anything here at first except 2271 01:29:00,480 --> 01:29:02,000 let's log 2272 01:29:02,000 --> 01:29:04,400 the request 2273 01:29:04,400 --> 01:29:06,480 dot url 2274 01:29:06,480 --> 01:29:08,840 and let's log the request 2275 01:29:08,840 --> 01:29:11,120 dot method 2276 01:29:11,120 --> 01:29:13,280 we're not quite ready to launch our 2277 01:29:13,280 --> 01:29:15,440 server yet because it still needs to 2278 01:29:15,440 --> 01:29:16,880 listen 2279 01:29:16,880 --> 01:29:20,400 for requests so we say server dot listen 2280 01:29:20,400 --> 01:29:22,639 and this should always be at the end of 2281 01:29:22,639 --> 01:29:25,040 your server.js file 2282 01:29:25,040 --> 01:29:27,360 and we give the port value 2283 01:29:27,360 --> 01:29:29,840 and then we have an anonymous function 2284 01:29:29,840 --> 01:29:32,480 and here we'll just say console.log and 2285 01:29:32,480 --> 01:29:34,159 inside this 2286 01:29:34,159 --> 01:29:37,040 console we'll use a template literal 2287 01:29:37,040 --> 01:29:39,679 we'll say server 2288 01:29:39,679 --> 01:29:42,320 running on port 2289 01:29:42,320 --> 01:29:44,480 and then let's once again give the 2290 01:29:44,480 --> 01:29:46,320 port value here 2291 01:29:46,320 --> 01:29:48,320 and that's really all we need so once 2292 01:29:48,320 --> 01:29:50,320 the server starts we can see this 2293 01:29:50,320 --> 01:29:52,320 message and i'll go ahead and press alt 2294 01:29:52,320 --> 01:29:54,800 z to make it wrap so we can see it i've 2295 01:29:54,800 --> 01:29:56,960 got chrome open over here so we can 2296 01:29:56,960 --> 01:29:59,600 eventually request a page as well 2297 01:29:59,600 --> 01:30:02,880 and we should see this statement in the 2298 01:30:02,880 --> 01:30:05,760 console once we start our server i'm 2299 01:30:05,760 --> 01:30:08,320 going to go ahead and save the file and 2300 01:30:08,320 --> 01:30:10,480 then open a terminal window by pressing 2301 01:30:10,480 --> 01:30:12,560 ctrl in the back tick you can also do 2302 01:30:12,560 --> 01:30:14,239 that from the terminal menu that's 2303 01:30:14,239 --> 01:30:16,159 hidden behind the three dots right now 2304 01:30:16,159 --> 01:30:17,280 in my 2305 01:30:17,280 --> 01:30:18,880 visual studio code 2306 01:30:18,880 --> 01:30:20,719 and now let's go ahead and start the 2307 01:30:20,719 --> 01:30:23,520 server by typing npm let me lower case 2308 01:30:23,520 --> 01:30:24,400 there 2309 01:30:24,400 --> 01:30:25,840 run dev 2310 01:30:25,840 --> 01:30:27,760 and this will start nodemon that will 2311 01:30:27,760 --> 01:30:29,679 listen for changes 2312 01:30:29,679 --> 01:30:33,600 and the server should start up 2313 01:30:33,600 --> 01:30:35,600 and we can see that we now have server 2314 01:30:35,600 --> 01:30:39,120 running on port 3500 it's not going to 2315 01:30:39,120 --> 01:30:40,960 serve anything we're not sending any 2316 01:30:40,960 --> 01:30:42,800 type of response back yet we're not 2317 01:30:42,800 --> 01:30:45,440 using the response but it will log the 2318 01:30:45,440 --> 01:30:47,600 request and the request method so let's 2319 01:30:47,600 --> 01:30:50,000 go ahead and type 2320 01:30:50,000 --> 01:30:54,120 localhost 3500 2321 01:30:54,159 --> 01:30:56,800 and notice in the console we've got the 2322 01:30:56,800 --> 01:31:00,320 request here which is just the slash 2323 01:31:00,320 --> 01:31:03,600 and then the method is a get request not 2324 01:31:03,600 --> 01:31:05,920 a post request or a put request or a 2325 01:31:05,920 --> 01:31:07,360 delete request 2326 01:31:07,360 --> 01:31:09,360 but a get request 2327 01:31:09,360 --> 01:31:12,000 and so we did successfully log those 2328 01:31:12,000 --> 01:31:14,239 requests but there's just nothing that 2329 01:31:14,239 --> 01:31:16,400 comes back yet i'm going to go ahead and 2330 01:31:16,400 --> 01:31:18,800 press ctrl c for now because we don't 2331 01:31:18,800 --> 01:31:20,480 need to run the server while we're 2332 01:31:20,480 --> 01:31:22,320 making a few changes and now of course 2333 01:31:22,320 --> 01:31:24,560 it can't serve a page 2334 01:31:24,560 --> 01:31:26,560 but that's no worry we'll be serving a 2335 01:31:26,560 --> 01:31:29,440 page soon what we can do is talk about 2336 01:31:29,440 --> 01:31:32,080 what else we could put in the server 2337 01:31:32,080 --> 01:31:35,360 and so after we're logging the url and 2338 01:31:35,360 --> 01:31:37,280 the method and the url was just the 2339 01:31:37,280 --> 01:31:39,920 slash as we requested the index page 2340 01:31:39,920 --> 01:31:41,360 what we could do 2341 01:31:41,360 --> 01:31:42,560 is 2342 01:31:42,560 --> 01:31:45,600 build a path and then serve the file so 2343 01:31:45,600 --> 01:31:47,600 i'm just going to copy some code in to 2344 01:31:47,600 --> 01:31:48,960 discuss it 2345 01:31:48,960 --> 01:31:51,440 but this is not what we'll want to do 2346 01:31:51,440 --> 01:31:53,040 and i'll discuss why i'm going to go 2347 01:31:53,040 --> 01:31:54,719 ahead and expand visual studio code for 2348 01:31:54,719 --> 01:31:56,320 now too so we don't have to wrap the 2349 01:31:56,320 --> 01:31:57,280 lines 2350 01:31:57,280 --> 01:32:00,960 but we could listen for a specific path 2351 01:32:00,960 --> 01:32:03,040 so we're defining path here we've got 2352 01:32:03,040 --> 01:32:05,280 two semicolons we just need the one 2353 01:32:05,280 --> 01:32:08,000 and if it is just the slash or even if 2354 01:32:08,000 --> 01:32:11,360 we specified index html either one of 2355 01:32:11,360 --> 01:32:12,719 these would work and this is an if 2356 01:32:12,719 --> 01:32:13,920 statement 2357 01:32:13,920 --> 01:32:15,920 then we could set the response dot 2358 01:32:15,920 --> 01:32:17,920 status code to 200 which means 2359 01:32:17,920 --> 01:32:19,280 successful 2360 01:32:19,280 --> 01:32:22,400 we can set the response header content 2361 01:32:22,400 --> 01:32:23,280 type 2362 01:32:23,280 --> 01:32:26,400 to text slash html because we would be 2363 01:32:26,400 --> 01:32:28,560 serving the html page 2364 01:32:28,560 --> 01:32:31,199 and then we could go ahead and use path 2365 01:32:31,199 --> 01:32:34,239 to define the path value 2366 01:32:34,239 --> 01:32:37,199 and look in our views folder 2367 01:32:37,199 --> 01:32:39,520 for the index page that we have right 2368 01:32:39,520 --> 01:32:40,639 there 2369 01:32:40,639 --> 01:32:43,280 and serve that page and how we would do 2370 01:32:43,280 --> 01:32:45,679 that would be to read the file that we 2371 01:32:45,679 --> 01:32:47,600 currently have 2372 01:32:47,600 --> 01:32:49,840 and i didn't bother with 2373 01:32:49,840 --> 01:32:51,679 handling the error in this 2374 01:32:51,679 --> 01:32:53,040 version but 2375 01:32:53,040 --> 01:32:55,040 what we would do is read the file and 2376 01:32:55,040 --> 01:32:56,800 take this data 2377 01:32:56,800 --> 01:32:59,280 and then send the data 2378 01:32:59,280 --> 01:33:01,199 and we would be sending the contents 2379 01:33:01,199 --> 01:33:04,800 essentially of the index.html file this 2380 01:33:04,800 --> 01:33:07,199 would work but this is not efficient we 2381 01:33:07,199 --> 01:33:09,120 would have a statement 2382 01:33:09,120 --> 01:33:12,560 for every address that came in and 2383 01:33:12,560 --> 01:33:14,960 actually every file because of course 2384 01:33:14,960 --> 01:33:17,360 we'll be serving some files that are not 2385 01:33:17,360 --> 01:33:20,880 text html and feel free to try this 2386 01:33:20,880 --> 01:33:22,880 version out i'm going to show another 2387 01:33:22,880 --> 01:33:25,280 version that we will not use but that 2388 01:33:25,280 --> 01:33:27,440 you would also see in a possible simple 2389 01:33:27,440 --> 01:33:28,960 server and i'm just showing part of it 2390 01:33:28,960 --> 01:33:30,320 here i guess i could go ahead and close 2391 01:33:30,320 --> 01:33:31,120 it out 2392 01:33:31,120 --> 01:33:33,280 but we could put in a switch statement 2393 01:33:33,280 --> 01:33:36,159 and we could look at the request url 2394 01:33:36,159 --> 01:33:38,719 value that comes in and here if it's 2395 01:33:38,719 --> 01:33:41,280 just a slash we could go ahead and once 2396 01:33:41,280 --> 01:33:44,320 again set the status code set the path 2397 01:33:44,320 --> 01:33:46,480 read the file and then of course break 2398 01:33:46,480 --> 01:33:48,000 out of our switch statement and we would 2399 01:33:48,000 --> 01:33:50,719 have to have a case for every value that 2400 01:33:50,719 --> 01:33:53,440 came in even a duplicate case 2401 01:33:53,440 --> 01:33:55,800 essentially if this were to be 2402 01:33:55,800 --> 01:33:59,199 index.html and not the slash and this 2403 01:33:59,199 --> 01:34:02,719 would also be very big and you would 2404 01:34:02,719 --> 01:34:04,719 have to think of every possible file 2405 01:34:04,719 --> 01:34:07,120 that could be requested your default 2406 01:34:07,120 --> 01:34:09,600 could of course be the 404 if it doesn't 2407 01:34:09,600 --> 01:34:12,639 exist but this also takes up a lot of 2408 01:34:12,639 --> 01:34:15,679 space and once again it's not dynamic 2409 01:34:15,679 --> 01:34:17,600 and so this is not what we want to do 2410 01:34:17,600 --> 01:34:20,400 today either a switch statement is 2411 01:34:20,400 --> 01:34:22,560 useful though so let's take a different 2412 01:34:22,560 --> 01:34:24,800 approach but we will still use a switch 2413 01:34:24,800 --> 01:34:26,960 statement as we construct what our 2414 01:34:26,960 --> 01:34:29,440 server does today so we'll start out 2415 01:34:29,440 --> 01:34:31,520 once again let's keep the console log 2416 01:34:31,520 --> 01:34:33,280 statement with the request url and 2417 01:34:33,280 --> 01:34:34,960 method that could be useful as we 2418 01:34:34,960 --> 01:34:37,120 monitor things during development and 2419 01:34:37,120 --> 01:34:39,840 then let's look at the extension 2420 01:34:39,840 --> 01:34:42,639 of the request url that we get and so we 2421 01:34:42,639 --> 01:34:44,320 can use the path 2422 01:34:44,320 --> 01:34:45,760 dot 2423 01:34:45,760 --> 01:34:48,719 name method to get that extension so 2424 01:34:48,719 --> 01:34:51,520 let's just pass in the request url now 2425 01:34:51,520 --> 01:34:53,760 once again if it's a slash maybe there 2426 01:34:53,760 --> 01:34:56,480 will not be an extension name but then 2427 01:34:56,480 --> 01:34:58,480 we can handle that as well so now that 2428 01:34:58,480 --> 01:35:00,880 we've defined the extension 2429 01:35:00,880 --> 01:35:03,119 let's also define 2430 01:35:03,119 --> 01:35:04,719 content type 2431 01:35:04,719 --> 01:35:06,960 and now we will use a switch statement 2432 01:35:06,960 --> 01:35:08,960 to set the content type and i'm going to 2433 01:35:08,960 --> 01:35:10,800 paste this in because we'll be looking 2434 01:35:10,800 --> 01:35:12,560 at all the different 2435 01:35:12,560 --> 01:35:14,800 possibilities for file extensions so 2436 01:35:14,800 --> 01:35:17,040 there are several and now we can go over 2437 01:35:17,040 --> 01:35:19,679 this but what we see up here above 2438 01:35:19,679 --> 01:35:21,920 is we set our extension 2439 01:35:21,920 --> 01:35:24,080 and then we define 2440 01:35:24,080 --> 01:35:26,560 content type with a let at least we 2441 01:35:26,560 --> 01:35:28,480 begin the definition and then we can set 2442 01:35:28,480 --> 01:35:31,360 it in the switch statement so here we're 2443 01:35:31,360 --> 01:35:33,360 looking at the extension 2444 01:35:33,360 --> 01:35:36,000 and now if it's dot css we set the 2445 01:35:36,000 --> 01:35:39,679 content type to text css and we break 2446 01:35:39,679 --> 01:35:42,480 and we go on and do this for every type 2447 01:35:42,480 --> 01:35:45,760 of request that we expect to get or file 2448 01:35:45,760 --> 01:35:47,520 content type that we expect to have 2449 01:35:47,520 --> 01:35:49,520 requested and you can see there's a 2450 01:35:49,520 --> 01:35:51,440 couple of different images here we've 2451 01:35:51,440 --> 01:35:54,719 got an image slash jpeg and an image 2452 01:35:54,719 --> 01:35:56,800 slash png 2453 01:35:56,800 --> 01:36:00,639 text for text slash plain we also have 2454 01:36:00,639 --> 01:36:03,280 json which is application json so all 2455 01:36:03,280 --> 01:36:05,119 the different types of files we expect 2456 01:36:05,119 --> 01:36:08,080 to serve and importantly here the 2457 01:36:08,080 --> 01:36:09,520 default 2458 01:36:09,520 --> 01:36:12,239 is the text slash html because this 2459 01:36:12,239 --> 01:36:13,280 could 2460 01:36:13,280 --> 01:36:15,520 not have an extension at all it could 2461 01:36:15,520 --> 01:36:17,520 just be the slash 2462 01:36:17,520 --> 01:36:21,280 or it could have the extension of html 2463 01:36:21,280 --> 01:36:23,040 and so we just handle that as the 2464 01:36:23,040 --> 01:36:26,159 default okay the next part if you have 2465 01:36:26,159 --> 01:36:28,719 not worked with chain ternary statements 2466 01:36:28,719 --> 01:36:31,280 you may not like me too well right now 2467 01:36:31,280 --> 01:36:33,679 but i do like chained ternary statements 2468 01:36:33,679 --> 01:36:36,800 so i will paste one in and explain it 2469 01:36:36,800 --> 01:36:39,280 what i want to do is set the value of 2470 01:36:39,280 --> 01:36:41,199 the file path 2471 01:36:41,199 --> 01:36:43,520 and now a chain ternary statement like 2472 01:36:43,520 --> 01:36:45,760 this one can be confusing especially if 2473 01:36:45,760 --> 01:36:47,760 you haven't worked with them much or if 2474 01:36:47,760 --> 01:36:49,440 you have worked with them at all but 2475 01:36:49,440 --> 01:36:51,119 let's break it down 2476 01:36:51,119 --> 01:36:53,360 we're saying that whatever the result is 2477 01:36:53,360 --> 01:36:55,840 will be saved here in the variable file 2478 01:36:55,840 --> 01:36:56,719 path 2479 01:36:56,719 --> 01:37:00,960 so we say if the content type is html 2480 01:37:00,960 --> 01:37:04,800 and the request url is just a slash 2481 01:37:04,800 --> 01:37:08,400 then this will be the value and we set 2482 01:37:08,400 --> 01:37:10,159 the path name 2483 01:37:10,159 --> 01:37:13,840 using the views directory as well as the 2484 01:37:13,840 --> 01:37:16,000 full path name here we get from the dur 2485 01:37:16,000 --> 01:37:17,600 name value 2486 01:37:17,600 --> 01:37:20,880 and we set it to index.html 2487 01:37:20,880 --> 01:37:23,280 but if that's not the case then we look 2488 01:37:23,280 --> 01:37:25,520 at the next condition and that next 2489 01:37:25,520 --> 01:37:28,080 condition is here and we're saying okay 2490 01:37:28,080 --> 01:37:31,199 if it's an html file type or content 2491 01:37:31,199 --> 01:37:32,159 type 2492 01:37:32,159 --> 01:37:33,199 then 2493 01:37:33,199 --> 01:37:36,719 if the last character in the request url 2494 01:37:36,719 --> 01:37:38,400 is a slash 2495 01:37:38,400 --> 01:37:39,920 this accounts 2496 01:37:39,920 --> 01:37:40,880 for 2497 01:37:40,880 --> 01:37:43,920 our subdirectory possibly and not just 2498 01:37:43,920 --> 01:37:46,880 the main directory and so this would be 2499 01:37:46,880 --> 01:37:48,719 just a little different because we not 2500 01:37:48,719 --> 01:37:51,440 only need the views in the file path but 2501 01:37:51,440 --> 01:37:53,360 we need the request url that would 2502 01:37:53,360 --> 01:37:55,920 specify the subdirectory 2503 01:37:55,920 --> 01:37:58,560 and so this is what happens if that is 2504 01:37:58,560 --> 01:37:59,679 true 2505 01:37:59,679 --> 01:38:02,080 and then if not we go ahead and just 2506 01:38:02,080 --> 01:38:02,960 check 2507 01:38:02,960 --> 01:38:05,840 now to see if the content type is html 2508 01:38:05,840 --> 01:38:07,760 at this point since these other two were 2509 01:38:07,760 --> 01:38:09,119 not true 2510 01:38:09,119 --> 01:38:12,159 and then we would look at whatever was 2511 01:38:12,159 --> 01:38:14,719 requested in the views folder because 2512 01:38:14,719 --> 01:38:17,280 that's where the html should be 2513 01:38:17,280 --> 01:38:19,280 however 2514 01:38:19,280 --> 01:38:21,440 if that's not the case 2515 01:38:21,440 --> 01:38:23,920 then we're going to go ahead and just 2516 01:38:23,920 --> 01:38:26,480 use the directory name or the file path 2517 01:38:26,480 --> 01:38:28,560 and the request url because this could 2518 01:38:28,560 --> 01:38:29,360 be 2519 01:38:29,360 --> 01:38:30,400 css 2520 01:38:30,400 --> 01:38:32,400 or an image or something in one of the 2521 01:38:32,400 --> 01:38:34,880 other folders that would be specified in 2522 01:38:34,880 --> 01:38:36,639 the request url 2523 01:38:36,639 --> 01:38:39,199 so another way to break this down 2524 01:38:39,199 --> 01:38:41,600 is everything that starts with content 2525 01:38:41,600 --> 01:38:44,400 type is a conditional statement and 2526 01:38:44,400 --> 01:38:47,040 everything here that starts with path is 2527 01:38:47,040 --> 01:38:49,600 a result if the previous conditional 2528 01:38:49,600 --> 01:38:52,639 statement is true and then the final one 2529 01:38:52,639 --> 01:38:55,040 the final path because there's two paths 2530 01:38:55,040 --> 01:38:57,199 in a row is just the default if none of 2531 01:38:57,199 --> 01:38:58,960 the others were true 2532 01:38:58,960 --> 01:39:01,360 and so you could break this out into an 2533 01:39:01,360 --> 01:39:02,239 if 2534 01:39:02,239 --> 01:39:05,920 else if else if else statement if you 2535 01:39:05,920 --> 01:39:08,639 want to do that i prefer chain ternaries 2536 01:39:08,639 --> 01:39:09,760 myself 2537 01:39:09,760 --> 01:39:12,639 and that is what we end up with let's go 2538 01:39:12,639 --> 01:39:14,719 ahead and add one more statement here 2539 01:39:14,719 --> 01:39:16,719 for the file path and this will be an if 2540 01:39:16,719 --> 01:39:18,560 and we'll say if 2541 01:39:18,560 --> 01:39:21,679 there is no extension which means it was 2542 01:39:21,679 --> 01:39:23,840 probably a slash and didn't have a file 2543 01:39:23,840 --> 01:39:25,119 extension 2544 01:39:25,119 --> 01:39:29,199 and the request url dot slice 2545 01:39:29,199 --> 01:39:31,360 minus one which is the very last 2546 01:39:31,360 --> 01:39:33,360 character of the request url 2547 01:39:33,360 --> 01:39:35,040 is not 2548 01:39:35,040 --> 01:39:36,800 equal to 2549 01:39:36,800 --> 01:39:38,239 a slash 2550 01:39:38,239 --> 01:39:40,800 so maybe we've just requested a file 2551 01:39:40,800 --> 01:39:43,760 like about or new page we have a new 2552 01:39:43,760 --> 01:39:46,480 page over here but we didn't 2553 01:39:46,480 --> 01:39:49,199 type the dot html afterwards well this 2554 01:39:49,199 --> 01:39:51,840 will make that work anyway and sometimes 2555 01:39:51,840 --> 01:39:53,360 that's cool to do so this wouldn't 2556 01:39:53,360 --> 01:39:56,080 really be required but we're just making 2557 01:39:56,080 --> 01:39:57,280 our 2558 01:39:57,280 --> 01:40:00,159 addresses work even if we don't add the 2559 01:40:00,159 --> 01:40:02,400 dot html at the end so then we'll just 2560 01:40:02,400 --> 01:40:05,520 say file path plus equals 2561 01:40:05,520 --> 01:40:08,239 and then we'll add the dot html here so 2562 01:40:08,239 --> 01:40:11,119 we still serve the correct file although 2563 01:40:11,119 --> 01:40:13,920 we might not have typed in the dot html 2564 01:40:13,920 --> 01:40:15,440 in the url 2565 01:40:15,440 --> 01:40:17,840 bar in the browser so we should probably 2566 01:40:17,840 --> 01:40:20,239 add a note to this and i'll just say 2567 01:40:20,239 --> 01:40:23,360 whoa i went too far here we go 2568 01:40:23,360 --> 01:40:25,040 makes the 2569 01:40:25,040 --> 01:40:27,440 dot html 2570 01:40:27,440 --> 01:40:29,600 extension 2571 01:40:29,600 --> 01:40:32,800 not required in the browser 2572 01:40:32,800 --> 01:40:35,040 and that's what that statement is for 2573 01:40:35,040 --> 01:40:37,840 okay now we have our file path and 2574 01:40:37,840 --> 01:40:40,080 content type so we're ready to actually 2575 01:40:40,080 --> 01:40:40,960 check 2576 01:40:40,960 --> 01:40:43,520 and see if we want to serve the file and 2577 01:40:43,520 --> 01:40:46,719 we can do that with defining file 2578 01:40:46,719 --> 01:40:47,760 exists 2579 01:40:47,760 --> 01:40:51,199 and setting this equal to fs dot 2580 01:40:51,199 --> 01:40:52,560 exists 2581 01:40:52,560 --> 01:40:53,600 sync 2582 01:40:53,600 --> 01:40:55,440 and let's just pass in the file path 2583 01:40:55,440 --> 01:40:56,800 because now 2584 01:40:56,800 --> 01:40:58,560 we are checking to see if this file 2585 01:40:58,560 --> 01:41:00,960 exists or not so this will be a true or 2586 01:41:00,960 --> 01:41:03,679 false value that we get back and we can 2587 01:41:03,679 --> 01:41:06,239 check that by saying if 2588 01:41:06,239 --> 01:41:08,639 file exists we don't need to say if it's 2589 01:41:08,639 --> 01:41:10,320 true we're just saying if the file 2590 01:41:10,320 --> 01:41:12,239 exists 2591 01:41:12,239 --> 01:41:14,560 then we're going to 2592 01:41:14,560 --> 01:41:16,719 serve the file 2593 01:41:16,719 --> 01:41:19,360 but if it doesn't exist we also need to 2594 01:41:19,360 --> 01:41:20,320 have 2595 01:41:20,320 --> 01:41:23,199 an else and there are possibilities if 2596 01:41:23,199 --> 01:41:26,960 it doesn't exist this could be a 404 2597 01:41:26,960 --> 01:41:30,000 or this could be a 301 which is a 2598 01:41:30,000 --> 01:41:32,960 redirect now we still aren't serving 2599 01:41:32,960 --> 01:41:35,440 anything yet we have determined our file 2600 01:41:35,440 --> 01:41:38,080 path and we have determined our content 2601 01:41:38,080 --> 01:41:40,239 type but this is where we would serve 2602 01:41:40,239 --> 01:41:43,520 the appropriate files right here for now 2603 01:41:43,520 --> 01:41:45,360 let's just go ahead and log to the 2604 01:41:45,360 --> 01:41:47,679 console and what i want to log here 2605 01:41:47,679 --> 01:41:51,600 inside of the else is the path dot parse 2606 01:41:51,600 --> 01:41:53,520 that will tell us the different parts of 2607 01:41:53,520 --> 01:41:54,880 the file path 2608 01:41:54,880 --> 01:41:57,600 let's go ahead and pass file path in and 2609 01:41:57,600 --> 01:42:00,480 take a look at a file path that doesn't 2610 01:42:00,480 --> 01:42:02,719 exist so we need to start the server 2611 01:42:02,719 --> 01:42:05,280 again i'm going to go ahead and change 2612 01:42:05,280 --> 01:42:09,199 this so we can see the browser as well 2613 01:42:09,199 --> 01:42:11,920 and then i'll open a terminal window 2614 01:42:11,920 --> 01:42:15,199 and now let's go ahead and type npm run 2615 01:42:15,199 --> 01:42:17,600 dev 2616 01:42:19,920 --> 01:42:22,400 and the server is running we can reload 2617 01:42:22,400 --> 01:42:24,239 first and just make sure we get the home 2618 01:42:24,239 --> 01:42:26,320 page well i guess we won't get a home 2619 01:42:26,320 --> 01:42:28,080 page but we can make sure the request 2620 01:42:28,080 --> 01:42:29,679 goes through there we go 2621 01:42:29,679 --> 01:42:32,400 and so we'll add the homepage very soon 2622 01:42:32,400 --> 01:42:34,560 but let's go ahead and look for 2623 01:42:34,560 --> 01:42:37,119 something that doesn't exist at all 2624 01:42:37,119 --> 01:42:39,280 because the file does exist so when it 2625 01:42:39,280 --> 01:42:41,199 checked it didn't get into that else 2626 01:42:41,199 --> 01:42:44,159 area let's just say this is 2627 01:42:44,159 --> 01:42:46,639 old right here 2628 01:42:46,639 --> 01:42:48,320 okay there was something that didn't 2629 01:42:48,320 --> 01:42:50,639 exist i'm going to expand the terminal 2630 01:42:50,639 --> 01:42:52,800 window up so we can see this broke out 2631 01:42:52,800 --> 01:42:54,719 now and we can see 2632 01:42:54,719 --> 01:42:57,760 using the path.parse we get these 2633 01:42:57,760 --> 01:42:59,119 different parts 2634 01:42:59,119 --> 01:43:00,800 of the file path and what we're 2635 01:43:00,800 --> 01:43:02,800 interested in right now is the base 2636 01:43:02,800 --> 01:43:03,960 notice it's 2637 01:43:03,960 --> 01:43:07,119 old.html even though it didn't exist 2638 01:43:07,119 --> 01:43:09,440 that's what we get because it was added 2639 01:43:09,440 --> 01:43:11,040 in the file path 2640 01:43:11,040 --> 01:43:13,360 so that's what we're going to use 2641 01:43:13,360 --> 01:43:16,480 as we come into this area that does not 2642 01:43:16,480 --> 01:43:17,760 know if the 2643 01:43:17,760 --> 01:43:21,679 file should be a 301 or a 404 2644 01:43:21,679 --> 01:43:23,920 and we'll determine that with a switch 2645 01:43:23,920 --> 01:43:25,760 statement i'm going to go ahead and 2646 01:43:25,760 --> 01:43:27,760 highlight this and just change that 2647 01:43:27,760 --> 01:43:29,440 console log now 2648 01:43:29,440 --> 01:43:31,440 into a switch 2649 01:43:31,440 --> 01:43:33,880 and now instead of just 2650 01:43:33,880 --> 01:43:36,239 path.parse.filepath we want to put 2651 01:43:36,239 --> 01:43:38,159 dot base at the end of that so that 2652 01:43:38,159 --> 01:43:40,880 would be that value 2653 01:43:40,880 --> 01:43:42,800 and then we get our curly braces for the 2654 01:43:42,800 --> 01:43:44,320 switch statement we don't really need 2655 01:43:44,320 --> 01:43:46,480 the semicolon there and we need to put 2656 01:43:46,480 --> 01:43:48,719 our cases in here so 2657 01:43:48,719 --> 01:43:51,280 our first case would be 2658 01:43:51,280 --> 01:43:53,040 old-page 2659 01:43:53,040 --> 01:43:54,880 dot html 2660 01:43:54,880 --> 01:43:57,679 and this is an instance where we had an 2661 01:43:57,679 --> 01:44:00,000 old page and we want to redirect it to 2662 01:44:00,000 --> 01:44:02,159 the new page that we see over here in 2663 01:44:02,159 --> 01:44:04,560 our file tree so let's handle this 2664 01:44:04,560 --> 01:44:08,159 redirect first and this will be response 2665 01:44:08,159 --> 01:44:10,320 dot right head 2666 01:44:10,320 --> 01:44:13,119 and then a redirect is a status code of 2667 01:44:13,119 --> 01:44:14,560 301 2668 01:44:14,560 --> 01:44:16,560 and now we need to go ahead and put in 2669 01:44:16,560 --> 01:44:19,520 the value here for the header and this 2670 01:44:19,520 --> 01:44:22,320 would be location 2671 01:44:22,320 --> 01:44:24,960 and then the location is slash 2672 01:44:24,960 --> 01:44:27,760 new dash page 2673 01:44:27,760 --> 01:44:30,239 dot html 2674 01:44:30,239 --> 01:44:32,639 and now that we've handled the redirect 2675 01:44:32,639 --> 01:44:35,840 we need to go ahead and end the response 2676 01:44:35,840 --> 01:44:38,000 and after we end the response well we 2677 01:44:38,000 --> 01:44:39,679 need a little column there 2678 01:44:39,679 --> 01:44:41,840 we break out of the switch statement 2679 01:44:41,840 --> 01:44:44,560 there and now let's add our next case 2680 01:44:44,560 --> 01:44:46,239 our next case is going to be another 2681 01:44:46,239 --> 01:44:47,679 redirect 2682 01:44:47,679 --> 01:44:51,080 and it's www.page.html 2683 01:44:52,800 --> 01:44:55,440 and now i'm just going to copy this much 2684 01:44:55,440 --> 01:44:57,280 because it's very similar here for 2685 01:44:57,280 --> 01:44:58,960 another redirect 2686 01:44:58,960 --> 01:45:02,159 and paste it down we've got a 301 again 2687 01:45:02,159 --> 01:45:04,880 but instead of new page we're just going 2688 01:45:04,880 --> 01:45:07,760 to redirect to the root which is the 2689 01:45:07,760 --> 01:45:09,280 slash 2690 01:45:09,280 --> 01:45:11,040 and so that will just redirect back to 2691 01:45:11,040 --> 01:45:15,199 the home page once again end and break 2692 01:45:15,199 --> 01:45:17,679 and now let's have a default as well for 2693 01:45:17,679 --> 01:45:19,600 our switch statement and this could be 2694 01:45:19,600 --> 01:45:22,639 larger if you had more redirects to put 2695 01:45:22,639 --> 01:45:24,719 in here but the default should be the 2696 01:45:24,719 --> 01:45:28,400 404 and so this is where we're going to 2697 01:45:28,400 --> 01:45:30,080 serve 2698 01:45:30,080 --> 01:45:31,760 a 404 2699 01:45:31,760 --> 01:45:34,639 response and let's save this 2700 01:45:34,639 --> 01:45:36,320 once again i'm going to expand visual 2701 01:45:36,320 --> 01:45:38,159 studio code because that was wrapping a 2702 01:45:38,159 --> 01:45:40,080 little weird but here you can see what 2703 01:45:40,080 --> 01:45:42,159 we have now we're currently not ready to 2704 01:45:42,159 --> 01:45:45,600 serve the new page or the home page yet 2705 01:45:45,600 --> 01:45:47,360 we haven't got anything else being 2706 01:45:47,360 --> 01:45:49,840 served but we did handle the redirect so 2707 01:45:49,840 --> 01:45:51,600 what we need now 2708 01:45:51,600 --> 01:45:54,080 is a function instead of repeating the 2709 01:45:54,080 --> 01:45:56,719 same function here for a 404 2710 01:45:56,719 --> 01:45:59,280 or up here for other files a function 2711 01:45:59,280 --> 01:46:01,840 that we can call in both spots okay 2712 01:46:01,840 --> 01:46:03,920 let's scroll back up to define our 2713 01:46:03,920 --> 01:46:06,880 function and we will put that before the 2714 01:46:06,880 --> 01:46:09,360 server and after the port 2715 01:46:09,360 --> 01:46:11,440 so let's call this function 2716 01:46:11,440 --> 01:46:13,840 serv file because that's what it will do 2717 01:46:13,840 --> 01:46:16,400 it'll need to be an async function 2718 01:46:16,400 --> 01:46:18,159 and then it's going to need the file 2719 01:46:18,159 --> 01:46:20,320 path so we can grab the file that's 2720 01:46:20,320 --> 01:46:22,400 needed it's also going to need the 2721 01:46:22,400 --> 01:46:24,719 content type so we can set the content 2722 01:46:24,719 --> 01:46:27,119 type that is going to be sent and it's 2723 01:46:27,119 --> 01:46:29,920 going to need the response object to go 2724 01:46:29,920 --> 01:46:32,960 ahead and send that response 2725 01:46:32,960 --> 01:46:35,199 so now that we have our parameters 2726 01:46:35,199 --> 01:46:36,880 defined and we have the name of the 2727 01:46:36,880 --> 01:46:37,840 function 2728 01:46:37,840 --> 01:46:40,960 let's go ahead and start by defining the 2729 01:46:40,960 --> 01:46:43,360 try block we already know it will be an 2730 01:46:43,360 --> 01:46:45,600 async function so there will be a try 2731 01:46:45,600 --> 01:46:48,159 and a catch 2732 01:46:48,159 --> 01:46:50,480 so we can use await as we get the 2733 01:46:50,480 --> 01:46:52,960 information we need let's go ahead and 2734 01:46:52,960 --> 01:46:55,520 set the error block first so we'll log 2735 01:46:55,520 --> 01:46:57,679 the error to the console 2736 01:46:57,679 --> 01:46:59,760 and then we also want to set the 2737 01:46:59,760 --> 01:47:01,679 response dot 2738 01:47:01,679 --> 01:47:03,280 status code 2739 01:47:03,280 --> 01:47:05,600 this would be 500 it would be a server 2740 01:47:05,600 --> 01:47:07,360 error here if we could not read the data 2741 01:47:07,360 --> 01:47:09,440 from the server that we want 2742 01:47:09,440 --> 01:47:12,000 and then we need to end the response 2743 01:47:12,000 --> 01:47:14,880 notice i called the parameter the full 2744 01:47:14,880 --> 01:47:18,560 word response before we were sending 2745 01:47:18,560 --> 01:47:22,320 the res for response right here and 2746 01:47:22,320 --> 01:47:24,480 that's what we will pass in as we get 2747 01:47:24,480 --> 01:47:27,199 down there further but in the function 2748 01:47:27,199 --> 01:47:28,719 i'm using the placeholder here's the 2749 01:47:28,719 --> 01:47:31,040 parameter with the full word response 2750 01:47:31,040 --> 01:47:33,199 and now in the try block let's get the 2751 01:47:33,199 --> 01:47:36,400 data from the file so let's define data 2752 01:47:36,400 --> 01:47:40,880 and set this equal to await fs promises 2753 01:47:40,880 --> 01:47:42,239 and then dot 2754 01:47:42,239 --> 01:47:43,760 read file 2755 01:47:43,760 --> 01:47:46,400 now we need the file path to read and 2756 01:47:46,400 --> 01:47:48,639 we'll use utf-8 2757 01:47:48,639 --> 01:47:51,280 and after we read the file and we are 2758 01:47:51,280 --> 01:47:53,679 awaiting that information we can use 2759 01:47:53,679 --> 01:47:57,199 response and then right head and inside 2760 01:47:57,199 --> 01:47:58,400 right head 2761 01:47:58,400 --> 01:48:00,639 we'll go ahead and say 2762 01:48:00,639 --> 01:48:02,880 200 status code 2763 01:48:02,880 --> 01:48:06,400 and then the content type 2764 01:48:07,360 --> 01:48:09,840 there we go content type will be equal 2765 01:48:09,840 --> 01:48:12,000 to the content type value that we have 2766 01:48:12,000 --> 01:48:14,000 passed in 2767 01:48:14,000 --> 01:48:16,480 and then finally we'll have response dot 2768 01:48:16,480 --> 01:48:19,920 end and we can just send the data back 2769 01:48:19,920 --> 01:48:21,840 now some of you may be thinking ahead 2770 01:48:21,840 --> 01:48:23,760 and there are some problems that we will 2771 01:48:23,760 --> 01:48:26,480 have with this but this is the very 2772 01:48:26,480 --> 01:48:29,199 basic to start out with so i want to do 2773 01:48:29,199 --> 01:48:31,920 that first now let's go back down into 2774 01:48:31,920 --> 01:48:33,760 the code and call the function where it 2775 01:48:33,760 --> 01:48:35,600 needs to be 2776 01:48:35,600 --> 01:48:37,600 so after we have the file path and the 2777 01:48:37,600 --> 01:48:40,239 file event exists here we say serve the 2778 01:48:40,239 --> 01:48:42,400 file so this is where we would call our 2779 01:48:42,400 --> 01:48:44,159 serve file 2780 01:48:44,159 --> 01:48:45,679 and this should have 2781 01:48:45,679 --> 01:48:47,760 the file path value 2782 01:48:47,760 --> 01:48:50,480 you have the content type value and it 2783 01:48:50,480 --> 01:48:52,320 needs the response object and notice 2784 01:48:52,320 --> 01:48:54,639 here it's res because that's what it's 2785 01:48:54,639 --> 01:48:57,679 defined as to begin with not in the 2786 01:48:57,679 --> 01:48:59,920 function definition of serve file but 2787 01:48:59,920 --> 01:49:02,000 what we're using in the server 2788 01:49:02,000 --> 01:49:04,159 okay let's just copy this and we'll make 2789 01:49:04,159 --> 01:49:06,000 some changes below 2790 01:49:06,000 --> 01:49:10,239 as we want to serve a 404 here 2791 01:49:10,239 --> 01:49:12,239 so i'm going to paste this in and then 2792 01:49:12,239 --> 01:49:14,480 we'll just change a few values 2793 01:49:14,480 --> 01:49:17,119 okay here we know exactly what file we 2794 01:49:17,119 --> 01:49:19,280 want to serve so we're going to use path 2795 01:49:19,280 --> 01:49:20,960 dot join 2796 01:49:20,960 --> 01:49:23,360 and pass in the directory name and then 2797 01:49:23,360 --> 01:49:25,760 we know it's in the views folder 2798 01:49:25,760 --> 01:49:27,119 and then we know 2799 01:49:27,119 --> 01:49:28,040 it's the 2800 01:49:28,040 --> 01:49:30,639 404.html file and we also know the 2801 01:49:30,639 --> 01:49:33,920 content type so we can just set that 2802 01:49:33,920 --> 01:49:37,199 to text html right away and then once 2803 01:49:37,199 --> 01:49:39,040 again we need to pass in the response 2804 01:49:39,040 --> 01:49:41,360 object so let's save that as well now 2805 01:49:41,360 --> 01:49:43,599 i'm going to resize visual studio code 2806 01:49:43,599 --> 01:49:44,639 again 2807 01:49:44,639 --> 01:49:46,960 and we'll go ahead and scroll down and 2808 01:49:46,960 --> 01:49:49,199 it looks like of course nodemon's 2809 01:49:49,199 --> 01:49:50,800 already working 2810 01:49:50,800 --> 01:49:53,280 and yes the requests are coming in and 2811 01:49:53,280 --> 01:49:56,880 we served our 404 already because old 2812 01:49:56,880 --> 01:49:59,920 doesn't exist so let's go ahead and back 2813 01:49:59,920 --> 01:50:01,040 out of here 2814 01:50:01,040 --> 01:50:03,840 and see if we can serve the index 2815 01:50:03,840 --> 01:50:06,560 and yes the index page serves just fine 2816 01:50:06,560 --> 01:50:08,080 too i'm going to go ahead and show the 2817 01:50:08,080 --> 01:50:09,440 terminal so we can see all these 2818 01:50:09,440 --> 01:50:11,040 requests coming in 2819 01:50:11,040 --> 01:50:13,280 and you can see old was a get request 2820 01:50:13,280 --> 01:50:16,560 and then it requested the style sheet 2821 01:50:16,560 --> 01:50:19,280 requested a fave icon which we don't 2822 01:50:19,280 --> 01:50:20,080 have 2823 01:50:20,080 --> 01:50:23,040 requested a index page then and once 2824 01:50:23,040 --> 01:50:25,040 again the style sheet and once again the 2825 01:50:25,040 --> 01:50:27,280 fave icon after that but we still have 2826 01:50:27,280 --> 01:50:30,000 some file types that will be issues so 2827 01:50:30,000 --> 01:50:31,679 let's take a look at some of those as 2828 01:50:31,679 --> 01:50:33,760 well say if we went to our 2829 01:50:33,760 --> 01:50:35,400 data folder 2830 01:50:35,400 --> 01:50:38,320 data.txt well that seems to work okay 2831 01:50:38,320 --> 01:50:41,360 even if we aren't sending the accurate 2832 01:50:41,360 --> 01:50:44,960 file type with it but what about data 2833 01:50:44,960 --> 01:50:47,280 dot json 2834 01:50:47,280 --> 01:50:50,560 that's just text that's not really json 2835 01:50:50,560 --> 01:50:52,480 like we would want to send back even 2836 01:50:52,480 --> 01:50:54,159 though it looks like it and could 2837 01:50:54,159 --> 01:50:57,280 possibly be parsed into it that is not 2838 01:50:57,280 --> 01:50:59,679 the format that we really want to send 2839 01:50:59,679 --> 01:51:02,880 let's also check our new page 2840 01:51:02,880 --> 01:51:05,440 because it should have an image on it 2841 01:51:05,440 --> 01:51:08,000 and the image doesn't work either so we 2842 01:51:08,000 --> 01:51:10,400 have some file types creating problems 2843 01:51:10,400 --> 01:51:13,280 what should be working is the redirect 2844 01:51:13,280 --> 01:51:16,360 so let's go ahead and type in 2845 01:51:16,360 --> 01:51:18,080 old-page.html 2846 01:51:18,080 --> 01:51:20,320 and yes it does redirect immediately to 2847 01:51:20,320 --> 01:51:22,719 the new page so that's good and what 2848 01:51:22,719 --> 01:51:23,599 about 2849 01:51:23,599 --> 01:51:26,880 requesting something in our subdirectory 2850 01:51:26,880 --> 01:51:29,440 and yes we got our subdirectory index 2851 01:51:29,440 --> 01:51:32,800 page so that also works like we expect 2852 01:51:32,800 --> 01:51:35,679 it to let's go back to the data and see 2853 01:51:35,679 --> 01:51:38,560 if we can fix that json first so data 2854 01:51:38,560 --> 01:51:41,040 slash data.json 2855 01:51:41,040 --> 01:51:43,040 i'm going to hide the terminal window 2856 01:51:43,040 --> 01:51:44,400 for the rest 2857 01:51:44,400 --> 01:51:47,360 of this and scroll back up to 2858 01:51:47,360 --> 01:51:49,920 the function that we created serv file 2859 01:51:49,920 --> 01:51:51,360 and i'll go ahead 2860 01:51:51,360 --> 01:51:53,679 and expand the window once again let's 2861 01:51:53,679 --> 01:51:56,080 start by changing data that we've 2862 01:51:56,080 --> 01:51:57,520 defined here 2863 01:51:57,520 --> 01:52:00,560 to raw data and then let's define data 2864 01:52:00,560 --> 01:52:03,840 separately so here we have data and this 2865 01:52:03,840 --> 01:52:05,840 is going to be equal to a ternary 2866 01:52:05,840 --> 01:52:08,159 statement we'll look at the content type 2867 01:52:08,159 --> 01:52:09,840 that we already know 2868 01:52:09,840 --> 01:52:12,880 and if the content type is equal to 2869 01:52:12,880 --> 01:52:16,000 application slash json 2870 01:52:16,000 --> 01:52:18,000 then we'll have a true value that 2871 01:52:18,000 --> 01:52:20,000 follows the question mark 2872 01:52:20,000 --> 01:52:21,920 and that true value will be 2873 01:52:21,920 --> 01:52:24,639 json.parse 2874 01:52:24,639 --> 01:52:26,880 and this would be parsing 2875 01:52:26,880 --> 01:52:29,199 the raw data that has been read 2876 01:52:29,199 --> 01:52:32,159 otherwise the false value would simply 2877 01:52:32,159 --> 01:52:33,280 be 2878 01:52:33,280 --> 01:52:36,000 the raw data likewise when we send the 2879 01:52:36,000 --> 01:52:38,080 response we're going to need an extra 2880 01:52:38,080 --> 01:52:40,320 line for this so we'll just do it 2881 01:52:40,320 --> 01:52:42,480 on a separate line and keep the 2882 01:52:42,480 --> 01:52:45,280 parentheses up here and down here 2883 01:52:45,280 --> 01:52:47,199 now what we need is another ternary 2884 01:52:47,199 --> 01:52:48,719 statement here and we'll look at the 2885 01:52:48,719 --> 01:52:50,880 content type once again 2886 01:52:50,880 --> 01:52:53,840 we'll say if it is equal to 2887 01:52:53,840 --> 01:52:57,760 application slash json 2888 01:52:57,760 --> 01:53:00,080 make sure i spell json correctly then 2889 01:53:00,080 --> 01:53:02,159 we're going to have a true and a false 2890 01:53:02,159 --> 01:53:03,679 and the true would be 2891 01:53:03,679 --> 01:53:06,239 json.stringify 2892 01:53:06,239 --> 01:53:09,599 and we can go ahead and pass in 2893 01:53:09,599 --> 01:53:12,400 oh not the raw data the data and 2894 01:53:12,400 --> 01:53:15,119 otherwise it's just going to be data 2895 01:53:15,119 --> 01:53:17,040 that we send oh and i don't need that 2896 01:53:17,040 --> 01:53:19,360 semicolon that would cause an error and 2897 01:53:19,360 --> 01:53:22,000 we can save our file let's go ahead and 2898 01:53:22,000 --> 01:53:25,280 resize visual studio code and reload our 2899 01:53:25,280 --> 01:53:28,480 json and now we can see it is served in 2900 01:53:28,480 --> 01:53:31,280 a much more expected manner here and 2901 01:53:31,280 --> 01:53:33,760 this is truly json that has been 2902 01:53:33,760 --> 01:53:35,280 stringified 2903 01:53:35,280 --> 01:53:37,440 and of course it was parsed to begin 2904 01:53:37,440 --> 01:53:39,760 with now let's go back to new page where 2905 01:53:39,760 --> 01:53:41,760 we have a broken image 2906 01:53:41,760 --> 01:53:44,159 and we can fix that as well so right now 2907 01:53:44,159 --> 01:53:46,800 we definitely get our new page html but 2908 01:53:46,800 --> 01:53:49,199 the image doesn't come through now this 2909 01:53:49,199 --> 01:53:52,320 is because the image would not use utf-8 2910 01:53:52,320 --> 01:53:54,400 encoding i'm going to expand visual 2911 01:53:54,400 --> 01:53:55,920 studio code again 2912 01:53:55,920 --> 01:53:58,000 and let's go ahead and put these values 2913 01:53:58,000 --> 01:54:00,560 on separate lines as well 2914 01:54:00,560 --> 01:54:02,400 and now we can close out the parentheses 2915 01:54:02,400 --> 01:54:04,560 down here on a separate line too 2916 01:54:04,560 --> 01:54:06,560 now we still want the file path here but 2917 01:54:06,560 --> 01:54:09,280 instead of just utf-8 we're going to 2918 01:54:09,280 --> 01:54:11,040 start out by saying 2919 01:54:11,040 --> 01:54:14,159 if the content type 2920 01:54:14,159 --> 01:54:16,320 and that is not so we'll say if it does 2921 01:54:16,320 --> 01:54:19,040 not include but we're using includes 2922 01:54:19,040 --> 01:54:20,000 here 2923 01:54:20,000 --> 01:54:22,320 and we're looking for the word image in 2924 01:54:22,320 --> 01:54:24,560 the content type because both 2925 01:54:24,560 --> 01:54:27,840 the png and the jpg 2926 01:54:27,840 --> 01:54:31,360 had the image in the uh content type 2927 01:54:31,360 --> 01:54:33,199 that we have down here in our switch 2928 01:54:33,199 --> 01:54:35,520 statement and you can see it right here 2929 01:54:35,520 --> 01:54:37,599 okay so we're looking for that to see if 2930 01:54:37,599 --> 01:54:40,000 the content type includes 2931 01:54:40,000 --> 01:54:43,040 image and if it does 2932 01:54:43,040 --> 01:54:44,960 or if it does not include image i'm 2933 01:54:44,960 --> 01:54:46,639 sorry because we have the exclamation 2934 01:54:46,639 --> 01:54:48,320 mark that means if it does not include 2935 01:54:48,320 --> 01:54:51,440 it would be utf-8 but if it does 2936 01:54:51,440 --> 01:54:53,520 let's just put an empty string because a 2937 01:54:53,520 --> 01:54:56,080 string is expected in that spot but if 2938 01:54:56,080 --> 01:54:59,040 we don't specify then it will go ahead 2939 01:54:59,040 --> 01:55:01,280 and let the image go through so now we 2940 01:55:01,280 --> 01:55:02,880 can save this 2941 01:55:02,880 --> 01:55:06,719 and go back and check our new image page 2942 01:55:06,719 --> 01:55:10,080 let's go ahead and reload it 2943 01:55:10,080 --> 01:55:12,560 and we've got our image now that we have 2944 01:55:12,560 --> 01:55:14,400 the cute dog image there's one more 2945 01:55:14,400 --> 01:55:17,119 change we need to make as i expand this 2946 01:55:17,119 --> 01:55:19,440 and if you have thought about this we're 2947 01:55:19,440 --> 01:55:22,320 sending a status 200 even when it's a 2948 01:55:22,320 --> 01:55:25,360 404 because the 404 processes through 2949 01:55:25,360 --> 01:55:27,760 here as well so we need to check we 2950 01:55:27,760 --> 01:55:30,239 don't want to send a 200 if it's a 404 2951 01:55:30,239 --> 01:55:32,639 so right now in the right head let's go 2952 01:55:32,639 --> 01:55:35,040 ahead and break these out as well 2953 01:55:35,040 --> 01:55:37,840 into separate lines 2954 01:55:37,840 --> 01:55:39,520 just gives us a little more room to work 2955 01:55:39,520 --> 01:55:41,599 and see everything that's happening so 2956 01:55:41,599 --> 01:55:44,639 now instead of just saying it's a 200 2957 01:55:44,639 --> 01:55:48,080 status code we can say if the file path 2958 01:55:48,080 --> 01:55:51,040 dot includes once again and if it 2959 01:55:51,040 --> 01:55:52,520 includes 2960 01:55:52,520 --> 01:55:54,159 404.html 2961 01:55:54,159 --> 01:55:57,599 well then we know it should be a 404 2962 01:55:57,599 --> 01:56:00,320 and else it should be a 200 so once 2963 01:56:00,320 --> 01:56:01,840 again a ternary statement just 2964 01:56:01,840 --> 01:56:03,599 determining the status code between a 2965 01:56:03,599 --> 01:56:04,800 200 2966 01:56:04,800 --> 01:56:07,840 and a 404 here in our function we can 2967 01:56:07,840 --> 01:56:10,480 save that and now we are close to being 2968 01:56:10,480 --> 01:56:12,960 finished we are serving all the files 2969 01:56:12,960 --> 01:56:15,199 and they are having the correct content 2970 01:56:15,199 --> 01:56:17,520 type header sent with them but we 2971 01:56:17,520 --> 01:56:19,760 haven't logged anything yet so let's go 2972 01:56:19,760 --> 01:56:21,119 ahead and 2973 01:56:21,119 --> 01:56:24,320 remove the comments here and let's copy 2974 01:56:24,320 --> 01:56:26,000 each one of these 2975 01:56:26,000 --> 01:56:27,679 and first let's take where we're going 2976 01:56:27,679 --> 01:56:30,639 to emit from before we handle the 2977 01:56:30,639 --> 01:56:31,840 listener 2978 01:56:31,840 --> 01:56:33,840 and as we do that we'll scroll up here 2979 01:56:33,840 --> 01:56:35,599 to see the server now right after the 2980 01:56:35,599 --> 01:56:37,520 console log where we're looking at the 2981 01:56:37,520 --> 01:56:40,239 url and method would be a good place 2982 01:56:40,239 --> 01:56:41,599 to emit 2983 01:56:41,599 --> 01:56:44,159 and we're going to admit a log but what 2984 01:56:44,159 --> 01:56:46,639 we want to say here is a template 2985 01:56:46,639 --> 01:56:48,880 literal instead of that generic log 2986 01:56:48,880 --> 01:56:51,360 event emitted that we had we'll go ahead 2987 01:56:51,360 --> 01:56:54,239 and pass in the request url 2988 01:56:54,239 --> 01:56:56,639 and then after the request url 2989 01:56:56,639 --> 01:56:58,719 we have a tab delimited log file so 2990 01:56:58,719 --> 01:57:00,239 we'll put in a tab 2991 01:57:00,239 --> 01:57:01,679 then we'll put in 2992 01:57:01,679 --> 01:57:02,880 the request 2993 01:57:02,880 --> 01:57:04,400 method 2994 01:57:04,400 --> 01:57:06,800 and after the request method we'll go 2995 01:57:06,800 --> 01:57:08,800 ahead and put a comma because we're 2996 01:57:08,800 --> 01:57:11,840 going to add another parameter to our 2997 01:57:11,840 --> 01:57:14,480 log event handler and so now what we 2998 01:57:14,480 --> 01:57:17,280 want to say is which file we want to put 2999 01:57:17,280 --> 01:57:19,119 this log to and this will be to the 3000 01:57:19,119 --> 01:57:21,119 request 3001 01:57:21,119 --> 01:57:22,239 log 3002 01:57:22,239 --> 01:57:27,119 dot txt so we can save that one 3003 01:57:27,119 --> 01:57:30,000 and we'll go ahead and copy the emitter 3004 01:57:30,000 --> 01:57:32,000 one more time because we also want to 3005 01:57:32,000 --> 01:57:35,119 put the emitter up in the error where we 3006 01:57:35,119 --> 01:57:37,280 could catch a server error 3007 01:57:37,280 --> 01:57:39,199 and here we'll log something different 3008 01:57:39,199 --> 01:57:42,080 so instead of request url this is going 3009 01:57:42,080 --> 01:57:42,960 to be 3010 01:57:42,960 --> 01:57:43,840 the 3011 01:57:43,840 --> 01:57:45,599 error.name 3012 01:57:45,599 --> 01:57:48,400 and instead of a tab here 3013 01:57:48,400 --> 01:57:50,639 we're going to have a colon and a space 3014 01:57:50,639 --> 01:57:54,080 and then we can put in the error 3015 01:57:54,080 --> 01:57:55,599 dot message 3016 01:57:55,599 --> 01:57:58,080 and we're going to go ahead and log this 3017 01:57:58,080 --> 01:57:59,360 to the 3018 01:57:59,360 --> 01:58:02,080 error log dot txt 3019 01:58:02,080 --> 01:58:04,480 and we can save that too now let's go 3020 01:58:04,480 --> 01:58:07,280 back and grab that listener 3021 01:58:07,280 --> 01:58:10,000 that we need 3022 01:58:11,679 --> 01:58:13,520 and get rid of these extra lines at the 3023 01:58:13,520 --> 01:58:16,000 bottom you can see we've used just about 3024 01:58:16,000 --> 01:58:18,400 a hundred lines even and let's go ahead 3025 01:58:18,400 --> 01:58:20,080 and get rid of one of the lines here 3026 01:58:20,080 --> 01:58:21,760 because by the time we finish we will 3027 01:58:21,760 --> 01:58:23,599 have 100 even that way 3028 01:58:23,599 --> 01:58:25,920 now where we'll put this is right 3029 01:58:25,920 --> 01:58:28,480 between the emitter and the port 3030 01:58:28,480 --> 01:58:31,840 and we will be listening for a log 3031 01:58:31,840 --> 01:58:34,239 but instead of just a message we're also 3032 01:58:34,239 --> 01:58:35,920 going to have 3033 01:58:35,920 --> 01:58:38,320 a file name passed in 3034 01:58:38,320 --> 01:58:40,960 and then here 3035 01:58:40,960 --> 01:58:43,119 we'll take the file name in the log 3036 01:58:43,119 --> 01:58:45,520 events function as well so let's save 3037 01:58:45,520 --> 01:58:46,639 that too 3038 01:58:46,639 --> 01:58:49,119 but we need to go back to our log events 3039 01:58:49,119 --> 01:58:50,159 function 3040 01:58:50,159 --> 01:58:52,320 and here 3041 01:58:52,320 --> 01:58:53,679 we can say 3042 01:58:53,679 --> 01:58:55,679 it's really the log name so let's say 3043 01:58:55,679 --> 01:58:58,320 log name and we need to replace the 3044 01:58:58,320 --> 01:59:00,400 event log that we used as a generic 3045 01:59:00,400 --> 01:59:03,199 example last time for log name right 3046 01:59:03,199 --> 01:59:05,599 here and with that saved we should be 3047 01:59:05,599 --> 01:59:07,360 good to go so let's go back to the 3048 01:59:07,360 --> 01:59:09,760 server just to have it pulled up and i'm 3049 01:59:09,760 --> 01:59:13,040 going to open the terminal window again 3050 01:59:13,040 --> 01:59:15,119 and looks like we're already running on 3051 01:59:15,119 --> 01:59:17,360 port 3500 3052 01:59:17,360 --> 01:59:19,280 so let's switch this over 3053 01:59:19,280 --> 01:59:23,280 and now let's reload our new page 3054 01:59:23,280 --> 01:59:26,000 and we got several things here including 3055 01:59:26,000 --> 01:59:27,679 the log statement because i believe 3056 01:59:27,679 --> 01:59:30,320 we're still logging those to the console 3057 01:59:30,320 --> 01:59:32,239 so that's kind of cool let's look in our 3058 01:59:32,239 --> 01:59:33,840 logs 3059 01:59:33,840 --> 01:59:35,920 and i'll make the terminal windows 3060 01:59:35,920 --> 01:59:36,880 smaller 3061 01:59:36,880 --> 01:59:38,639 and here's a request log so we got a 3062 01:59:38,639 --> 01:59:41,440 request we got several requests in 3063 01:59:41,440 --> 01:59:43,199 and we logged 3064 01:59:43,199 --> 01:59:45,520 exactly what we were getting the request 3065 01:59:45,520 --> 01:59:48,560 url and the method and they're all get 3066 01:59:48,560 --> 01:59:50,880 methods they're all get requests so what 3067 01:59:50,880 --> 01:59:52,400 about an error well we're going to have 3068 01:59:52,400 --> 01:59:54,880 to create an error so let's do something 3069 01:59:54,880 --> 01:59:56,320 simple like 3070 01:59:56,320 --> 01:59:58,560 change something we know is wrong so 3071 01:59:58,560 --> 02:00:00,480 where it says read file let's just 3072 02:00:00,480 --> 02:00:01,920 remove 3073 02:00:01,920 --> 02:00:04,639 the e off of read file 3074 02:00:04,639 --> 02:00:07,199 we'll save nodemon will restart the 3075 02:00:07,199 --> 02:00:08,239 server 3076 02:00:08,239 --> 02:00:10,159 and when we reload we should definitely 3077 02:00:10,159 --> 02:00:12,880 get an error 3078 02:00:12,880 --> 02:00:14,800 yes and we log the air to the console 3079 02:00:14,800 --> 02:00:16,880 because that's part of our function 3080 02:00:16,880 --> 02:00:19,520 we didn't get a page served because we 3081 02:00:19,520 --> 02:00:21,840 crashed with the error here or at least 3082 02:00:21,840 --> 02:00:23,119 it caught it and that's what we should 3083 02:00:23,119 --> 02:00:23,920 do 3084 02:00:23,920 --> 02:00:26,080 let's look at the error log and this is 3085 02:00:26,080 --> 02:00:27,840 what we would want to check out if we 3086 02:00:27,840 --> 02:00:30,159 had a problem it tells us of course the 3087 02:00:30,159 --> 02:00:33,119 date and time is the stamp and there is 3088 02:00:33,119 --> 02:00:35,679 the error name a type error and there is 3089 02:00:35,679 --> 02:00:37,040 the error message 3090 02:00:37,040 --> 02:00:40,159 fs promises dot read fill is not a 3091 02:00:40,159 --> 02:00:42,239 function and that's because we had a 3092 02:00:42,239 --> 02:00:45,520 typo with read file so we can go back 3093 02:00:45,520 --> 02:00:46,960 and fix that 3094 02:00:46,960 --> 02:00:49,599 now being in development mode 3095 02:00:49,599 --> 02:00:51,599 nodemon is going to restart due to the 3096 02:00:51,599 --> 02:00:54,080 changes and all should be well as soon 3097 02:00:54,080 --> 02:00:56,719 as the servers back up and run 3098 02:00:56,719 --> 02:00:59,679 so let's go ahead and reload 3099 02:00:59,679 --> 02:01:01,920 and we got our new page again of course 3100 02:01:01,920 --> 02:01:04,880 we can get our index page as well 3101 02:01:04,880 --> 02:01:07,280 and everything seems to be serving as it 3102 02:01:07,280 --> 02:01:10,159 should now there are some issues here as 3103 02:01:10,159 --> 02:01:12,560 we look at our typical 3104 02:01:12,560 --> 02:01:15,520 request log file what really stands out 3105 02:01:15,520 --> 02:01:17,440 is these are all get requests we didn't 3106 02:01:17,440 --> 02:01:19,679 put anything in our code 3107 02:01:19,679 --> 02:01:22,239 to handle any other type of request 3108 02:01:22,239 --> 02:01:24,719 really we just assume the request method 3109 02:01:24,719 --> 02:01:26,880 is git and of course we would have to 3110 02:01:26,880 --> 02:01:27,840 put in 3111 02:01:27,840 --> 02:01:30,639 more checks to see if it was a post or a 3112 02:01:30,639 --> 02:01:33,199 put or what we might want to do with the 3113 02:01:33,199 --> 02:01:34,880 information in interacting with a 3114 02:01:34,880 --> 02:01:36,719 database so there could be a lot more to 3115 02:01:36,719 --> 02:01:39,679 it but this is a dynamic server that 3116 02:01:39,679 --> 02:01:42,000 will serve the typical files you would 3117 02:01:42,000 --> 02:01:44,480 have with a website and even some data 3118 02:01:44,480 --> 02:01:47,440 files that we have as far as data 3119 02:01:47,440 --> 02:01:50,960 json and text file here now 3120 02:01:50,960 --> 02:01:54,400 express js makes this so much simpler 3121 02:01:54,400 --> 02:01:56,880 but this gives us a lot of context to 3122 02:01:56,880 --> 02:02:00,080 think about and what all goes into 3123 02:02:00,080 --> 02:02:02,960 creating a web server so i think you 3124 02:02:02,960 --> 02:02:05,520 will appreciate express js 3125 02:02:05,520 --> 02:02:07,280 that much more once we begin to work 3126 02:02:07,280 --> 02:02:10,239 with it now that you have created a web 3127 02:02:10,239 --> 02:02:14,400 server with node.js only 3128 02:02:14,400 --> 02:02:16,639 here we're at the webpage for express 3129 02:02:16,639 --> 02:02:18,560 and you can see the definition of 3130 02:02:18,560 --> 02:02:21,360 express it is a fast unopinionated 3131 02:02:21,360 --> 02:02:24,960 minimalist web framework for nodejs and 3132 02:02:24,960 --> 02:02:28,320 this is found at expressjs.com 3133 02:02:28,320 --> 02:02:30,239 now you need to have node installed 3134 02:02:30,239 --> 02:02:33,040 already and be able to use npm and then 3135 02:02:33,040 --> 02:02:35,920 you can add express to your project and 3136 02:02:35,920 --> 02:02:38,880 as it shows here it shows npm install 3137 02:02:38,880 --> 02:02:41,920 express and then dash dash save you no 3138 02:02:41,920 --> 02:02:43,599 longer need that you can actually just 3139 02:02:43,599 --> 02:02:45,679 type npm i 3140 02:02:45,679 --> 02:02:47,760 express today we're going to reference 3141 02:02:47,760 --> 02:02:49,840 some documentation not from the getting 3142 02:02:49,840 --> 02:02:52,480 started link but from the guide and go 3143 02:02:52,480 --> 02:02:54,719 to the routing page and this way you'll 3144 02:02:54,719 --> 02:02:56,719 know how to find the documentation for 3145 02:02:56,719 --> 02:02:58,560 what we're going over today let's go to 3146 02:02:58,560 --> 02:03:00,960 visual studio code in the last node.js 3147 02:03:00,960 --> 02:03:02,800 tutorial we build a web server using 3148 02:03:02,800 --> 02:03:05,520 only node.js i'm going to start with the 3149 02:03:05,520 --> 02:03:07,440 completed code repository from that 3150 02:03:07,440 --> 02:03:09,040 tutorial and that's what you see right 3151 02:03:09,040 --> 02:03:11,280 here in visual studio code but if you 3152 02:03:11,280 --> 02:03:13,679 don't have this repository you should be 3153 02:03:13,679 --> 02:03:15,920 able to easily follow along without it 3154 02:03:15,920 --> 02:03:18,239 or you can download or clone it from the 3155 02:03:18,239 --> 02:03:20,080 starter source code link that i'm 3156 02:03:20,080 --> 02:03:22,400 providing in the description below okay 3157 02:03:22,400 --> 02:03:24,560 i'm looking at the package.json file for 3158 02:03:24,560 --> 02:03:26,800 the repository now if you just 3159 02:03:26,800 --> 02:03:29,599 downloaded or cloned the repository 3160 02:03:29,599 --> 02:03:31,840 you're still going to need to install 3161 02:03:31,840 --> 02:03:33,520 the dependencies not that we're going to 3162 02:03:33,520 --> 02:03:35,119 use all of these today but if you just 3163 02:03:35,119 --> 02:03:36,880 want to get the repository up to where 3164 02:03:36,880 --> 02:03:38,960 we are you could go to the terminal from 3165 02:03:38,960 --> 02:03:40,960 the terminal menu and choose new 3166 02:03:40,960 --> 02:03:43,840 terminal or you can use control and the 3167 02:03:43,840 --> 02:03:46,239 backtick that's what i use in windows 3168 02:03:46,239 --> 02:03:48,560 mine defaulted to powershell and that's 3169 02:03:48,560 --> 02:03:50,560 not what i want let me switch that over 3170 02:03:50,560 --> 02:03:51,840 real quick 3171 02:03:51,840 --> 02:03:54,639 to get bash that's what i expected it to 3172 02:03:54,639 --> 02:03:55,520 be 3173 02:03:55,520 --> 02:03:57,920 and once i have a get bash terminal you 3174 02:03:57,920 --> 02:04:00,560 would type npm 3175 02:04:00,560 --> 02:04:01,760 install 3176 02:04:01,760 --> 02:04:04,159 and this will go ahead and update all of 3177 02:04:04,159 --> 02:04:06,400 the dependencies or install them if you 3178 02:04:06,400 --> 02:04:08,719 don't have them so i'll go ahead and do 3179 02:04:08,719 --> 02:04:10,880 that as well and you can see it will 3180 02:04:10,880 --> 02:04:12,960 just check my dependencies here it 3181 02:04:12,960 --> 02:04:15,040 shouldn't take long and when it's 3182 02:04:15,040 --> 02:04:16,960 finished i'll know i've got everything 3183 02:04:16,960 --> 02:04:18,560 installed that would be 3184 02:04:18,560 --> 02:04:21,520 in this repository so currently i have 3185 02:04:21,520 --> 02:04:24,560 nodemon as a dev dependency uuid and 3186 02:04:24,560 --> 02:04:28,079 date dash fns which stands for functions 3187 02:04:28,079 --> 02:04:30,400 all installed as either a dev dependency 3188 02:04:30,400 --> 02:04:33,040 or a regular dependency now what we want 3189 02:04:33,040 --> 02:04:38,400 to add is express so we'll type npm i 3190 02:04:38,400 --> 02:04:40,719 express we don't need the dash dash save 3191 02:04:40,719 --> 02:04:41,520 that's 3192 02:04:41,520 --> 02:04:43,440 understood at this point so we want to 3193 02:04:43,440 --> 02:04:47,119 install express as a dependency 3194 02:04:47,119 --> 02:04:49,119 and it just takes a few seconds and 3195 02:04:49,119 --> 02:04:51,360 express should be added and you can see 3196 02:04:51,360 --> 02:04:53,679 it here in my dependencies listed 3197 02:04:53,679 --> 02:04:57,679 between date dash fns and uuid now just 3198 02:04:57,679 --> 02:04:59,520 to update a couple of things for this 3199 02:04:59,520 --> 02:05:02,000 tutorial i'll switch the name to 3200 02:05:02,000 --> 02:05:04,800 zero six tut instead of zero five and 3201 02:05:04,800 --> 02:05:07,920 instead of node.js web server tutorial 3202 02:05:07,920 --> 02:05:09,199 i'll just put 3203 02:05:09,199 --> 02:05:11,440 express tutorial in the description but 3204 02:05:11,440 --> 02:05:13,520 those are minor changes and nothing that 3205 02:05:13,520 --> 02:05:15,520 you actually have to do i just wanted to 3206 02:05:15,520 --> 02:05:17,440 go ahead and update that let's move over 3207 02:05:17,440 --> 02:05:20,079 to the server js file or we'll make the 3208 02:05:20,079 --> 02:05:22,560 rest of our changes today now much of 3209 02:05:22,560 --> 02:05:24,000 what is here and i can close the 3210 02:05:24,000 --> 02:05:26,159 terminal as well much of what is here we 3211 02:05:26,159 --> 02:05:28,239 don't need anymore so if you want to 3212 02:05:28,239 --> 02:05:30,239 make a copy of this file or if you want 3213 02:05:30,239 --> 02:05:35,040 to rename the server.js to old server.js 3214 02:05:35,040 --> 02:05:37,199 or something like that to save this code 3215 02:05:37,199 --> 02:05:39,040 in your repository go right ahead 3216 02:05:39,040 --> 02:05:40,560 because we're going to get rid of most 3217 02:05:40,560 --> 02:05:43,440 of it we need to keep the path 3218 02:05:43,440 --> 02:05:46,239 requirement the path import at the top 3219 02:05:46,239 --> 02:05:48,000 and then i'm going to remove everything 3220 02:05:48,000 --> 02:05:48,800 else 3221 02:05:48,800 --> 02:05:51,760 down to line 12 where we keep the port 3222 02:05:51,760 --> 02:05:53,199 definition 3223 02:05:53,199 --> 02:05:54,960 and then i'm really going to remove 3224 02:05:54,960 --> 02:05:56,800 everything for now 3225 02:05:56,800 --> 02:05:58,320 except 3226 02:05:58,320 --> 02:06:01,040 where we listen here at the very bottom 3227 02:06:01,040 --> 02:06:03,599 on the specific port now we can go ahead 3228 02:06:03,599 --> 02:06:05,760 and make some changes right at the very 3229 02:06:05,760 --> 02:06:08,079 top on line one let's go ahead and 3230 02:06:08,079 --> 02:06:11,040 import express so we'll define express 3231 02:06:11,040 --> 02:06:14,840 and we'll set it equal to require 3232 02:06:14,840 --> 02:06:18,000 express after that let's define app now 3233 02:06:18,000 --> 02:06:19,760 we could use another name here like 3234 02:06:19,760 --> 02:06:21,520 we've used server 3235 02:06:21,520 --> 02:06:22,320 but 3236 02:06:22,320 --> 02:06:25,040 typically you'll see examples using the 3237 02:06:25,040 --> 02:06:27,040 word app so that's what we'll go ahead 3238 02:06:27,040 --> 02:06:29,760 and define as well and you set it equal 3239 02:06:29,760 --> 02:06:32,560 to express and you call express and so 3240 02:06:32,560 --> 02:06:34,320 now we can use the app 3241 02:06:34,320 --> 02:06:36,560 where we've been using server so we'll 3242 02:06:36,560 --> 02:06:39,760 have app dot listen at the specific port 3243 02:06:39,760 --> 02:06:41,920 that we have provided now we'll use path 3244 02:06:41,920 --> 02:06:43,360 later it's the only thing we haven't 3245 02:06:43,360 --> 02:06:45,679 used up to now and now let's come down a 3246 02:06:45,679 --> 02:06:47,679 couple of lines and we can define our 3247 02:06:47,679 --> 02:06:50,639 first route so we use app and then we 3248 02:06:50,639 --> 02:06:54,639 specify the http method that we want to 3249 02:06:54,639 --> 02:06:57,360 route so we'll use the get method here 3250 02:06:57,360 --> 02:07:00,000 so any get request that comes in 3251 02:07:00,000 --> 02:07:01,440 that looks for 3252 02:07:01,440 --> 02:07:03,520 just the root this would be our index 3253 02:07:03,520 --> 02:07:04,560 page 3254 02:07:04,560 --> 02:07:07,360 then we can specify what to do with that 3255 02:07:07,360 --> 02:07:09,520 and this takes an anonymous function 3256 02:07:09,520 --> 02:07:11,360 here or a function it doesn't have to be 3257 02:07:11,360 --> 02:07:12,800 anonymous but this is what you would 3258 02:07:12,800 --> 02:07:15,280 typically see and what i will do 3259 02:07:15,280 --> 02:07:19,040 and so now what we do with the route 3260 02:07:19,040 --> 02:07:20,880 it happens right here inside this 3261 02:07:20,880 --> 02:07:22,880 anonymous function so let's start out 3262 02:07:22,880 --> 02:07:24,800 very simply as you might expect and 3263 02:07:24,800 --> 02:07:27,760 let's just say response dot send 3264 02:07:27,760 --> 02:07:29,760 and then we can say 3265 02:07:29,760 --> 02:07:31,440 let me use a single quote just because i 3266 02:07:31,440 --> 02:07:34,320 like to better hello world 3267 02:07:34,320 --> 02:07:36,079 and that's what would be sent in 3268 02:07:36,079 --> 02:07:37,440 response 3269 02:07:37,440 --> 02:07:40,000 and so now we can go ahead and start the 3270 02:07:40,000 --> 02:07:42,400 server and we will expect to get this at 3271 02:07:42,400 --> 02:07:44,400 the index page now to start our 3272 02:07:44,400 --> 02:07:46,639 development server if you remember as we 3273 02:07:46,639 --> 02:07:50,159 specified in our package json we can use 3274 02:07:50,159 --> 02:07:52,639 our dev script that uses nodemon so 3275 02:07:52,639 --> 02:07:55,360 nodemon knows to restart anytime we make 3276 02:07:55,360 --> 02:07:57,760 changes when we save our file it will 3277 02:07:57,760 --> 02:07:59,280 identify those changes and it will 3278 02:07:59,280 --> 02:08:01,119 restart the server 3279 02:08:01,119 --> 02:08:03,520 js so let's go ahead and open our 3280 02:08:03,520 --> 02:08:05,520 terminal again and with the terminal 3281 02:08:05,520 --> 02:08:08,400 open at the bottom i'll type npm 3282 02:08:08,400 --> 02:08:10,880 run dev and it should take just a second 3283 02:08:10,880 --> 02:08:12,800 but the server will start up and we 3284 02:08:12,800 --> 02:08:15,119 expect to see that server running on 3285 02:08:15,119 --> 02:08:16,480 port message 3286 02:08:16,480 --> 02:08:18,719 inside the terminal window let me drag 3287 02:08:18,719 --> 02:08:20,960 this up and see what we get 3288 02:08:20,960 --> 02:08:24,320 there we go server running on port 3500 3289 02:08:24,320 --> 02:08:26,719 so now we'll go to chrome 3290 02:08:26,719 --> 02:08:30,079 and open up chrome let's open a new tab 3291 02:08:30,079 --> 02:08:32,800 and let's go to localhost and we've got 3292 02:08:32,800 --> 02:08:34,719 port 3500 3293 02:08:34,719 --> 02:08:37,760 and we get a very small hello world that 3294 02:08:37,760 --> 02:08:40,400 is simply delivered in text okay let's 3295 02:08:40,400 --> 02:08:42,000 minimize chrome 3296 02:08:42,000 --> 02:08:43,119 and let's 3297 02:08:43,119 --> 02:08:44,719 drag the terminal down so it doesn't 3298 02:08:44,719 --> 02:08:46,320 take up quite as much space but we'll 3299 02:08:46,320 --> 02:08:47,440 keep the 3300 02:08:47,440 --> 02:08:49,040 dev server running 3301 02:08:49,040 --> 02:08:51,360 but let's go ahead and serve our index 3302 02:08:51,360 --> 02:08:53,520 file that we have in the views folder 3303 02:08:53,520 --> 02:08:56,079 now we do have an index.html here 3304 02:08:56,079 --> 02:08:58,719 instead of just sending hello world we 3305 02:08:58,719 --> 02:09:02,079 can send a file and you do that with res 3306 02:09:02,079 --> 02:09:04,639 which is the response dot 3307 02:09:04,639 --> 02:09:07,520 send file now inside of send file 3308 02:09:07,520 --> 02:09:09,440 there's a couple of different ways we 3309 02:09:09,440 --> 02:09:11,199 can specify 3310 02:09:11,199 --> 02:09:14,400 the file to send in express one 3311 02:09:14,400 --> 02:09:16,480 is to put in 3312 02:09:16,480 --> 02:09:21,159 the path say dot slash views slash 3313 02:09:21,159 --> 02:09:22,880 index.html 3314 02:09:22,880 --> 02:09:25,840 and then we can provide options 3315 02:09:25,840 --> 02:09:28,000 and one option is to specify the root 3316 02:09:28,000 --> 02:09:30,320 directory and this is where we can use 3317 02:09:30,320 --> 02:09:31,280 that 3318 02:09:31,280 --> 02:09:33,679 dame value that is 3319 02:09:33,679 --> 02:09:35,440 native to node 3320 02:09:35,440 --> 02:09:37,679 and this will tell 3321 02:09:37,679 --> 02:09:40,639 node and express exactly where the root 3322 02:09:40,639 --> 02:09:42,719 directory name is and then inside the 3323 02:09:42,719 --> 02:09:44,880 root directory we would look inside of 3324 02:09:44,880 --> 02:09:47,520 views and there we would find the index 3325 02:09:47,520 --> 02:09:49,760 file so this should provide the file 3326 02:09:49,760 --> 02:09:52,000 let's go ahead and save 3327 02:09:52,000 --> 02:09:53,679 and it says it's restarting due to 3328 02:09:53,679 --> 02:09:55,920 changes once the server restarts will 3329 02:09:55,920 --> 02:09:58,000 bring 3330 02:09:58,000 --> 02:10:00,880 chrome back up and now that we've got it 3331 02:10:00,880 --> 02:10:03,199 let's reload 3332 02:10:03,199 --> 02:10:05,440 and now we've got our index page and i'm 3333 02:10:05,440 --> 02:10:08,239 going to resize chrome so we can 3334 02:10:08,239 --> 02:10:10,639 just see it and the console when we want 3335 02:10:10,639 --> 02:10:12,960 to at least when i pull chrome back up 3336 02:10:12,960 --> 02:10:15,599 if i can get it to drop down 3337 02:10:15,599 --> 02:10:17,040 not that we really need the whole thing 3338 02:10:17,040 --> 02:10:19,040 we don't have much content here 3339 02:10:19,040 --> 02:10:20,960 but we've got that so now i'll go ahead 3340 02:10:20,960 --> 02:10:22,719 and minimize and pull it back up again 3341 02:10:22,719 --> 02:10:24,639 when we need it so that is one way to do 3342 02:10:24,639 --> 02:10:26,719 it now let me copy this down 3343 02:10:26,719 --> 02:10:28,639 so we can just leave that in 3344 02:10:28,639 --> 02:10:30,880 commented 3345 02:10:30,880 --> 02:10:33,040 and the other way to serve a file is 3346 02:10:33,040 --> 02:10:35,360 what we're already used to doing in node 3347 02:10:35,360 --> 02:10:38,320 and that's why i usually do it this way 3348 02:10:38,320 --> 02:10:39,679 is we can use 3349 02:10:39,679 --> 02:10:41,440 path.join 3350 02:10:41,440 --> 02:10:42,480 and then 3351 02:10:42,480 --> 02:10:45,360 we specify the directory name 3352 02:10:45,360 --> 02:10:48,239 and then we join in the views folder 3353 02:10:48,239 --> 02:10:50,840 and then we go ahead and join in the 3354 02:10:50,840 --> 02:10:53,280 index.html and this will work in the 3355 02:10:53,280 --> 02:10:55,679 same way so let's go ahead and save this 3356 02:10:55,679 --> 02:10:57,760 now let's bring chrome back and we can 3357 02:10:57,760 --> 02:10:59,679 reload 3358 02:10:59,679 --> 02:11:01,679 and we should just get the same result 3359 02:11:01,679 --> 02:11:03,920 and we do we still have the index page 3360 02:11:03,920 --> 02:11:05,760 so that is more 3361 02:11:05,760 --> 02:11:07,599 how i would do it because i'm used to 3362 02:11:07,599 --> 02:11:10,159 using path.join but you could do this 3363 02:11:10,159 --> 02:11:12,560 either way okay in the file tree we can 3364 02:11:12,560 --> 02:11:15,840 see we also have new dash page.html so 3365 02:11:15,840 --> 02:11:18,800 let's just highlight all of this and i 3366 02:11:18,800 --> 02:11:21,679 press shift alt and the down arrow and i 3367 02:11:21,679 --> 02:11:24,719 can just copy all of it down and then 3368 02:11:24,719 --> 02:11:28,480 we'll remove the commented outline 3369 02:11:28,480 --> 02:11:31,199 and then we'll change our app.git route 3370 02:11:31,199 --> 02:11:33,599 here so instead of just looking for the 3371 02:11:33,599 --> 02:11:36,480 root we're looking for new dash page 3372 02:11:36,480 --> 02:11:39,920 dot html and upon that request of course 3373 02:11:39,920 --> 02:11:42,639 we want to switch this to new dash page 3374 02:11:42,639 --> 02:11:44,960 dot html as well 3375 02:11:44,960 --> 02:11:46,400 and save 3376 02:11:46,400 --> 02:11:48,960 now let's go ahead and look at chrome 3377 02:11:48,960 --> 02:11:50,639 and see what we get 3378 02:11:50,639 --> 02:11:54,560 if we type in our new dash page 3379 02:11:54,560 --> 02:11:57,360 dot html so we have slash new dash page 3380 02:11:57,360 --> 02:11:58,719 dot html 3381 02:11:58,719 --> 02:12:01,360 and we get the new page so that works 3382 02:12:01,360 --> 02:12:03,360 just fine so we can tell 3383 02:12:03,360 --> 02:12:05,679 that we're not just intercepting the 3384 02:12:05,679 --> 02:12:08,159 route right here because this is only 3385 02:12:08,159 --> 02:12:10,079 looking for the slash so even though 3386 02:12:10,079 --> 02:12:13,840 this has a slash this does make it past 3387 02:12:13,840 --> 02:12:17,280 and this is okay we get new dash page 3388 02:12:17,280 --> 02:12:20,880 dot html instead of stopping right here 3389 02:12:20,880 --> 02:12:23,920 because this does read this down like a 3390 02:12:23,920 --> 02:12:26,960 waterfall so it would check this route 3391 02:12:26,960 --> 02:12:27,920 first 3392 02:12:27,920 --> 02:12:30,079 but now we know it does make it to this 3393 02:12:30,079 --> 02:12:32,320 route however if you have the same 3394 02:12:32,320 --> 02:12:34,400 question in your mind that i would you'd 3395 02:12:34,400 --> 02:12:36,000 be thinking about what if somebody 3396 02:12:36,000 --> 02:12:38,520 requests this slash 3397 02:12:38,520 --> 02:12:41,440 index.html well this is where express 3398 02:12:41,440 --> 02:12:44,719 really helps us out because express also 3399 02:12:44,719 --> 02:12:48,239 accepts some regular expressions in the 3400 02:12:48,239 --> 02:12:51,360 routing so instead of just saying slash 3401 02:12:51,360 --> 02:12:53,760 here we can specify 3402 02:12:53,760 --> 02:12:56,159 and we can say this must 3403 02:12:56,159 --> 02:12:59,840 begin with the slash and this must 3404 02:12:59,840 --> 02:13:01,679 end with the slash 3405 02:13:01,679 --> 02:13:03,840 and then we can say 3406 02:13:03,840 --> 02:13:04,960 or 3407 02:13:04,960 --> 02:13:08,239 and then we can also say slash 3408 02:13:08,239 --> 02:13:09,440 index 3409 02:13:09,440 --> 02:13:12,800 dot html so if that request comes in it 3410 02:13:12,800 --> 02:13:17,599 will route also to the index.html 3411 02:13:17,599 --> 02:13:19,599 let's go ahead and save that and once 3412 02:13:19,599 --> 02:13:22,480 again look at chrome 3413 02:13:22,480 --> 02:13:24,719 and now let's switch back to our 3414 02:13:24,719 --> 02:13:27,280 slash without the index 3415 02:13:27,280 --> 02:13:30,280 and now if we go ahead and put slash 3416 02:13:30,280 --> 02:13:31,840 index.html 3417 02:13:31,840 --> 02:13:33,920 we still get the index and if you 3418 02:13:33,920 --> 02:13:35,840 remember when we built the node server 3419 02:13:35,840 --> 02:13:38,159 we also made it so you could just type 3420 02:13:38,159 --> 02:13:40,480 in index without the dot html let's see 3421 02:13:40,480 --> 02:13:42,560 what happens if we do that 3422 02:13:42,560 --> 02:13:45,360 oh we get cannot get index 3423 02:13:45,360 --> 02:13:47,520 so that means it could not find that 3424 02:13:47,520 --> 02:13:50,639 page and this is what express returns by 3425 02:13:50,639 --> 02:13:53,760 default if it's a 404 and cannot find 3426 02:13:53,760 --> 02:13:56,000 that and it's also worth mentioning 3427 02:13:56,000 --> 02:13:58,000 where we did a considerable amount of 3428 02:13:58,000 --> 02:14:00,480 code to handle things like the status 3429 02:14:00,480 --> 02:14:04,239 code and the content type in our nodejs 3430 02:14:04,239 --> 02:14:07,679 only web server here express 3431 02:14:07,679 --> 02:14:10,320 automatically sets the correct status 3432 02:14:10,320 --> 02:14:13,760 code and content type so for that 404 it 3433 02:14:13,760 --> 02:14:16,560 already set the 404 likewise for this 3434 02:14:16,560 --> 02:14:19,199 html and we're sending an html file it 3435 02:14:19,199 --> 02:14:21,599 already sets the correct content type 3436 02:14:21,599 --> 02:14:24,159 and of course it sends a 200 if it finds 3437 02:14:24,159 --> 02:14:26,400 it as a successful response 3438 02:14:26,400 --> 02:14:28,480 now we can add just a little bit more 3439 02:14:28,480 --> 02:14:29,760 regex 3440 02:14:29,760 --> 02:14:32,880 and that way we can make the dot html 3441 02:14:32,880 --> 02:14:34,000 optional 3442 02:14:34,000 --> 02:14:35,679 on this request 3443 02:14:35,679 --> 02:14:38,320 and if we put the dot html inside of 3444 02:14:38,320 --> 02:14:40,320 parentheses and put the question after 3445 02:14:40,320 --> 02:14:42,880 it that's exactly what it does so let's 3446 02:14:42,880 --> 02:14:44,880 go ahead and do that to our new page as 3447 02:14:44,880 --> 02:14:46,000 well 3448 02:14:46,000 --> 02:14:49,119 and so we'll put the dot html 3449 02:14:49,119 --> 02:14:51,840 and a question mark after it making the 3450 02:14:51,840 --> 02:14:54,480 dot html optional in the request 3451 02:14:54,480 --> 02:14:56,320 so we'll save both of those 3452 02:14:56,320 --> 02:14:58,800 let's come back to chrome 3453 02:14:58,800 --> 02:15:02,400 take another look and now index without 3454 02:15:02,400 --> 02:15:04,719 the dot html let's go ahead and reload 3455 02:15:04,719 --> 02:15:06,560 oh and the server is still restarting as 3456 02:15:06,560 --> 02:15:08,239 we see down here restarting due to 3457 02:15:08,239 --> 02:15:09,840 changes let's see if it's going to 3458 02:15:09,840 --> 02:15:12,239 restart it's taking just a little bit 3459 02:15:12,239 --> 02:15:13,599 may need to restart the server 3460 02:15:13,599 --> 02:15:15,520 altogether do control c to get out of 3461 02:15:15,520 --> 02:15:18,400 that and then i'll do npm run dev again 3462 02:15:18,400 --> 02:15:20,400 and we'll get that server running again 3463 02:15:20,400 --> 02:15:22,159 okay with the dev server once again 3464 02:15:22,159 --> 02:15:23,679 running i'm not sure why it hung up 3465 02:15:23,679 --> 02:15:25,440 there let's go ahead and refresh the 3466 02:15:25,440 --> 02:15:28,880 page and now we get our index page with 3467 02:15:28,880 --> 02:15:32,400 no dot html inside of the url something 3468 02:15:32,400 --> 02:15:35,199 else we handled in our node web server 3469 02:15:35,199 --> 02:15:37,440 that we created was a redirect so let's 3470 02:15:37,440 --> 02:15:40,159 do that with this express server now and 3471 02:15:40,159 --> 02:15:43,119 i will copy down the new page request 3472 02:15:43,119 --> 02:15:45,280 but now if we're requesting 3473 02:15:45,280 --> 02:15:47,760 an old dash page 3474 02:15:47,760 --> 02:15:50,400 with or without the dot html at the end 3475 02:15:50,400 --> 02:15:53,520 we want to redirect that to the new page 3476 02:15:53,520 --> 02:15:56,239 express makes this very simple so let's 3477 02:15:56,239 --> 02:15:57,440 go ahead 3478 02:15:57,440 --> 02:16:00,000 and remove a lot of what you see right 3479 02:16:00,000 --> 02:16:01,119 here 3480 02:16:01,119 --> 02:16:03,280 and we'll put in 3481 02:16:03,280 --> 02:16:05,360 res.redirect 3482 02:16:05,360 --> 02:16:07,840 and now we are sending that to the new 3483 02:16:07,840 --> 02:16:10,400 page but we do need to put a slash here 3484 02:16:10,400 --> 02:16:12,800 so we have our new page and we'll go 3485 02:16:12,800 --> 02:16:14,079 ahead and get rid of that parenthesis we 3486 02:16:14,079 --> 02:16:16,400 don't need now there is one thing 3487 02:16:16,400 --> 02:16:19,199 missing and what's missing here is the 3488 02:16:19,199 --> 02:16:21,679 response code now one will be sent by 3489 02:16:21,679 --> 02:16:25,360 express but it's going to send a 302 3490 02:16:25,360 --> 02:16:27,040 by default 3491 02:16:27,040 --> 02:16:30,320 and a 302 will not necessarily get the 3492 02:16:30,320 --> 02:16:32,638 search engine to change 3493 02:16:32,638 --> 02:16:34,558 saying it is a permanent redirect what 3494 02:16:34,558 --> 02:16:37,840 we really want is a 301 so we can do 3495 02:16:37,840 --> 02:16:40,000 that by specifying 3496 02:16:40,000 --> 02:16:42,318 the status code at the beginning of the 3497 02:16:42,318 --> 02:16:44,240 redirect and then putting a comma 3498 02:16:44,240 --> 02:16:45,519 afterwards 3499 02:16:45,519 --> 02:16:47,760 and that will go ahead and send the 3500 02:16:47,760 --> 02:16:49,679 correct status code that we need because 3501 02:16:49,679 --> 02:16:51,359 we would want the search engines to go 3502 02:16:51,359 --> 02:16:52,799 ahead and say hey this has been 3503 02:16:52,799 --> 02:16:56,160 permanently moved to new page there is 3504 02:16:56,160 --> 02:16:58,318 no more old page and we can go ahead and 3505 02:16:58,318 --> 02:17:00,000 check that in chrome as well i'll pull 3506 02:17:00,000 --> 02:17:02,478 that back up and inside of here we can 3507 02:17:02,478 --> 02:17:03,679 just type 3508 02:17:03,679 --> 02:17:05,760 old dash page we don't even need the 3509 02:17:05,760 --> 02:17:07,200 html 3510 02:17:07,200 --> 02:17:10,160 and it redirects to the new page now as 3511 02:17:10,160 --> 02:17:12,718 i mentioned express handles these routes 3512 02:17:12,718 --> 02:17:15,280 like a waterfall so at the very end of 3513 02:17:15,280 --> 02:17:17,040 the route you can essentially put your 3514 02:17:17,040 --> 02:17:20,000 default your catch all if you want to 3515 02:17:20,000 --> 02:17:21,920 and here once again we'll use just for a 3516 02:17:21,920 --> 02:17:23,359 get request 3517 02:17:23,359 --> 02:17:25,040 we'll put in the slash 3518 02:17:25,040 --> 02:17:27,359 and then an asterisk afterwards which is 3519 02:17:27,359 --> 02:17:28,718 the all 3520 02:17:28,718 --> 02:17:30,080 and that's kind of like if you're used 3521 02:17:30,080 --> 02:17:32,558 to writing sql a select all 3522 02:17:32,558 --> 02:17:35,439 so what we have here with regex is a 3523 02:17:35,439 --> 02:17:38,478 slash followed by anything will default 3524 02:17:38,478 --> 02:17:40,959 to this route let's go ahead and put in 3525 02:17:40,959 --> 02:17:42,879 our function as well so we have our 3526 02:17:42,879 --> 02:17:44,160 request 3527 02:17:44,160 --> 02:17:46,240 and response 3528 02:17:46,240 --> 02:17:48,718 and the arrow function and now inside of 3529 02:17:48,718 --> 02:17:51,439 here what we can do is essentially send 3530 02:17:51,439 --> 02:17:54,318 our file so i'm going to copy this line 3531 02:17:54,318 --> 02:17:55,840 and bring it down here and we do have a 3532 02:17:55,840 --> 02:17:58,160 custom 404 page and that's what this is 3533 02:17:58,160 --> 02:18:02,398 for because express will send a 404 3534 02:18:02,398 --> 02:18:04,718 status code and tell you it cannot find 3535 02:18:04,718 --> 02:18:06,478 what it's looking for even if you don't 3536 02:18:06,478 --> 02:18:08,638 supply anything but we want to supply a 3537 02:18:08,638 --> 02:18:10,398 custom 404 3538 02:18:10,398 --> 02:18:12,240 and so i'll go ahead and switch new page 3539 02:18:12,240 --> 02:18:15,519 to 404 here but what it won't do because 3540 02:18:15,519 --> 02:18:18,799 it will find the file to send is it will 3541 02:18:18,799 --> 02:18:21,280 not send the 404 status code it would be 3542 02:18:21,280 --> 02:18:22,638 a 200 3543 02:18:22,638 --> 02:18:24,718 because it could find the file and it 3544 02:18:24,718 --> 02:18:26,959 would be serving exactly what we told it 3545 02:18:26,959 --> 02:18:30,318 to so it wouldn't know to send a 404 at 3546 02:18:30,318 --> 02:18:33,040 this point so what we can do is chain in 3547 02:18:33,040 --> 02:18:35,959 the status code here with a status 3548 02:18:35,959 --> 02:18:37,599 404 3549 02:18:37,599 --> 02:18:40,160 and just another dot so that all changed 3550 02:18:40,160 --> 02:18:42,638 together so now we have a response with 3551 02:18:42,638 --> 02:18:45,760 a status code of 404 and we're sending 3552 02:18:45,760 --> 02:18:49,200 our custom 404 file let's save this and 3553 02:18:49,200 --> 02:18:51,040 go back and just request something that 3554 02:18:51,040 --> 02:18:52,879 doesn't exist so 3555 02:18:52,879 --> 02:18:55,679 up here instead of new page 3556 02:18:55,679 --> 02:18:58,160 i'll just put page 3557 02:18:58,160 --> 02:19:00,799 and we get our custom 404 what we 3558 02:19:00,799 --> 02:19:02,718 haven't talked about yet are route 3559 02:19:02,718 --> 02:19:05,120 handlers so i'll just put that here in a 3560 02:19:05,120 --> 02:19:06,718 comma 3561 02:19:06,718 --> 02:19:08,478 we have route handlers and that's 3562 02:19:08,478 --> 02:19:11,200 exactly what these are following our app 3563 02:19:11,200 --> 02:19:13,679 get route where we tell it what we are 3564 02:19:13,679 --> 02:19:16,398 routing these anonymous function 3565 02:19:16,398 --> 02:19:18,478 expressions that we have here following 3566 02:19:18,478 --> 02:19:22,000 the route are route handlers and we can 3567 02:19:22,000 --> 02:19:23,920 chain those or use more than one of 3568 02:19:23,920 --> 02:19:25,840 those so let me give you an example of 3569 02:19:25,840 --> 02:19:28,638 that so we'll have another app get and 3570 02:19:28,638 --> 02:19:31,359 let's say the request comes in for 3571 02:19:31,359 --> 02:19:33,519 hello.html 3572 02:19:33,519 --> 02:19:35,040 and let's go ahead and make that 3573 02:19:35,040 --> 02:19:36,718 optional as well since we're doing it 3574 02:19:36,718 --> 02:19:38,638 with the others so we have the dot html 3575 02:19:38,638 --> 02:19:40,000 is optional 3576 02:19:40,000 --> 02:19:42,638 and after the quote we put a comma and 3577 02:19:42,638 --> 02:19:44,000 now we have 3578 02:19:44,000 --> 02:19:45,840 our function expression here the 3579 02:19:45,840 --> 02:19:48,318 anonymous function and it's request 3580 02:19:48,318 --> 02:19:52,640 response but then we also add in a next 3581 02:19:52,640 --> 02:19:54,800 and this is an arrow function 3582 02:19:54,800 --> 02:19:57,120 inside this function i'm going to 3583 02:19:57,120 --> 02:20:00,160 console.log and just say 3584 02:20:00,160 --> 02:20:01,680 attempted 3585 02:20:01,680 --> 02:20:05,200 to load hello.html 3586 02:20:05,200 --> 02:20:07,600 and then after our console log or our 3587 02:20:07,600 --> 02:20:09,600 call to the console log 3588 02:20:09,600 --> 02:20:12,160 we call next and what that does is it 3589 02:20:12,160 --> 02:20:14,880 moves on to the next handler or the next 3590 02:20:14,880 --> 02:20:17,920 expression and you won't see this often 3591 02:20:17,920 --> 02:20:20,000 but you can put a comma 3592 02:20:20,000 --> 02:20:21,920 and then just put the next 3593 02:20:21,920 --> 02:20:23,600 function right afterwards so here we'll 3594 02:20:23,600 --> 02:20:25,920 have a request and response we could 3595 02:20:25,920 --> 02:20:27,520 have a next if we were going to chain 3596 02:20:27,520 --> 02:20:29,200 another one after this 3597 02:20:29,200 --> 02:20:31,840 but if this is the last one in the chain 3598 02:20:31,840 --> 02:20:33,439 it won't have a next it would just be 3599 02:20:33,439 --> 02:20:35,720 the request and response and here we can 3600 02:20:35,720 --> 02:20:37,760 response.send and we'll just go back to 3601 02:20:37,760 --> 02:20:38,960 our 3602 02:20:38,960 --> 02:20:41,120 hello world message that we had at the 3603 02:20:41,120 --> 02:20:42,479 very beginning 3604 02:20:42,479 --> 02:20:44,880 and if we save this we'll go ahead and 3605 02:20:44,880 --> 02:20:46,960 be able to see the console at the same 3606 02:20:46,960 --> 02:20:50,240 time let's pull up chrome 3607 02:20:50,240 --> 02:20:52,479 and over here we'll go ahead and request 3608 02:20:52,479 --> 02:20:54,479 the hello 3609 02:20:54,479 --> 02:20:56,880 and now we get hello world in our small 3610 02:20:56,880 --> 02:20:58,399 text in the browser 3611 02:20:58,399 --> 02:21:00,560 and we also got our console log message 3612 02:21:00,560 --> 02:21:04,080 attempted to load hello.html so you can 3613 02:21:04,080 --> 02:21:04,960 see 3614 02:21:04,960 --> 02:21:08,160 we're calling one function and then when 3615 02:21:08,160 --> 02:21:09,680 next is called 3616 02:21:09,680 --> 02:21:12,319 it goes on and calls the next function 3617 02:21:12,319 --> 02:21:14,960 in the chain now let me show you another 3618 02:21:14,960 --> 02:21:16,720 way that you might see functions chained 3619 02:21:16,720 --> 02:21:19,120 together and this way is 3620 02:21:19,120 --> 02:21:21,359 really the way you would probably see it 3621 02:21:21,359 --> 02:21:24,240 more often i'm just pasting in some very 3622 02:21:24,240 --> 02:21:26,160 very basic functions here so i've got 3623 02:21:26,160 --> 02:21:28,479 function one that has request response 3624 02:21:28,479 --> 02:21:31,200 and next and logs of the console 1 and 3625 02:21:31,200 --> 02:21:34,399 calls next function 2 same construction 3626 02:21:34,399 --> 02:21:36,240 logs of the console 2. 3627 02:21:36,240 --> 02:21:39,200 function 3 doesn't have a next it logs 3628 02:21:39,200 --> 02:21:42,080 the console 3 it also sends the response 3629 02:21:42,080 --> 02:21:43,120 finished 3630 02:21:43,120 --> 02:21:45,760 and then it is complete so how would we 3631 02:21:45,760 --> 02:21:48,399 use that in a route all three of those 3632 02:21:48,399 --> 02:21:50,640 so instead of the commas we saw chaining 3633 02:21:50,640 --> 02:21:52,840 the anonymous functions we can say 3634 02:21:52,840 --> 02:21:55,600 app.get and let's say this is for the 3635 02:21:55,600 --> 02:21:58,000 route let's just call it chain and we'll 3636 02:21:58,000 --> 02:22:00,080 go ahead and make the html optional as 3637 02:22:00,080 --> 02:22:01,040 well 3638 02:22:01,040 --> 02:22:03,040 and after that 3639 02:22:03,040 --> 02:22:04,960 you can provide 3640 02:22:04,960 --> 02:22:08,000 these handlers in an array so we'll just 3641 02:22:08,000 --> 02:22:10,319 have one two 3642 02:22:10,319 --> 02:22:11,760 and three 3643 02:22:11,760 --> 02:22:14,240 in this array so all the route handlers 3644 02:22:14,240 --> 02:22:15,520 are provided 3645 02:22:15,520 --> 02:22:17,680 right here in this simple line and we're 3646 02:22:17,680 --> 02:22:19,280 calling all of the functions that are 3647 02:22:19,280 --> 02:22:22,160 above so let's go ahead and save and go 3648 02:22:22,160 --> 02:22:26,160 to the route chain in the browser 3649 02:22:26,160 --> 02:22:27,920 i'll switch from hello up here at the 3650 02:22:27,920 --> 02:22:29,439 top 3651 02:22:29,439 --> 02:22:31,600 type in chain and you can see in the 3652 02:22:31,600 --> 02:22:33,760 console we got one two three and in the 3653 02:22:33,760 --> 02:22:35,920 browser we got finished in the small 3654 02:22:35,920 --> 02:22:38,160 text because it just sent the text 3655 02:22:38,160 --> 02:22:41,120 finished and these route handlers work 3656 02:22:41,120 --> 02:22:43,680 in a way that is very similar to what we 3657 02:22:43,680 --> 02:22:46,080 would call middleware and middleware is 3658 02:22:46,080 --> 02:22:47,680 what we'll be covering in the next 3659 02:22:47,680 --> 02:22:50,160 tutorial but you'll get used to seeing 3660 02:22:50,160 --> 02:22:52,560 this call to next so it can call the 3661 02:22:52,560 --> 02:22:55,120 middleware likewise we'll make a change 3662 02:22:55,120 --> 02:22:58,160 for our 404 as well because right now 3663 02:22:58,160 --> 02:23:01,120 we're just delivering a custom 404 3664 02:23:01,120 --> 02:23:03,040 for get requests 3665 02:23:03,040 --> 02:23:05,359 and that come in right here with this 3666 02:23:05,359 --> 02:23:07,760 path but we can go ahead and improve 3667 02:23:07,760 --> 02:23:10,160 that using middleware as well 3668 02:23:10,160 --> 02:23:12,800 and we'll also talk about how to serve 3669 02:23:12,800 --> 02:23:15,840 those static deliverables like our style 3670 02:23:15,840 --> 02:23:18,960 sheet the css any javascript that we 3671 02:23:18,960 --> 02:23:21,680 want to go to the browser and anything 3672 02:23:21,680 --> 02:23:24,319 else like images that we have currently 3673 02:23:24,319 --> 02:23:26,080 broken on the page when we go to new 3674 02:23:26,080 --> 02:23:28,800 page or some data so we'll talk about 3675 02:23:28,800 --> 02:23:31,439 how to provide all of those as we cover 3676 02:23:31,439 --> 02:23:34,640 middleware in the next tutorial 3677 02:23:34,640 --> 02:23:37,280 today we'll learn about middleware in 3678 02:23:37,280 --> 02:23:39,680 the expressjs framework we're going to 3679 02:23:39,680 --> 02:23:41,680 start out with the code repository from 3680 02:23:41,680 --> 02:23:43,920 the last tutorial but if you don't have 3681 02:23:43,920 --> 02:23:45,760 it you should be able to easily follow 3682 02:23:45,760 --> 02:23:48,720 along without it or you can download or 3683 02:23:48,720 --> 02:23:50,640 clone the starter source code from the 3684 02:23:50,640 --> 02:23:52,479 link i'm providing in the description 3685 02:23:52,479 --> 02:23:55,840 below so what is middleware it's really 3686 02:23:55,840 --> 02:23:57,840 anything between the request and the 3687 02:23:57,840 --> 02:23:59,920 response so the route handlers we 3688 02:23:59,920 --> 02:24:02,399 created in the last tutorial are really 3689 02:24:02,399 --> 02:24:04,800 middleware too and we can see one of 3690 02:24:04,800 --> 02:24:06,399 those right here that i'll highlight on 3691 02:24:06,399 --> 02:24:08,800 the page we've got a route handler for 3692 02:24:08,800 --> 02:24:11,439 the new page route so you could consider 3693 02:24:11,439 --> 02:24:13,280 this middleware and of course we covered 3694 02:24:13,280 --> 02:24:14,880 different things you can do with these 3695 02:24:14,880 --> 02:24:17,200 route handlers including chaining them 3696 02:24:17,200 --> 02:24:19,600 as we see right here i've got a route 3697 02:24:19,600 --> 02:24:22,080 handler a comma and then a second 3698 02:24:22,080 --> 02:24:25,120 handler or we chain them in an array by 3699 02:24:25,120 --> 02:24:28,000 creating three separate handlers here 3700 02:24:28,000 --> 02:24:30,000 and then putting them in an array in the 3701 02:24:30,000 --> 02:24:31,760 route right here so just a little bit of 3702 02:24:31,760 --> 02:24:34,880 a recap but those are overall considered 3703 02:24:34,880 --> 02:24:36,960 route handlers even though they are 3704 02:24:36,960 --> 02:24:39,359 essentially middleware so what about 3705 02:24:39,359 --> 02:24:42,319 creating some specific middle well where 3706 02:24:42,319 --> 02:24:44,880 there are three types of middleware 3707 02:24:44,880 --> 02:24:47,200 there's built-in middleware there's 3708 02:24:47,200 --> 02:24:49,520 custom middleware and there's middleware 3709 02:24:49,520 --> 02:24:51,840 from third parties of course the custom 3710 02:24:51,840 --> 02:24:54,479 middleware is what we create ourselves 3711 02:24:54,479 --> 02:24:56,560 so let's get started with the built-in 3712 02:24:56,560 --> 02:24:58,319 middleware i'm going to create some 3713 02:24:58,319 --> 02:25:00,240 space right underneath the port 3714 02:25:00,240 --> 02:25:02,720 definition and i'm going to paste in 3715 02:25:02,720 --> 02:25:05,200 some built-in middleware and the first 3716 02:25:05,200 --> 02:25:07,040 thing we should notice here is that is 3717 02:25:07,040 --> 02:25:09,520 app.use 3718 02:25:09,520 --> 02:25:13,359 and app.use is what we often use 3719 02:25:13,359 --> 02:25:16,479 to apply middleware to all routes that 3720 02:25:16,479 --> 02:25:19,120 are coming in and of course just like 3721 02:25:19,120 --> 02:25:22,000 our http methods get and of course we 3722 02:25:22,000 --> 02:25:24,640 could use post delete put and other 3723 02:25:24,640 --> 02:25:27,520 methods as well just like those methods 3724 02:25:27,520 --> 02:25:28,720 this all 3725 02:25:28,720 --> 02:25:31,479 works as a waterfall so if we put 3726 02:25:31,479 --> 02:25:34,720 app.use above our routes 3727 02:25:34,720 --> 02:25:37,439 then this will apply to all routes that 3728 02:25:37,439 --> 02:25:38,640 come in 3729 02:25:38,640 --> 02:25:40,560 and you can see in here we're calling 3730 02:25:40,560 --> 02:25:44,160 express dot url encoded and then this 3731 02:25:44,160 --> 02:25:45,920 receives an options 3732 02:25:45,920 --> 02:25:48,479 options object there i can say it 3733 02:25:48,479 --> 02:25:51,040 and extended is set to false and that's 3734 02:25:51,040 --> 02:25:52,720 usually how you see it you can look in 3735 02:25:52,720 --> 02:25:54,560 the documentation if you want to dive 3736 02:25:54,560 --> 02:25:56,240 deeper into that but that is typically 3737 02:25:56,240 --> 02:25:58,640 what you see so what is this middleware 3738 02:25:58,640 --> 02:25:59,439 for 3739 02:25:59,439 --> 02:26:02,960 well it's for handling url encoded data 3740 02:26:02,960 --> 02:26:04,319 in other words 3741 02:26:04,319 --> 02:26:06,479 form data so when that comes in in the 3742 02:26:06,479 --> 02:26:07,920 url 3743 02:26:07,920 --> 02:26:10,160 then you can pull the data out as a 3744 02:26:10,160 --> 02:26:12,160 parameter so we need to add this 3745 02:26:12,160 --> 02:26:14,640 middleware that is built into express in 3746 02:26:14,640 --> 02:26:18,000 order to get that data when form data is 3747 02:26:18,000 --> 02:26:20,399 submitted now i'll add an additional 3748 02:26:20,399 --> 02:26:21,920 layer of middleware 3749 02:26:21,920 --> 02:26:24,720 and this is for json as you might expect 3750 02:26:24,720 --> 02:26:28,479 so not form data but if json data is 3751 02:26:28,479 --> 02:26:29,920 submitted we need to be able to get 3752 02:26:29,920 --> 02:26:32,720 those parameters or that data out of the 3753 02:26:32,720 --> 02:26:35,040 submission and we use this middleware to 3754 02:26:35,040 --> 02:26:39,319 do it so once again app.use and then 3755 02:26:39,319 --> 02:26:41,760 express.json and now this is applied to 3756 02:26:41,760 --> 02:26:44,160 all routes as it comes in and finally 3757 02:26:44,160 --> 02:26:45,920 for the last piece of built-in 3758 02:26:45,920 --> 02:26:48,479 middleware that we'll apply today we're 3759 02:26:48,479 --> 02:26:50,479 going to serve static files and this is 3760 02:26:50,479 --> 02:26:52,080 important because if you remember from 3761 02:26:52,080 --> 02:26:53,920 the last tutorial we were not yet 3762 02:26:53,920 --> 02:26:57,280 applying css we had a broken image in 3763 02:26:57,280 --> 02:26:59,600 our new page and files that should be 3764 02:26:59,600 --> 02:27:01,760 available to the public were not 3765 02:27:01,760 --> 02:27:03,520 available and that's what this 3766 02:27:03,520 --> 02:27:05,439 middleware does it serves static files 3767 02:27:05,439 --> 02:27:08,319 so once again we start with app use 3768 02:27:08,319 --> 02:27:11,840 and then it's express dot static and we 3769 02:27:11,840 --> 02:27:14,080 supply the path and here i'm using path 3770 02:27:14,080 --> 02:27:16,479 join as we have many times before we 3771 02:27:16,479 --> 02:27:19,040 supply the directory name value 3772 02:27:19,040 --> 02:27:20,080 and then 3773 02:27:20,080 --> 02:27:22,080 the public folder is what we're going to 3774 02:27:22,080 --> 02:27:24,560 put all of those in so when we save this 3775 02:27:24,560 --> 02:27:29,399 we now need to create a public folder 3776 02:27:30,000 --> 02:27:32,000 and inside this public folder we need to 3777 02:27:32,000 --> 02:27:35,439 drag our css folder and we need to drag 3778 02:27:35,439 --> 02:27:38,560 our image folder 3779 02:27:38,800 --> 02:27:41,280 and let's go ahead and create one more 3780 02:27:41,280 --> 02:27:44,880 folder in here and let's call this text 3781 02:27:44,880 --> 02:27:47,439 and then from our data folder let's pull 3782 02:27:47,439 --> 02:27:50,240 down the data dot text and put it inside 3783 02:27:50,240 --> 02:27:52,960 the text folder so now we have some 3784 02:27:52,960 --> 02:27:55,439 files that we want readily available for 3785 02:27:55,439 --> 02:27:57,359 the public and express will 3786 02:27:57,359 --> 02:27:58,880 automatically 3787 02:27:58,880 --> 02:28:01,840 route those files there because this is 3788 02:28:01,840 --> 02:28:04,479 applied before our other routes so it 3789 02:28:04,479 --> 02:28:07,760 will search the public directory for the 3790 02:28:07,760 --> 02:28:10,160 requests before it moves to these other 3791 02:28:10,160 --> 02:28:12,160 routes let's go ahead and open a 3792 02:28:12,160 --> 02:28:14,160 terminal window from the terminal menu 3793 02:28:14,160 --> 02:28:16,399 you can choose new terminal or like me 3794 02:28:16,399 --> 02:28:18,000 control and 3795 02:28:18,000 --> 02:28:20,080 the backtick and it opens up the 3796 02:28:20,080 --> 02:28:23,280 terminal here i've got a bash window and 3797 02:28:23,280 --> 02:28:25,280 that should work just fine i'm going to 3798 02:28:25,280 --> 02:28:28,880 type npm run dev to start nodemon for 3799 02:28:28,880 --> 02:28:31,200 our development server and this should 3800 02:28:31,200 --> 02:28:33,120 launch everything and yes we're already 3801 02:28:33,120 --> 02:28:38,399 running on port 3500. let's go to chrome 3802 02:28:38,399 --> 02:28:40,319 and i've got a blank window open in 3803 02:28:40,319 --> 02:28:43,280 chrome so we can type localhost 3500 was 3804 02:28:43,280 --> 02:28:45,600 already there and we get index and it's 3805 02:28:45,600 --> 02:28:48,560 already applying the css so now let's go 3806 02:28:48,560 --> 02:28:50,720 ahead and comment out 3807 02:28:50,720 --> 02:28:53,120 the middleware 3808 02:28:53,120 --> 02:28:55,680 so we're not using those static files 3809 02:28:55,680 --> 02:28:58,800 and let's reload our index page 3810 02:28:58,800 --> 02:29:01,520 and see what we get now there's no css 3811 02:29:01,520 --> 02:29:04,160 applied so it is absolutely finding 3812 02:29:04,160 --> 02:29:06,800 those public files and applying them 3813 02:29:06,800 --> 02:29:09,600 however we didn't really change the path 3814 02:29:09,600 --> 02:29:12,240 in those files and we should now i'll 3815 02:29:12,240 --> 02:29:15,120 reload this and we do see it's applied 3816 02:29:15,120 --> 02:29:17,840 but let's take a look at those files 3817 02:29:17,840 --> 02:29:20,640 here's our index and we were going up 3818 02:29:20,640 --> 02:29:22,720 out of the views directory 3819 02:29:22,720 --> 02:29:25,280 and then into the css directory and 3820 02:29:25,280 --> 02:29:27,439 that's not really there so this is being 3821 02:29:27,439 --> 02:29:30,319 a little forgiving let's just get rid of 3822 02:29:30,319 --> 02:29:32,640 that because we know this path is 3823 02:29:32,640 --> 02:29:34,640 available to the public of course after 3824 02:29:34,640 --> 02:29:37,520 the directory name there so after the 3825 02:29:37,520 --> 02:29:41,439 root of the url it should just be css 3826 02:29:41,439 --> 02:29:42,399 style 3827 02:29:42,399 --> 02:29:45,760 dot css and we can double check that by 3828 02:29:45,760 --> 02:29:48,240 going to chrome 3829 02:29:48,240 --> 02:29:51,600 and from there we can just type slash 3830 02:29:51,600 --> 02:29:53,120 css 3831 02:29:53,120 --> 02:29:56,240 style dot css sure enough we pull up our 3832 02:29:56,240 --> 02:29:58,399 css file in the browser so it is 3833 02:29:58,399 --> 02:30:00,479 available to the public let's go ahead 3834 02:30:00,479 --> 02:30:02,800 and make these same changes 3835 02:30:02,800 --> 02:30:05,840 on our new page dot html we can get rid 3836 02:30:05,840 --> 02:30:08,399 of the dot dot slash for image for the 3837 02:30:08,399 --> 02:30:10,319 image directory and for the css 3838 02:30:10,319 --> 02:30:13,200 directory and save that and also in our 3839 02:30:13,200 --> 02:30:14,960 custom 404 3840 02:30:14,960 --> 02:30:17,359 we can get rid of the dot dot slash and 3841 02:30:17,359 --> 02:30:20,080 save now all of those should be working 3842 02:30:20,080 --> 02:30:21,760 fine and they really were before but 3843 02:30:21,760 --> 02:30:24,080 this cleans it up now let's go back to 3844 02:30:24,080 --> 02:30:27,200 the server js and let's consider adding 3845 02:30:27,200 --> 02:30:29,280 some custom middleware that we write 3846 02:30:29,280 --> 02:30:32,080 ourselves let's create a custom logger 3847 02:30:32,080 --> 02:30:34,160 and we want it before anything else 3848 02:30:34,160 --> 02:30:38,000 because it we could put it i guess after 3849 02:30:38,000 --> 02:30:40,399 the static files are served but then we 3850 02:30:40,399 --> 02:30:42,880 would not log the request for the css or 3851 02:30:42,880 --> 02:30:45,200 the images or anything like that so if 3852 02:30:45,200 --> 02:30:47,359 we put it at the very top 3853 02:30:47,359 --> 02:30:49,040 then we'll see the requests for 3854 02:30:49,040 --> 02:30:51,359 everything as they come through so we'll 3855 02:30:51,359 --> 02:30:54,080 just call this custom 3856 02:30:54,080 --> 02:30:55,359 middleware 3857 02:30:55,359 --> 02:30:59,680 logger and now we'll start with app.use 3858 02:30:59,680 --> 02:31:01,920 and inside of app.use we'll put an 3859 02:31:01,920 --> 02:31:04,399 anonymous function that gets a request 3860 02:31:04,399 --> 02:31:07,280 response and since we're creating this 3861 02:31:07,280 --> 02:31:10,880 it needs to have next so we can move on 3862 02:31:10,880 --> 02:31:13,600 notice the built-in middleware did not 3863 02:31:13,600 --> 02:31:16,160 need next call it's already built into 3864 02:31:16,160 --> 02:31:19,200 that but we need to call next inside of 3865 02:31:19,200 --> 02:31:21,520 our custom middleware so let's log to 3866 02:31:21,520 --> 02:31:24,160 the console inside this logger 3867 02:31:24,160 --> 02:31:26,800 and we'll go ahead and log the request 3868 02:31:26,800 --> 02:31:29,359 method where here's the request dot 3869 02:31:29,359 --> 02:31:30,479 method 3870 02:31:30,479 --> 02:31:32,960 and then give a space and we'll log the 3871 02:31:32,960 --> 02:31:35,840 request dot path as well 3872 02:31:35,840 --> 02:31:37,520 and now with both of those sent to the 3873 02:31:37,520 --> 02:31:40,800 log the only other thing we need to do 3874 02:31:40,800 --> 02:31:44,160 is called next so let's save this 3875 02:31:44,160 --> 02:31:47,840 and now let's go back to chrome 3876 02:31:47,840 --> 02:31:49,680 and we can still see our console here 3877 02:31:49,680 --> 02:31:51,600 let's request the page 3878 02:31:51,600 --> 02:31:54,240 and you can see we get the get request 3879 02:31:54,240 --> 02:31:56,640 here and there's the slash which is the 3880 02:31:56,640 --> 02:31:59,760 home page and we also see the request 3881 02:31:59,760 --> 02:32:02,160 for the css file while logging to the 3882 02:32:02,160 --> 02:32:04,960 console is okay for our middleware 3883 02:32:04,960 --> 02:32:07,520 logger what we really would like to do 3884 02:32:07,520 --> 02:32:10,080 is create a log file and we've already 3885 02:32:10,080 --> 02:32:12,960 created a log events function 3886 02:32:12,960 --> 02:32:15,359 back when we were working just with node 3887 02:32:15,359 --> 02:32:17,680 so we can go ahead and use this log 3888 02:32:17,680 --> 02:32:19,120 events function 3889 02:32:19,120 --> 02:32:21,760 in our logger as well let's create a new 3890 02:32:21,760 --> 02:32:23,920 folder in the file tree and call this 3891 02:32:23,920 --> 02:32:27,040 folder middleware 3892 02:32:27,200 --> 02:32:30,479 from here let's pull the log events 3893 02:32:30,479 --> 02:32:34,640 dot js file straight into that directory 3894 02:32:34,640 --> 02:32:37,200 and now that we have log events there we 3895 02:32:37,200 --> 02:32:39,920 really don't need to change much and we 3896 02:32:39,920 --> 02:32:42,560 are already exporting log events as well 3897 02:32:42,560 --> 02:32:45,120 so that's good what we will need to 3898 02:32:45,120 --> 02:32:47,680 change is where it writes the logs 3899 02:32:47,680 --> 02:32:49,439 because we don't want it to create a 3900 02:32:49,439 --> 02:32:52,640 logs folder in the middleware folder so 3901 02:32:52,640 --> 02:32:56,160 if our with our path dot join 3902 02:32:56,160 --> 02:32:57,760 what we need to do 3903 02:32:57,760 --> 02:33:01,439 is add in one extra spot here i did not 3904 02:33:01,439 --> 02:33:04,319 mean to hit return there we go and 3905 02:33:04,319 --> 02:33:06,720 just go one directory up with the two 3906 02:33:06,720 --> 02:33:07,680 dots 3907 02:33:07,680 --> 02:33:10,399 and now that we've got that i'll copy it 3908 02:33:10,399 --> 02:33:13,359 and we need to add it to this path 3909 02:33:13,359 --> 02:33:16,240 and this path as well so when the logs 3910 02:33:16,240 --> 02:33:18,399 directory is created 3911 02:33:18,399 --> 02:33:20,319 it is here where we already have our 3912 02:33:20,319 --> 02:33:22,720 logs directory from the past rather than 3913 02:33:22,720 --> 02:33:24,240 being created in the middleware 3914 02:33:24,240 --> 02:33:27,200 directory and now we need to take log 3915 02:33:27,200 --> 02:33:28,319 events 3916 02:33:28,319 --> 02:33:31,439 and import it into our server so we'll 3917 02:33:31,439 --> 02:33:34,080 scroll to the top and let's just put it 3918 02:33:34,080 --> 02:33:35,760 underneath path 3919 02:33:35,760 --> 02:33:38,880 and we'll say const log events 3920 02:33:38,880 --> 02:33:42,080 equals require 3921 02:33:42,080 --> 02:33:44,080 dot slash 3922 02:33:44,080 --> 02:33:46,479 middleware slash 3923 02:33:46,479 --> 02:33:48,560 log events 3924 02:33:48,560 --> 02:33:51,600 and that completes that we can save 3925 02:33:51,600 --> 02:33:54,319 and now we can use log events 3926 02:33:54,319 --> 02:33:56,640 inside of our custom middleware logger 3927 02:33:56,640 --> 02:33:58,800 let's put it right above the console log 3928 02:33:58,800 --> 02:34:01,040 and we have log events and log events 3929 02:34:01,040 --> 02:34:03,280 accepts two parameters the first is the 3930 02:34:03,280 --> 02:34:06,800 message and the next is the file that it 3931 02:34:06,800 --> 02:34:09,040 should write to or create 3932 02:34:09,040 --> 02:34:11,760 so inside for the message we'll create a 3933 02:34:11,760 --> 02:34:13,840 template literal and i'm just going to 3934 02:34:13,840 --> 02:34:16,160 paste this in so you can see the full 3935 02:34:16,160 --> 02:34:18,880 message it is the request method and 3936 02:34:18,880 --> 02:34:22,080 then we put a slash t for tab so we have 3937 02:34:22,080 --> 02:34:24,640 a tab delimited log file and then it's 3938 02:34:24,640 --> 02:34:27,439 the request.headers.org 3939 02:34:27,439 --> 02:34:30,319 which should be saying where the request 3940 02:34:30,319 --> 02:34:33,600 is coming from what website sent it to 3941 02:34:33,600 --> 02:34:34,399 us 3942 02:34:34,399 --> 02:34:35,680 and then 3943 02:34:35,680 --> 02:34:39,680 we should get what url was requested so 3944 02:34:39,680 --> 02:34:41,920 say it's a get method and the request is 3945 02:34:41,920 --> 02:34:44,800 coming from google.com and they are 3946 02:34:44,800 --> 02:34:48,319 requesting our index page after that we 3947 02:34:48,319 --> 02:34:51,280 need to tell it what file to create or 3948 02:34:51,280 --> 02:34:52,319 write to 3949 02:34:52,319 --> 02:34:54,160 and we'll call this rec for 3950 02:34:54,160 --> 02:34:57,160 request.log.txt 3951 02:34:57,680 --> 02:35:00,319 and now we should be good to go with our 3952 02:35:00,319 --> 02:35:02,640 log events function let's go back to 3953 02:35:02,640 --> 02:35:04,560 chrome one more time 3954 02:35:04,560 --> 02:35:07,280 and let's go ahead and request our index 3955 02:35:07,280 --> 02:35:08,479 page 3956 02:35:08,479 --> 02:35:10,960 it was requested we see the requests 3957 02:35:10,960 --> 02:35:14,399 here and now we also 3958 02:35:14,399 --> 02:35:16,640 see a request log has been created and 3959 02:35:16,640 --> 02:35:18,399 there are two requests 3960 02:35:18,399 --> 02:35:21,120 it was a get request both times and it 3961 02:35:21,120 --> 02:35:23,280 was undefined because we're just running 3962 02:35:23,280 --> 02:35:25,680 our node server right here on our local 3963 02:35:25,680 --> 02:35:26,560 host 3964 02:35:26,560 --> 02:35:28,240 and you can see what was requested as 3965 02:35:28,240 --> 02:35:32,000 well the slash for index and the css 3966 02:35:32,000 --> 02:35:33,680 style.css 3967 02:35:33,680 --> 02:35:35,680 and while this works once again let's 3968 02:35:35,680 --> 02:35:37,680 look at the server.js 3969 02:35:37,680 --> 02:35:40,800 while this works we're really importing 3970 02:35:40,800 --> 02:35:42,960 a function to use inside of our 3971 02:35:42,960 --> 02:35:45,920 anonymous function that is our logger 3972 02:35:45,920 --> 02:35:48,160 that has the request response and next 3973 02:35:48,160 --> 02:35:50,800 as any middleware should so we can clean 3974 02:35:50,800 --> 02:35:52,880 this up a little bit let me go ahead and 3975 02:35:52,880 --> 02:35:54,720 copy the anonymous function and 3976 02:35:54,720 --> 02:35:56,640 everything inside of it 3977 02:35:56,640 --> 02:35:59,520 ctrl c to do that and now we'll go back 3978 02:35:59,520 --> 02:36:02,800 to the log events.js file and here we 3979 02:36:02,800 --> 02:36:04,160 can define 3980 02:36:04,160 --> 02:36:06,560 another function just called logger 3981 02:36:06,560 --> 02:36:09,120 we'll set that equal to this anonymous 3982 02:36:09,120 --> 02:36:13,120 function and now logger can be our 3983 02:36:13,120 --> 02:36:16,000 custom middleware that we export so down 3984 02:36:16,000 --> 02:36:18,000 here instead of just exporting log 3985 02:36:18,000 --> 02:36:20,319 events which we'll use later or we 3986 02:36:20,319 --> 02:36:22,960 wouldn't need to right now at all 3987 02:36:22,960 --> 02:36:25,359 i'll go ahead and put both inside of 3988 02:36:25,359 --> 02:36:27,920 curly braces once again we won't be 3989 02:36:27,920 --> 02:36:30,960 using log events anymore inside of the 3990 02:36:30,960 --> 02:36:33,120 server js though i'm just exporting this 3991 02:36:33,120 --> 02:36:36,319 for use later when the need arises so 3992 02:36:36,319 --> 02:36:38,800 back at the server js instead of log 3993 02:36:38,800 --> 02:36:41,120 events we'll need curly braces now that 3994 02:36:41,120 --> 02:36:43,439 there's two functions being exported 3995 02:36:43,439 --> 02:36:46,080 and we'll import logger from the log 3996 02:36:46,080 --> 02:36:49,120 events file and now with logger being 3997 02:36:49,120 --> 02:36:52,000 imported and all of this logic inside of 3998 02:36:52,000 --> 02:36:54,960 logger we can just say app use 3999 02:36:54,960 --> 02:36:57,280 logger 4000 02:36:57,280 --> 02:37:00,160 and now this is much cleaner and should 4001 02:37:00,160 --> 02:37:02,640 still work in the same way so let's pull 4002 02:37:02,640 --> 02:37:04,640 up chrome once again 4003 02:37:04,640 --> 02:37:06,800 and let's request the index 4004 02:37:06,800 --> 02:37:09,840 and we see the console logs still work 4005 02:37:09,840 --> 02:37:12,160 let's look at the request log 4006 02:37:12,160 --> 02:37:14,080 and yes we have two more requests in the 4007 02:37:14,080 --> 02:37:16,240 request log once again notice that it 4008 02:37:16,240 --> 02:37:18,240 says undefined here is the origin 4009 02:37:18,240 --> 02:37:21,040 because we're just using our local host 4010 02:37:21,040 --> 02:37:23,520 server here as we pull this up so let's 4011 02:37:23,520 --> 02:37:26,240 make a request from another domain i'll 4012 02:37:26,240 --> 02:37:28,560 pull chrome back up 4013 02:37:28,560 --> 02:37:29,680 and now 4014 02:37:29,680 --> 02:37:33,680 a new tab and we'll go to google 4015 02:37:33,680 --> 02:37:35,600 and on google let me go ahead and expand 4016 02:37:35,600 --> 02:37:37,920 this and open up the dev tools with 4017 02:37:37,920 --> 02:37:40,720 control shift i now that i have dev 4018 02:37:40,720 --> 02:37:42,399 tools open 4019 02:37:42,399 --> 02:37:43,920 what i want to do 4020 02:37:43,920 --> 02:37:46,640 is just call fetch and i'm not really 4021 02:37:46,640 --> 02:37:48,319 interested in the data we're just going 4022 02:37:48,319 --> 02:37:51,040 to fetch the destination and let's see 4023 02:37:51,040 --> 02:37:52,880 what we get 4024 02:37:52,880 --> 02:37:57,040 we get a course error it says no access 4025 02:37:57,040 --> 02:38:00,080 control allow origin but let's go ahead 4026 02:38:00,080 --> 02:38:03,200 and check our log file 4027 02:38:03,200 --> 02:38:04,399 and 4028 02:38:04,399 --> 02:38:07,920 our log file does show google.com as the 4029 02:38:07,920 --> 02:38:10,399 request origin so you may have seen that 4030 02:38:10,399 --> 02:38:12,319 error we got before from google that 4031 02:38:12,319 --> 02:38:14,720 showed the cores error and that stands 4032 02:38:14,720 --> 02:38:17,920 for cross origin resource sharing and 4033 02:38:17,920 --> 02:38:20,160 now this will lead us to third party 4034 02:38:20,160 --> 02:38:22,080 middleware so we can install a 4035 02:38:22,080 --> 02:38:24,640 dependency i pressed control c to stop 4036 02:38:24,640 --> 02:38:28,240 our dev server i'm going to type npm i 4037 02:38:28,240 --> 02:38:29,600 for install 4038 02:38:29,600 --> 02:38:32,960 and then cores cors and this should take 4039 02:38:32,960 --> 02:38:35,439 just a second to install and we'll be 4040 02:38:35,439 --> 02:38:37,920 able to see the new dependency in our 4041 02:38:37,920 --> 02:38:41,280 package json when this is finished 4042 02:38:41,280 --> 02:38:43,040 and it says it's finished already let's 4043 02:38:43,040 --> 02:38:45,600 check the package json and here we see 4044 02:38:45,600 --> 02:38:48,640 cores now added as a dependency so let's 4045 02:38:48,640 --> 02:38:51,920 go back to the server.js file 4046 02:38:51,920 --> 02:38:54,800 okay so we're in the server.js file here 4047 02:38:54,800 --> 02:38:57,760 and we need to go ahead and import 4048 02:38:57,760 --> 02:38:59,920 cores so we define 4049 02:38:59,920 --> 02:39:02,080 cores and set it equal to 4050 02:39:02,080 --> 02:39:03,680 require 4051 02:39:03,680 --> 02:39:05,200 cores 4052 02:39:05,200 --> 02:39:07,920 once we have that we need to apply the 4053 02:39:07,920 --> 02:39:10,479 middleware so we want it to come as soon 4054 02:39:10,479 --> 02:39:13,359 as possible but after the logger so 4055 02:39:13,359 --> 02:39:16,479 let's go ahead and apply cores here so 4056 02:39:16,479 --> 02:39:19,120 we'll say app.use 4057 02:39:19,120 --> 02:39:21,120 and then we can just call cores inside 4058 02:39:21,120 --> 02:39:23,520 of the app.use i'll go ahead and put a 4059 02:39:23,520 --> 02:39:26,319 note here that core stands for 4060 02:39:26,319 --> 02:39:27,280 cross 4061 02:39:27,280 --> 02:39:28,640 origin 4062 02:39:28,640 --> 02:39:30,000 resource 4063 02:39:30,000 --> 02:39:31,200 sharing 4064 02:39:31,200 --> 02:39:33,600 let's save that and now go back and 4065 02:39:33,600 --> 02:39:36,399 start our dev server again with npm 4066 02:39:36,399 --> 02:39:38,800 run dev and once the dev server has 4067 02:39:38,800 --> 02:39:41,280 started we'll pull up google it says 4068 02:39:41,280 --> 02:39:43,520 it's good to go it looks like 4069 02:39:43,520 --> 02:39:45,840 and let's go back to google and let's 4070 02:39:45,840 --> 02:39:48,640 make the same request 4071 02:39:48,640 --> 02:39:50,800 and now there's no cores error we get a 4072 02:39:50,800 --> 02:39:53,359 promise pending of course from fetch as 4073 02:39:53,359 --> 02:39:56,160 we expect you there but there's no error 4074 02:39:56,160 --> 02:39:58,960 and that's the main thing is we can 4075 02:39:58,960 --> 02:40:02,000 request data now from the google.com 4076 02:40:02,000 --> 02:40:04,560 domain and it doesn't create a course 4077 02:40:04,560 --> 02:40:07,040 error let's minimize chrome 4078 02:40:07,040 --> 02:40:09,040 and let's look at our request log again 4079 02:40:09,040 --> 02:40:11,760 and we once again see google in there so 4080 02:40:11,760 --> 02:40:15,120 the domain the request origin did come 4081 02:40:15,120 --> 02:40:17,760 from google.com okay now let's go a 4082 02:40:17,760 --> 02:40:19,680 little deeper into course i don't want 4083 02:40:19,680 --> 02:40:22,240 to make this a full tutorial on cores 4084 02:40:22,240 --> 02:40:25,040 because that can get pretty deep however 4085 02:40:25,040 --> 02:40:28,800 cores for cross origin resource sharing 4086 02:40:28,800 --> 02:40:30,640 is something that does need to be 4087 02:40:30,640 --> 02:40:32,880 applied or you will get those errors 4088 02:40:32,880 --> 02:40:35,279 however this just leaves it open if you 4089 02:40:35,279 --> 02:40:38,560 had an open to the public api 4090 02:40:38,560 --> 02:40:41,120 this would be fine you're using cores 4091 02:40:41,120 --> 02:40:43,439 and everyone can use it but for many 4092 02:40:43,439 --> 02:40:45,840 applications that's not really what you 4093 02:40:45,840 --> 02:40:48,960 want so let's create a white list and 4094 02:40:48,960 --> 02:40:51,920 we'll define it with const 4095 02:40:51,920 --> 02:40:53,600 white list 4096 02:40:53,600 --> 02:40:56,640 and inside of that we can have more than 4097 02:40:56,640 --> 02:40:58,080 one domain 4098 02:40:58,080 --> 02:41:02,160 so let's say https 4099 02:41:02,160 --> 02:41:06,080 slash and then www dot your site 4100 02:41:06,080 --> 02:41:09,439 whatever your dot com is whatever 4101 02:41:09,439 --> 02:41:12,800 application web application domain will 4102 02:41:12,800 --> 02:41:16,319 that will access this back end node 4103 02:41:16,319 --> 02:41:18,080 server that's where you want to put this 4104 02:41:18,080 --> 02:41:20,960 domain here but say you might also have 4105 02:41:20,960 --> 02:41:23,120 a variation maybe you've got a variation 4106 02:41:23,120 --> 02:41:26,560 without the www dot or 4107 02:41:26,560 --> 02:41:28,640 maybe you're working on a local dev 4108 02:41:28,640 --> 02:41:31,680 server and you launch from go live down 4109 02:41:31,680 --> 02:41:34,160 here click to run live server live 4110 02:41:34,160 --> 02:41:37,200 server always runs on http 4111 02:41:37,200 --> 02:41:40,000 colon slash 4112 02:41:40,279 --> 02:41:43,040 127.0.0.1 which is your computer and 4113 02:41:43,040 --> 02:41:45,840 then it gets a port like 5500 i believe 4114 02:41:45,840 --> 02:41:47,439 is the default so 4115 02:41:47,439 --> 02:41:50,560 maybe as you're building a react app 4116 02:41:50,560 --> 02:41:52,880 you're running your react app on this 4117 02:41:52,880 --> 02:41:55,200 port or a different port on your local 4118 02:41:55,200 --> 02:41:59,319 host or you've used the ip address which 4119 02:41:59,319 --> 02:42:01,279 127.0.0.1 is the same as having 4120 02:42:01,279 --> 02:42:04,479 localhost there anything like that so 4121 02:42:04,479 --> 02:42:06,560 let's add those to the whitelist and of 4122 02:42:06,560 --> 02:42:08,880 course we should add 4123 02:42:08,880 --> 02:42:11,279 http slash 4124 02:42:11,279 --> 02:42:14,160 local host and we're using 3500 here 4125 02:42:14,160 --> 02:42:17,040 today now you would take these out after 4126 02:42:17,040 --> 02:42:18,800 development but of course you would 4127 02:42:18,800 --> 02:42:22,160 leave in whatever domains should be able 4128 02:42:22,160 --> 02:42:22,880 to 4129 02:42:22,880 --> 02:42:26,080 access your data or your back-end server 4130 02:42:26,080 --> 02:42:28,000 and that would be your web application 4131 02:42:28,000 --> 02:42:30,080 say your react application is at 4132 02:42:30,080 --> 02:42:32,720 whatever domain you would leave that in 4133 02:42:32,720 --> 02:42:35,439 here so we've created a list that is 4134 02:42:35,439 --> 02:42:38,720 allowed to access the back end that 4135 02:42:38,720 --> 02:42:41,040 cores will not prevent so now we need to 4136 02:42:41,040 --> 02:42:43,279 create the function that will allow 4137 02:42:43,279 --> 02:42:45,439 cores to do this and this is all 4138 02:42:45,439 --> 02:42:47,840 contained within the cores options so 4139 02:42:47,840 --> 02:42:49,600 that's what we'll call this lowercase 4140 02:42:49,600 --> 02:42:51,680 cores and then camelcase here with a 4141 02:42:51,680 --> 02:42:54,399 capital o for options and we set this 4142 02:42:54,399 --> 02:42:55,760 equal to 4143 02:42:55,760 --> 02:42:58,880 an options object and the first property 4144 02:42:58,880 --> 02:43:01,680 is origin and origin can accept a 4145 02:43:01,680 --> 02:43:03,359 function so we have an anonymous 4146 02:43:03,359 --> 02:43:04,880 function here 4147 02:43:04,880 --> 02:43:07,279 and it gets the origin which is not the 4148 02:43:07,279 --> 02:43:08,720 same as the origin right here 4149 02:43:08,720 --> 02:43:11,439 necessarily this is the origin 4150 02:43:11,439 --> 02:43:13,920 coming from whoever requested it say 4151 02:43:13,920 --> 02:43:15,439 google.com 4152 02:43:15,439 --> 02:43:17,760 and then a callback function 4153 02:43:17,760 --> 02:43:19,920 and again the two origins a little 4154 02:43:19,920 --> 02:43:21,520 confusing but it's right from the 4155 02:43:21,520 --> 02:43:23,680 documentation so that's kind of how we 4156 02:43:23,680 --> 02:43:26,240 need to do it so we've got the origin 4157 02:43:26,240 --> 02:43:28,319 there with the anonymous function and we 4158 02:43:28,319 --> 02:43:30,560 can say if 4159 02:43:30,560 --> 02:43:33,040 the white list 4160 02:43:33,040 --> 02:43:36,399 dot index of 4161 02:43:36,399 --> 02:43:38,640 and then we pass in the origin that is 4162 02:43:38,640 --> 02:43:41,840 passed in to the function 4163 02:43:41,840 --> 02:43:44,800 if that index is not equal to 4164 02:43:44,800 --> 02:43:47,040 -1 in other words 4165 02:43:47,040 --> 02:43:50,399 if the domain is in the white list 4166 02:43:50,399 --> 02:43:51,840 then we're going to go ahead and let it 4167 02:43:51,840 --> 02:43:53,120 pass 4168 02:43:53,120 --> 02:43:56,000 and then we can do the callback 4169 02:43:56,000 --> 02:43:58,000 and the first parameter in the callback 4170 02:43:58,000 --> 02:44:02,080 is null which is usually the error so 4171 02:44:02,080 --> 02:44:04,399 there's no error and then we set the 4172 02:44:04,399 --> 02:44:07,359 second one to true and that just means 4173 02:44:07,359 --> 02:44:10,080 that the origin will be sent back saying 4174 02:44:10,080 --> 02:44:12,319 yes that's the same origin and it is 4175 02:44:12,319 --> 02:44:15,520 allowed and then we can have an else 4176 02:44:15,520 --> 02:44:18,479 and in the callback here 4177 02:44:18,479 --> 02:44:21,680 we have an error instead of null and we 4178 02:44:21,680 --> 02:44:23,520 create a new error and we can just say 4179 02:44:23,520 --> 02:44:24,720 not 4180 02:44:24,720 --> 02:44:26,399 allowed by 4181 02:44:26,399 --> 02:44:27,600 cores 4182 02:44:27,600 --> 02:44:29,680 and so that is the message to go along 4183 02:44:29,680 --> 02:44:32,800 with the error now that is the first 4184 02:44:32,800 --> 02:44:34,240 property here 4185 02:44:34,240 --> 02:44:36,319 in the course options we need to add one 4186 02:44:36,319 --> 02:44:39,840 more so we put a comma and then we put 4187 02:44:39,840 --> 02:44:43,359 lowercase here options 4188 02:44:44,840 --> 02:44:46,399 success 4189 02:44:46,399 --> 02:44:50,000 status there it is and we send a 200. 4190 02:44:50,000 --> 02:44:53,279 and with that we can save 4191 02:44:53,279 --> 02:44:54,960 and now that we've created the course 4192 02:44:54,960 --> 02:44:57,920 options we need to pass those 4193 02:44:57,920 --> 02:45:01,279 in right here and notice we did not 4194 02:45:01,279 --> 02:45:03,760 include google in these course options 4195 02:45:03,760 --> 02:45:07,040 now google could access without an error 4196 02:45:07,040 --> 02:45:09,760 before we put in the options let's go 4197 02:45:09,760 --> 02:45:11,680 back and make the same request from 4198 02:45:11,680 --> 02:45:15,120 google and see what happens now 4199 02:45:15,120 --> 02:45:17,600 we once again have a course error so 4200 02:45:17,600 --> 02:45:19,760 let's go back 4201 02:45:19,760 --> 02:45:22,800 and let's go ahead and change your site 4202 02:45:22,800 --> 02:45:24,560 to 4203 02:45:24,560 --> 02:45:27,520 google.com 4204 02:45:27,680 --> 02:45:29,760 server is restarting it looks like it's 4205 02:45:29,760 --> 02:45:31,600 hanging up nope it's going to go ahead 4206 02:45:31,600 --> 02:45:33,120 and do it there we got it 4207 02:45:33,120 --> 02:45:35,120 and we go back to google once again and 4208 02:45:35,120 --> 02:45:37,040 now it's in the white list 4209 02:45:37,040 --> 02:45:39,520 and we fetch and there is no error so we 4210 02:45:39,520 --> 02:45:41,200 know our function is working as we 4211 02:45:41,200 --> 02:45:43,359 expect it to so when we come back and 4212 02:45:43,359 --> 02:45:46,640 look once again you can change this to 4213 02:45:46,640 --> 02:45:48,720 your domain whatever your project 4214 02:45:48,720 --> 02:45:50,399 wherever it will be hosted say you've 4215 02:45:50,399 --> 02:45:52,560 created it in react or view or even 4216 02:45:52,560 --> 02:45:55,520 vanilla javascript spelt even whatever 4217 02:45:55,520 --> 02:45:57,840 you might create a front end project 4218 02:45:57,840 --> 02:46:00,240 with that will access this back end you 4219 02:46:00,240 --> 02:46:03,200 want to put that domain in here or maybe 4220 02:46:03,200 --> 02:46:04,880 variations of again remember you might 4221 02:46:04,880 --> 02:46:08,160 not have the www or it might be optional 4222 02:46:08,160 --> 02:46:10,479 anything like that so that allows you to 4223 02:46:10,479 --> 02:46:14,319 put in domains that can access the 4224 02:46:14,319 --> 02:46:16,720 routes and otherwise cores will prevent 4225 02:46:16,720 --> 02:46:19,120 them so very useful third-party 4226 02:46:19,120 --> 02:46:21,279 middleware i want to go back to chrome 4227 02:46:21,279 --> 02:46:24,000 one more time and create that error one 4228 02:46:24,000 --> 02:46:25,680 more time now that we've taken google 4229 02:46:25,680 --> 02:46:27,600 out of the white list 4230 02:46:27,600 --> 02:46:29,120 at least i thought we took google out of 4231 02:46:29,120 --> 02:46:31,120 the white list maybe i didn't save after 4232 02:46:31,120 --> 02:46:32,800 we did that that's exactly what happened 4233 02:46:32,800 --> 02:46:35,279 i hadn't saved the server js file yet 4234 02:46:35,279 --> 02:46:36,960 save here 4235 02:46:36,960 --> 02:46:39,040 nodemon is restarting 4236 02:46:39,040 --> 02:46:41,840 it's taking it just a little bit 4237 02:46:41,840 --> 02:46:43,920 maybe i need to go ahead and restart it 4238 02:46:43,920 --> 02:46:45,439 once again every now and then i have to 4239 02:46:45,439 --> 02:46:49,040 restart it manually so npm run dev 4240 02:46:49,040 --> 02:46:51,279 get the nodemon server up and running 4241 02:46:51,279 --> 02:46:53,520 i'll pull up chrome 4242 02:46:53,520 --> 02:46:55,200 and let's see if we can get that error 4243 02:46:55,200 --> 02:46:56,319 again 4244 02:46:56,319 --> 02:46:59,279 so yes we've got the cores error here 4245 02:46:59,279 --> 02:47:01,359 now let's look back in 4246 02:47:01,359 --> 02:47:04,399 visual studio code as well 4247 02:47:04,399 --> 02:47:06,080 and you can see we got the server's 4248 02:47:06,080 --> 02:47:08,000 still running but we got this big error 4249 02:47:08,000 --> 02:47:10,080 and up here it says our message error 4250 02:47:10,080 --> 02:47:11,840 not allowed by cores 4251 02:47:11,840 --> 02:47:14,560 and this huge error here now we can 4252 02:47:14,560 --> 02:47:17,840 handle errors in express as well but 4253 02:47:17,840 --> 02:47:20,560 express is already handling them it has 4254 02:47:20,560 --> 02:47:23,120 the built-in error handler by default 4255 02:47:23,120 --> 02:47:24,479 and that's what's happening when we 4256 02:47:24,479 --> 02:47:26,640 throw this error it is going there 4257 02:47:26,640 --> 02:47:29,439 however let's go ahead and add a little 4258 02:47:29,439 --> 02:47:31,279 custom error handling 4259 02:47:31,279 --> 02:47:33,120 so we scroll down to the very end 4260 02:47:33,120 --> 02:47:34,880 remember it's a full chain so we want 4261 02:47:34,880 --> 02:47:37,520 this after everything else even after 4262 02:47:37,520 --> 02:47:41,120 our 404 here i'm going to paste this in 4263 02:47:41,120 --> 02:47:43,359 and you can see i'm using app use once 4264 02:47:43,359 --> 02:47:45,680 again and then this is an anonymous 4265 02:47:45,680 --> 02:47:47,359 function 4266 02:47:47,359 --> 02:47:49,359 and it doesn't just get the request 4267 02:47:49,359 --> 02:47:52,240 response and next it also receives an 4268 02:47:52,240 --> 02:47:53,840 error parameter 4269 02:47:53,840 --> 02:47:55,920 and then we can log to the console the 4270 02:47:55,920 --> 02:47:57,920 error stack if we want to or just the 4271 02:47:57,920 --> 02:48:00,640 message or whatever we can go ahead and 4272 02:48:00,640 --> 02:48:04,399 send a status 500 server error and we 4273 02:48:04,399 --> 02:48:06,640 can send the message to be displayed in 4274 02:48:06,640 --> 02:48:09,279 the browser so i'm going to save this 4275 02:48:09,279 --> 02:48:10,880 and we'll go back to chrome but we won't 4276 02:48:10,880 --> 02:48:13,520 use google anymore let's go ahead and 4277 02:48:13,520 --> 02:48:15,920 just use the page that we have here and 4278 02:48:15,920 --> 02:48:18,000 i'll see if i can resize there we go i'm 4279 02:48:18,000 --> 02:48:19,920 going to reload 4280 02:48:19,920 --> 02:48:22,160 and look we got a not allowed by cores 4281 02:48:22,160 --> 02:48:24,560 even though we requested our index page 4282 02:48:24,560 --> 02:48:27,439 but our custom error did work and it 4283 02:48:27,439 --> 02:48:30,800 sent this to the browser now why did we 4284 02:48:30,800 --> 02:48:33,520 get that well let's go ahead and look at 4285 02:48:33,520 --> 02:48:36,240 the log here's our request log 4286 02:48:36,240 --> 02:48:39,040 you can see it's once again undefined 4287 02:48:39,040 --> 02:48:41,040 so undefined is a problem because that's 4288 02:48:41,040 --> 02:48:43,439 not in the white list so during 4289 02:48:43,439 --> 02:48:45,760 development you want to go ahead and 4290 02:48:45,760 --> 02:48:49,840 make one modification to this function 4291 02:48:49,840 --> 02:48:52,000 right here after the minus one go ahead 4292 02:48:52,000 --> 02:48:55,279 and put in the or operator and then 4293 02:48:55,279 --> 02:48:57,680 say no origin essentially the 4294 02:48:57,680 --> 02:48:59,760 exclamation mark 4295 02:48:59,760 --> 02:49:02,560 origin which would be the equivalent of 4296 02:49:02,560 --> 02:49:04,479 undefined or false 4297 02:49:04,479 --> 02:49:07,359 and we save that and now it should work 4298 02:49:07,359 --> 02:49:09,840 now after development we probably want 4299 02:49:09,840 --> 02:49:12,160 to remove that as well just like we want 4300 02:49:12,160 --> 02:49:13,200 to remove 4301 02:49:13,200 --> 02:49:14,800 some of the 4302 02:49:14,800 --> 02:49:17,040 development urls that we have up here in 4303 02:49:17,040 --> 02:49:18,960 the white list so now let's go back to 4304 02:49:18,960 --> 02:49:20,160 chrome 4305 02:49:20,160 --> 02:49:22,560 and see if we can reload our index page 4306 02:49:22,560 --> 02:49:24,560 yes and now everything is working as we 4307 02:49:24,560 --> 02:49:27,439 expect it to and we can also tidy up 4308 02:49:27,439 --> 02:49:29,200 that custom error handler so let's 4309 02:49:29,200 --> 02:49:31,439 scroll back down to it 4310 02:49:31,439 --> 02:49:33,200 and we see the function that we have 4311 02:49:33,200 --> 02:49:35,359 here inside of app use i'm just going to 4312 02:49:35,359 --> 02:49:37,840 copy that complete function 4313 02:49:37,840 --> 02:49:40,399 and now inside the middleware folder 4314 02:49:40,399 --> 02:49:42,399 let's create a new file and let's call 4315 02:49:42,399 --> 02:49:44,720 this error handler with a capital h 4316 02:49:44,720 --> 02:49:47,080 there so camelcase 4317 02:49:47,080 --> 02:49:51,120 errorhandlerjs and now let's define our 4318 02:49:51,120 --> 02:49:53,120 error handler 4319 02:49:53,120 --> 02:49:54,800 and set it equal to 4320 02:49:54,800 --> 02:49:56,720 and we'll remove 4321 02:49:56,720 --> 02:49:58,960 the function keyword and we'll go ahead 4322 02:49:58,960 --> 02:50:02,240 and put an arrow after this 4323 02:50:02,240 --> 02:50:04,960 and we've defined our error handler but 4324 02:50:04,960 --> 02:50:07,040 what we want to do that we're not doing 4325 02:50:07,040 --> 02:50:10,800 yet is also create an error log and this 4326 02:50:10,800 --> 02:50:12,720 is where exporting the log events 4327 02:50:12,720 --> 02:50:15,200 function that we left here 4328 02:50:15,200 --> 02:50:17,840 comes in very handy so now we need to 4329 02:50:17,840 --> 02:50:20,640 import that here not the logger but the 4330 02:50:20,640 --> 02:50:24,479 log events so let's define 4331 02:50:25,439 --> 02:50:28,319 log events inside of the curly braces 4332 02:50:28,319 --> 02:50:31,760 set that equal to require 4333 02:50:31,760 --> 02:50:33,680 dot slash 4334 02:50:33,680 --> 02:50:35,520 log events 4335 02:50:35,520 --> 02:50:37,359 and now that we're importing log events 4336 02:50:37,359 --> 02:50:39,359 we can go ahead and use that in the 4337 02:50:39,359 --> 02:50:41,760 function as well so we'll call log 4338 02:50:41,760 --> 02:50:43,439 events right here before we log to the 4339 02:50:43,439 --> 02:50:45,359 console and once again this function 4340 02:50:45,359 --> 02:50:47,600 gets a message first and then the name 4341 02:50:47,600 --> 02:50:49,600 of the file that it needs to write to or 4342 02:50:49,600 --> 02:50:51,200 create so this will be a template 4343 02:50:51,200 --> 02:50:53,040 literal and i'm just going to paste in 4344 02:50:53,040 --> 02:50:56,080 the values once again here instead of 4345 02:50:56,080 --> 02:50:57,920 anything from the request such as the 4346 02:50:57,920 --> 02:51:00,240 method like we were logging before we're 4347 02:51:00,240 --> 02:51:02,160 going to log the error name 4348 02:51:02,160 --> 02:51:04,720 and the error message and then we'll put 4349 02:51:04,720 --> 02:51:07,840 a comma and go ahead and specify error 4350 02:51:07,840 --> 02:51:11,520 log dot txt as to where we want to write 4351 02:51:11,520 --> 02:51:14,160 the error to and we can save this but 4352 02:51:14,160 --> 02:51:16,399 we're not quite finished yet because we 4353 02:51:16,399 --> 02:51:18,479 need to export this function so we do 4354 02:51:18,479 --> 02:51:20,800 module dot exports 4355 02:51:20,800 --> 02:51:23,040 set this equal to 4356 02:51:23,040 --> 02:51:25,040 error handler 4357 02:51:25,040 --> 02:51:27,760 and now save and now we need to import 4358 02:51:27,760 --> 02:51:31,120 error handler into our server so let's 4359 02:51:31,120 --> 02:51:34,240 scroll to the top of our server js and 4360 02:51:34,240 --> 02:51:36,080 right underneath logger i'm just going 4361 02:51:36,080 --> 02:51:39,120 to go ahead and copy this down 4362 02:51:39,120 --> 02:51:41,279 and let's change this 4363 02:51:41,279 --> 02:51:42,560 to 4364 02:51:42,560 --> 02:51:43,760 error 4365 02:51:43,760 --> 02:51:44,960 handler 4366 02:51:44,960 --> 02:51:47,120 and instead of log events we're 4367 02:51:47,120 --> 02:51:50,240 importing from middleware 4368 02:51:50,240 --> 02:51:51,840 error handler 4369 02:51:51,840 --> 02:51:53,200 and save 4370 02:51:53,200 --> 02:51:56,720 now we need to use the error handler 4371 02:51:56,720 --> 02:51:58,720 where we previously had the larger 4372 02:51:58,720 --> 02:52:01,120 function in this file so it will be much 4373 02:52:01,120 --> 02:52:03,439 cleaner so let's go ahead and select 4374 02:52:03,439 --> 02:52:05,600 this full function 4375 02:52:05,600 --> 02:52:07,920 delete that and paste in 4376 02:52:07,920 --> 02:52:09,600 error handler 4377 02:52:09,600 --> 02:52:11,120 and we can save 4378 02:52:11,120 --> 02:52:13,279 and we got an error the app crashed 4379 02:52:13,279 --> 02:52:15,920 let's see what we've got so if i scroll 4380 02:52:15,920 --> 02:52:16,800 up 4381 02:52:16,800 --> 02:52:20,479 it says app.use requires a middleware 4382 02:52:20,479 --> 02:52:22,880 function so something about the function 4383 02:52:22,880 --> 02:52:25,840 probably didn't get imported correctly 4384 02:52:25,840 --> 02:52:28,000 let's go ahead and scroll up to the top 4385 02:52:28,000 --> 02:52:30,399 and look at our import statement and oh 4386 02:52:30,399 --> 02:52:32,800 yeah we were using curly braces around 4387 02:52:32,800 --> 02:52:35,040 logger because it was one of two 4388 02:52:35,040 --> 02:52:36,800 functions from the file but from the 4389 02:52:36,800 --> 02:52:39,760 error handler it's the only one so let's 4390 02:52:39,760 --> 02:52:41,680 go ahead and remove those curly braces 4391 02:52:41,680 --> 02:52:44,240 around error handler and see how 4392 02:52:44,240 --> 02:52:46,399 everything goes it's restarting the 4393 02:52:46,399 --> 02:52:48,720 server right now and server running on 4394 02:52:48,720 --> 02:52:51,760 port 3500 that part looks good so with 4395 02:52:51,760 --> 02:52:53,520 the server running the best way i know 4396 02:52:53,520 --> 02:52:55,760 to create an error would be to go ahead 4397 02:52:55,760 --> 02:52:58,560 and remove this or 4398 02:52:58,560 --> 02:53:01,120 no origin for now 4399 02:53:01,120 --> 02:53:03,200 and we'll go ahead and save and then 4400 02:53:03,200 --> 02:53:05,279 we'll try to reload 4401 02:53:05,279 --> 02:53:08,399 the index page once again in chrome so 4402 02:53:08,399 --> 02:53:10,960 it's starting the node.js server 4403 02:53:10,960 --> 02:53:14,640 and now that it's running let's reload 4404 02:53:14,640 --> 02:53:17,279 and we've got a not allowed by cores as 4405 02:53:17,279 --> 02:53:20,640 we expected an error in the console 4406 02:53:20,640 --> 02:53:23,680 and now we have an error log file over 4407 02:53:23,680 --> 02:53:25,920 here in the file tree and we can see we 4408 02:53:25,920 --> 02:53:28,880 got our error not allowed by cores 4409 02:53:28,880 --> 02:53:29,720 at 4410 02:53:29,720 --> 02:53:31,279 1827 4411 02:53:31,279 --> 02:53:32,800 so let's go ahead and look at the 4412 02:53:32,800 --> 02:53:34,479 request log and we should be able to 4413 02:53:34,479 --> 02:53:36,479 line that up with what request happened 4414 02:53:36,479 --> 02:53:39,439 here as well and we can't so everything 4415 02:53:39,439 --> 02:53:42,080 there is working as expected so now that 4416 02:53:42,080 --> 02:53:45,040 the error is logging let's go ahead and 4417 02:53:45,040 --> 02:53:46,720 put that or 4418 02:53:46,720 --> 02:53:49,359 no origin statement back in for now so 4419 02:53:49,359 --> 02:53:52,399 we do not keep getting rejected by cores 4420 02:53:52,399 --> 02:53:53,920 we can go back 4421 02:53:53,920 --> 02:53:56,240 and we can once again request the index 4422 02:53:56,240 --> 02:53:57,200 page 4423 02:53:57,200 --> 02:53:59,840 and everything should work well when the 4424 02:53:59,840 --> 02:54:01,600 server restarts there we go we've got 4425 02:54:01,600 --> 02:54:04,399 the index page now okay there's one more 4426 02:54:04,399 --> 02:54:06,640 thing to do before we're finished today 4427 02:54:06,640 --> 02:54:10,000 and that is to compare app use 4428 02:54:10,000 --> 02:54:13,279 to app dot all and that's what we're 4429 02:54:13,279 --> 02:54:15,120 going to do right here as we change up 4430 02:54:15,120 --> 02:54:18,479 the 404 just a little bit so with app 4431 02:54:18,479 --> 02:54:19,439 use 4432 02:54:19,439 --> 02:54:22,319 we could do something like this 4433 02:54:22,319 --> 02:54:24,720 app use and specify 4434 02:54:24,720 --> 02:54:27,200 here at the end of the chain everything 4435 02:54:27,200 --> 02:54:29,120 basically that came in 4436 02:54:29,120 --> 02:54:31,680 from the slash which would be the root 4437 02:54:31,680 --> 02:54:34,240 but app used does not accept regex and 4438 02:54:34,240 --> 02:54:36,800 also app use overall 4439 02:54:36,800 --> 02:54:39,680 is more likely to be used for middleware 4440 02:54:39,680 --> 02:54:41,680 but app.all 4441 02:54:41,680 --> 02:54:43,760 is used for routing and this means it 4442 02:54:43,760 --> 02:54:48,240 will apply to all http methods so it 4443 02:54:48,240 --> 02:54:52,000 also does accept regex so what we can do 4444 02:54:52,000 --> 02:54:54,240 inside of this is just put an asterisk 4445 02:54:54,240 --> 02:54:56,880 it's at the very end of the page 4446 02:54:56,880 --> 02:54:59,600 so we can just say pretty much anything 4447 02:54:59,600 --> 02:55:01,520 that made it here 4448 02:55:01,520 --> 02:55:02,560 should 4449 02:55:02,560 --> 02:55:06,000 get the 404 at this point let's go ahead 4450 02:55:06,000 --> 02:55:08,880 and save the file and test it out i'll 4451 02:55:08,880 --> 02:55:10,800 pull up chrome 4452 02:55:10,800 --> 02:55:11,920 and here 4453 02:55:11,920 --> 02:55:13,600 we can put in the 4454 02:55:13,600 --> 02:55:15,439 slash dave 4455 02:55:15,439 --> 02:55:18,319 and yes there's no page that's dave html 4456 02:55:18,319 --> 02:55:21,200 so that is a 404 although the request 4457 02:55:21,200 --> 02:55:24,240 did log just fine we can customize our 4458 02:55:24,240 --> 02:55:27,359 404 response just a little more so let's 4459 02:55:27,359 --> 02:55:29,439 start that out by just setting the 4460 02:55:29,439 --> 02:55:31,279 response status because we know it's 4461 02:55:31,279 --> 02:55:34,640 going to be a 404 no matter what 4462 02:55:34,640 --> 02:55:37,200 after that let's check the type so we 4463 02:55:37,200 --> 02:55:38,720 can say if 4464 02:55:38,720 --> 02:55:40,960 the request 4465 02:55:40,960 --> 02:55:42,319 accepts 4466 02:55:42,319 --> 02:55:44,479 and now we'll look at the type the 4467 02:55:44,479 --> 02:55:46,960 content type and we can just say html 4468 02:55:46,960 --> 02:55:49,600 here and express will translate that for 4469 02:55:49,600 --> 02:55:52,080 us and after that 4470 02:55:52,080 --> 02:55:54,160 then we'll put what happens because if 4471 02:55:54,160 --> 02:55:57,040 this will resolve till true or false 4472 02:55:57,040 --> 02:55:58,399 here so 4473 02:55:58,399 --> 02:55:59,279 then 4474 02:55:59,279 --> 02:56:01,359 we can go ahead and look at sending a 4475 02:56:01,359 --> 02:56:04,319 file or something else inside of here so 4476 02:56:04,319 --> 02:56:06,399 what we'll do is say 4477 02:56:06,399 --> 02:56:08,560 response 4478 02:56:08,560 --> 02:56:11,680 dot send file and go ahead and send that 4479 02:56:11,680 --> 02:56:15,040 404 now this is very much like what we 4480 02:56:15,040 --> 02:56:17,279 were already doing but now we can check 4481 02:56:17,279 --> 02:56:19,600 for other types as well that might be 4482 02:56:19,600 --> 02:56:21,680 accepted so let's go ahead 4483 02:56:21,680 --> 02:56:23,600 i'll just copy all of this down and 4484 02:56:23,600 --> 02:56:26,319 we'll change some of it so shift alt and 4485 02:56:26,319 --> 02:56:28,640 the down arrow copies everything 4486 02:56:28,640 --> 02:56:31,200 and now we'll say if the request accepts 4487 02:56:31,200 --> 02:56:33,439 json well then we don't want to send a 4488 02:56:33,439 --> 02:56:35,439 file at all so this will be entirely 4489 02:56:35,439 --> 02:56:36,399 different 4490 02:56:36,399 --> 02:56:38,880 let's get rid of all of this 4491 02:56:38,880 --> 02:56:41,120 and say response.json 4492 02:56:41,120 --> 02:56:43,600 and we can put some json here inside of 4493 02:56:43,600 --> 02:56:46,720 our response and let's just say error 4494 02:56:46,720 --> 02:56:48,160 and over here 4495 02:56:48,160 --> 02:56:51,520 we can say 404 4496 02:56:51,520 --> 02:56:53,760 not page let's just say 4497 02:56:53,760 --> 02:56:55,840 not found which is essentially what a 4498 02:56:55,840 --> 02:56:58,319 404 is and really what we're doing is 4499 02:56:58,319 --> 02:57:00,640 creating a small waterfall effect here 4500 02:57:00,640 --> 02:57:04,080 with the if so we could make it another 4501 02:57:04,080 --> 02:57:05,040 else 4502 02:57:05,040 --> 02:57:07,520 if here and then finally 4503 02:57:07,520 --> 02:57:10,000 an else for our last 4504 02:57:10,000 --> 02:57:12,240 possible outcome 4505 02:57:12,240 --> 02:57:14,560 once i can spell else and here we'll 4506 02:57:14,560 --> 02:57:17,680 just respond and we can set the type 4507 02:57:17,680 --> 02:57:19,439 to text 4508 02:57:19,439 --> 02:57:22,479 just txt for the type actually and then 4509 02:57:22,479 --> 02:57:23,520 we can 4510 02:57:23,520 --> 02:57:26,080 send and inside the send we'll just put 4511 02:57:26,080 --> 02:57:28,399 the same text that we have up here 4512 02:57:28,399 --> 02:57:29,920 404 4513 02:57:29,920 --> 02:57:32,720 not found so i'll paste that in and that 4514 02:57:32,720 --> 02:57:34,880 should work as well so now i'll go ahead 4515 02:57:34,880 --> 02:57:37,520 and remove these blank lines and we will 4516 02:57:37,520 --> 02:57:40,800 test out our 404 once again 4517 02:57:40,800 --> 02:57:43,279 i'll go to chrome 4518 02:57:43,279 --> 02:57:45,040 and i've already got 4519 02:57:45,040 --> 02:57:47,120 slash dave in the url so i'll just see 4520 02:57:47,120 --> 02:57:49,680 if i can reload this 404 once the server 4521 02:57:49,680 --> 02:57:50,960 restarts 4522 02:57:50,960 --> 02:57:54,160 and now it's restarted i'll hit reload 4523 02:57:54,160 --> 02:57:57,760 and we got the 404 and it is html 4524 02:57:57,760 --> 02:57:59,840 now i would have to use postman or 4525 02:57:59,840 --> 02:58:03,040 something else to ex try out the json 4526 02:58:03,040 --> 02:58:06,000 file or possibly text as well we would 4527 02:58:06,000 --> 02:58:07,840 change the headers for that and i'm just 4528 02:58:07,840 --> 02:58:09,600 going to let you trust me that that will 4529 02:58:09,600 --> 02:58:11,520 work for now instead of breaking out 4530 02:58:11,520 --> 02:58:13,680 postman or anything to just try out 4531 02:58:13,680 --> 02:58:16,160 those two other routes but overall that 4532 02:58:16,160 --> 02:58:18,560 is what you can do to customize a 404 4533 02:58:18,560 --> 02:58:21,120 with app all and i hope you understand 4534 02:58:21,120 --> 02:58:22,560 at least a little bit of the difference 4535 02:58:22,560 --> 02:58:25,279 between app.use which is for 4536 02:58:25,279 --> 02:58:28,080 middleware and does not accept regex and 4537 02:58:28,080 --> 02:58:30,880 app.all which is more for routing and it 4538 02:58:30,880 --> 02:58:36,080 will apply to all http methods at once 4539 02:58:36,080 --> 02:58:38,560 today we're going to talk about creating 4540 02:58:38,560 --> 02:58:41,600 routers in the express js framework and 4541 02:58:41,600 --> 02:58:43,040 we're going to start out with the code 4542 02:58:43,040 --> 02:58:45,359 repository from the last tutorial but if 4543 02:58:45,359 --> 02:58:47,120 you don't have it you should be able to 4544 02:58:47,120 --> 02:58:50,080 easily follow along or you can download 4545 02:58:50,080 --> 02:58:52,000 or clone the starter source code from 4546 02:58:52,000 --> 02:58:53,520 the link i'm providing in the 4547 02:58:53,520 --> 02:58:55,520 description below alright in visual 4548 02:58:55,520 --> 02:58:58,960 studio code we've got our server.js file 4549 02:58:58,960 --> 02:59:01,040 and from the previous tutorial we've got 4550 02:59:01,040 --> 02:59:03,760 a lot of code in here and really what we 4551 02:59:03,760 --> 02:59:06,880 can do is break these routes out into 4552 02:59:06,880 --> 02:59:09,359 individual routers that are essentially 4553 02:59:09,359 --> 02:59:12,560 mini servers for each specific route or 4554 02:59:12,560 --> 02:59:14,960 many apps as we create our app with 4555 02:59:14,960 --> 02:59:17,680 express here these will be mini apps and 4556 02:59:17,680 --> 02:59:20,720 we'll for refer to them as routers so 4557 02:59:20,720 --> 02:59:22,800 over in the file tree let's create a new 4558 02:59:22,800 --> 02:59:25,439 directory and let's call this directory 4559 02:59:25,439 --> 02:59:26,560 routes 4560 02:59:26,560 --> 02:59:29,359 and now inside the routes we can create 4561 02:59:29,359 --> 02:59:32,720 routers for each route that we are 4562 02:59:32,720 --> 02:59:35,760 handling so let's create a new file here 4563 02:59:35,760 --> 02:59:38,359 and let's call this 4564 02:59:38,359 --> 02:59:40,880 subdur.js because 4565 02:59:40,880 --> 02:59:44,319 we have a subdir directory in our views 4566 02:59:44,319 --> 02:59:46,880 folder that we haven't handled at all 4567 02:59:46,880 --> 02:59:47,680 yet 4568 02:59:47,680 --> 02:59:50,319 now in the subdir dot js we need to 4569 02:59:50,319 --> 02:59:52,000 start out with a few requirements once 4570 02:59:52,000 --> 02:59:54,240 again and the first would be express 4571 02:59:54,240 --> 02:59:57,359 just like we did in the regular app or 4572 02:59:57,359 --> 03:00:01,200 server.js if you will so we have require 4573 03:00:01,200 --> 03:00:02,479 express 4574 03:00:02,479 --> 03:00:04,720 and then after that just like we have 4575 03:00:04,720 --> 03:00:05,840 before 4576 03:00:05,840 --> 03:00:09,439 we have applied express however now 4577 03:00:09,439 --> 03:00:12,479 we'll define a router instead of an app 4578 03:00:12,479 --> 03:00:15,040 and we'll set this equal to express dot 4579 03:00:15,040 --> 03:00:17,200 router with a capital r 4580 03:00:17,200 --> 03:00:18,800 and then we're going to go ahead and 4581 03:00:18,800 --> 03:00:21,840 need the path module as well from 4582 03:00:21,840 --> 03:00:24,399 common core module in node.js 4583 03:00:24,399 --> 03:00:27,120 so we'll require path 4584 03:00:27,120 --> 03:00:28,640 after that 4585 03:00:28,640 --> 03:00:30,479 let's just go ahead and put our module 4586 03:00:30,479 --> 03:00:31,279 dot 4587 03:00:31,279 --> 03:00:33,760 exports and set that equal to router 4588 03:00:33,760 --> 03:00:35,520 because that's what we'll always do when 4589 03:00:35,520 --> 03:00:37,600 we create a router and then we can 4590 03:00:37,600 --> 03:00:39,920 import that back in the server.js when 4591 03:00:39,920 --> 03:00:42,319 we need it now we're ready to just pull 4592 03:00:42,319 --> 03:00:44,560 our routes in so let's look at the 4593 03:00:44,560 --> 03:00:46,080 server.js 4594 03:00:46,080 --> 03:00:48,960 and the routes that we had or did not 4595 03:00:48,960 --> 03:00:51,120 have at this point for the subdirectory 4596 03:00:51,120 --> 03:00:52,960 and i don't believe we had any yet we 4597 03:00:52,960 --> 03:00:55,920 were just routing to our specific views 4598 03:00:55,920 --> 03:00:59,359 for index and the new page and of course 4599 03:00:59,359 --> 03:01:01,200 we were redirecting the old page so we 4600 03:01:01,200 --> 03:01:03,200 hadn't handled anything for the 4601 03:01:03,200 --> 03:01:06,160 subdirectory yet it has its own index 4602 03:01:06,160 --> 03:01:09,279 and it has a file called test html so 4603 03:01:09,279 --> 03:01:11,120 i'm going to copy how we handled the 4604 03:01:11,120 --> 03:01:13,359 index for the root 4605 03:01:13,359 --> 03:01:15,520 and then we can just modify that in the 4606 03:01:15,520 --> 03:01:17,120 subdirectory 4607 03:01:17,120 --> 03:01:20,479 and instead of app.get we now have a 4608 03:01:20,479 --> 03:01:23,840 router so let's replace app 4609 03:01:23,840 --> 03:01:26,960 with router and now it's router.git and 4610 03:01:26,960 --> 03:01:29,920 then we can handle this slash or index 4611 03:01:29,920 --> 03:01:32,399 for the subdirectory the same way we did 4612 03:01:32,399 --> 03:01:34,960 for our root and we have a request and 4613 03:01:34,960 --> 03:01:37,040 response and we can get remove this 4614 03:01:37,040 --> 03:01:38,880 commented line that we had in there 4615 03:01:38,880 --> 03:01:40,080 before 4616 03:01:40,080 --> 03:01:42,160 and then we are sending a file and we're 4617 03:01:42,160 --> 03:01:44,880 sending the root directory we get the 4618 03:01:44,880 --> 03:01:46,800 directory name here 4619 03:01:46,800 --> 03:01:49,040 and then we want to have views but now 4620 03:01:49,040 --> 03:01:52,160 we have to think about this because 4621 03:01:52,160 --> 03:01:53,920 we are not 4622 03:01:53,920 --> 03:01:56,560 inside the root so we can't just look 4623 03:01:56,560 --> 03:01:59,840 directly into the views directory so we 4624 03:01:59,840 --> 03:02:02,560 need to come up out of the routes 4625 03:02:02,560 --> 03:02:05,040 directory we created and then into the 4626 03:02:05,040 --> 03:02:07,840 views directory and then 4627 03:02:07,840 --> 03:02:10,080 we're not only just looking for the 4628 03:02:10,080 --> 03:02:11,920 index because that would be the root 4629 03:02:11,920 --> 03:02:14,800 index now we need to specify 4630 03:02:14,800 --> 03:02:17,120 the subdirectory that we have 4631 03:02:17,120 --> 03:02:19,680 and then the index.html that's within 4632 03:02:19,680 --> 03:02:21,680 that subdirectory file 4633 03:02:21,680 --> 03:02:24,080 after that let's copy this down and i 4634 03:02:24,080 --> 03:02:26,560 can press shift alt and the down arrow 4635 03:02:26,560 --> 03:02:28,720 i'm using windows it may be different if 4636 03:02:28,720 --> 03:02:30,399 you're on mac or linux 4637 03:02:30,399 --> 03:02:33,200 and now we have another git route but 4638 03:02:33,200 --> 03:02:35,200 this will be for 4639 03:02:35,200 --> 03:02:38,880 the test dot html and so we can replace 4640 03:02:38,880 --> 03:02:41,920 index with test and remove the initial 4641 03:02:41,920 --> 03:02:44,960 slash we'll leave the html optional this 4642 03:02:44,960 --> 03:02:46,960 is regex that we can use within the 4643 03:02:46,960 --> 03:02:47,920 route 4644 03:02:47,920 --> 03:02:50,000 and now we're sending a file once again 4645 03:02:50,000 --> 03:02:52,000 but instead of sending index we're 4646 03:02:52,000 --> 03:02:53,040 sending 4647 03:02:53,040 --> 03:02:56,800 test not text dot html and we can save 4648 03:02:56,800 --> 03:02:58,800 that now that we've created the router 4649 03:02:58,800 --> 03:03:00,960 file for the subdirectory we need to go 4650 03:03:00,960 --> 03:03:03,040 back to server.js 4651 03:03:03,040 --> 03:03:03,840 and 4652 03:03:03,840 --> 03:03:05,920 underneath where we serve the static 4653 03:03:05,920 --> 03:03:08,080 files and we set that 4654 03:03:08,080 --> 03:03:10,319 we need to go ahead and provide a route 4655 03:03:10,319 --> 03:03:13,359 and we do that with app.use 4656 03:03:13,359 --> 03:03:16,399 in the server file and now let's provide 4657 03:03:16,399 --> 03:03:17,920 the route 4658 03:03:17,920 --> 03:03:21,120 that we are providing the router for 4659 03:03:21,120 --> 03:03:25,040 and then we can go ahead and require the 4660 03:03:25,040 --> 03:03:27,600 router that we have created and now this 4661 03:03:27,600 --> 03:03:30,000 will be in dot slash 4662 03:03:30,000 --> 03:03:31,920 routes slash 4663 03:03:31,920 --> 03:03:34,319 and there's the sub directory and after 4664 03:03:34,319 --> 03:03:36,720 we provide that we should be finished 4665 03:03:36,720 --> 03:03:38,880 and now this will route 4666 03:03:38,880 --> 03:03:42,000 any request coming for the subdirectory 4667 03:03:42,000 --> 03:03:44,399 to the router instead of the routes that 4668 03:03:44,399 --> 03:03:46,960 we are providing below through app.git 4669 03:03:46,960 --> 03:03:49,760 such as index and new page so this will 4670 03:03:49,760 --> 03:03:52,800 match in front of these and of course we 4671 03:03:52,800 --> 03:03:54,960 can put all of these in a router and we 4672 03:03:54,960 --> 03:03:57,840 will next but let's go ahead and start 4673 03:03:57,840 --> 03:03:58,720 the 4674 03:03:58,720 --> 03:04:01,040 dev server i'm going to press control on 4675 03:04:01,040 --> 03:04:02,720 the backtick but you can go to the 4676 03:04:02,720 --> 03:04:04,720 terminal menu and open a new terminal 4677 03:04:04,720 --> 03:04:06,160 window 4678 03:04:06,160 --> 03:04:09,200 and if you remember we type npm 4679 03:04:09,200 --> 03:04:10,160 run 4680 03:04:10,160 --> 03:04:13,040 dev and this will start our local dev 4681 03:04:13,040 --> 03:04:15,279 server with nodemon which will listen 4682 03:04:15,279 --> 03:04:17,840 for any changes and it's on port 3500 so 4683 03:04:17,840 --> 03:04:19,600 let's go to chrome 4684 03:04:19,600 --> 03:04:22,399 and i'll go to my blank page i'll type 4685 03:04:22,399 --> 03:04:24,000 local host 4686 03:04:24,000 --> 03:04:26,560 port 3500 4687 03:04:26,560 --> 03:04:28,560 and there we've got our index file and 4688 03:04:28,560 --> 03:04:30,720 that's fine but we want to go to the sub 4689 03:04:30,720 --> 03:04:32,479 directory and see if this is working so 4690 03:04:32,479 --> 03:04:34,080 we've got sub 4691 03:04:34,080 --> 03:04:35,520 dur 4692 03:04:35,520 --> 03:04:38,479 and yes we've got our subdirectory index 4693 03:04:38,479 --> 03:04:40,960 page and let's go ahead and see if the 4694 03:04:40,960 --> 03:04:43,840 test index page in the subdirectory is 4695 03:04:43,840 --> 03:04:47,279 also working i said index test.html 4696 03:04:47,279 --> 03:04:49,439 actually and there's our test page in 4697 03:04:49,439 --> 03:04:51,920 the subdirectory as well okay now let's 4698 03:04:51,920 --> 03:04:55,200 ask for a page that does not exist 4699 03:04:55,200 --> 03:04:57,920 in this sub directory and let's see what 4700 03:04:57,920 --> 03:04:59,279 we get 4701 03:04:59,279 --> 03:05:01,840 well we've got our 404 page but there's 4702 03:05:01,840 --> 03:05:04,080 something important to notice here in 4703 03:05:04,080 --> 03:05:06,800 the past when we've got our 404 page it 4704 03:05:06,800 --> 03:05:08,319 had css 4705 03:05:08,319 --> 03:05:12,160 supplied for it now we didn't put css 4706 03:05:12,160 --> 03:05:14,399 on the previous subdirectory pages that 4707 03:05:14,399 --> 03:05:16,960 was not included anyway but if we 4708 03:05:16,960 --> 03:05:19,120 requested a 404 4709 03:05:19,120 --> 03:05:21,279 on any other page that was not in the 4710 03:05:21,279 --> 03:05:22,880 subdirectory 4711 03:05:22,880 --> 03:05:26,160 it does have css applied so why is the 4712 03:05:26,160 --> 03:05:27,520 css 4713 03:05:27,520 --> 03:05:30,160 not working for our subdirectory well 4714 03:05:30,160 --> 03:05:32,880 it's because we haven't told express to 4715 03:05:32,880 --> 03:05:33,920 use 4716 03:05:33,920 --> 03:05:36,479 the public folder or public directory if 4717 03:05:36,479 --> 03:05:39,120 you will for the subdirectory so let's 4718 03:05:39,120 --> 03:05:41,600 copy this line down with shift alt and 4719 03:05:41,600 --> 03:05:44,840 the down arrow once again and now we can 4720 03:05:44,840 --> 03:05:49,279 specify the subdirectory here 4721 03:05:50,160 --> 03:05:53,040 and tell express to use that public 4722 03:05:53,040 --> 03:05:55,680 directory before we didn't supply a 4723 03:05:55,680 --> 03:05:58,960 directory but the default is the slash 4724 03:05:58,960 --> 03:06:01,439 anyway and so that's essentially what we 4725 03:06:01,439 --> 03:06:04,240 were doing but we can save these 4726 03:06:04,240 --> 03:06:07,279 and now we should be able to go back and 4727 03:06:07,279 --> 03:06:10,319 request our 404 basically any page that 4728 03:06:10,319 --> 03:06:13,120 doesn't exist in the subdirectory 4729 03:06:13,120 --> 03:06:18,560 and probably get the formatted 404 yes 4730 03:06:18,560 --> 03:06:20,160 now even though we're in the 4731 03:06:20,160 --> 03:06:22,160 subdirectory and i requested a page 4732 03:06:22,160 --> 03:06:25,680 named dave that doesn't exist 4733 03:06:25,680 --> 03:06:29,279 we got a 404 that actually has the css 4734 03:06:29,279 --> 03:06:31,200 supplied you can even see the request 4735 03:06:31,200 --> 03:06:34,399 coming through down here for the css in 4736 03:06:34,399 --> 03:06:36,960 the sub directory and it now works now 4737 03:06:36,960 --> 03:06:39,279 i'd like to make one quick correction 4738 03:06:39,279 --> 03:06:42,000 from the last tutorial that i did 4739 03:06:42,000 --> 03:06:44,720 provide information that was correct in 4740 03:06:44,720 --> 03:06:47,520 older versions however in newer versions 4741 03:06:47,520 --> 03:06:51,439 of express express does support regex 4742 03:06:51,439 --> 03:06:54,640 inside of app.use and i said it didn't 4743 03:06:54,640 --> 03:06:58,000 when i compared app.use to app.all but 4744 03:06:58,000 --> 03:07:01,279 now you can have regex there and so if 4745 03:07:01,279 --> 03:07:04,319 we are to require a router 4746 03:07:04,319 --> 03:07:07,840 and we put a slash with an asterisk here 4747 03:07:07,840 --> 03:07:10,399 this would supersede the subdirectory 4748 03:07:10,399 --> 03:07:12,399 router in other words all 4749 03:07:12,399 --> 03:07:15,200 routes would go to this router if we 4750 03:07:15,200 --> 03:07:16,960 supplied that of course we don't want to 4751 03:07:16,960 --> 03:07:20,000 do that so we want to specifically state 4752 03:07:20,000 --> 03:07:22,720 just the slash or we could even get more 4753 03:07:22,720 --> 03:07:25,120 specific and we could say hey it must 4754 03:07:25,120 --> 03:07:26,880 start with the slash 4755 03:07:26,880 --> 03:07:28,560 and it must 4756 03:07:28,560 --> 03:07:30,399 end with the slash 4757 03:07:30,399 --> 03:07:32,640 and then of course we would only be 4758 03:07:32,640 --> 03:07:34,960 providing that for the slash right here 4759 03:07:34,960 --> 03:07:35,760 but 4760 03:07:35,760 --> 03:07:38,240 that is because it supports regex and in 4761 03:07:38,240 --> 03:07:41,760 previous older versions express did not 4762 03:07:41,760 --> 03:07:44,560 support regex inside of app.use but in 4763 03:07:44,560 --> 03:07:47,120 the route it now does let's go ahead and 4764 03:07:47,120 --> 03:07:50,000 create a router for this 4765 03:07:50,000 --> 03:07:52,080 root directory here so let's say dot 4766 03:07:52,080 --> 03:07:53,279 slash 4767 03:07:53,279 --> 03:07:55,920 routes slash 4768 03:07:55,920 --> 03:07:56,800 root 4769 03:07:56,800 --> 03:07:58,640 and now add the 4770 03:07:58,640 --> 03:08:01,040 semicolon one more over 4771 03:08:01,040 --> 03:08:03,359 and save and now we need to go ahead and 4772 03:08:03,359 --> 03:08:05,279 create this route because right now it 4773 03:08:05,279 --> 03:08:06,560 doesn't exist 4774 03:08:06,560 --> 03:08:08,560 so let's go over here to the file tree 4775 03:08:08,560 --> 03:08:10,640 i'll collapse the views folder we don't 4776 03:08:10,640 --> 03:08:12,399 need to see what's open there but inside 4777 03:08:12,399 --> 03:08:15,680 of routes let's go ahead and create this 4778 03:08:15,680 --> 03:08:16,960 root 4779 03:08:16,960 --> 03:08:18,800 dot js 4780 03:08:18,800 --> 03:08:21,680 and now inside of the js file 4781 03:08:21,680 --> 03:08:23,920 let's go back to server and grab the 4782 03:08:23,920 --> 03:08:25,920 routes that we were using 4783 03:08:25,920 --> 03:08:28,560 for the root and we have our index page 4784 03:08:28,560 --> 03:08:30,080 our new page 4785 03:08:30,080 --> 03:08:33,279 and a redirect for an old page and 4786 03:08:33,279 --> 03:08:36,080 control x will cut those out and we can 4787 03:08:36,080 --> 03:08:37,359 paste those 4788 03:08:37,359 --> 03:08:40,080 inside of the root but we also need 4789 03:08:40,080 --> 03:08:42,560 those imports and basically 4790 03:08:42,560 --> 03:08:44,479 everything you would normally start out 4791 03:08:44,479 --> 03:08:45,279 with 4792 03:08:45,279 --> 03:08:47,520 for a router these three imports at the 4793 03:08:47,520 --> 03:08:49,040 top 4794 03:08:49,040 --> 03:08:51,840 express router and path 4795 03:08:51,840 --> 03:08:53,840 so we'll put those at the top of root as 4796 03:08:53,840 --> 03:08:54,640 well 4797 03:08:54,640 --> 03:08:56,880 and then at the bottom 4798 03:08:56,880 --> 03:08:59,120 once again you want to have module dot 4799 03:08:59,120 --> 03:09:01,680 exports and set that equal to 4800 03:09:01,680 --> 03:09:04,000 the router now what haven't we changed 4801 03:09:04,000 --> 03:09:06,720 yet is the keyword app here that we were 4802 03:09:06,720 --> 03:09:09,040 using in the server.js we need to switch 4803 03:09:09,040 --> 03:09:10,960 to router as well 4804 03:09:10,960 --> 03:09:12,479 we can once again get rid of this 4805 03:09:12,479 --> 03:09:14,000 commented file 4806 03:09:14,000 --> 03:09:16,479 and we have to once again consider the 4807 03:09:16,479 --> 03:09:18,319 route that we're providing because now 4808 03:09:18,319 --> 03:09:21,120 we're in the routes folder so yes we 4809 03:09:21,120 --> 03:09:23,120 have the directory name value but then 4810 03:09:23,120 --> 03:09:25,680 we need to come up out of 4811 03:09:25,680 --> 03:09:27,279 the routes folder 4812 03:09:27,279 --> 03:09:29,120 and then look in the views folder now 4813 03:09:29,120 --> 03:09:30,800 this will be correct now we're not 4814 03:09:30,800 --> 03:09:32,560 looking in a subdirectory so we no 4815 03:09:32,560 --> 03:09:34,800 longer have to provide it there let's go 4816 03:09:34,800 --> 03:09:36,640 ahead and copy this 4817 03:09:36,640 --> 03:09:38,640 and paste it into the route for the new 4818 03:09:38,640 --> 03:09:41,200 page as well however the route for the 4819 03:09:41,200 --> 03:09:44,160 old page is a redirect and this will be 4820 03:09:44,160 --> 03:09:46,800 okay to leave it as it is so let's save 4821 03:09:46,800 --> 03:09:47,840 the root 4822 03:09:47,840 --> 03:09:49,840 and let's go back to the server we can 4823 03:09:49,840 --> 03:09:51,920 also get rid of some of these examples 4824 03:09:51,920 --> 03:09:53,840 of route handlers that we had such as 4825 03:09:53,840 --> 03:09:56,319 the hello right here and the chaining 4826 03:09:56,319 --> 03:09:58,080 the route handlers that i had previously 4827 03:09:58,080 --> 03:10:00,239 provided for the chain route let's just 4828 03:10:00,239 --> 03:10:01,840 delete all of that to clean it up a 4829 03:10:01,840 --> 03:10:03,120 little bit 4830 03:10:03,120 --> 03:10:05,600 and save our server.js 4831 03:10:05,600 --> 03:10:08,479 it's now restarted so let's see if these 4832 03:10:08,479 --> 03:10:10,399 routes are now working with the new 4833 03:10:10,399 --> 03:10:11,439 router 4834 03:10:11,439 --> 03:10:13,760 and we'll come back and just go to the 4835 03:10:13,760 --> 03:10:14,720 root 4836 03:10:14,720 --> 03:10:16,960 and yes we got the root page 4837 03:10:16,960 --> 03:10:20,560 let's go to slash new dash page 4838 03:10:20,560 --> 03:10:22,800 html should be optional and there's our 4839 03:10:22,800 --> 03:10:25,040 new page and our image is working which 4840 03:10:25,040 --> 03:10:27,760 means we're getting our static file 4841 03:10:27,760 --> 03:10:30,080 routed correctly to the public directory 4842 03:10:30,080 --> 03:10:32,319 where the image is stored and now let's 4843 03:10:32,319 --> 03:10:34,880 go to old page and see if the redirect 4844 03:10:34,880 --> 03:10:36,319 still works 4845 03:10:36,319 --> 03:10:38,479 and yes we redirected back to the new 4846 03:10:38,479 --> 03:10:41,520 page let's go ahead and type in index 4847 03:10:41,520 --> 03:10:43,600 instead of just the slash and yes we 4848 03:10:43,600 --> 03:10:45,359 still get the index page everything in 4849 03:10:45,359 --> 03:10:47,840 the new router for the root is working 4850 03:10:47,840 --> 03:10:50,239 correctly with our routes working 4851 03:10:50,239 --> 03:10:51,520 correctly 4852 03:10:51,520 --> 03:10:53,920 let's talk about how we would set up an 4853 03:10:53,920 --> 03:10:57,359 api or a rest api if you will 4854 03:10:57,359 --> 03:10:59,840 and that's what is more important when 4855 03:10:59,840 --> 03:11:02,080 we're working with say the mern stack as 4856 03:11:02,080 --> 03:11:02,880 say 4857 03:11:02,880 --> 03:11:05,840 express node and of course react 4858 03:11:05,840 --> 03:11:08,319 on the front end or you could substitute 4859 03:11:08,319 --> 03:11:10,800 any database technology for but 4860 03:11:10,800 --> 03:11:13,359 we've got node and express here 4861 03:11:13,359 --> 03:11:15,520 and we want to organize 4862 03:11:15,520 --> 03:11:18,640 a api which is what we would more than 4863 03:11:18,640 --> 03:11:21,200 likely create with node and express say 4864 03:11:21,200 --> 03:11:22,800 compared to 4865 03:11:22,800 --> 03:11:25,920 a static server for web pages although 4866 03:11:25,920 --> 03:11:27,680 you can do that 4867 03:11:27,680 --> 03:11:29,200 it's much more likely that you'll create 4868 03:11:29,200 --> 03:11:31,439 an api and that's what we'll focus on 4869 03:11:31,439 --> 03:11:32,720 here so 4870 03:11:32,720 --> 03:11:35,040 underneath these routes we're going to 4871 03:11:35,040 --> 03:11:37,279 create one more route 4872 03:11:37,279 --> 03:11:39,279 and this will be 4873 03:11:39,279 --> 03:11:40,399 to 4874 03:11:40,399 --> 03:11:42,720 employees 4875 03:11:42,720 --> 03:11:44,960 and then inside of routes it's going to 4876 03:11:44,960 --> 03:11:48,080 be in a subfolder named api 4877 03:11:48,080 --> 03:11:51,439 and then the employees and notice this 4878 03:11:51,439 --> 03:11:54,319 will just be routed by the user going to 4879 03:11:54,319 --> 03:11:57,439 employees they will not have to type api 4880 03:11:57,439 --> 03:11:59,600 so let's go ahead and save this and then 4881 03:11:59,600 --> 03:12:02,160 let's start creating that route inside 4882 03:12:02,160 --> 03:12:04,399 of routes we want to create another 4883 03:12:04,399 --> 03:12:05,680 directory 4884 03:12:05,680 --> 03:12:07,359 and that's api 4885 03:12:07,359 --> 03:12:09,520 and then inside of the api 4886 03:12:09,520 --> 03:12:13,040 we want to create our employees router 4887 03:12:13,040 --> 03:12:14,040 and that's 4888 03:12:14,040 --> 03:12:17,279 employees.js now up here inside the data 4889 03:12:17,279 --> 03:12:19,640 folder we've already got a file named 4890 03:12:19,640 --> 03:12:23,040 data.json let's go ahead and rename this 4891 03:12:23,040 --> 03:12:24,080 file 4892 03:12:24,080 --> 03:12:27,040 and let's call it 4893 03:12:27,080 --> 03:12:29,040 employees.json 4894 03:12:29,040 --> 03:12:31,359 and let's give each employee inside of 4895 03:12:31,359 --> 03:12:33,840 this file an id 4896 03:12:33,840 --> 03:12:36,880 and we'll just make these chronological 4897 03:12:36,880 --> 03:12:38,720 so we've got one 4898 03:12:38,720 --> 03:12:41,359 and an id of 4899 03:12:41,359 --> 03:12:42,560 two 4900 03:12:42,560 --> 03:12:44,880 and now that we got that we can just go 4901 03:12:44,880 --> 03:12:47,520 ahead and save our small employees data 4902 03:12:47,520 --> 03:12:50,720 file and remember that's employees.json 4903 03:12:50,720 --> 03:12:52,640 inside the data folder 4904 03:12:52,640 --> 03:12:55,279 and we've created the route here inside 4905 03:12:55,279 --> 03:12:58,479 of server using app use now it does not 4906 03:12:58,479 --> 03:13:01,040 need to have any static files as we'll 4907 03:13:01,040 --> 03:13:03,760 just be delivering json data from our 4908 03:13:03,760 --> 03:13:06,319 api so we wouldn't typically have any 4909 03:13:06,319 --> 03:13:08,080 static files like we would with a web 4910 03:13:08,080 --> 03:13:10,640 page so now let's go back to our 4911 03:13:10,640 --> 03:13:12,720 employees.js 4912 03:13:12,720 --> 03:13:14,880 route let's start out our route just 4913 03:13:14,880 --> 03:13:16,560 like we did the others so we want to 4914 03:13:16,560 --> 03:13:19,760 define express we'll set this equal to 4915 03:13:19,760 --> 03:13:23,120 requiring express 4916 03:13:23,120 --> 03:13:24,960 and then we want to go ahead and define 4917 03:13:24,960 --> 03:13:27,279 our router once again and we'll set this 4918 03:13:27,279 --> 03:13:30,399 equal to express dot router with a 4919 03:13:30,399 --> 03:13:33,520 capital r and call router into action 4920 03:13:33,520 --> 03:13:35,520 and we'll once again need the path 4921 03:13:35,520 --> 03:13:38,080 common core module from node.js so we 4922 03:13:38,080 --> 03:13:39,840 can require 4923 03:13:39,840 --> 03:13:41,120 path 4924 03:13:41,120 --> 03:13:44,319 and after path let's go ahead and define 4925 03:13:44,319 --> 03:13:47,680 an empty object named data 4926 03:13:47,680 --> 03:13:51,279 and from there let's set data dot 4927 03:13:51,279 --> 03:13:52,880 employees 4928 03:13:52,880 --> 03:13:54,720 equal to 4929 03:13:54,720 --> 03:13:58,000 require and now let's get that json file 4930 03:13:58,000 --> 03:14:00,319 so we need to go up out of the api 4931 03:14:00,319 --> 03:14:03,359 folder and then we need to go up out of 4932 03:14:03,359 --> 03:14:05,040 the routes folder 4933 03:14:05,040 --> 03:14:07,840 then into the data folder and then to 4934 03:14:07,840 --> 03:14:10,160 get employees.json 4935 03:14:10,160 --> 03:14:12,000 and so this is kind of like connecting 4936 03:14:12,000 --> 03:14:14,000 to a database this is just what we're 4937 03:14:14,000 --> 03:14:17,279 doing for this tutorial and and dev of 4938 03:14:17,279 --> 03:14:18,880 course in the future 4939 03:14:18,880 --> 03:14:21,680 we'll work on connecting to 4940 03:14:21,680 --> 03:14:24,319 and of course you could replace that 4941 03:14:24,319 --> 03:14:26,640 with your database technology of choice 4942 03:14:26,640 --> 03:14:28,720 but once again just for this tutorial 4943 03:14:28,720 --> 03:14:30,800 just for dev we're just going to import 4944 03:14:30,800 --> 03:14:34,160 some json to simply work with here so 4945 03:14:34,160 --> 03:14:36,239 now let's go ahead and create our 4946 03:14:36,239 --> 03:14:38,160 module.exports 4947 03:14:38,160 --> 03:14:40,399 and set that equal to router again as we 4948 03:14:40,399 --> 03:14:42,239 always do with a router 4949 03:14:42,239 --> 03:14:44,800 and now we can set up our routes now 4950 03:14:44,800 --> 03:14:46,279 instead of just saying 4951 03:14:46,279 --> 03:14:48,720 router.git and creating 4952 03:14:48,720 --> 03:14:50,880 anotherrouter.post 4953 03:14:50,880 --> 03:14:53,040 and another router dot put 4954 03:14:53,040 --> 03:14:54,560 and of course putting those paths in 4955 03:14:54,560 --> 03:14:56,160 there like we could 4956 03:14:56,160 --> 03:14:59,760 one thing we can do is say router dot 4957 03:14:59,760 --> 03:15:00,960 route 4958 03:15:00,960 --> 03:15:03,840 and then provide the route and then we 4959 03:15:03,840 --> 03:15:06,640 can chain the different http 4960 03:15:06,640 --> 03:15:09,279 methods that we want to provide for this 4961 03:15:09,279 --> 03:15:12,560 same route as a get request and a post 4962 03:15:12,560 --> 03:15:15,279 request can both go to the same route 4963 03:15:15,279 --> 03:15:17,359 but there can be a different result for 4964 03:15:17,359 --> 03:15:19,439 each so let's handle the get request 4965 03:15:19,439 --> 03:15:21,840 first and here's a request and a 4966 03:15:21,840 --> 03:15:24,160 response 4967 03:15:24,160 --> 03:15:26,239 and now inside the handler of the get 4968 03:15:26,239 --> 03:15:27,520 request 4969 03:15:27,520 --> 03:15:31,279 let's just return json and let's return 4970 03:15:31,279 --> 03:15:33,439 all the employees so that will be 4971 03:15:33,439 --> 03:15:35,600 data.employees 4972 03:15:35,600 --> 03:15:38,479 now for the post request 4973 03:15:38,479 --> 03:15:41,040 we can just come down here and say 4974 03:15:41,040 --> 03:15:43,920 dot post and once again 4975 03:15:43,920 --> 03:15:45,680 we can have a request 4976 03:15:45,680 --> 03:15:47,120 and a response 4977 03:15:47,120 --> 03:15:48,960 we can handle this completely 4978 03:15:48,960 --> 03:15:52,319 differently for the http method post 4979 03:15:52,319 --> 03:15:54,239 than we do for git 4980 03:15:54,239 --> 03:15:56,399 and now with the post we'll get 4981 03:15:56,399 --> 03:15:58,479 parameters coming in 4982 03:15:58,479 --> 03:16:00,720 to the request and we can refer to those 4983 03:16:00,720 --> 03:16:03,200 parameters with request dot body and 4984 03:16:03,200 --> 03:16:04,960 then dot whatever the name of the 4985 03:16:04,960 --> 03:16:06,640 parameter is 4986 03:16:06,640 --> 03:16:09,200 and we had a first name and a last name 4987 03:16:09,200 --> 03:16:11,840 and a post request would be posting a 4988 03:16:11,840 --> 03:16:13,600 new employee 4989 03:16:13,600 --> 03:16:15,600 so what we want to do i'm not going to 4990 03:16:15,600 --> 03:16:17,680 put in the full code that we would have 4991 03:16:17,680 --> 03:16:19,760 in an api at this moment because it's 4992 03:16:19,760 --> 03:16:22,160 not really about coding an api right now 4993 03:16:22,160 --> 03:16:24,239 we're handling routes so we just want to 4994 03:16:24,239 --> 03:16:26,399 see how each one of these would work 4995 03:16:26,399 --> 03:16:28,560 so let's say the first name that we 4996 03:16:28,560 --> 03:16:31,200 return in the json will return what is 4997 03:16:31,200 --> 03:16:33,920 sent and that's request dot body dot 4998 03:16:33,920 --> 03:16:35,600 first name 4999 03:16:35,600 --> 03:16:37,439 and then let's go ahead 5000 03:16:37,439 --> 03:16:40,000 and return the last name as well 5001 03:16:40,000 --> 03:16:43,120 that's request dot body dot last name 5002 03:16:43,120 --> 03:16:45,600 once again not writing out all the code 5003 03:16:45,600 --> 03:16:48,319 for an api at this point just showing 5004 03:16:48,319 --> 03:16:49,840 how we can get 5005 03:16:49,840 --> 03:16:52,640 the parameters from a post request and 5006 03:16:52,640 --> 03:16:54,560 then we're just sending those parameters 5007 03:16:54,560 --> 03:16:56,880 back but this is how we could handle 5008 03:16:56,880 --> 03:16:59,040 each route in an api 5009 03:16:59,040 --> 03:17:00,880 now let's go ahead and do the same thing 5010 03:17:00,880 --> 03:17:03,120 with a put request 5011 03:17:03,120 --> 03:17:05,279 i have a request and a response and this 5012 03:17:05,279 --> 03:17:06,319 would be 5013 03:17:06,319 --> 03:17:09,120 the http method that you would use when 5014 03:17:09,120 --> 03:17:12,000 you're updating an employee for example 5015 03:17:12,000 --> 03:17:13,840 but we could do the same thing we could 5016 03:17:13,840 --> 03:17:15,960 get the 5017 03:17:15,960 --> 03:17:18,160 res.json here and as a matter of fact i 5018 03:17:18,160 --> 03:17:21,520 can just copy this down 5019 03:17:21,680 --> 03:17:23,200 and of course the code would be 5020 03:17:23,200 --> 03:17:25,359 different if you were writing an actual 5021 03:17:25,359 --> 03:17:27,359 api because you would be updating at 5022 03:17:27,359 --> 03:17:29,439 that point and we'll do that in the near 5023 03:17:29,439 --> 03:17:31,600 future just not going to do it yet and 5024 03:17:31,600 --> 03:17:33,760 then let's get a delete now a delete 5025 03:17:33,760 --> 03:17:35,040 would be different we wouldn't be 5026 03:17:35,040 --> 03:17:37,520 getting the first name and the last name 5027 03:17:37,520 --> 03:17:40,800 we would be getting the id 5028 03:17:40,800 --> 03:17:42,880 so we'll just send the id back in this 5029 03:17:42,880 --> 03:17:43,840 case 5030 03:17:43,840 --> 03:17:46,160 and that would be response.json once 5031 03:17:46,160 --> 03:17:47,040 again 5032 03:17:47,040 --> 03:17:50,080 let's go ahead and send the id back here 5033 03:17:50,080 --> 03:17:51,160 and this would be 5034 03:17:51,160 --> 03:17:52,960 request.body 5035 03:17:52,960 --> 03:17:54,479 dot id 5036 03:17:54,479 --> 03:17:57,520 now you can see we've chained each http 5037 03:17:57,520 --> 03:18:00,720 method together and we've got git post 5038 03:18:00,720 --> 03:18:01,600 put 5039 03:18:01,600 --> 03:18:03,920 and delete all coming into the same 5040 03:18:03,920 --> 03:18:06,000 route on our router they're just 5041 03:18:06,000 --> 03:18:08,399 different request methods and of course 5042 03:18:08,399 --> 03:18:10,319 this being the last one we'll go ahead 5043 03:18:10,319 --> 03:18:11,600 and stop 5044 03:18:11,600 --> 03:18:13,840 right there and i'm going to save this 5045 03:18:13,840 --> 03:18:16,080 but we're going to add one more route we 5046 03:18:16,080 --> 03:18:18,720 have the route coming in with just the 5047 03:18:18,720 --> 03:18:22,720 slash just the root but then also 5048 03:18:22,720 --> 03:18:24,000 we could have 5049 03:18:24,000 --> 03:18:25,359 router 5050 03:18:25,359 --> 03:18:26,720 dot route 5051 03:18:26,720 --> 03:18:29,840 and then a slash but we could have a 5052 03:18:29,840 --> 03:18:33,680 parameter directly in the url and that's 5053 03:18:33,680 --> 03:18:35,439 what we're showing here is a parameter 5054 03:18:35,439 --> 03:18:36,960 and this would be 5055 03:18:36,960 --> 03:18:40,080 a get request that has a parameter 5056 03:18:40,080 --> 03:18:43,279 inside of the url so now down here and 5057 03:18:43,279 --> 03:18:44,560 of course you could do that with other 5058 03:18:44,560 --> 03:18:46,080 requests too we're going to use a get 5059 03:18:46,080 --> 03:18:50,000 request so let's go ahead and put 5060 03:18:50,000 --> 03:18:52,399 the handler for our get request here we 5061 03:18:52,399 --> 03:18:56,160 have our request and response 5062 03:18:56,160 --> 03:18:57,840 and now inside the handler will once 5063 03:18:57,840 --> 03:19:00,080 again respond with json 5064 03:19:00,080 --> 03:19:01,520 and we'll just say 5065 03:19:01,520 --> 03:19:02,840 here's the id 5066 03:19:02,840 --> 03:19:05,680 requested and now this is different 5067 03:19:05,680 --> 03:19:07,279 instead of request.body it's 5068 03:19:07,279 --> 03:19:10,279 request.params.id 5069 03:19:11,040 --> 03:19:12,960 and that's because we're pulling the 5070 03:19:12,960 --> 03:19:14,160 parameter 5071 03:19:14,160 --> 03:19:16,160 it's a named parameter actually and 5072 03:19:16,160 --> 03:19:19,040 we're pulling it directly out of the url 5073 03:19:19,040 --> 03:19:21,120 so here we're responding once again with 5074 03:19:21,120 --> 03:19:24,399 just what we received just as an example 5075 03:19:24,399 --> 03:19:27,040 now i'll put in p future code will go 5076 03:19:27,040 --> 03:19:29,920 ahead and actually code out an api right 5077 03:19:29,920 --> 03:19:32,640 now handling the routes and so all of 5078 03:19:32,640 --> 03:19:35,120 this is inside the router and i guess we 5079 03:19:35,120 --> 03:19:36,720 didn't need path right here at this 5080 03:19:36,720 --> 03:19:39,200 moment so we can get rid of that but all 5081 03:19:39,200 --> 03:19:41,680 of this is inside the router for 5082 03:19:41,680 --> 03:19:44,640 employees let's go ahead and save this 5083 03:19:44,640 --> 03:19:46,960 and we can test it out and our server is 5084 03:19:46,960 --> 03:19:49,120 still running below so we can now check 5085 03:19:49,120 --> 03:19:51,200 these routes with thunderclient 5086 03:19:51,200 --> 03:19:53,439 thunderclient is an extension 5087 03:19:53,439 --> 03:19:55,600 in the past i've used postman which is a 5088 03:19:55,600 --> 03:19:58,720 separate program to test apis let's go 5089 03:19:58,720 --> 03:20:00,960 ahead and try this extension out you can 5090 03:20:00,960 --> 03:20:03,520 find it under extension search for 5091 03:20:03,520 --> 03:20:04,960 thunder client 5092 03:20:04,960 --> 03:20:07,279 and it allows you to save different 5093 03:20:07,279 --> 03:20:09,040 routes you want to test 5094 03:20:09,040 --> 03:20:11,359 so right here i'm going to go ahead and 5095 03:20:11,359 --> 03:20:13,120 choose the 5096 03:20:13,120 --> 03:20:14,960 get route that i created first there it 5097 03:20:14,960 --> 03:20:16,800 is for all employees which of course 5098 03:20:16,800 --> 03:20:19,120 there's only two and you can see 5099 03:20:19,120 --> 03:20:22,399 here is our http method and here is the 5100 03:20:22,399 --> 03:20:24,960 route and now i'll click send 5101 03:20:24,960 --> 03:20:27,200 and here's the response i can just make 5102 03:20:27,200 --> 03:20:28,960 the response full screen but we've got 5103 03:20:28,960 --> 03:20:31,439 both of our employees returned 5104 03:20:31,439 --> 03:20:33,840 and that's exactly what we expected so 5105 03:20:33,840 --> 03:20:36,800 now after testing the get route let's go 5106 03:20:36,800 --> 03:20:39,760 ahead and test the post route so i'll 5107 03:20:39,760 --> 03:20:42,000 choose that from our list well let me go 5108 03:20:42,000 --> 03:20:43,840 to collections i believe i saved 5109 03:20:43,840 --> 03:20:45,600 everything there there we go 5110 03:20:45,600 --> 03:20:47,520 let's choose post 5111 03:20:47,520 --> 03:20:49,439 and now we're once again going to the 5112 03:20:49,439 --> 03:20:51,920 employees route but it's got post 5113 03:20:51,920 --> 03:20:53,840 let's look at the parameters and see if 5114 03:20:53,840 --> 03:20:55,600 those were saved 5115 03:20:55,600 --> 03:20:56,640 and 5116 03:20:56,640 --> 03:20:58,399 it should be under body actually there 5117 03:20:58,399 --> 03:21:01,120 we go and i'm posting a new employee 5118 03:21:01,120 --> 03:21:03,120 named john doe 5119 03:21:03,120 --> 03:21:05,359 so let's see if we get that in return 5120 03:21:05,359 --> 03:21:07,760 we'll send that and let's look at what 5121 03:21:07,760 --> 03:21:10,399 was returned yes we got john doe 5122 03:21:10,399 --> 03:21:12,239 sent back to us and that's all we're 5123 03:21:12,239 --> 03:21:14,239 going for today once again as we're not 5124 03:21:14,239 --> 03:21:16,720 writing out the actual code to add the 5125 03:21:16,720 --> 03:21:18,560 employee or update the employee and 5126 03:21:18,560 --> 03:21:20,720 things like that in the near future yes 5127 03:21:20,720 --> 03:21:22,479 i will okay 5128 03:21:22,479 --> 03:21:24,960 put should be the next one to test so 5129 03:21:24,960 --> 03:21:28,319 now it's a put method at the same url 5130 03:21:28,319 --> 03:21:31,279 and once again we are sending some john 5131 03:21:31,279 --> 03:21:34,560 doe oh actually sending just the id 5132 03:21:34,560 --> 03:21:36,960 and the first name of what we wanted to 5133 03:21:36,960 --> 03:21:39,439 update and of course i think i had the 5134 03:21:39,439 --> 03:21:41,520 put request just sending back the first 5135 03:21:41,520 --> 03:21:43,200 name and last name so maybe we'll just 5136 03:21:43,200 --> 03:21:44,960 get the first name back let's see what 5137 03:21:44,960 --> 03:21:46,720 we get when we test 5138 03:21:46,720 --> 03:21:48,000 put 5139 03:21:48,000 --> 03:21:49,279 and 5140 03:21:49,279 --> 03:21:51,520 yes we just got the first name back and 5141 03:21:51,520 --> 03:21:53,680 that's what i expected so that works 5142 03:21:53,680 --> 03:21:54,720 also 5143 03:21:54,720 --> 03:21:56,800 now let's test the delete route of 5144 03:21:56,800 --> 03:21:58,640 course it won't actually 5145 03:21:58,640 --> 03:22:01,520 delete the employee this time around 5146 03:22:01,520 --> 03:22:04,640 and looks like we're sending just the id 5147 03:22:04,640 --> 03:22:08,080 and we should get that id back 5148 03:22:08,080 --> 03:22:10,880 expand and yes in return we've got the 5149 03:22:10,880 --> 03:22:13,760 id back so that route is also working 5150 03:22:13,760 --> 03:22:15,680 now there's one more git route where we 5151 03:22:15,680 --> 03:22:18,640 could get a specific employee 5152 03:22:18,640 --> 03:22:21,520 and let's see if that one's here or here 5153 03:22:21,520 --> 03:22:23,520 here we go so 5154 03:22:23,520 --> 03:22:25,840 this is our route with employees but 5155 03:22:25,840 --> 03:22:28,319 then it has slash and the number one 5156 03:22:28,319 --> 03:22:30,080 where we're requesting 5157 03:22:30,080 --> 03:22:33,359 just one employee not both so let's go 5158 03:22:33,359 --> 03:22:35,520 ahead and send that and see what we get 5159 03:22:35,520 --> 03:22:39,120 in return and we returned the id of one 5160 03:22:39,120 --> 03:22:42,160 so that route is also working and again 5161 03:22:42,160 --> 03:22:44,239 this extension is called thunder client 5162 03:22:44,239 --> 03:22:45,760 if you want to check it out for testing 5163 03:22:45,760 --> 03:22:47,920 the routes in your api now as a 5164 03:22:47,920 --> 03:22:50,080 challenge before my next tutorial you 5165 03:22:50,080 --> 03:22:53,040 could go ahead and write out the code to 5166 03:22:53,040 --> 03:22:54,880 update the 5167 03:22:54,880 --> 03:22:57,600 json that we were receiving and of 5168 03:22:57,600 --> 03:22:59,040 course just hold it in memory don't 5169 03:22:59,040 --> 03:23:00,640 really update the file but you could 5170 03:23:00,640 --> 03:23:03,439 test out what you want to do here for 5171 03:23:03,439 --> 03:23:05,439 each of these routes and you could 5172 03:23:05,439 --> 03:23:07,520 handle each one of those and i'll come 5173 03:23:07,520 --> 03:23:09,120 back with some code for that in the next 5174 03:23:09,120 --> 03:23:11,359 tutorial and what i really also want to 5175 03:23:11,359 --> 03:23:13,359 talk about in the next tutorial is 5176 03:23:13,359 --> 03:23:16,160 organizing the rest of this api 5177 03:23:16,160 --> 03:23:18,080 including the server file 5178 03:23:18,080 --> 03:23:20,560 and setting it up because after that 5179 03:23:20,560 --> 03:23:22,399 we'll pretty much be ready to connect 5180 03:23:22,399 --> 03:23:24,239 everything to whichever database 5181 03:23:24,239 --> 03:23:26,160 technology we would want to so we'll 5182 03:23:26,160 --> 03:23:28,880 organize this with an mvc pattern that 5183 03:23:28,880 --> 03:23:31,279 is model view controller and of course i 5184 03:23:31,279 --> 03:23:34,000 can come back and add some extra code 5185 03:23:34,000 --> 03:23:37,279 here to handle the api data just for the 5186 03:23:37,279 --> 03:23:40,239 json as an extra test it's not really 5187 03:23:40,239 --> 03:23:42,720 anything to do with specifically express 5188 03:23:42,720 --> 03:23:45,520 that's just javascript in handling how 5189 03:23:45,520 --> 03:23:46,720 you would 5190 03:23:46,720 --> 03:23:49,359 basically do crud operations create read 5191 03:23:49,359 --> 03:23:52,319 update and delete 5192 03:23:52,319 --> 03:23:55,120 today we will organize our node.js and 5193 03:23:55,120 --> 03:23:58,000 express api server with the model view 5194 03:23:58,000 --> 03:24:00,080 controller design pattern you may have 5195 03:24:00,080 --> 03:24:02,160 also heard this design pattern referred 5196 03:24:02,160 --> 03:24:06,399 to as mvc that said remember express is 5197 03:24:06,399 --> 03:24:09,760 by definition an unopinionated framework 5198 03:24:09,760 --> 03:24:11,279 and you can organize your project 5199 03:24:11,279 --> 03:24:13,040 however you would like i'm just 5200 03:24:13,040 --> 03:24:15,760 demonstrating mbc because it's a popular 5201 03:24:15,760 --> 03:24:17,600 pattern we're going to start with the 5202 03:24:17,600 --> 03:24:19,600 code repository from the last tutorial 5203 03:24:19,600 --> 03:24:21,359 but if you don't have it you should be 5204 03:24:21,359 --> 03:24:23,439 able to easily follow along without it 5205 03:24:23,439 --> 03:24:25,680 or you can download or clone the starter 5206 03:24:25,680 --> 03:24:27,600 source code from the link i'm providing 5207 03:24:27,600 --> 03:24:29,840 in the description below okay we're 5208 03:24:29,840 --> 03:24:31,760 right here in visual studio code and 5209 03:24:31,760 --> 03:24:34,479 we've got our server.js file and we want 5210 03:24:34,479 --> 03:24:36,960 to use the correct terminology so we 5211 03:24:36,960 --> 03:24:40,560 were keeping our data inside of a data 5212 03:24:40,560 --> 03:24:42,720 folder and you can see i've renamed it 5213 03:24:42,720 --> 03:24:45,080 to model it still has the 5214 03:24:45,080 --> 03:24:47,040 employees.json file in there and we're 5215 03:24:47,040 --> 03:24:50,520 just emulating a database here with a 5216 03:24:50,520 --> 03:24:52,399 employees.json file instead of 5217 03:24:52,399 --> 03:24:54,160 connecting to one at this point because 5218 03:24:54,160 --> 03:24:56,399 you might have my sequel or in 5219 03:24:56,399 --> 03:24:58,399 mind or whatever later on in the series 5220 03:24:58,399 --> 03:25:01,279 i plan to show however right now 5221 03:25:01,279 --> 03:25:03,439 we're just going to simulate that 5222 03:25:03,439 --> 03:25:05,439 connection to a database 5223 03:25:05,439 --> 03:25:07,840 and now besides that we have a views 5224 03:25:07,840 --> 03:25:11,200 folder and really with a restful api we 5225 03:25:11,200 --> 03:25:14,319 won't have many views if hardly one 5226 03:25:14,319 --> 03:25:17,040 maybe we would have a welcome page that 5227 03:25:17,040 --> 03:25:19,840 gives some directions about the api but 5228 03:25:19,840 --> 03:25:21,520 that's about it we're not really serving 5229 03:25:21,520 --> 03:25:24,479 a bunch of static pages or resources 5230 03:25:24,479 --> 03:25:27,040 what we will have though is a controller 5231 03:25:27,040 --> 03:25:30,399 folder so let's go ahead and create that 5232 03:25:30,399 --> 03:25:32,479 when i said controller and really i 5233 03:25:32,479 --> 03:25:34,479 should make this plural because it could 5234 03:25:34,479 --> 03:25:36,319 host more than one so let's go ahead and 5235 03:25:36,319 --> 03:25:39,279 call this controllers and it will hold 5236 03:25:39,279 --> 03:25:42,399 controller files so inside of here let's 5237 03:25:42,399 --> 03:25:45,359 go ahead and create an employees 5238 03:25:45,359 --> 03:25:47,359 controller 5239 03:25:47,359 --> 03:25:50,560 dot js file now currently we have all of 5240 03:25:50,560 --> 03:25:53,520 our logic really inside of our routes so 5241 03:25:53,520 --> 03:25:55,920 if we open up routes and the api 5242 03:25:55,920 --> 03:25:59,359 directory and then look at employees js 5243 03:25:59,359 --> 03:26:01,439 our logic is essentially the route 5244 03:26:01,439 --> 03:26:02,960 handlers so 5245 03:26:02,960 --> 03:26:05,520 for router dot route and then we come to 5246 03:26:05,520 --> 03:26:08,960 the get http method we've got this 5247 03:26:08,960 --> 03:26:10,560 handler that returns all of the 5248 03:26:10,560 --> 03:26:12,880 employees so what we would want to do 5249 03:26:12,880 --> 03:26:15,680 would be remove that logic from here and 5250 03:26:15,680 --> 03:26:18,000 take it to the controller so we could 5251 03:26:18,000 --> 03:26:20,160 name this something like 5252 03:26:20,160 --> 03:26:23,600 get all employees 5253 03:26:23,840 --> 03:26:25,920 and then we can just assign 5254 03:26:25,920 --> 03:26:28,239 that route handler as a function so now 5255 03:26:28,239 --> 03:26:31,200 we have a get all employees function 5256 03:26:31,200 --> 03:26:33,680 that is part of the controller so let's 5257 03:26:33,680 --> 03:26:36,160 save that but what we also need here is 5258 03:26:36,160 --> 03:26:38,239 the data so the data that we were 5259 03:26:38,239 --> 03:26:41,760 pulling in to the employees js route 5260 03:26:41,760 --> 03:26:43,520 inside the api 5261 03:26:43,520 --> 03:26:46,160 is actually will need to be inside of 5262 03:26:46,160 --> 03:26:48,640 the controller at this point instead of 5263 03:26:48,640 --> 03:26:51,760 the route we're breaking that out 5264 03:26:51,760 --> 03:26:53,840 and so we'll put that at the top of this 5265 03:26:53,840 --> 03:26:56,800 file as well so these functions can work 5266 03:26:56,800 --> 03:26:59,600 with that data so i'll save this again 5267 03:26:59,600 --> 03:27:01,760 and now back inside of employees let's 5268 03:27:01,760 --> 03:27:04,640 pull out the logic from the post 5269 03:27:04,640 --> 03:27:08,000 and here we go with that logic and now 5270 03:27:08,000 --> 03:27:09,439 we'll call this 5271 03:27:09,439 --> 03:27:12,840 function a different name we've got 5272 03:27:12,840 --> 03:27:15,520 create new 5273 03:27:15,520 --> 03:27:17,279 employee 5274 03:27:17,279 --> 03:27:20,319 and we'll set it equal to that action 5275 03:27:20,319 --> 03:27:22,319 and then the next function will go ahead 5276 03:27:22,319 --> 03:27:24,880 and name as well and that will be update 5277 03:27:24,880 --> 03:27:27,880 employee 5278 03:27:29,040 --> 03:27:32,000 we'll set it equal to what we find here 5279 03:27:32,000 --> 03:27:33,520 in the put 5280 03:27:33,520 --> 03:27:36,000 portion of this route 5281 03:27:36,000 --> 03:27:37,920 and now we'll come back and assign that 5282 03:27:37,920 --> 03:27:40,479 to update employee 5283 03:27:40,479 --> 03:27:42,560 and finally as you might guess we'll 5284 03:27:42,560 --> 03:27:43,920 have 5285 03:27:43,920 --> 03:27:46,960 delete employee 5286 03:27:46,960 --> 03:27:49,200 and we'll set that equal to the handler 5287 03:27:49,200 --> 03:27:50,800 for delete 5288 03:27:50,800 --> 03:27:53,200 not copy but cut there we go 5289 03:27:53,200 --> 03:27:55,359 and we can paste that in here as well 5290 03:27:55,359 --> 03:27:56,880 now i'll go ahead and save that now 5291 03:27:56,880 --> 03:27:58,479 we're not finished with the controller 5292 03:27:58,479 --> 03:28:00,960 by any means but we have the 5293 03:28:00,960 --> 03:28:03,680 first actions in here for these routes 5294 03:28:03,680 --> 03:28:05,760 and we haven't put anything back in here 5295 03:28:05,760 --> 03:28:08,399 yet but we will but there's one more 5296 03:28:08,399 --> 03:28:10,560 function to create here and this would 5297 03:28:10,560 --> 03:28:13,680 be called just git employee instead of 5298 03:28:13,680 --> 03:28:16,399 get all employees so let's go ahead and 5299 03:28:16,399 --> 03:28:18,399 get this 5300 03:28:18,399 --> 03:28:20,239 action as well 5301 03:28:20,239 --> 03:28:22,319 or route handlers what we could refer to 5302 03:28:22,319 --> 03:28:23,680 those as still 5303 03:28:23,680 --> 03:28:26,560 and let's say git 5304 03:28:26,560 --> 03:28:28,560 employee 5305 03:28:28,560 --> 03:28:31,439 and now we have all of these different 5306 03:28:31,439 --> 03:28:33,920 functions defined inside of our 5307 03:28:33,920 --> 03:28:35,760 employees controller 5308 03:28:35,760 --> 03:28:37,520 and of course we're pulling in the data 5309 03:28:37,520 --> 03:28:39,760 here at the top but as you might expect 5310 03:28:39,760 --> 03:28:42,560 we need to export these as well so we'll 5311 03:28:42,560 --> 03:28:47,120 have module dot exports 5312 03:28:47,120 --> 03:28:49,279 let's set this equal to an object now 5313 03:28:49,279 --> 03:28:50,840 and so we had get 5314 03:28:50,840 --> 03:28:53,680 all employees 5315 03:28:53,680 --> 03:28:55,520 and then we had 5316 03:28:55,520 --> 03:28:57,439 create new employee 5317 03:28:57,439 --> 03:29:00,080 and we had update employee 5318 03:29:00,080 --> 03:29:01,439 and 5319 03:29:01,439 --> 03:29:04,720 delete employee and then we had get 5320 03:29:04,720 --> 03:29:07,279 employee and so we're exporting all of 5321 03:29:07,279 --> 03:29:09,600 these functions now let's go back to the 5322 03:29:09,600 --> 03:29:11,439 employees js 5323 03:29:11,439 --> 03:29:14,319 routes and let's import our controller 5324 03:29:14,319 --> 03:29:16,479 so let's call this 5325 03:29:16,479 --> 03:29:18,160 employees 5326 03:29:18,160 --> 03:29:20,319 controller and we'll set it equal to 5327 03:29:20,319 --> 03:29:22,560 require and then we have to consider 5328 03:29:22,560 --> 03:29:25,520 where we are so we go up a folder 5329 03:29:25,520 --> 03:29:28,000 and then up another folder and once 5330 03:29:28,000 --> 03:29:29,600 we're there now we can select the 5331 03:29:29,600 --> 03:29:31,840 controllers folder and inside there we 5332 03:29:31,840 --> 03:29:34,399 find our employees controller now we 5333 03:29:34,399 --> 03:29:36,960 could deconstruct those functions here 5334 03:29:36,960 --> 03:29:39,439 as we import those in but i think i'll 5335 03:29:39,439 --> 03:29:41,439 leave the name employees controller 5336 03:29:41,439 --> 03:29:43,120 because we're emphasizing creating a 5337 03:29:43,120 --> 03:29:45,200 controller here and then we'll just put 5338 03:29:45,200 --> 03:29:47,279 in the functions and in visual studio 5339 03:29:47,279 --> 03:29:49,359 code i believe if i start typing the 5340 03:29:49,359 --> 03:29:50,880 functions 5341 03:29:50,880 --> 03:29:52,720 it's going to import the part that says 5342 03:29:52,720 --> 03:29:54,640 employees controller for me sure enough 5343 03:29:54,640 --> 03:29:56,880 it does so now we can have create new 5344 03:29:56,880 --> 03:29:58,960 employee and select that 5345 03:29:58,960 --> 03:30:00,720 and here we're going to have 5346 03:30:00,720 --> 03:30:02,560 update employee 5347 03:30:02,560 --> 03:30:04,960 and finally we should have 5348 03:30:04,960 --> 03:30:07,120 delete employee not this delete we 5349 03:30:07,120 --> 03:30:10,160 didn't quite get that right 5350 03:30:10,160 --> 03:30:11,279 delete 5351 03:30:11,279 --> 03:30:14,080 employee there it is and so all of these 5352 03:30:14,080 --> 03:30:15,760 actions are coming from the controller 5353 03:30:15,760 --> 03:30:17,840 or route handlers we could say and this 5354 03:30:17,840 --> 03:30:20,640 still doesn't look right we didn't get 5355 03:30:20,640 --> 03:30:22,000 we've got a d in front of there 5356 03:30:22,000 --> 03:30:24,399 employees controller dot 5357 03:30:24,399 --> 03:30:26,319 delete employee that's what we should 5358 03:30:26,319 --> 03:30:30,800 have and now here we should have git 5359 03:30:30,800 --> 03:30:31,840 employee 5360 03:30:31,840 --> 03:30:33,840 with the employees controller so this 5361 03:30:33,840 --> 03:30:35,840 definitely cleaned up our route file 5362 03:30:35,840 --> 03:30:38,000 here quite a bit and we've separated the 5363 03:30:38,000 --> 03:30:40,800 logic into our controller and that's 5364 03:30:40,800 --> 03:30:42,880 really what the model view controller 5365 03:30:42,880 --> 03:30:45,520 pattern is all about and so we could do 5366 03:30:45,520 --> 03:30:47,279 that with other routes if we had other 5367 03:30:47,279 --> 03:30:49,520 routes as well but now we've really 5368 03:30:49,520 --> 03:30:52,000 cleaned up our whole project let's go 5369 03:30:52,000 --> 03:30:54,640 back and do just a little more cleanup 5370 03:30:54,640 --> 03:30:56,720 in the server.js and we can get rid of 5371 03:30:56,720 --> 03:30:58,399 some of these views that we're no longer 5372 03:30:58,399 --> 03:31:00,560 going to use one other thing i'd like to 5373 03:31:00,560 --> 03:31:02,479 do is you see we're pulling some things 5374 03:31:02,479 --> 03:31:04,560 into the server file right at the 5375 03:31:04,560 --> 03:31:06,560 beginning and we're starting to use the 5376 03:31:06,560 --> 03:31:08,720 logger right away but we're taking up a 5377 03:31:08,720 --> 03:31:11,279 lot of space here with cores and we 5378 03:31:11,279 --> 03:31:13,439 don't have any kind of configuration 5379 03:31:13,439 --> 03:31:15,359 folder over here in the file tree yet so 5380 03:31:15,359 --> 03:31:17,359 let's go ahead and create a 5381 03:31:17,359 --> 03:31:21,600 config folder not js just config folder 5382 03:31:21,600 --> 03:31:24,840 and now we can go ahead and create a 5383 03:31:24,840 --> 03:31:27,200 course options 5384 03:31:27,200 --> 03:31:29,600 dot js file right there 5385 03:31:29,600 --> 03:31:32,239 and back in the server let's go ahead 5386 03:31:32,239 --> 03:31:34,560 and take our white list and cores 5387 03:31:34,560 --> 03:31:35,760 options 5388 03:31:35,760 --> 03:31:37,920 and cut those out of here 5389 03:31:37,920 --> 03:31:40,960 and paste them into our cores options js 5390 03:31:40,960 --> 03:31:42,640 here and i'm going to put each one of 5391 03:31:42,640 --> 03:31:46,160 the urls on its own line 5392 03:31:46,160 --> 03:31:49,600 just so it's easier to read 5393 03:31:50,960 --> 03:31:52,800 and after we define the white list at 5394 03:31:52,800 --> 03:31:55,200 the top we have our cores options 5395 03:31:55,200 --> 03:31:57,200 and as you might guess we need to do 5396 03:31:57,200 --> 03:31:58,560 module dot 5397 03:31:58,560 --> 03:31:59,760 exports 5398 03:31:59,760 --> 03:32:03,040 set this equal to cores 5399 03:32:03,040 --> 03:32:04,080 options 5400 03:32:04,080 --> 03:32:06,000 and now we'll need to import this back 5401 03:32:06,000 --> 03:32:08,640 inside of the server 5402 03:32:08,640 --> 03:32:10,479 so at the top of the server here we 5403 03:32:10,479 --> 03:32:12,960 could just put it right under cores 5404 03:32:12,960 --> 03:32:14,239 and we'll say 5405 03:32:14,239 --> 03:32:16,319 const cores 5406 03:32:16,319 --> 03:32:18,800 options and set this equal to 5407 03:32:18,800 --> 03:32:21,200 require dot slash 5408 03:32:21,200 --> 03:32:24,640 dot slash there we go config 5409 03:32:24,640 --> 03:32:26,560 and now inside of the config folder we 5410 03:32:26,560 --> 03:32:29,760 have our course options 5411 03:32:29,760 --> 03:32:31,359 we can save that let's see if there's 5412 03:32:31,359 --> 03:32:33,040 anything else we can clean up i've got 5413 03:32:33,040 --> 03:32:34,800 some extra comments in here of course 5414 03:32:34,800 --> 03:32:36,880 this being a tutorial that kind of makes 5415 03:32:36,880 --> 03:32:37,840 sense 5416 03:32:37,840 --> 03:32:40,080 let's make them a little bit smaller 5417 03:32:40,080 --> 03:32:44,000 let's put form data besides that 5418 03:32:44,000 --> 03:32:46,640 and the routes look good and we have our 5419 03:32:46,640 --> 03:32:49,120 catch all 404 here 5420 03:32:49,120 --> 03:32:50,880 and we have our error handler and we 5421 03:32:50,880 --> 03:32:54,560 have made our server.js much smaller 5422 03:32:54,560 --> 03:32:56,720 we're down to 44 lines of code including 5423 03:32:56,720 --> 03:32:59,439 some spaces here and we have pulled that 5424 03:32:59,439 --> 03:33:00,560 logic 5425 03:33:00,560 --> 03:33:03,200 out into a controller so that has really 5426 03:33:03,200 --> 03:33:04,720 helped clean up but we're really not 5427 03:33:04,720 --> 03:33:06,720 going to need these example of a 5428 03:33:06,720 --> 03:33:09,040 subdirectory anymore so let's go ahead 5429 03:33:09,040 --> 03:33:10,720 and get rid of that 5430 03:33:10,720 --> 03:33:13,120 and that means we can get rid of the app 5431 03:33:13,120 --> 03:33:15,359 used for the static files in the 5432 03:33:15,359 --> 03:33:17,279 subdirectory as well 5433 03:33:17,279 --> 03:33:19,359 and save that once again and that means 5434 03:33:19,359 --> 03:33:20,399 we can 5435 03:33:20,399 --> 03:33:21,600 delete 5436 03:33:21,600 --> 03:33:24,399 the subdirectory route from the tree 5437 03:33:24,399 --> 03:33:26,720 and we can also go ahead and delete the 5438 03:33:26,720 --> 03:33:29,760 subdirectory over here in the views 5439 03:33:29,760 --> 03:33:32,319 and let's go ahead and delete the new 5440 03:33:32,319 --> 03:33:34,160 page 5441 03:33:34,160 --> 03:33:36,800 and that means we can go into our root 5442 03:33:36,800 --> 03:33:37,760 routes 5443 03:33:37,760 --> 03:33:39,920 and we will not be routing to a new page 5444 03:33:39,920 --> 03:33:42,319 or an old page any longer either and get 5445 03:33:42,319 --> 03:33:43,920 rid of both of those 5446 03:33:43,920 --> 03:33:45,439 and now that's quite a bit of cleanup 5447 03:33:45,439 --> 03:33:47,359 but at least we still have our splash 5448 03:33:47,359 --> 03:33:49,520 page for the index if we wanted to put 5449 03:33:49,520 --> 03:33:51,920 some directions for our api there 5450 03:33:51,920 --> 03:33:54,800 and so we're handling that inside of the 5451 03:33:54,800 --> 03:33:56,560 root route 5452 03:33:56,560 --> 03:33:58,160 and that's in our routes folder and then 5453 03:33:58,160 --> 03:34:00,080 we have our api directory and that 5454 03:34:00,080 --> 03:34:03,040 handles the employees routes here 5455 03:34:03,040 --> 03:34:05,279 okay with everything cleaned up in the 5456 03:34:05,279 --> 03:34:08,479 last tutorial i promised to go ahead and 5457 03:34:08,479 --> 03:34:10,960 provide the javascript code this is not 5458 03:34:10,960 --> 03:34:13,200 so much just dealing with express or 5459 03:34:13,200 --> 03:34:15,760 node but the code if you were to have an 5460 03:34:15,760 --> 03:34:19,120 api that was going to update 5461 03:34:19,120 --> 03:34:22,399 not the json file but just in memory and 5462 03:34:22,399 --> 03:34:25,600 kind of emulate an api before we've 5463 03:34:25,600 --> 03:34:27,920 connected it to a database so at this 5464 03:34:27,920 --> 03:34:30,080 point i can go ahead and provide that 5465 03:34:30,080 --> 03:34:32,000 code and if you had went ahead and 5466 03:34:32,000 --> 03:34:33,680 worked on the challenge that i said at 5467 03:34:33,680 --> 03:34:35,680 the end of the last tutorial you can 5468 03:34:35,680 --> 03:34:38,479 compare your code to what i did i'm just 5469 03:34:38,479 --> 03:34:40,720 noticing i didn't change the path to the 5470 03:34:40,720 --> 03:34:42,479 data folder here either for the 5471 03:34:42,479 --> 03:34:44,560 employees because really it didn't need 5472 03:34:44,560 --> 03:34:46,239 to come up to anymore so you would just 5473 03:34:46,239 --> 03:34:48,560 come up out of the controller folder and 5474 03:34:48,560 --> 03:34:50,479 then you're not even going into a data 5475 03:34:50,479 --> 03:34:53,200 folder at this point you're going into a 5476 03:34:53,200 --> 03:34:54,560 model folder 5477 03:34:54,560 --> 03:34:57,120 but i'm going to change this all of this 5478 03:34:57,120 --> 03:34:59,200 now but i wanted to say that correction 5479 03:34:59,200 --> 03:35:00,560 about the path 5480 03:35:00,560 --> 03:35:02,800 and let's have the data 5481 03:35:02,800 --> 03:35:05,120 object set like this and if you work 5482 03:35:05,120 --> 03:35:07,200 with react i'm thinking in more of a 5483 03:35:07,200 --> 03:35:09,600 reactive way when i do this but i've got 5484 03:35:09,600 --> 03:35:12,000 the employees data set still pulling it 5485 03:35:12,000 --> 03:35:13,840 in and just setting it directly to the 5486 03:35:13,840 --> 03:35:16,479 employees property of the data object 5487 03:35:16,479 --> 03:35:18,239 but then i've got a set employees 5488 03:35:18,239 --> 03:35:20,399 function here as well that i'll use 5489 03:35:20,399 --> 03:35:23,120 inside of these other functions now as 5490 03:35:23,120 --> 03:35:25,520 you might expect get all employees 5491 03:35:25,520 --> 03:35:27,520 doesn't really need any extra work we're 5492 03:35:27,520 --> 03:35:30,160 just returning all of the employees by 5493 03:35:30,160 --> 03:35:32,720 referencing data dot employees and 5494 03:35:32,720 --> 03:35:35,439 that's fine now to create a new employee 5495 03:35:35,439 --> 03:35:38,319 i'll paste in my code and we can review 5496 03:35:38,319 --> 03:35:39,520 it 5497 03:35:39,520 --> 03:35:41,600 there's a little bit more work there so 5498 03:35:41,600 --> 03:35:43,840 when we get a new employee we want to 5499 03:35:43,840 --> 03:35:45,760 create the new id and instead of 5500 03:35:45,760 --> 03:35:48,160 importing a package like uuid or 5501 03:35:48,160 --> 03:35:50,399 something like that i'm just grabbing 5502 03:35:50,399 --> 03:35:52,479 the last employee 5503 03:35:52,479 --> 03:35:54,399 in the json array 5504 03:35:54,399 --> 03:35:57,439 and i'm adding one to the id that exists 5505 03:35:57,439 --> 03:35:59,760 there whatever the last one is so of 5506 03:35:59,760 --> 03:36:01,040 course i need to make sure they're in 5507 03:36:01,040 --> 03:36:02,800 chronological order which will come into 5508 03:36:02,800 --> 03:36:04,800 play later but that's what i'm doing 5509 03:36:04,800 --> 03:36:06,960 here other than that 5510 03:36:06,960 --> 03:36:10,160 first name and last name assigned to the 5511 03:36:10,160 --> 03:36:11,520 parameters we're getting from 5512 03:36:11,520 --> 03:36:13,120 request.body 5513 03:36:13,120 --> 03:36:14,720 and then of course it's first name and 5514 03:36:14,720 --> 03:36:16,000 last name 5515 03:36:16,000 --> 03:36:18,880 after that we're making sure 5516 03:36:18,880 --> 03:36:22,000 that a first name and last name are sent 5517 03:36:22,000 --> 03:36:24,720 and if not we're sending a response 5518 03:36:24,720 --> 03:36:27,439 400 saying hey 5519 03:36:27,439 --> 03:36:30,560 we didn't get the required data after 5520 03:36:30,560 --> 03:36:33,359 that i'm using the set employees 5521 03:36:33,359 --> 03:36:36,800 function to go ahead and set the 5522 03:36:36,800 --> 03:36:39,439 employees to the new information and of 5523 03:36:39,439 --> 03:36:41,920 course the new employee as it at the end 5524 03:36:41,920 --> 03:36:44,080 of the array and i'm just realizing here 5525 03:36:44,080 --> 03:36:46,560 i didn't send a different status when we 5526 03:36:46,560 --> 03:36:48,319 send the employees back and i'm sending 5527 03:36:48,319 --> 03:36:49,920 all of the employees instead of just the 5528 03:36:49,920 --> 03:36:52,080 new ones so we could see the update but 5529 03:36:52,080 --> 03:36:55,680 here we could also send a status of 201 5530 03:36:55,680 --> 03:36:57,920 which means it created the new record 5531 03:36:57,920 --> 03:37:00,000 and we'll go ahead and save that okay 5532 03:37:00,000 --> 03:37:01,920 update employee gets a little more 5533 03:37:01,920 --> 03:37:04,000 complicated but it's not too bad so 5534 03:37:04,000 --> 03:37:05,840 let's go ahead and paste this code in 5535 03:37:05,840 --> 03:37:06,960 and review it 5536 03:37:06,960 --> 03:37:09,760 so here we grab the employee if we 5537 03:37:09,760 --> 03:37:12,080 received an id 5538 03:37:12,080 --> 03:37:14,080 as a parameter and we should because 5539 03:37:14,080 --> 03:37:16,479 we're updating an existing employee and 5540 03:37:16,479 --> 03:37:18,479 if the employee does not exist we're 5541 03:37:18,479 --> 03:37:20,880 returning a response status 400 meaning 5542 03:37:20,880 --> 03:37:23,840 we did not get good data to update we 5543 03:37:23,840 --> 03:37:25,439 have the message here as well employee 5544 03:37:25,439 --> 03:37:27,840 id is not found and that would be the 5545 03:37:27,840 --> 03:37:29,920 end of it if we didn't get an id but 5546 03:37:29,920 --> 03:37:32,319 then if we did then we're checking to 5547 03:37:32,319 --> 03:37:34,800 see if we got a first name go ahead and 5548 03:37:34,800 --> 03:37:37,439 set the employee that we found to the 5549 03:37:37,439 --> 03:37:40,160 new parameter value and the same for 5550 03:37:40,160 --> 03:37:41,920 last name and of course to that 5551 03:37:41,920 --> 03:37:43,760 parameter value as well 5552 03:37:43,760 --> 03:37:46,800 and then we filter the array and we 5553 03:37:46,800 --> 03:37:47,840 remove 5554 03:37:47,840 --> 03:37:50,880 the existing employee record from it so 5555 03:37:50,880 --> 03:37:54,319 then we have an array with the existing 5556 03:37:54,319 --> 03:37:56,080 or i mean without 5557 03:37:56,080 --> 03:37:58,399 the existing employee id and then we 5558 03:37:58,399 --> 03:37:59,760 have the new 5559 03:37:59,760 --> 03:38:01,040 employee 5560 03:38:01,040 --> 03:38:03,680 data that we've updated here so we go 5561 03:38:03,680 --> 03:38:06,479 ahead and create an unsorted array and 5562 03:38:06,479 --> 03:38:08,080 we need to remember it's unsorted 5563 03:38:08,080 --> 03:38:10,160 because we do need this array in 5564 03:38:10,160 --> 03:38:13,359 chronological order by id 5565 03:38:13,359 --> 03:38:16,800 and so when i call the set employees 5566 03:38:16,800 --> 03:38:20,640 function from our data object i go ahead 5567 03:38:20,640 --> 03:38:24,160 and sort the array by that id column and 5568 03:38:24,160 --> 03:38:26,080 this is a chain ternary statement this 5569 03:38:26,080 --> 03:38:27,199 is where it could look a little 5570 03:38:27,199 --> 03:38:29,600 complicated if you have not worked with 5571 03:38:29,600 --> 03:38:32,800 sorting arrays before so 5572 03:38:32,800 --> 03:38:34,560 we start out with the ternary statement 5573 03:38:34,560 --> 03:38:37,040 here same pretty much if 5574 03:38:37,040 --> 03:38:40,560 the one element of the array dot id is 5575 03:38:40,560 --> 03:38:42,960 greater than the other than return one 5576 03:38:42,960 --> 03:38:45,680 and normally if we were just doing that 5577 03:38:45,680 --> 03:38:47,680 then if not it would be returning a 5578 03:38:47,680 --> 03:38:50,160 negative one but we need to have a zero 5579 03:38:50,160 --> 03:38:52,160 if they're even as well so then we chain 5580 03:38:52,160 --> 03:38:54,479 this into another ternary 5581 03:38:54,479 --> 03:38:55,600 and there 5582 03:38:55,600 --> 03:38:57,760 it returns a minus one 5583 03:38:57,760 --> 03:39:00,080 or the zero at the end of that 5584 03:39:00,080 --> 03:39:02,720 and then of course we send the employees 5585 03:39:02,720 --> 03:39:04,800 after they've been updated by set 5586 03:39:04,800 --> 03:39:07,680 employees and that puts the array the 5587 03:39:07,680 --> 03:39:09,920 json array back in the order we need it 5588 03:39:09,920 --> 03:39:11,920 right there okay scrolling up to get a 5589 03:39:11,920 --> 03:39:13,840 little room to put in the code for the 5590 03:39:13,840 --> 03:39:15,680 delete employee 5591 03:39:15,680 --> 03:39:17,760 we paste this in it looks very similar 5592 03:39:17,760 --> 03:39:19,840 we find that employee but if we didn't 5593 03:39:19,840 --> 03:39:22,560 get the id we send the 400 response just 5594 03:39:22,560 --> 03:39:25,760 like above we also create a filtered ray 5595 03:39:25,760 --> 03:39:28,319 a filtered array just like above we did 5596 03:39:28,319 --> 03:39:30,319 in the update but then at this point 5597 03:39:30,319 --> 03:39:32,239 we've deleted the employee so all we 5598 03:39:32,239 --> 03:39:34,960 need to do is set the employees to the 5599 03:39:34,960 --> 03:39:37,760 filtered array and then we once again 5600 03:39:37,760 --> 03:39:40,880 return the employees and finally the get 5601 03:39:40,880 --> 03:39:42,720 employee and of course 5602 03:39:42,720 --> 03:39:44,239 this is 5603 03:39:44,239 --> 03:39:46,800 handling a request for the data of just 5604 03:39:46,800 --> 03:39:48,960 one employee so we find out who that 5605 03:39:48,960 --> 03:39:51,840 employee is and once again if the 5606 03:39:51,840 --> 03:39:54,000 employee doesn't exist we send that same 5607 03:39:54,000 --> 03:39:56,160 response that we did not get good data 5608 03:39:56,160 --> 03:39:58,399 but if the employee exists all we need 5609 03:39:58,399 --> 03:40:01,760 to do is return that specific employee 5610 03:40:01,760 --> 03:40:03,600 and then we're down here to the exports 5611 03:40:03,600 --> 03:40:05,920 so i will include all of this code 5612 03:40:05,920 --> 03:40:07,520 inside of 5613 03:40:07,520 --> 03:40:11,120 the git hub repository for the end this 5614 03:40:11,120 --> 03:40:13,040 will not be the starter code but the 5615 03:40:13,040 --> 03:40:15,920 beginning and then you can see this and 5616 03:40:15,920 --> 03:40:17,520 compare to years if any of my 5617 03:40:17,520 --> 03:40:19,840 explanation did not make sense and it 5618 03:40:19,840 --> 03:40:21,680 looks like i've got an error here in the 5619 03:40:21,680 --> 03:40:23,760 code i think i missed the ending curly 5620 03:40:23,760 --> 03:40:25,600 brace there it is they should be yellow 5621 03:40:25,600 --> 03:40:27,920 at least the way i have my visual studio 5622 03:40:27,920 --> 03:40:29,439 code set up 5623 03:40:29,439 --> 03:40:32,080 tab that over and save and all looks 5624 03:40:32,080 --> 03:40:34,640 good now i think i still have an issue 5625 03:40:34,640 --> 03:40:36,880 here the delete employee did not close 5626 03:40:36,880 --> 03:40:38,720 out 5627 03:40:38,720 --> 03:40:40,560 if i close that out 5628 03:40:40,560 --> 03:40:43,680 then i can remove that there we go 5629 03:40:43,680 --> 03:40:45,359 almost had a bigger mistake that would 5630 03:40:45,359 --> 03:40:47,600 have taken me longer to figure out so 5631 03:40:47,600 --> 03:40:49,520 save that and now it looks like get 5632 03:40:49,520 --> 03:40:52,399 employee delete employee update employee 5633 03:40:52,399 --> 03:40:54,160 create new employee 5634 03:40:54,160 --> 03:40:56,080 get all employees that all looks like 5635 03:40:56,080 --> 03:40:58,239 they're defined correctly and there's no 5636 03:40:58,239 --> 03:41:00,720 more error here in the file getting rid 5637 03:41:00,720 --> 03:41:03,600 of the extra space and let's go to 5638 03:41:03,600 --> 03:41:06,319 thunder client but we also need to go 5639 03:41:06,319 --> 03:41:07,439 ahead 5640 03:41:07,439 --> 03:41:09,279 and start the server up because i don't 5641 03:41:09,279 --> 03:41:11,359 think we have done that yet 5642 03:41:11,359 --> 03:41:13,680 and with the dev server open 5643 03:41:13,680 --> 03:41:15,439 type in i mean the terminal open we'll 5644 03:41:15,439 --> 03:41:17,040 type npm 5645 03:41:17,040 --> 03:41:18,960 run dev 5646 03:41:18,960 --> 03:41:20,080 there we go 5647 03:41:20,080 --> 03:41:22,239 and start the dev server and then we'll 5648 03:41:22,239 --> 03:41:24,800 be able to test out our api running on 5649 03:41:24,800 --> 03:41:27,040 port 3500 that's what we needed we'll 5650 03:41:27,040 --> 03:41:30,000 open that employees api 5651 03:41:30,000 --> 03:41:32,319 collection that i'd created in thunder 5652 03:41:32,319 --> 03:41:34,160 client and now this you'll have to 5653 03:41:34,160 --> 03:41:36,000 create yourself of course if you haven't 5654 03:41:36,000 --> 03:41:38,720 but let's test get employees and see 5655 03:41:38,720 --> 03:41:40,239 what we start with there so we've got a 5656 03:41:40,239 --> 03:41:42,319 get request and we're going to the 5657 03:41:42,319 --> 03:41:45,040 employees route 5658 03:41:45,040 --> 03:41:47,680 see the results and we've got the two 5659 03:41:47,680 --> 03:41:50,319 starter employees dave gray and john 5660 03:41:50,319 --> 03:41:51,600 smith 5661 03:41:51,600 --> 03:41:54,560 okay now let's go to post and let's post 5662 03:41:54,560 --> 03:41:57,600 a new employee and if we go to body you 5663 03:41:57,600 --> 03:41:59,920 can see i'm sending some raw json here 5664 03:41:59,920 --> 03:42:03,520 we're posting employee john doe 5665 03:42:03,520 --> 03:42:06,000 we'll send that post request there's a 5666 03:42:06,000 --> 03:42:08,880 201 created response like we wanted to 5667 03:42:08,880 --> 03:42:11,199 send and now we have john doe here and 5668 03:42:11,199 --> 03:42:14,080 he has an id of three as we expected him 5669 03:42:14,080 --> 03:42:15,120 too 5670 03:42:15,120 --> 03:42:17,199 so now let's go to the 5671 03:42:17,199 --> 03:42:19,439 put employee 5672 03:42:19,439 --> 03:42:20,319 and 5673 03:42:20,319 --> 03:42:22,960 same route with a put request we'll go 5674 03:42:22,960 --> 03:42:24,720 to the body and see what i'm sending in 5675 03:42:24,720 --> 03:42:26,160 the json 5676 03:42:26,160 --> 03:42:29,359 and let's change this let's update 5677 03:42:29,359 --> 03:42:31,920 the second employee so it's right in the 5678 03:42:31,920 --> 03:42:32,880 middle 5679 03:42:32,880 --> 03:42:35,600 and change his name to david 5680 03:42:35,600 --> 03:42:38,560 now let's go ahead and send and now 5681 03:42:38,560 --> 03:42:40,399 let's look at our employees that were 5682 03:42:40,399 --> 03:42:42,800 sent back and yes second employee is now 5683 03:42:42,800 --> 03:42:44,960 david smith and they're still in the 5684 03:42:44,960 --> 03:42:46,319 correct order 5685 03:42:46,319 --> 03:42:48,640 he did not get inserted at the end it's 5686 03:42:48,640 --> 03:42:50,640 not out of order and so 5687 03:42:50,640 --> 03:42:53,279 that all worked as we wanted it to 5688 03:42:53,279 --> 03:42:57,439 now let's try the delete employee 5689 03:42:57,520 --> 03:42:59,520 and let's look at the body once again 5690 03:42:59,520 --> 03:43:02,080 what's being sent in the request and 5691 03:43:02,080 --> 03:43:04,160 we're going to delete employee number 5692 03:43:04,160 --> 03:43:08,239 three so let's go ahead and do that 5693 03:43:08,239 --> 03:43:10,160 and now if we look all the employees 5694 03:43:10,160 --> 03:43:11,600 were sent back now we only have two 5695 03:43:11,600 --> 03:43:14,000 employees once again and now let's 5696 03:43:14,000 --> 03:43:17,120 finally request an employee this is get 5697 03:43:17,120 --> 03:43:18,720 an employee we're going to request 5698 03:43:18,720 --> 03:43:21,359 employee number one 5699 03:43:21,359 --> 03:43:23,760 send that request and it looks like we 5700 03:43:23,760 --> 03:43:25,920 received the information back for 5701 03:43:25,920 --> 03:43:28,560 employee number one so all of our routes 5702 03:43:28,560 --> 03:43:31,120 are working as expected once again i'll 5703 03:43:31,120 --> 03:43:34,080 include everything that you saw today in 5704 03:43:34,080 --> 03:43:36,239 the employee controller if you're 5705 03:43:36,239 --> 03:43:38,880 curious about the api code i'll include 5706 03:43:38,880 --> 03:43:41,520 that and more in the github 5707 03:43:41,520 --> 03:43:43,120 link that i'm going to put in and don't 5708 03:43:43,120 --> 03:43:46,800 mistake the final code or the starter 5709 03:43:46,800 --> 03:43:50,720 source code i will have a link to both 5710 03:43:50,720 --> 03:43:53,600 today we will add user authorization to 5711 03:43:53,600 --> 03:43:56,160 our express api we're going to start 5712 03:43:56,160 --> 03:43:58,000 with the code repository from the last 5713 03:43:58,000 --> 03:44:00,000 tutorial but if you don't have it you 5714 03:44:00,000 --> 03:44:01,600 should be able to easily follow along 5715 03:44:01,600 --> 03:44:04,000 without it or you can download or clone 5716 03:44:04,000 --> 03:44:05,680 the starter source code from the link 5717 03:44:05,680 --> 03:44:07,359 that i've provided in the description 5718 03:44:07,359 --> 03:44:09,359 below we're going to get started by 5719 03:44:09,359 --> 03:44:12,080 simulating a user's database table in 5720 03:44:12,080 --> 03:44:15,040 our model directory with the user's json 5721 03:44:15,040 --> 03:44:17,600 file so let's open the model directory 5722 03:44:17,600 --> 03:44:20,000 and let's create a new file and name it 5723 03:44:20,000 --> 03:44:22,319 users.json 5724 03:44:22,319 --> 03:44:24,000 and now we really don't need to put 5725 03:44:24,000 --> 03:44:25,680 anything in here but i'll go ahead and 5726 03:44:25,680 --> 03:44:28,160 just put in an empty array as if it 5727 03:44:28,160 --> 03:44:30,720 would hold json but it's not right now 5728 03:44:30,720 --> 03:44:32,239 because we're going to write over this 5729 03:44:32,239 --> 03:44:35,520 really now user authorization requires 5730 03:44:35,520 --> 03:44:37,760 two routes a registration route to 5731 03:44:37,760 --> 03:44:40,399 register a new user account and an 5732 03:44:40,399 --> 03:44:42,560 authorization route to authorize the 5733 03:44:42,560 --> 03:44:44,960 user after they have created an account 5734 03:44:44,960 --> 03:44:46,960 but we're going to start in the 5735 03:44:46,960 --> 03:44:48,720 controllers folder because that's where 5736 03:44:48,720 --> 03:44:49,840 we'll really 5737 03:44:49,840 --> 03:44:51,840 handle the routes and what we do with 5738 03:44:51,840 --> 03:44:54,160 them so let's go ahead and create a new 5739 03:44:54,160 --> 03:44:57,040 controller and let's call this 5740 03:44:57,040 --> 03:44:58,640 register 5741 03:44:58,640 --> 03:45:00,800 controller.js 5742 03:45:00,800 --> 03:45:02,960 okay in the register controller let's 5743 03:45:02,960 --> 03:45:06,160 start out by pulling in our users from 5744 03:45:06,160 --> 03:45:08,399 the users database that we're simulating 5745 03:45:08,399 --> 03:45:10,640 with that json file i'm going to create 5746 03:45:10,640 --> 03:45:13,520 an object and set this up much like you 5747 03:45:13,520 --> 03:45:16,239 would see with use state and react where 5748 03:45:16,239 --> 03:45:18,640 we have our users state and we'll go 5749 03:45:18,640 --> 03:45:21,120 ahead and require those users 5750 03:45:21,120 --> 03:45:23,520 and this will be slash model 5751 03:45:23,520 --> 03:45:26,000 slash users.json 5752 03:45:26,000 --> 03:45:27,920 and after we do that let's go ahead and 5753 03:45:27,920 --> 03:45:31,279 have a set user's method for this object 5754 03:45:31,279 --> 03:45:34,160 as well and here we can say function 5755 03:45:34,160 --> 03:45:35,920 pass in the data 5756 03:45:35,920 --> 03:45:38,399 and now oops i'm so used to writing an 5757 03:45:38,399 --> 03:45:40,160 arrow function but this will not have an 5758 03:45:40,160 --> 03:45:41,040 arrow 5759 03:45:41,040 --> 03:45:43,120 and it's users 5760 03:45:43,120 --> 03:45:44,479 equals 5761 03:45:44,479 --> 03:45:46,319 data there we go 5762 03:45:46,319 --> 03:45:48,880 and now that we've got our set users 5763 03:45:48,880 --> 03:45:51,279 function and then our users pulled in so 5764 03:45:51,279 --> 03:45:53,199 we'll have our users database object 5765 03:45:53,199 --> 03:45:55,680 that contains both of these just a quick 5766 03:45:55,680 --> 03:45:57,439 way to simulate that so if you're used 5767 03:45:57,439 --> 03:45:59,439 to working with react this should look 5768 03:45:59,439 --> 03:46:01,199 kind of familiar even though it's not 5769 03:46:01,199 --> 03:46:03,439 really the use state and if you're not i 5770 03:46:03,439 --> 03:46:04,880 hope you're familiar enough with 5771 03:46:04,880 --> 03:46:07,279 javascript to understand how this will 5772 03:46:07,279 --> 03:46:09,199 be the users and of course we'll set the 5773 03:46:09,199 --> 03:46:11,439 users using our setter function right 5774 03:46:11,439 --> 03:46:14,239 here okay now we need some other things 5775 03:46:14,239 --> 03:46:15,680 because we're going to work with that 5776 03:46:15,680 --> 03:46:18,560 json file so let's go ahead and pull in 5777 03:46:18,560 --> 03:46:22,000 fs promises and that's the file system 5778 03:46:22,000 --> 03:46:24,720 so we require fs but then after it we 5779 03:46:24,720 --> 03:46:27,920 put dot promises 5780 03:46:27,920 --> 03:46:30,000 and then we're also going to need path 5781 03:46:30,000 --> 03:46:32,479 so let's require 5782 03:46:32,479 --> 03:46:34,479 path well there we go 5783 03:46:34,479 --> 03:46:36,960 if i can type path we'll get it and now 5784 03:46:36,960 --> 03:46:39,600 we're going to need to install a package 5785 03:46:39,600 --> 03:46:42,800 called bcrypt and bcrypt will help us 5786 03:46:42,800 --> 03:46:45,760 hash and salt the passwords that we come 5787 03:46:45,760 --> 03:46:48,560 in so we can securely and safely store 5788 03:46:48,560 --> 03:46:50,960 them in our database so let's go ahead 5789 03:46:50,960 --> 03:46:52,800 and open a terminal window i press 5790 03:46:52,800 --> 03:46:54,720 control back tick here in windows to 5791 03:46:54,720 --> 03:46:56,880 open my terminal window you could also 5792 03:46:56,880 --> 03:46:59,199 go to the terminal menu and choose new 5793 03:46:59,199 --> 03:47:01,279 terminal but here we're going to type 5794 03:47:01,279 --> 03:47:05,359 npm i and then b crypt 5795 03:47:05,359 --> 03:47:08,640 that's b c r y p t and we'll go ahead 5796 03:47:08,640 --> 03:47:10,479 and install this package it shouldn't 5797 03:47:10,479 --> 03:47:11,600 take long 5798 03:47:11,600 --> 03:47:13,880 and when it's complete we'll check our 5799 03:47:13,880 --> 03:47:16,560 package.json file to ensure that it has 5800 03:47:16,560 --> 03:47:18,479 been added to our project and it looks 5801 03:47:18,479 --> 03:47:20,399 like we're about ready to do that now so 5802 03:47:20,399 --> 03:47:23,040 let's see we've got it installed 5803 03:47:23,040 --> 03:47:24,560 scroll down here and i'll choose 5804 03:47:24,560 --> 03:47:26,319 package.json 5805 03:47:26,319 --> 03:47:29,120 and yes bcrypt has been added to our 5806 03:47:29,120 --> 03:47:31,600 package json file so go ahead and close 5807 03:47:31,600 --> 03:47:34,640 that out now we can require bcrypt 5808 03:47:34,640 --> 03:47:36,640 at the top of our file with our other 5809 03:47:36,640 --> 03:47:38,880 requirements so set that equal to 5810 03:47:38,880 --> 03:47:40,319 require 5811 03:47:40,319 --> 03:47:41,120 and 5812 03:47:41,120 --> 03:47:43,439 pull in bcrypt 5813 03:47:43,439 --> 03:47:44,880 okay now that we've done that let's go 5814 03:47:44,880 --> 03:47:48,399 ahead and define our handler for the new 5815 03:47:48,399 --> 03:47:50,399 user information that we'll receive at 5816 03:47:50,399 --> 03:47:52,640 this register route so i'm going to call 5817 03:47:52,640 --> 03:47:53,680 this 5818 03:47:53,680 --> 03:47:55,359 handle 5819 03:47:55,359 --> 03:47:57,120 new user 5820 03:47:57,120 --> 03:48:00,000 and this is going to receive a request 5821 03:48:00,000 --> 03:48:02,000 and a response 5822 03:48:02,000 --> 03:48:04,640 now what i forgot to do was make this an 5823 03:48:04,640 --> 03:48:07,520 async function which we need to do we 5824 03:48:07,520 --> 03:48:11,040 can use async await with bcrypt and we 5825 03:48:11,040 --> 03:48:13,040 might need it somewhere else too now 5826 03:48:13,040 --> 03:48:15,520 when we first pull this information in 5827 03:48:15,520 --> 03:48:17,600 the request is going to have a user and 5828 03:48:17,600 --> 03:48:20,080 a password so let's destructure that 5829 03:48:20,080 --> 03:48:21,920 from the request body so we'll get the 5830 03:48:21,920 --> 03:48:23,120 user 5831 03:48:23,120 --> 03:48:25,120 and the password 5832 03:48:25,120 --> 03:48:28,319 set this equal to the request.body 5833 03:48:28,319 --> 03:48:30,960 and now we can say if 5834 03:48:30,960 --> 03:48:33,199 there's no user or 5835 03:48:33,199 --> 03:48:35,760 there's no password we'll go ahead and 5836 03:48:35,760 --> 03:48:39,199 return right here with the response 5837 03:48:39,199 --> 03:48:41,840 the status will be 400 which stands for 5838 03:48:41,840 --> 03:48:45,840 bad request that's an http status 5839 03:48:45,840 --> 03:48:48,720 code and then json will go ahead and 5840 03:48:48,720 --> 03:48:49,760 send 5841 03:48:49,760 --> 03:48:51,439 a message down 5842 03:48:51,439 --> 03:48:53,439 and we'll say message 5843 03:48:53,439 --> 03:48:55,199 and now we'll say 5844 03:48:55,199 --> 03:48:57,199 username 5845 03:48:57,199 --> 03:48:59,840 and password 5846 03:48:59,840 --> 03:49:01,199 are 5847 03:49:01,199 --> 03:49:04,080 required and i'm noticing that we're not 5848 03:49:04,080 --> 03:49:06,160 wrapping lines so i'm going to press alt 5849 03:49:06,160 --> 03:49:08,800 z in windows that's what goes ahead and 5850 03:49:08,800 --> 03:49:10,640 tells visual studio code to wrap the 5851 03:49:10,640 --> 03:49:12,720 lines of code it may be different if 5852 03:49:12,720 --> 03:49:15,120 you're on mac or linux 5853 03:49:15,120 --> 03:49:18,080 okay so we're sending that response if 5854 03:49:18,080 --> 03:49:20,720 we did not receive the user password but 5855 03:49:20,720 --> 03:49:22,720 now at this point we'll know we have 5856 03:49:22,720 --> 03:49:25,680 received both the user and password and 5857 03:49:25,680 --> 03:49:27,680 this is where we would let me go ahead 5858 03:49:27,680 --> 03:49:30,239 and put a comment here check for 5859 03:49:30,239 --> 03:49:32,000 duplicate 5860 03:49:32,000 --> 03:49:33,680 usernames 5861 03:49:33,680 --> 03:49:35,439 in the database 5862 03:49:35,439 --> 03:49:38,479 and now since we're connecting to our 5863 03:49:38,479 --> 03:49:41,760 json file to use as our database just as 5864 03:49:41,760 --> 03:49:44,399 a simulation for this tutorial we can go 5865 03:49:44,399 --> 03:49:46,880 ahead and check for a duplicate in that 5866 03:49:46,880 --> 03:49:49,439 json by doing this let's go ahead and 5867 03:49:49,439 --> 03:49:51,040 define duplicate 5868 03:49:51,040 --> 03:49:53,600 and then we'll set that equal to 5869 03:49:53,600 --> 03:49:55,600 usersdb 5870 03:49:55,600 --> 03:49:56,880 dot 5871 03:49:56,880 --> 03:49:59,680 users which will pull in the users then 5872 03:49:59,680 --> 03:50:03,279 we can use find and now out of each user 5873 03:50:03,279 --> 03:50:04,960 let's go ahead and call this person so 5874 03:50:04,960 --> 03:50:07,439 it just doesn't get confusing by too 5875 03:50:07,439 --> 03:50:09,760 many users or using the word user over 5876 03:50:09,760 --> 03:50:12,239 and over this will be person and then 5877 03:50:12,239 --> 03:50:14,239 we're seeing if the person 5878 03:50:14,239 --> 03:50:15,520 dot 5879 03:50:15,520 --> 03:50:18,319 username from our user database 5880 03:50:18,319 --> 03:50:21,680 is equal to the user that has been 5881 03:50:21,680 --> 03:50:23,520 submitted the username that's been 5882 03:50:23,520 --> 03:50:25,199 submitted by the user 5883 03:50:25,199 --> 03:50:27,439 and therefore we can see if we get a 5884 03:50:27,439 --> 03:50:30,239 result as duplicate and then we can say 5885 03:50:30,239 --> 03:50:31,359 if 5886 03:50:31,359 --> 03:50:33,199 we have a duplicate 5887 03:50:33,199 --> 03:50:36,080 then we want to return a response 5888 03:50:36,080 --> 03:50:37,840 it's response dot 5889 03:50:37,840 --> 03:50:41,760 send status and it's 409 5890 03:50:41,760 --> 03:50:45,600 and that is a conflict is what that http 5891 03:50:45,600 --> 03:50:49,279 status code stands for but if not we're 5892 03:50:49,279 --> 03:50:52,000 going to start a try catch now so here's 5893 03:50:52,000 --> 03:50:54,720 our try and of course we have a 5894 03:50:54,720 --> 03:50:56,640 catch afterwards where we would catch an 5895 03:50:56,640 --> 03:50:58,960 error 5896 03:50:59,359 --> 03:51:01,199 and now let's go ahead and fill out that 5897 03:51:01,199 --> 03:51:03,359 error block first so if we get an error 5898 03:51:03,359 --> 03:51:05,279 here we're going to send a response with 5899 03:51:05,279 --> 03:51:07,279 a status of 5900 03:51:07,279 --> 03:51:10,239 500 which would be a server error 5901 03:51:10,239 --> 03:51:12,880 we'll have json and let's go ahead and 5902 03:51:12,880 --> 03:51:16,239 send a message here as well 5903 03:51:16,239 --> 03:51:19,359 and we can just pass in the error dot 5904 03:51:19,359 --> 03:51:22,720 message that is received 5905 03:51:22,720 --> 03:51:24,960 okay but in our try block now we need to 5906 03:51:24,960 --> 03:51:27,520 go ahead and create our new user and of 5907 03:51:27,520 --> 03:51:29,920 course also use bcrypt to hash the 5908 03:51:29,920 --> 03:51:31,040 password 5909 03:51:31,040 --> 03:51:32,880 okay i'll put a comment right here that 5910 03:51:32,880 --> 03:51:35,120 we're going to encrypt the password with 5911 03:51:35,120 --> 03:51:37,120 the next line and this is where we'll 5912 03:51:37,120 --> 03:51:40,960 use bcrypt so let's define our hashed 5913 03:51:40,960 --> 03:51:43,760 password and we'll set that equal to a 5914 03:51:43,760 --> 03:51:46,479 weight and then use bcrypt 5915 03:51:46,479 --> 03:51:48,560 and now oops i'm sorry we don't want 5916 03:51:48,560 --> 03:51:51,199 compare we want hash 5917 03:51:51,199 --> 03:51:53,040 and inside of that we pass in the 5918 03:51:53,040 --> 03:51:54,880 password that we've received from the 5919 03:51:54,880 --> 03:51:57,359 user we're going to hash it and here we 5920 03:51:57,359 --> 03:52:00,000 determine the salt rounds so it not only 5921 03:52:00,000 --> 03:52:02,800 hashes the password but it adds a salt 5922 03:52:02,800 --> 03:52:06,399 to it and that really helps protect the 5923 03:52:06,399 --> 03:52:09,279 password if your database is somehow 5924 03:52:09,279 --> 03:52:12,239 compromised because at that point if a 5925 03:52:12,239 --> 03:52:14,880 hacker were able to figure out the hash 5926 03:52:14,880 --> 03:52:16,880 they could crack all of the passwords in 5927 03:52:16,880 --> 03:52:19,120 the database but adding the individual 5928 03:52:19,120 --> 03:52:21,840 salts makes that much more difficult and 5929 03:52:21,840 --> 03:52:24,399 unique for each one so here we'll pass 5930 03:52:24,399 --> 03:52:27,600 in 10 salt rounds which is essentially 5931 03:52:27,600 --> 03:52:29,840 the default or the standard there 5932 03:52:29,840 --> 03:52:33,279 and that will add the salt and the hash 5933 03:52:33,279 --> 03:52:35,520 all at once and bcrip does a great job 5934 03:52:35,520 --> 03:52:37,920 of storing those together at the same 5935 03:52:37,920 --> 03:52:40,000 time so that's really all we have to do 5936 03:52:40,000 --> 03:52:41,840 there now that we've got the hashed 5937 03:52:41,840 --> 03:52:45,279 password we're going to go ahead and 5938 03:52:45,279 --> 03:52:47,600 store i'll put store 5939 03:52:47,600 --> 03:52:49,359 the new user 5940 03:52:49,359 --> 03:52:51,680 and this remember is just going to our 5941 03:52:51,680 --> 03:52:53,760 simulated user table that we're using in 5942 03:52:53,760 --> 03:52:56,880 the json so here i'm going to say 5943 03:52:56,880 --> 03:52:59,040 new user and define the new user object 5944 03:52:59,040 --> 03:53:00,399 first 5945 03:53:00,399 --> 03:53:04,000 we'll set this equal to username 5946 03:53:04,000 --> 03:53:06,319 we'll pass in the user and then we'll 5947 03:53:06,319 --> 03:53:07,600 have a 5948 03:53:07,600 --> 03:53:09,600 password that we're going to store 5949 03:53:09,600 --> 03:53:11,840 and this will be our hashed 5950 03:53:11,840 --> 03:53:14,239 password that we've created now let's 5951 03:53:14,239 --> 03:53:18,000 set the new data with usersdb dot 5952 03:53:18,000 --> 03:53:19,920 set users 5953 03:53:19,920 --> 03:53:22,720 and here we'll pass in all of the data 5954 03:53:22,720 --> 03:53:25,040 that we have so we're working with 5955 03:53:25,040 --> 03:53:27,199 immutable data instead of adding to the 5956 03:53:27,199 --> 03:53:29,520 existing array we're going to create 5957 03:53:29,520 --> 03:53:32,000 a brand new array and then set all of 5958 03:53:32,000 --> 03:53:34,160 that in the database very similar to 5959 03:53:34,160 --> 03:53:36,560 using state and react here so this will 5960 03:53:36,560 --> 03:53:38,239 be dot dot 5961 03:53:38,239 --> 03:53:39,520 users 5962 03:53:39,520 --> 03:53:43,680 db dot users and then we'll also add 5963 03:53:43,680 --> 03:53:44,800 the new 5964 03:53:44,800 --> 03:53:47,359 user all in this new array that we are 5965 03:53:47,359 --> 03:53:48,239 setting 5966 03:53:48,239 --> 03:53:49,120 as 5967 03:53:49,120 --> 03:53:52,239 the new users or the new users data 5968 03:53:52,239 --> 03:53:54,560 okay now that we've set that let's go 5969 03:53:54,560 --> 03:53:57,680 ahead and write it to our json file 5970 03:53:57,680 --> 03:54:00,319 which is our database in this simulation 5971 03:54:00,319 --> 03:54:02,239 so we'll have fs 5972 03:54:02,239 --> 03:54:03,680 promises 5973 03:54:03,680 --> 03:54:04,640 dot 5974 03:54:04,640 --> 03:54:06,479 write file 5975 03:54:06,479 --> 03:54:08,160 and now i'm going to break this out into 5976 03:54:08,160 --> 03:54:09,760 separate lines just so we can see 5977 03:54:09,760 --> 03:54:11,520 everything better i'll go ahead and 5978 03:54:11,520 --> 03:54:15,040 close our terminal window too so now on 5979 03:54:15,040 --> 03:54:17,040 the first line i'm going to use path dot 5980 03:54:17,040 --> 03:54:19,439 join and we're going to pull in the 5981 03:54:19,439 --> 03:54:21,520 directory name 5982 03:54:21,520 --> 03:54:25,199 and after that we need to go up 5983 03:54:25,199 --> 03:54:28,080 out of the controllers directory 5984 03:54:28,080 --> 03:54:30,239 and then we need to go down into the 5985 03:54:30,239 --> 03:54:32,640 model directory 5986 03:54:32,640 --> 03:54:35,279 and then we need to go ahead and write 5987 03:54:35,279 --> 03:54:36,680 our 5988 03:54:36,680 --> 03:54:39,760 users.json file so this will overwrite 5989 03:54:39,760 --> 03:54:42,239 any existing file there 5990 03:54:42,239 --> 03:54:44,000 and once we've done that we need to 5991 03:54:44,000 --> 03:54:46,640 specify the data we're sending and this 5992 03:54:46,640 --> 03:54:48,520 will say 5993 03:54:48,520 --> 03:54:50,800 json.stringify and we're going to pass 5994 03:54:50,800 --> 03:54:54,439 in our usersdb.users 5995 03:54:55,120 --> 03:54:57,279 okay from there let's just log to the 5996 03:54:57,279 --> 03:55:00,319 console so we can see all of the users 5997 03:55:00,319 --> 03:55:03,199 or at least the one user that we add but 5998 03:55:03,199 --> 03:55:05,120 we'll log all in case we go ahead and 5999 03:55:05,120 --> 03:55:07,120 test it more than once and add more than 6000 03:55:07,120 --> 03:55:08,399 one user 6001 03:55:08,399 --> 03:55:09,920 log those to the console and then we 6002 03:55:09,920 --> 03:55:12,800 need to send a status 6003 03:55:12,800 --> 03:55:15,359 and that status is 201 to say the new 6004 03:55:15,359 --> 03:55:17,920 user was created and let's go ahead 6005 03:55:17,920 --> 03:55:20,880 and send some json again with a success 6006 03:55:20,880 --> 03:55:22,880 you could put message here or whatever 6007 03:55:22,880 --> 03:55:24,479 you want but we'll put 6008 03:55:24,479 --> 03:55:26,319 new user 6009 03:55:26,319 --> 03:55:28,160 and then we'll pass in the user so we 6010 03:55:28,160 --> 03:55:29,840 get the username 6011 03:55:29,840 --> 03:55:32,479 and we'll say created and i needed to 6012 03:55:32,479 --> 03:55:33,760 make this 6013 03:55:33,760 --> 03:55:36,319 a template literal instead of using 6014 03:55:36,319 --> 03:55:38,960 single quotes we need backticks 6015 03:55:38,960 --> 03:55:41,040 and that way we can pass the user 6016 03:55:41,040 --> 03:55:42,720 straight into the string that we're 6017 03:55:42,720 --> 03:55:43,920 sending back 6018 03:55:43,920 --> 03:55:45,680 so we've created the new user and 6019 03:55:45,680 --> 03:55:47,439 there's the response 6020 03:55:47,439 --> 03:55:49,840 and that's basically it except at the 6021 03:55:49,840 --> 03:55:52,399 very bottom now we need to use 6022 03:55:52,399 --> 03:55:55,840 module dot exports and let's go ahead 6023 03:55:55,840 --> 03:55:57,120 and put this 6024 03:55:57,120 --> 03:55:59,760 in an object so we can pull in the full 6025 03:55:59,760 --> 03:56:02,880 name of the controller when we import it 6026 03:56:02,880 --> 03:56:04,800 just like we have previously done with 6027 03:56:04,800 --> 03:56:07,359 the employees controller and there were 6028 03:56:07,359 --> 03:56:10,239 finished with the register controller so 6029 03:56:10,239 --> 03:56:12,479 now let's go to the routes folder and 6030 03:56:12,479 --> 03:56:15,040 we'll need to create a new route for 6031 03:56:15,040 --> 03:56:17,199 this let's just call this 6032 03:56:17,199 --> 03:56:19,359 register.js 6033 03:56:19,359 --> 03:56:21,680 and now at the top of register js we 6034 03:56:21,680 --> 03:56:24,399 need to pull in express again and this 6035 03:56:24,399 --> 03:56:27,279 will be equal to require 6036 03:56:27,279 --> 03:56:28,640 express 6037 03:56:28,640 --> 03:56:31,600 and then we need to define our router 6038 03:56:31,600 --> 03:56:34,080 so we'll set the router equal to 6039 03:56:34,080 --> 03:56:36,800 express.router 6040 03:56:36,800 --> 03:56:38,239 and then we'll need to go ahead and pull 6041 03:56:38,239 --> 03:56:40,239 in the controller so let's call this 6042 03:56:40,239 --> 03:56:41,840 register 6043 03:56:41,840 --> 03:56:43,040 controller 6044 03:56:43,040 --> 03:56:45,840 and we'll set it equal to require 6045 03:56:45,840 --> 03:56:47,680 and then we need 6046 03:56:47,680 --> 03:56:50,800 to come up out of the routes folder and 6047 03:56:50,800 --> 03:56:53,040 then we'll go into the controllers 6048 03:56:53,040 --> 03:56:54,160 folder 6049 03:56:54,160 --> 03:56:56,560 and from there we'll choose the register 6050 03:56:56,560 --> 03:56:58,560 controller 6051 03:56:58,560 --> 03:56:59,920 okay now that we've got that let's 6052 03:56:59,920 --> 03:57:02,479 define our router or our route that 6053 03:57:02,479 --> 03:57:05,760 we're looking for here and it's a post 6054 03:57:05,760 --> 03:57:08,000 and this will come in at the slash just 6055 03:57:08,000 --> 03:57:09,520 the root 6056 03:57:09,520 --> 03:57:11,760 and then we'll pull in our register 6057 03:57:11,760 --> 03:57:14,160 controller dot 6058 03:57:14,160 --> 03:57:16,239 handle new user 6059 03:57:16,239 --> 03:57:18,560 and after that we can use module exports 6060 03:57:18,560 --> 03:57:21,840 on it i can spell module 6061 03:57:21,840 --> 03:57:23,680 we'll set this equal to 6062 03:57:23,680 --> 03:57:26,080 router as we do with our routes and now 6063 03:57:26,080 --> 03:57:28,560 finally in the server.js at the very 6064 03:57:28,560 --> 03:57:31,040 bottom of our file tree we want to go 6065 03:57:31,040 --> 03:57:33,439 ahead and add this route 6066 03:57:33,439 --> 03:57:35,840 with the other routes so i'll just copy 6067 03:57:35,840 --> 03:57:37,600 this down to start off 6068 03:57:37,600 --> 03:57:39,600 we have this slash and this will be app 6069 03:57:39,600 --> 03:57:41,279 use and now we're going to use the 6070 03:57:41,279 --> 03:57:42,880 register route 6071 03:57:42,880 --> 03:57:46,840 and we're going to require routes slash 6072 03:57:46,840 --> 03:57:49,760 register and we can save that i'm going 6073 03:57:49,760 --> 03:57:51,920 to collapse this open editor so we can 6074 03:57:51,920 --> 03:57:53,840 see all the file tree at once and if you 6075 03:57:53,840 --> 03:57:56,720 remember in our package json we have 6076 03:57:56,720 --> 03:57:59,600 nodemon installed as a dev dependency 6077 03:57:59,600 --> 03:58:01,840 and that will help us go ahead and start 6078 03:58:01,840 --> 03:58:03,840 this dev server so i'm going to press 6079 03:58:03,840 --> 03:58:06,239 ctrl backtick once again to launch a new 6080 03:58:06,239 --> 03:58:08,000 terminal window again you can do that 6081 03:58:08,000 --> 03:58:09,840 from the terminal menu as well 6082 03:58:09,840 --> 03:58:12,319 and now i'm going to type npm 6083 03:58:12,319 --> 03:58:15,520 run dev to launch our server and we 6084 03:58:15,520 --> 03:58:17,120 should get a message here saying what 6085 03:58:17,120 --> 03:58:19,279 port it's running on and it looks like 6086 03:58:19,279 --> 03:58:21,279 we've got an error let's see where the 6087 03:58:21,279 --> 03:58:23,920 error is 6088 03:58:24,399 --> 03:58:26,399 cannot find module 6089 03:58:26,399 --> 03:58:29,199 model user json 6090 03:58:29,199 --> 03:58:32,080 oh and that's because i named it users 6091 03:58:32,080 --> 03:58:35,279 json so we didn't import that correctly 6092 03:58:35,279 --> 03:58:38,960 let's go back to our register controller 6093 03:58:38,960 --> 03:58:42,479 and change that to users.json 6094 03:58:42,479 --> 03:58:45,199 and we'll save and now we should see 6095 03:58:45,199 --> 03:58:47,920 nodemon restart and we're running on 6096 03:58:47,920 --> 03:58:50,960 port 3500. okay now we can check our 6097 03:58:50,960 --> 03:58:52,880 route and i'm going to use an extension 6098 03:58:52,880 --> 03:58:55,359 called thunderclient to do that you see 6099 03:58:55,359 --> 03:58:57,359 thunderclient up here in the top left if 6100 03:58:57,359 --> 03:58:59,279 you don't have thunderclient installed 6101 03:58:59,279 --> 03:59:01,040 you can do that or you can use something 6102 03:59:01,040 --> 03:59:02,560 else like the app 6103 03:59:02,560 --> 03:59:04,720 postman would be a good one that is 6104 03:59:04,720 --> 03:59:07,120 normally used to check api routes with 6105 03:59:07,120 --> 03:59:09,199 but you can find thunderclient in vs 6106 03:59:09,199 --> 03:59:11,199 code extensions and i've found it's 6107 03:59:11,199 --> 03:59:14,399 pretty good to check apa api routes with 6108 03:59:14,399 --> 03:59:17,359 in a dev environment so here it is on 6109 03:59:17,359 --> 03:59:19,120 the left i've got the circle with the 6110 03:59:19,120 --> 03:59:21,680 lightning bolt and we can go ahead and 6111 03:59:21,680 --> 03:59:23,760 create a new request 6112 03:59:23,760 --> 03:59:26,000 and you can also save collections and 6113 03:59:26,000 --> 03:59:28,319 i've got a collection here already 6114 03:59:28,319 --> 03:59:30,800 with the request that i want to make so 6115 03:59:30,800 --> 03:59:33,199 i'll look at this first one and you can 6116 03:59:33,199 --> 03:59:33,920 see 6117 03:59:33,920 --> 03:59:36,720 it's a post request and it's going to 6118 03:59:36,720 --> 03:59:39,840 http colon slash 6119 03:59:39,840 --> 03:59:41,600 port 3500 6120 03:59:41,600 --> 03:59:43,520 and this goes to auth i don't want the 6121 03:59:43,520 --> 03:59:45,680 auth we created the register one let me 6122 03:59:45,680 --> 03:59:48,000 find that there's the register one okay 6123 03:59:48,000 --> 03:59:49,600 now if i click on 6124 03:59:49,600 --> 03:59:51,199 thunder client or the file tree or 6125 03:59:51,199 --> 03:59:52,960 anything else it will hide this and i've 6126 03:59:52,960 --> 03:59:54,960 noticed that thunder client has a much 6127 03:59:54,960 --> 03:59:58,160 better appearance when we hide that bar 6128 03:59:58,160 --> 04:00:01,199 over here so now we have the request on 6129 04:00:01,199 --> 04:00:02,960 the left we'll get the response on the 6130 04:00:02,960 --> 04:00:06,640 right and i can still see the node.js 6131 04:00:06,640 --> 04:00:09,760 console down here at the bottom so 6132 04:00:09,760 --> 04:00:12,080 with the register route in here and it's 6133 04:00:12,080 --> 04:00:14,399 a post route i need to check the body 6134 04:00:14,399 --> 04:00:16,880 and make sure i'm sending something i'm 6135 04:00:16,880 --> 04:00:19,760 going to send user walt 3 i believe i'll 6136 04:00:19,760 --> 04:00:22,640 just create walt 1 this time around 6137 04:00:22,640 --> 04:00:24,560 and here's a password 6138 04:00:24,560 --> 04:00:27,279 and of course it's unencrypted right now 6139 04:00:27,279 --> 04:00:30,080 but we'll see what we get in the node.js 6140 04:00:30,080 --> 04:00:32,239 console because remember we're going to 6141 04:00:32,239 --> 04:00:35,439 log to the console the users and that 6142 04:00:35,439 --> 04:00:38,000 should also include the password if i 6143 04:00:38,000 --> 04:00:39,520 remember correctly and of course we'll 6144 04:00:39,520 --> 04:00:41,120 get the response over here that the 6145 04:00:41,120 --> 04:00:42,560 client would get 6146 04:00:42,560 --> 04:00:45,199 so i'll click send 6147 04:00:45,199 --> 04:00:48,160 and on the right we've got success new 6148 04:00:48,160 --> 04:00:51,040 user walt one created now here at the 6149 04:00:51,040 --> 04:00:53,040 bottom it's got just a little more 6150 04:00:53,040 --> 04:00:54,800 information so we need to pull this up 6151 04:00:54,800 --> 04:00:56,479 so we can see more 6152 04:00:56,479 --> 04:00:58,080 and there you see 6153 04:00:58,080 --> 04:01:02,560 our user's json and it created wall one 6154 04:01:02,560 --> 04:01:04,880 and here is the encrypted password that 6155 04:01:04,880 --> 04:01:08,239 is both hashed and salted that bcrypt 6156 04:01:08,239 --> 04:01:10,239 helped us create from the password that 6157 04:01:10,239 --> 04:01:11,600 we passed in 6158 04:01:11,600 --> 04:01:13,600 so now let's go ahead and pull this down 6159 04:01:13,600 --> 04:01:15,840 and let's create one more user let's 6160 04:01:15,840 --> 04:01:17,040 just create 6161 04:01:17,040 --> 04:01:18,560 walt 2 6162 04:01:18,560 --> 04:01:21,359 and we'll send this 6163 04:01:21,359 --> 04:01:24,399 success new user walt 2 created 6164 04:01:24,399 --> 04:01:26,560 and now let's once again look at the 6165 04:01:26,560 --> 04:01:29,040 node console and you can see now both 6166 04:01:29,040 --> 04:01:32,399 users were logged to the console here in 6167 04:01:32,399 --> 04:01:35,199 node and they have different passwords 6168 04:01:35,199 --> 04:01:37,120 as well that are encrypted 6169 04:01:37,120 --> 04:01:38,720 and of course we could break all that 6170 04:01:38,720 --> 04:01:41,120 down but i'll let you look at the link 6171 04:01:41,120 --> 04:01:43,359 to be crypt and i've got another good 6172 04:01:43,359 --> 04:01:44,960 article on that as well i'll link to 6173 04:01:44,960 --> 04:01:46,640 both of those in the description below 6174 04:01:46,640 --> 04:01:48,960 so make sure you do that okay we have 6175 04:01:48,960 --> 04:01:52,319 tested and handled the registration but 6176 04:01:52,319 --> 04:01:54,800 of course now we need to go ahead and 6177 04:01:54,800 --> 04:01:57,439 handle the authorization as well i'm 6178 04:01:57,439 --> 04:01:59,680 going to close the terminal again 6179 04:01:59,680 --> 04:02:02,080 and i'm going to copy the import of 6180 04:02:02,080 --> 04:02:05,279 usersdb with users in set users 6181 04:02:05,279 --> 04:02:07,520 and i'll go ahead and create an auth 6182 04:02:07,520 --> 04:02:09,600 controller now in the controllers 6183 04:02:09,600 --> 04:02:11,120 directory so we'll call this 6184 04:02:11,120 --> 04:02:14,120 authcontroller.js 6185 04:02:14,720 --> 04:02:17,120 and now the first thing i'll do is paste 6186 04:02:17,120 --> 04:02:20,080 in the user's db where we import in our 6187 04:02:20,080 --> 04:02:21,120 users 6188 04:02:21,120 --> 04:02:23,600 json that simulates our users database 6189 04:02:23,600 --> 04:02:25,439 table and we're also going to need 6190 04:02:25,439 --> 04:02:28,000 bcrypt again so let's set this equal to 6191 04:02:28,000 --> 04:02:30,000 require and 6192 04:02:30,000 --> 04:02:32,640 be crypt 6193 04:02:32,640 --> 04:02:34,479 and now we can begin constructing our 6194 04:02:34,479 --> 04:02:37,120 function let's call this handle 6195 04:02:37,120 --> 04:02:38,319 login 6196 04:02:38,319 --> 04:02:40,239 and let's set it equal to an async 6197 04:02:40,239 --> 04:02:42,080 function that has a request and a 6198 04:02:42,080 --> 04:02:44,640 response 6199 04:02:45,040 --> 04:02:47,040 now inside the function will start out 6200 04:02:47,040 --> 04:02:49,279 just like we did inside the register 6201 04:02:49,279 --> 04:02:52,399 controller where we expect to get a user 6202 04:02:52,399 --> 04:02:55,600 and a password parameter both sent to us 6203 04:02:55,600 --> 04:02:58,000 and if we don't we'll send back that 6204 04:02:58,000 --> 04:03:02,239 400 http status code that says it is a 6205 04:03:02,239 --> 04:03:04,880 bad request so we can paste those right 6206 04:03:04,880 --> 04:03:07,439 in i'll press alt z again so the code 6207 04:03:07,439 --> 04:03:08,960 wraps 6208 04:03:08,960 --> 04:03:11,040 so they start out the same way but after 6209 04:03:11,040 --> 04:03:12,239 that there are definitely some 6210 04:03:12,239 --> 04:03:14,800 differences and what we need to do now 6211 04:03:14,800 --> 04:03:17,120 is try to find the user that's sent in 6212 04:03:17,120 --> 04:03:19,359 before we even concern ourselves with 6213 04:03:19,359 --> 04:03:20,720 the password let's just see if the 6214 04:03:20,720 --> 04:03:22,840 username exists so i'm going to call 6215 04:03:22,840 --> 04:03:25,680 this found user 6216 04:03:25,680 --> 04:03:30,279 and set it equal to usersdb.users.find 6217 04:03:32,160 --> 04:03:34,080 and then we'll look at each person in 6218 04:03:34,080 --> 04:03:35,840 the users table and we'll say 6219 04:03:35,840 --> 04:03:38,840 person.username 6220 04:03:38,960 --> 04:03:40,000 equal 6221 04:03:40,000 --> 04:03:43,040 to user and find works this way until it 6222 04:03:43,040 --> 04:03:45,840 actually finds somebody and then if it 6223 04:03:45,840 --> 04:03:49,120 finds somebody it will return that value 6224 04:03:49,120 --> 04:03:52,399 and if not of course it will be false or 6225 04:03:52,399 --> 04:03:54,399 undefined at that point so 6226 04:03:54,399 --> 04:03:56,640 now we could log this and take a look 6227 04:03:56,640 --> 04:03:58,720 but let's just say if 6228 04:03:58,720 --> 04:04:00,640 we do not 6229 04:04:00,640 --> 04:04:03,520 find a user if we do not have a found 6230 04:04:03,520 --> 04:04:04,560 user 6231 04:04:04,560 --> 04:04:07,040 we'll go ahead and return the response 6232 04:04:07,040 --> 04:04:08,319 dot 6233 04:04:08,319 --> 04:04:10,080 send status 6234 04:04:10,080 --> 04:04:11,920 and we're going to send a 401 which 6235 04:04:11,920 --> 04:04:14,800 means unauthorized 6236 04:04:14,800 --> 04:04:17,840 and i could put a link to mdn that lists 6237 04:04:17,840 --> 04:04:20,560 all of the http status codes in the 6238 04:04:20,560 --> 04:04:22,319 description below as well i think i will 6239 04:04:22,319 --> 04:04:24,640 do that so there if we do not find the 6240 04:04:24,640 --> 04:04:26,880 user we know hey that's it there's no 6241 04:04:26,880 --> 04:04:28,880 need to look any further this is 6242 04:04:28,880 --> 04:04:31,279 unauthorized and so it won't work 6243 04:04:31,279 --> 04:04:33,520 but now if we do find a user we need to 6244 04:04:33,520 --> 04:04:36,399 go ahead and let me put a note here just 6245 04:04:36,399 --> 04:04:37,600 say 6246 04:04:37,600 --> 04:04:40,600 evaluate 6247 04:04:40,880 --> 04:04:42,479 now if i could spell evaluate there we 6248 04:04:42,479 --> 04:04:44,479 go evaluate password 6249 04:04:44,479 --> 04:04:47,120 and we'll use b to do that so let's 6250 04:04:47,120 --> 04:04:49,120 say match 6251 04:04:49,120 --> 04:04:50,800 is what we're going to define and we can 6252 04:04:50,800 --> 04:04:52,880 just say await 6253 04:04:52,880 --> 04:04:54,720 bcrypt dot 6254 04:04:54,720 --> 04:04:56,000 compare 6255 04:04:56,000 --> 04:04:58,000 let's pass in the password that we 6256 04:04:58,000 --> 04:05:00,399 receive with the request and we're going 6257 04:05:00,399 --> 04:05:03,359 to compare it to the user that we've 6258 04:05:03,359 --> 04:05:04,479 found 6259 04:05:04,479 --> 04:05:07,199 and the password that exists for that 6260 04:05:07,199 --> 04:05:10,960 user and now if we do have a match 6261 04:05:10,960 --> 04:05:13,840 if the passwords match we can reply 6262 04:05:13,840 --> 04:05:16,479 with the json and once again we could 6263 04:05:16,479 --> 04:05:18,880 send a success message 6264 04:05:18,880 --> 04:05:21,600 and we'll just say oh back ticks again 6265 04:05:21,600 --> 04:05:23,120 and we'll say 6266 04:05:23,120 --> 04:05:26,479 user and pass in the user value 6267 04:05:26,479 --> 04:05:29,760 and from there we'll say is 6268 04:05:29,760 --> 04:05:31,920 logged in 6269 04:05:31,920 --> 04:05:36,080 but we could also have an else there 6270 04:05:36,080 --> 04:05:36,880 and 6271 04:05:36,880 --> 04:05:39,120 if we do not have a match at this point 6272 04:05:39,120 --> 04:05:42,880 then we'll say response send status and 6273 04:05:42,880 --> 04:05:44,960 once again this will be a 401 6274 04:05:44,960 --> 04:05:46,640 unauthorized 6275 04:05:46,640 --> 04:05:48,000 and we can save that and we're 6276 04:05:48,000 --> 04:05:49,760 essentially finished with the function 6277 04:05:49,760 --> 04:05:51,040 except for 6278 04:05:51,040 --> 04:05:53,600 adding the module.exports here once 6279 04:05:53,600 --> 04:05:54,560 again 6280 04:05:54,560 --> 04:05:57,120 and we'll put it in an object again and 6281 04:05:57,120 --> 04:05:59,600 we'll say handle login 6282 04:05:59,600 --> 04:06:01,680 but the other note that i want to add to 6283 04:06:01,680 --> 04:06:03,920 this and we're not doing it in this 6284 04:06:03,920 --> 04:06:06,239 tutorial but we will in the next 6285 04:06:06,239 --> 04:06:09,600 is this is where we would create 6286 04:06:09,600 --> 04:06:12,399 a jwt to send 6287 04:06:12,399 --> 04:06:14,880 to use with the other routes that we 6288 04:06:14,880 --> 04:06:17,359 want protected in our api 6289 04:06:17,359 --> 04:06:19,600 so we'd actually create and send a 6290 04:06:19,600 --> 04:06:23,120 couple of jwts which would be a normal 6291 04:06:23,120 --> 04:06:25,760 token and then the refresh token so 6292 04:06:25,760 --> 04:06:27,479 let's put create 6293 04:06:27,479 --> 04:06:29,680 jwts that would be right here in this 6294 04:06:29,680 --> 04:06:31,439 spot and we'll just leave this note here 6295 04:06:31,439 --> 04:06:33,840 for the next tutorial but now we've 6296 04:06:33,840 --> 04:06:36,080 created our handle login 6297 04:06:36,080 --> 04:06:39,199 in our auth controller so we once again 6298 04:06:39,199 --> 04:06:40,479 need to create 6299 04:06:40,479 --> 04:06:42,479 here we go scrolling the tree we need to 6300 04:06:42,479 --> 04:06:45,120 create a route for auth 6301 04:06:45,120 --> 04:06:49,040 so we'll just put auth.js right here 6302 04:06:49,040 --> 04:06:50,720 and we can copy 6303 04:06:50,720 --> 04:06:52,960 most everything we have in the register 6304 04:06:52,960 --> 04:06:56,239 and just change this over in auth so at 6305 04:06:56,239 --> 04:06:58,720 the top we need express and router just 6306 04:06:58,720 --> 04:07:01,040 the same but instead of register this is 6307 04:07:01,040 --> 04:07:02,800 the auth controller 6308 04:07:02,800 --> 04:07:04,880 we can select all of those with control 6309 04:07:04,880 --> 04:07:07,760 d and change them to auth 6310 04:07:07,760 --> 04:07:09,840 and after that instead of handle new 6311 04:07:09,840 --> 04:07:10,880 user 6312 04:07:10,880 --> 04:07:12,560 it is handle 6313 04:07:12,560 --> 04:07:14,319 login that we want from the auth 6314 04:07:14,319 --> 04:07:17,840 controller and save and our auth 6315 04:07:17,840 --> 04:07:20,640 route is complete now let's go back to 6316 04:07:20,640 --> 04:07:22,720 the server once again 6317 04:07:22,720 --> 04:07:24,960 and let's add the auth route as well so 6318 04:07:24,960 --> 04:07:27,359 let's just copy down i press shift alt 6319 04:07:27,359 --> 04:07:29,600 and the down arrow to copy a line down 6320 04:07:29,600 --> 04:07:31,199 in windows 6321 04:07:31,199 --> 04:07:32,960 and i'm going to put in the auth route 6322 04:07:32,960 --> 04:07:35,680 here i'll press ctrl d to select both 6323 04:07:35,680 --> 04:07:37,600 and just put off 6324 04:07:37,600 --> 04:07:40,080 and save that as well with that complete 6325 04:07:40,080 --> 04:07:42,560 let's go back to thunder client 6326 04:07:42,560 --> 04:07:44,720 and we'll look at our auth collection 6327 04:07:44,720 --> 04:07:45,920 there it is 6328 04:07:45,920 --> 04:07:47,520 and let's see if we can pull up the 6329 04:07:47,520 --> 04:07:49,760 correct route this is the auth route 6330 04:07:49,760 --> 04:07:51,520 that's great so i'll hide the rest and 6331 04:07:51,520 --> 04:07:53,840 we get a better look here i want to see 6332 04:07:53,840 --> 04:07:55,439 that terminal again so i'm going to 6333 04:07:55,439 --> 04:07:58,319 press control and back tick to pull up 6334 04:07:58,319 --> 04:08:00,560 the node terminal and here we are in 6335 04:08:00,560 --> 04:08:04,319 port 3500 so we're sending another post 6336 04:08:04,319 --> 04:08:08,080 this is going to localhost port 3500 and 6337 04:08:08,080 --> 04:08:10,720 the auth route right here so let's look 6338 04:08:10,720 --> 04:08:12,720 at what we're sending in the body 6339 04:08:12,720 --> 04:08:16,000 and we want to log in with walt 6340 04:08:16,000 --> 04:08:17,120 1 6341 04:08:17,120 --> 04:08:19,520 and the password that we had previously 6342 04:08:19,520 --> 04:08:21,120 submitted as well so we'll see the 6343 04:08:21,120 --> 04:08:22,880 client what the client receives here on 6344 04:08:22,880 --> 04:08:23,920 the right 6345 04:08:23,920 --> 04:08:25,920 and we'll see if anything is logged i 6346 04:08:25,920 --> 04:08:27,439 can't remember if we logged anything to 6347 04:08:27,439 --> 04:08:29,439 the console this time around let's go 6348 04:08:29,439 --> 04:08:31,600 ahead and send this 6349 04:08:31,600 --> 04:08:33,680 well we at least log what route we hit 6350 04:08:33,680 --> 04:08:35,279 here in the console but now this says 6351 04:08:35,279 --> 04:08:37,520 user wall 1 is logged in 6352 04:08:37,520 --> 04:08:39,199 let's go ahead and try to log in with 6353 04:08:39,199 --> 04:08:42,399 the user we don't have like walt 3 6354 04:08:42,399 --> 04:08:45,040 and now we get our 401 unauthorized 6355 04:08:45,040 --> 04:08:47,600 which is exactly what we wanted and i'm 6356 04:08:47,600 --> 04:08:49,600 just thinking now earlier 6357 04:08:49,600 --> 04:08:52,239 we didn't send something to register 6358 04:08:52,239 --> 04:08:54,720 that already existed so let's try to 6359 04:08:54,720 --> 04:08:56,319 register a name that we've already 6360 04:08:56,319 --> 04:08:58,560 registered we registered walt 2 last 6361 04:08:58,560 --> 04:09:00,640 time this is going to the register route 6362 04:09:00,640 --> 04:09:03,439 so now let's try to register 6363 04:09:03,439 --> 04:09:06,080 and we got a 409 conflict that's exactly 6364 04:09:06,080 --> 04:09:07,760 what we expected there as well so it 6365 04:09:07,760 --> 04:09:10,960 appears our routes are working correctly 6366 04:09:10,960 --> 04:09:13,359 and everything seems good so this is 6367 04:09:13,359 --> 04:09:14,479 handling 6368 04:09:14,479 --> 04:09:16,399 authorization with username and a 6369 04:09:16,399 --> 04:09:18,960 password on the back end 6370 04:09:18,960 --> 04:09:22,239 and we have simulated a database by 6371 04:09:22,239 --> 04:09:23,319 using 6372 04:09:23,319 --> 04:09:25,520 users.json but in the future we could 6373 04:09:25,520 --> 04:09:27,680 connect this to mongodb 6374 04:09:27,680 --> 04:09:30,000 mysql or some type of database 6375 04:09:30,000 --> 04:09:32,640 technology and you really wouldn't use 6376 04:09:32,640 --> 04:09:34,960 just a json file in production or at 6377 04:09:34,960 --> 04:09:37,760 least i wouldn't recommend it but we 6378 04:09:37,760 --> 04:09:39,840 will come back in the next tutorial and 6379 04:09:39,840 --> 04:09:42,800 learn how to protect routes with json 6380 04:09:42,800 --> 04:09:45,359 web tokens 6381 04:09:45,359 --> 04:09:48,080 today we will add protected routes to 6382 04:09:48,080 --> 04:09:52,000 our express api by using jwts let's 6383 04:09:52,000 --> 04:09:54,800 discuss the jwt strategy we're going to 6384 04:09:54,800 --> 04:09:58,560 implement today so what are jwts what is 6385 04:09:58,560 --> 04:10:02,159 the acronym jwt and abbreviation for 6386 04:10:02,159 --> 04:10:04,399 well i'm very excited about the upcoming 6387 04:10:04,399 --> 04:10:06,800 launch of the james webb telescope but 6388 04:10:06,800 --> 04:10:09,120 in this case that is not the correct 6389 04:10:09,120 --> 04:10:11,920 answer concerning our node and express 6390 04:10:11,920 --> 04:10:15,680 rest api jwt is an abbreviation for json 6391 04:10:15,680 --> 04:10:19,120 web tokens jwts can be considered to be 6392 04:10:19,120 --> 04:10:22,159 a form of user identification that is 6393 04:10:22,159 --> 04:10:24,239 issued after the initial user 6394 04:10:24,239 --> 04:10:26,399 authentication takes place 6395 04:10:26,399 --> 04:10:28,319 when a user completes their login 6396 04:10:28,319 --> 04:10:30,239 process and they are authenticated our 6397 04:10:30,239 --> 04:10:32,319 rest api will issue the client 6398 04:10:32,319 --> 04:10:35,279 application an access token and a 6399 04:10:35,279 --> 04:10:38,159 refresh token the access token is given 6400 04:10:38,159 --> 04:10:40,159 a short time before it expires for 6401 04:10:40,159 --> 04:10:43,199 example 5 to 15 minutes and the refresh 6402 04:10:43,199 --> 04:10:45,520 token is given a longer duration before 6403 04:10:45,520 --> 04:10:48,560 it expires possibly several hours a day 6404 04:10:48,560 --> 04:10:51,279 or even days while no security measures 6405 04:10:51,279 --> 04:10:53,439 are perfect we do want to consider the 6406 04:10:53,439 --> 04:10:56,080 risks of cross-site scripting and 6407 04:10:56,080 --> 04:10:58,640 cross-site request forgery i will 6408 04:10:58,640 --> 04:11:00,319 provide links about both in the 6409 04:11:00,319 --> 04:11:03,199 description below our api will send and 6410 04:11:03,199 --> 04:11:06,000 receive access tokens as json data to 6411 04:11:06,000 --> 04:11:08,560 avoid the previously mentioned risks it 6412 04:11:08,560 --> 04:11:10,239 is recommended for front-end client 6413 04:11:10,239 --> 04:11:12,720 applications to only store access tokens 6414 04:11:12,720 --> 04:11:15,120 in memory so they will be automatically 6415 04:11:15,120 --> 04:11:17,520 lost when the app is closed they should 6416 04:11:17,520 --> 04:11:19,680 not be stored in local storage or in a 6417 04:11:19,680 --> 04:11:22,080 cookie essentially if you can store it 6418 04:11:22,080 --> 04:11:24,640 somewhere with javascript a hacker can 6419 04:11:24,640 --> 04:11:27,120 also retrieve it with javascript just 6420 04:11:27,120 --> 04:11:29,359 keep access tokens in memory which you 6421 04:11:29,359 --> 04:11:31,279 might also refer to as the current 6422 04:11:31,279 --> 04:11:33,840 application state our api will issue 6423 04:11:33,840 --> 04:11:37,120 refresh tokens in an http only cookie 6424 04:11:37,120 --> 04:11:39,040 this type of cookie is not accessible 6425 04:11:39,040 --> 04:11:41,840 with javascript refresh tokens do need 6426 04:11:41,840 --> 04:11:44,000 to have an expiration which will then 6427 04:11:44,000 --> 04:11:46,800 require users to log in again refresh 6428 04:11:46,800 --> 04:11:48,720 tokens should not have the ability to 6429 04:11:48,720 --> 04:11:51,040 issue new refresh tokens because that 6430 04:11:51,040 --> 04:11:54,239 essentially grants indefinite access if 6431 04:11:54,239 --> 04:11:56,159 a refresh token falls into the wrong 6432 04:11:56,159 --> 04:11:58,880 hands so the overall access token 6433 04:11:58,880 --> 04:12:01,520 process involves issuing an access token 6434 04:12:01,520 --> 04:12:04,159 during user authorization the user's 6435 04:12:04,159 --> 04:12:06,560 application can then access our rest 6436 04:12:06,560 --> 04:12:09,520 api's protected routes with the access 6437 04:12:09,520 --> 04:12:11,600 token until it expires 6438 04:12:11,600 --> 04:12:14,159 our api will verify the access token 6439 04:12:14,159 --> 04:12:16,239 with middleware every time the access 6440 04:12:16,239 --> 04:12:18,399 token is used to make a request 6441 04:12:18,399 --> 04:12:20,720 when the access token does expire the 6442 04:12:20,720 --> 04:12:22,399 user's application will need to send 6443 04:12:22,399 --> 04:12:25,199 their refresh token to our api's refresh 6444 04:12:25,199 --> 04:12:27,840 endpoint to get a new access token of 6445 04:12:27,840 --> 04:12:30,640 course the refresh token is also issued 6446 04:12:30,640 --> 04:12:33,680 during user authorization our rest api's 6447 04:12:33,680 --> 04:12:36,000 refresh endpoint will verify the token 6448 04:12:36,000 --> 04:12:38,159 and cross-reference the refresh token in 6449 04:12:38,159 --> 04:12:40,720 our database too storing a reference to 6450 04:12:40,720 --> 04:12:42,960 the refresh token in the database will 6451 04:12:42,960 --> 04:12:45,040 allow refresh tokens to be terminated 6452 04:12:45,040 --> 04:12:47,760 early if the user decides to log out and 6453 04:12:47,760 --> 04:12:50,159 again refresh tokens need to be allowed 6454 04:12:50,159 --> 04:12:53,520 to expire so indefinite access cannot be 6455 04:12:53,520 --> 04:12:55,279 gained we're going to start with the 6456 04:12:55,279 --> 04:12:57,840 code repository from the last tutorial 6457 04:12:57,840 --> 04:12:59,439 but if you don't have it you should be 6458 04:12:59,439 --> 04:13:01,279 able to easily follow along without it 6459 04:13:01,279 --> 04:13:03,439 or you can download or clone the starter 6460 04:13:03,439 --> 04:13:04,880 source code from the link that i've 6461 04:13:04,880 --> 04:13:07,120 provided in the description below let's 6462 04:13:07,120 --> 04:13:09,600 get started today by installing the node 6463 04:13:09,600 --> 04:13:12,239 packages that we're going to need so i'm 6464 04:13:12,239 --> 04:13:14,479 going to press ctrl and the back tick 6465 04:13:14,479 --> 04:13:16,960 you could also go to the terminal menu 6466 04:13:16,960 --> 04:13:19,199 and open a new terminal window from 6467 04:13:19,199 --> 04:13:22,640 there i'm going to type npm i and then 6468 04:13:22,640 --> 04:13:24,399 we can add all of the packages that we 6469 04:13:24,399 --> 04:13:26,800 need all on one line so the first one is 6470 04:13:26,800 --> 04:13:28,720 dot env 6471 04:13:28,720 --> 04:13:32,239 the next one is json web token 6472 04:13:32,239 --> 04:13:35,120 no spaces there and then the last one is 6473 04:13:35,120 --> 04:13:36,560 cookie 6474 04:13:36,560 --> 04:13:37,840 dash 6475 04:13:37,840 --> 04:13:40,399 parser and we'll press enter and all of 6476 04:13:40,399 --> 04:13:42,640 those packages will install and then 6477 04:13:42,640 --> 04:13:44,880 we'll check our package json just to 6478 04:13:44,880 --> 04:13:46,960 make sure the dependencies have all been 6479 04:13:46,960 --> 04:13:49,359 added and that was fairly quick let's go 6480 04:13:49,359 --> 04:13:52,000 ahead and look at the package.json 6481 04:13:52,000 --> 04:13:53,840 and we can see under our dependencies 6482 04:13:53,840 --> 04:13:56,319 we've got the cookie parser we've got 6483 04:13:56,319 --> 04:13:57,680 dot env 6484 04:13:57,680 --> 04:14:00,319 and we've got the json web token package 6485 04:14:00,319 --> 04:14:02,080 with that complete i'm going to close 6486 04:14:02,080 --> 04:14:04,399 the terminal window and over in the file 6487 04:14:04,399 --> 04:14:06,399 tree i'm going to create a new file at 6488 04:14:06,399 --> 04:14:08,319 the root level and i'm going to name 6489 04:14:08,319 --> 04:14:12,000 this dot env just like that and now we 6490 04:14:12,000 --> 04:14:14,159 can put our environment variables in 6491 04:14:14,159 --> 04:14:16,800 here i'm going to use all caps and type 6492 04:14:16,800 --> 04:14:18,239 access 6493 04:14:18,239 --> 04:14:19,359 underscore 6494 04:14:19,359 --> 04:14:22,640 token underscore secret 6495 04:14:22,640 --> 04:14:24,560 and set that to an equals and then we'll 6496 04:14:24,560 --> 04:14:26,560 get the value in a second and then i'm 6497 04:14:26,560 --> 04:14:28,800 just going to copy that down and change 6498 04:14:28,800 --> 04:14:30,640 the word access 6499 04:14:30,640 --> 04:14:34,000 to refresh because we're going to create 6500 04:14:34,000 --> 04:14:36,880 an access token and a refresh token so 6501 04:14:36,880 --> 04:14:39,520 let's save this much for now open the 6502 04:14:39,520 --> 04:14:42,159 terminal window back up and now we can 6503 04:14:42,159 --> 04:14:45,680 type node in the terminal to go into 6504 04:14:45,680 --> 04:14:47,680 node and now we're running node at the 6505 04:14:47,680 --> 04:14:50,319 command line interface node has a common 6506 04:14:50,319 --> 04:14:52,640 core module called crypto so right here 6507 04:14:52,640 --> 04:14:55,760 at the command line we can type require 6508 04:14:55,760 --> 04:14:57,199 and now we can put 6509 04:14:57,199 --> 04:15:01,359 crypto in here and from there we can add 6510 04:15:01,359 --> 04:15:04,080 random bytes with camelcase with a 6511 04:15:04,080 --> 04:15:05,359 capital b 6512 04:15:05,359 --> 04:15:07,359 the number 64 6513 04:15:07,359 --> 04:15:12,080 and then dot to string and we'll say hex 6514 04:15:12,080 --> 04:15:13,920 and this will give us 6515 04:15:13,920 --> 04:15:14,720 a 6516 04:15:14,720 --> 04:15:18,080 random crypto bytes string that we can 6517 04:15:18,080 --> 04:15:20,479 use as our access token secret so i'll 6518 04:15:20,479 --> 04:15:21,920 press enter 6519 04:15:21,920 --> 04:15:24,479 and now i'm going to copy this and we 6520 04:15:24,479 --> 04:15:26,960 don't need the quotes at all inside of 6521 04:15:26,960 --> 04:15:30,319 our dot enb now ctrl c to copy and i'll 6522 04:15:30,319 --> 04:15:32,399 just paste this in here as our access 6523 04:15:32,399 --> 04:15:35,279 token secret now i can press alt z in 6524 04:15:35,279 --> 04:15:37,840 visual studio code to get that to wrap 6525 04:15:37,840 --> 04:15:39,359 so it doesn't go 6526 04:15:39,359 --> 04:15:42,239 beyond the edge of the screen so that is 6527 04:15:42,239 --> 04:15:44,640 our full crypto line that we just 6528 04:15:44,640 --> 04:15:46,640 created there now let's go ahead and 6529 04:15:46,640 --> 04:15:49,040 create one more i can press the up arrow 6530 04:15:49,040 --> 04:15:50,479 in the command line just to pull that 6531 04:15:50,479 --> 04:15:53,359 command back up press enter and it 6532 04:15:53,359 --> 04:15:55,199 generates another 6533 04:15:55,199 --> 04:15:58,239 random crypto line here that we can 6534 04:15:58,239 --> 04:16:01,439 copy and paste right into our dot env 6535 04:16:01,439 --> 04:16:03,040 file and with that i'm going to press 6536 04:16:03,040 --> 04:16:06,080 control s to save and close the terminal 6537 04:16:06,080 --> 04:16:07,520 actually i'm going to open the terminal 6538 04:16:07,520 --> 04:16:09,040 back up real quick because we didn't 6539 04:16:09,040 --> 04:16:11,840 exit node we press control c and then it 6540 04:16:11,840 --> 04:16:14,560 says press control c again so we press 6541 04:16:14,560 --> 04:16:17,040 it again and now we have exited node at 6542 04:16:17,040 --> 04:16:18,800 the command line now i'll close the 6543 04:16:18,800 --> 04:16:20,720 terminal window one other thing we want 6544 04:16:20,720 --> 04:16:23,439 to do with the dot env file is make sure 6545 04:16:23,439 --> 04:16:26,159 it has been added to our get ignore file 6546 04:16:26,159 --> 04:16:28,319 you do not want to send your environment 6547 04:16:28,319 --> 04:16:31,359 variable values that you're storing in 6548 04:16:31,359 --> 04:16:33,680 the dot env file or you could just say 6549 04:16:33,680 --> 04:16:36,640 you do not want to send your env file to 6550 04:16:36,640 --> 04:16:38,720 github you want to 6551 04:16:38,720 --> 04:16:41,600 keep this in your dev environment and 6552 04:16:41,600 --> 04:16:43,840 then when you host somewhere whichever 6553 04:16:43,840 --> 04:16:46,399 host you have they should have a way to 6554 04:16:46,399 --> 04:16:49,040 put the environment variables into their 6555 04:16:49,040 --> 04:16:51,199 hosting service then you can pull those 6556 04:16:51,199 --> 04:16:53,680 out now we need to go to our controllers 6557 04:16:53,680 --> 04:16:56,640 folder and open up our auth controller 6558 04:16:56,640 --> 04:16:58,479 that we've created in the last tutorial 6559 04:16:58,479 --> 04:17:00,640 and we left a note for ourselves that 6560 04:17:00,640 --> 04:17:04,399 this is where we want to create the jwts 6561 04:17:04,399 --> 04:17:07,680 on line 15. before we get started on 6562 04:17:07,680 --> 04:17:10,080 line 15 let's go ahead and pull in 6563 04:17:10,080 --> 04:17:12,000 everything we will require today so 6564 04:17:12,000 --> 04:17:14,399 let's define jwt 6565 04:17:14,399 --> 04:17:16,720 set that equal to require and then pull 6566 04:17:16,720 --> 04:17:20,159 in our json web token package 6567 04:17:20,159 --> 04:17:22,080 after that we need to go ahead and 6568 04:17:22,080 --> 04:17:23,680 require 6569 04:17:23,680 --> 04:17:24,479 dot 6570 04:17:24,479 --> 04:17:26,000 env 6571 04:17:26,000 --> 04:17:30,319 and then we need dot config after that 6572 04:17:30,319 --> 04:17:33,359 and after that we need our 6573 04:17:33,359 --> 04:17:36,399 fs promises because we have not 6574 04:17:36,399 --> 04:17:38,800 integrated or any other database 6575 04:17:38,800 --> 04:17:41,359 technology yet so we are still working 6576 04:17:41,359 --> 04:17:44,800 with simple files and json values so we 6577 04:17:44,800 --> 04:17:47,199 need this fs promises and we'll set that 6578 04:17:47,199 --> 04:17:48,479 equal to 6579 04:17:48,479 --> 04:17:50,640 the fs module 6580 04:17:50,640 --> 04:17:54,080 and after that we need dot promises 6581 04:17:54,080 --> 04:17:57,680 and then finally we need the path module 6582 04:17:57,680 --> 04:18:01,359 we'll set that equal to path 6583 04:18:02,159 --> 04:18:04,000 now let's scroll down so we have just a 6584 04:18:04,000 --> 04:18:06,080 little more room to work on the screen 6585 04:18:06,080 --> 04:18:08,560 and we're back here and now line 15 6586 04:18:08,560 --> 04:18:10,720 turned into line 20 where we're creating 6587 04:18:10,720 --> 04:18:14,000 our jwts and let's define our access 6588 04:18:14,000 --> 04:18:15,760 token 6589 04:18:15,760 --> 04:18:18,080 we use camelcase with a capital t on 6590 04:18:18,080 --> 04:18:20,640 token and we'll set this equal to jwt 6591 04:18:20,640 --> 04:18:21,439 dot 6592 04:18:21,439 --> 04:18:22,399 sign 6593 04:18:22,399 --> 04:18:24,239 the first thing we need to pass into the 6594 04:18:24,239 --> 04:18:25,920 jwt 6595 04:18:25,920 --> 04:18:28,960 is a payload so what we're going to use 6596 04:18:28,960 --> 04:18:32,080 is our username object you don't want to 6597 04:18:32,080 --> 04:18:35,279 pass in anything like a password or 6598 04:18:35,279 --> 04:18:37,040 anything that would 6599 04:18:37,040 --> 04:18:39,840 otherwise hurt your security because 6600 04:18:39,840 --> 04:18:42,399 this is available to all if they get a 6601 04:18:42,399 --> 04:18:44,880 hold of your token so what we want to 6602 04:18:44,880 --> 04:18:46,960 pass in is just the username i'm going 6603 04:18:46,960 --> 04:18:48,800 to do this on a separate line and so 6604 04:18:48,800 --> 04:18:50,640 we'll create this object 6605 04:18:50,640 --> 04:18:52,880 with username 6606 04:18:52,880 --> 04:18:56,159 and here above we had a found user and 6607 04:18:56,159 --> 04:18:57,760 then we can say 6608 04:18:57,760 --> 04:19:00,880 username now this is coming from up here 6609 04:19:00,880 --> 04:19:03,840 where we found the user that was logging 6610 04:19:03,840 --> 04:19:06,479 in after that the second thing we need 6611 04:19:06,479 --> 04:19:09,359 to create our access token is our secret 6612 04:19:09,359 --> 04:19:11,840 that we defined in the dot env file so 6613 04:19:11,840 --> 04:19:14,479 we access this by process 6614 04:19:14,479 --> 04:19:17,760 dot env dot 6615 04:19:17,760 --> 04:19:19,359 access 6616 04:19:19,359 --> 04:19:20,479 token 6617 04:19:20,479 --> 04:19:21,680 underscore 6618 04:19:21,680 --> 04:19:23,520 secret 6619 04:19:23,520 --> 04:19:26,239 and then finally what we need is an 6620 04:19:26,239 --> 04:19:28,399 options value here and we can say when 6621 04:19:28,399 --> 04:19:30,479 this token expires 6622 04:19:30,479 --> 04:19:33,040 and this is camelcase expires in and 6623 04:19:33,040 --> 04:19:34,960 we're going to make this very short for 6624 04:19:34,960 --> 04:19:37,840 this tutorial that will allow me to show 6625 04:19:37,840 --> 04:19:40,800 the token expiring and see what happens 6626 04:19:40,800 --> 04:19:42,640 but what you might want to do in 6627 04:19:42,640 --> 04:19:45,199 production would be maybe 5 minutes or 6628 04:19:45,199 --> 04:19:48,960 15 minutes some small window of time but 6629 04:19:48,960 --> 04:19:51,520 30 seconds might be too short it is all 6630 04:19:51,520 --> 04:19:53,120 a preference though and with that 6631 04:19:53,120 --> 04:19:56,159 defined let's go ahead and highlight all 6632 04:19:56,159 --> 04:19:58,960 of this and then i'll just press shift 6633 04:19:58,960 --> 04:20:01,600 alt and the down arrow to copy it all 6634 04:20:01,600 --> 04:20:03,600 down and we'll just make a few changes 6635 04:20:03,600 --> 04:20:06,560 to go ahead and create our refresh token 6636 04:20:06,560 --> 04:20:08,560 as well so i'm going to type 6637 04:20:08,560 --> 04:20:12,080 refresh here instead of access we're 6638 04:20:12,080 --> 04:20:14,560 once again going to pass in the username 6639 04:20:14,560 --> 04:20:17,040 but then we need the refresh token 6640 04:20:17,040 --> 04:20:18,640 secret 6641 04:20:18,640 --> 04:20:21,199 now a refresh token needs to last much 6642 04:20:21,199 --> 04:20:23,840 longer than the access token but you do 6643 04:20:23,840 --> 04:20:26,640 want it to expire at some point and so 6644 04:20:26,640 --> 04:20:28,239 i'm going to make this 6645 04:20:28,239 --> 04:20:30,960 one day and i'm going to give a link to 6646 04:20:30,960 --> 04:20:33,760 the package the json web token package 6647 04:20:33,760 --> 04:20:37,359 on npm js and there you can see 6648 04:20:37,359 --> 04:20:39,920 another link and more documentation on 6649 04:20:39,920 --> 04:20:41,680 the different values you could put in 6650 04:20:41,680 --> 04:20:44,159 here but one day is what we're going for 6651 04:20:44,159 --> 04:20:46,080 and then we want it to expire and we 6652 04:20:46,080 --> 04:20:48,640 want our users to have to log back in 6653 04:20:48,640 --> 04:20:50,800 you could make an indefinite refresh 6654 04:20:50,800 --> 04:20:53,279 token but that means if someone gets a 6655 04:20:53,279 --> 04:20:54,880 hold of it and they're not the correct 6656 04:20:54,880 --> 04:20:57,840 person they will always have access and 6657 04:20:57,840 --> 04:21:00,000 you do not want that with both tokens 6658 04:21:00,000 --> 04:21:02,000 created i'm going to scroll up for a 6659 04:21:02,000 --> 04:21:04,319 little more room again and we want to 6660 04:21:04,319 --> 04:21:06,960 save our refresh token in the database 6661 04:21:06,960 --> 04:21:09,600 which will also allow us to create a 6662 04:21:09,600 --> 04:21:11,439 logout route in the future that will 6663 04:21:11,439 --> 04:21:13,920 allow us to invalidate the refresh token 6664 04:21:13,920 --> 04:21:16,560 when a user logs out so let's start by 6665 04:21:16,560 --> 04:21:18,800 doing that and once again we're just 6666 04:21:18,800 --> 04:21:21,920 working with our json files for now and 6667 04:21:21,920 --> 04:21:23,680 we haven't connected this to an actual 6668 04:21:23,680 --> 04:21:25,600 database that we will do in future 6669 04:21:25,600 --> 04:21:27,600 tutorials but of course that lets us 6670 04:21:27,600 --> 04:21:30,960 modulize these tutorials and then we can 6671 04:21:30,960 --> 04:21:34,800 attack say or go over to postgres 6672 04:21:34,800 --> 04:21:36,880 or anything we want to in separate 6673 04:21:36,880 --> 04:21:40,560 tutorials so for now just the json file 6674 04:21:40,560 --> 04:21:42,880 and i'm going to define other 6675 04:21:42,880 --> 04:21:45,359 users we had a found user 6676 04:21:45,359 --> 04:21:47,520 so now we'll set other users equal to 6677 04:21:47,520 --> 04:21:48,840 the users 6678 04:21:48,840 --> 04:21:51,920 db dot users 6679 04:21:51,920 --> 04:21:53,760 dot filter 6680 04:21:53,760 --> 04:21:56,560 and now for each person that we have 6681 04:21:56,560 --> 04:21:59,520 we're going to say person dot 6682 04:21:59,520 --> 04:22:01,199 username 6683 04:22:01,199 --> 04:22:03,040 not equal to 6684 04:22:03,040 --> 04:22:05,279 and now let's have that not equal to i 6685 04:22:05,279 --> 04:22:07,040 believe it was 6686 04:22:07,040 --> 04:22:09,279 the user that we came in with the 6687 04:22:09,279 --> 04:22:12,319 request right here however we also have 6688 04:22:12,319 --> 04:22:14,880 our found user dot user name and i 6689 04:22:14,880 --> 04:22:16,640 believe i'd like to use that since we 6690 04:22:16,640 --> 04:22:19,120 have found it so let's say found 6691 04:22:19,120 --> 04:22:20,399 user 6692 04:22:20,399 --> 04:22:22,239 dot username 6693 04:22:22,239 --> 04:22:24,080 and now that creates 6694 04:22:24,080 --> 04:22:26,880 an array of the other users that are not 6695 04:22:26,880 --> 04:22:28,960 the username that is logged in and now 6696 04:22:28,960 --> 04:22:32,560 let's create a current user 6697 04:22:32,560 --> 04:22:34,239 we're going to set this current user 6698 04:22:34,239 --> 04:22:38,000 equal to the found user 6699 04:22:38,000 --> 04:22:40,399 and then we're also going to add in the 6700 04:22:40,399 --> 04:22:43,359 refresh token 6701 04:22:44,560 --> 04:22:46,399 that will be saved with this current 6702 04:22:46,399 --> 04:22:48,800 user and now let's set our users once 6703 04:22:48,800 --> 04:22:52,800 again and we can say users db dot 6704 04:22:52,800 --> 04:22:54,720 set users 6705 04:22:54,720 --> 04:22:56,960 and in this new array 6706 04:22:56,960 --> 04:22:59,120 we'll go ahead and 6707 04:22:59,120 --> 04:23:01,680 add the other users 6708 04:23:01,680 --> 04:23:03,840 and then we'll add the current user that 6709 04:23:03,840 --> 04:23:06,159 we just created i really don't know why 6710 04:23:06,159 --> 04:23:08,399 i'm putting spaces there really don't 6711 04:23:08,399 --> 04:23:10,319 usually do that in an array and okay 6712 04:23:10,319 --> 04:23:12,479 let's go ahead and put the semicolon and 6713 04:23:12,479 --> 04:23:14,319 let's write our new users file this 6714 04:23:14,319 --> 04:23:17,279 would be await and then fs 6715 04:23:17,279 --> 04:23:18,720 promises 6716 04:23:18,720 --> 04:23:20,960 dot write file 6717 04:23:20,960 --> 04:23:22,640 i'll go down to a separate line i'm 6718 04:23:22,640 --> 04:23:24,560 going to scroll up once again just to 6719 04:23:24,560 --> 04:23:27,920 put that in the middle say path dot join 6720 04:23:27,920 --> 04:23:29,359 we'll pull in 6721 04:23:29,359 --> 04:23:31,520 the directory name 6722 04:23:31,520 --> 04:23:35,600 then we need to go up one directory 6723 04:23:35,600 --> 04:23:37,359 and then we need to go into the model 6724 04:23:37,359 --> 04:23:38,560 directory 6725 04:23:38,560 --> 04:23:40,080 and then we need to go ahead and name 6726 04:23:40,080 --> 04:23:44,479 this users.json as our users file is 6727 04:23:44,479 --> 04:23:46,720 after that what we want to put in we 6728 04:23:46,720 --> 04:23:49,199 need to json stringify and pass in 6729 04:23:49,199 --> 04:23:50,800 usersdb 6730 04:23:50,800 --> 04:23:51,920 dot 6731 04:23:51,920 --> 04:23:54,319 users which will write our new users 6732 04:23:54,319 --> 04:23:56,239 file and so i should have put a little 6733 04:23:56,239 --> 04:23:59,840 comment here at the top this is 6734 04:24:00,159 --> 04:24:02,239 saving 6735 04:24:02,239 --> 04:24:04,560 refresh 6736 04:24:04,560 --> 04:24:06,399 token with 6737 04:24:06,399 --> 04:24:09,520 current user which again doing this will 6738 04:24:09,520 --> 04:24:11,680 allow us to invalidate that refresh 6739 04:24:11,680 --> 04:24:14,319 token as the current user logs out if 6740 04:24:14,319 --> 04:24:16,560 they log out before their one day has 6741 04:24:16,560 --> 04:24:19,359 expired so as a quick review we have 6742 04:24:19,359 --> 04:24:22,960 created the jwts once we have authorized 6743 04:24:22,960 --> 04:24:25,359 the login which was with bcrypt in the 6744 04:24:25,359 --> 04:24:27,040 previous tutorial 6745 04:24:27,040 --> 04:24:29,199 but now that we authorize the login we 6746 04:24:29,199 --> 04:24:32,000 create the jwts we have a refresh token 6747 04:24:32,000 --> 04:24:33,520 that's been saved 6748 04:24:33,520 --> 04:24:36,080 into the online database and an access 6749 04:24:36,080 --> 04:24:39,439 token and yet we still need to send both 6750 04:24:39,439 --> 04:24:41,120 the refresh token 6751 04:24:41,120 --> 04:24:44,080 and the access token to the user the 6752 04:24:44,080 --> 04:24:47,120 easy part of this is to just remove what 6753 04:24:47,120 --> 04:24:51,120 we were sending in our json and instead 6754 04:24:51,120 --> 04:24:53,199 send the access token we can send that 6755 04:24:53,199 --> 04:24:56,399 as json and on the front end the front 6756 04:24:56,399 --> 04:24:58,560 end developer or as a full stack 6757 04:24:58,560 --> 04:25:00,319 developer if you're developing the back 6758 04:25:00,319 --> 04:25:02,479 end and the front end you really want to 6759 04:25:02,479 --> 04:25:05,520 store this access token in memory 6760 04:25:05,520 --> 04:25:09,279 it's not secure in local storage and any 6761 04:25:09,279 --> 04:25:11,359 cookie that you can access with a 6762 04:25:11,359 --> 04:25:14,399 javascript could also be accessed so 6763 04:25:14,399 --> 04:25:16,319 anything that really that javascript can 6764 04:25:16,319 --> 04:25:18,800 access where you would store it it's not 6765 04:25:18,800 --> 04:25:21,279 really that secure so if you store the 6766 04:25:21,279 --> 04:25:23,439 access token in memory which has a very 6767 04:25:23,439 --> 04:25:25,199 short life span we only gave it 30 6768 04:25:25,199 --> 04:25:26,880 seconds here but like i said maybe you'd 6769 04:25:26,880 --> 04:25:29,520 give it 15 minutes by keeping that in 6770 04:25:29,520 --> 04:25:30,479 memory 6771 04:25:30,479 --> 04:25:31,840 you're not storing it anywhere 6772 04:25:31,840 --> 04:25:35,279 vulnerable after that the refresh token 6773 04:25:35,279 --> 04:25:36,960 well what we're going to do i've 6774 04:25:36,960 --> 04:25:39,040 definitely seen tutorials that also just 6775 04:25:39,040 --> 04:25:41,120 send it along in the json but that kind 6776 04:25:41,120 --> 04:25:43,359 of creates a dilemma for the front end 6777 04:25:43,359 --> 04:25:45,359 developer or as you develop the front 6778 04:25:45,359 --> 04:25:47,439 end because you do need to store the 6779 04:25:47,439 --> 04:25:50,000 refresh token we want to send it as a 6780 04:25:50,000 --> 04:25:52,720 cookie and i know i said a cookie could 6781 04:25:52,720 --> 04:25:55,120 be vulnerable to javascript but if we 6782 04:25:55,120 --> 04:25:59,279 set the cookie as http only it is not 6783 04:25:59,279 --> 04:26:01,439 available to javascript so that's what 6784 04:26:01,439 --> 04:26:05,760 we want to do so let's say res.cookie 6785 04:26:05,760 --> 04:26:07,840 and now here we can name the cookie i'll 6786 04:26:07,840 --> 04:26:10,000 just name it jwt 6787 04:26:10,000 --> 04:26:11,520 from there we want to pass in our 6788 04:26:11,520 --> 04:26:13,600 refresh token 6789 04:26:13,600 --> 04:26:15,840 and after that we want to give options 6790 04:26:15,840 --> 04:26:17,840 and the first option i'll give a space 6791 04:26:17,840 --> 04:26:20,560 here http only 6792 04:26:20,560 --> 04:26:22,479 set that to true 6793 04:26:22,479 --> 04:26:27,439 and after that we want to set a max age 6794 04:26:27,520 --> 04:26:29,439 here we go and now i'm going to set this 6795 04:26:29,439 --> 04:26:32,720 to 24 which would be 24 hours 6796 04:26:32,720 --> 04:26:36,000 60 times 60 times 6797 04:26:36,000 --> 04:26:39,359 1000 and this is in milliseconds here so 6798 04:26:39,359 --> 04:26:41,040 what we're really getting 6799 04:26:41,040 --> 04:26:44,239 is one day because that's what that is 6800 04:26:44,239 --> 04:26:47,040 equal to as we set the max age of the 6801 04:26:47,040 --> 04:26:49,680 cookie and i'll go ahead and cap that 6802 04:26:49,680 --> 04:26:51,600 off here you could set it for longer as 6803 04:26:51,600 --> 04:26:53,840 well now cookie is always sent with 6804 04:26:53,840 --> 04:26:56,239 every request but the nice thing about 6805 04:26:56,239 --> 04:26:59,359 an http only cookie is that it is not 6806 04:26:59,359 --> 04:27:02,239 available to javascript and while it's 6807 04:27:02,239 --> 04:27:04,399 not a hundred percent secure it's much 6808 04:27:04,399 --> 04:27:07,120 more secure than storing your refresh 6809 04:27:07,120 --> 04:27:09,279 token in local storage or in another 6810 04:27:09,279 --> 04:27:12,159 cookie that is available to javascript 6811 04:27:12,159 --> 04:27:14,880 so let's save this and now we know we 6812 04:27:14,880 --> 04:27:18,159 are sending our refresh cookie that is 6813 04:27:18,159 --> 04:27:21,840 http only to the user we're also sending 6814 04:27:21,840 --> 04:27:23,840 the access token 6815 04:27:23,840 --> 04:27:25,600 oh here we're sending our refresh token 6816 04:27:25,600 --> 04:27:27,760 is what i meant to say in a cookie that 6817 04:27:27,760 --> 04:27:29,439 is http only 6818 04:27:29,439 --> 04:27:31,600 we're also sending the access token as 6819 04:27:31,600 --> 04:27:33,840 json that the front end developer can 6820 04:27:33,840 --> 04:27:34,800 grab 6821 04:27:34,800 --> 04:27:37,279 and we're also storing this same refresh 6822 04:27:37,279 --> 04:27:40,000 token in a database here that can be 6823 04:27:40,000 --> 04:27:42,880 cross-referenced when it is sent back to 6824 04:27:42,880 --> 04:27:45,760 create another access token with our 6825 04:27:45,760 --> 04:27:48,080 auth controller complete let's now go 6826 04:27:48,080 --> 04:27:50,640 look at the middleware folder 6827 04:27:50,640 --> 04:27:53,680 and we need to create a new middleware 6828 04:27:53,680 --> 04:27:58,080 file here and let's call this verify jwt 6829 04:27:58,080 --> 04:28:02,000 dot js and we're going to need jwt and 6830 04:28:02,000 --> 04:28:04,640 dot env once again in this file so i'm 6831 04:28:04,640 --> 04:28:06,399 back in the auth controller just long 6832 04:28:06,399 --> 04:28:08,880 enough to copy these two and then go 6833 04:28:08,880 --> 04:28:12,880 back to the verify jwt dot js 6834 04:28:12,880 --> 04:28:15,680 and i'll paste in our required packages 6835 04:28:15,680 --> 04:28:18,159 right at the top 6836 04:28:18,159 --> 04:28:21,040 after that i'm going to create verify 6837 04:28:21,040 --> 04:28:22,720 jwt 6838 04:28:22,720 --> 04:28:24,720 set this equal to a function that has a 6839 04:28:24,720 --> 04:28:26,479 request 6840 04:28:26,479 --> 04:28:31,199 response and next as middleware should 6841 04:28:31,199 --> 04:28:34,479 and now let's define the auth header at 6842 04:28:34,479 --> 04:28:36,239 the very top and this is going to be 6843 04:28:36,239 --> 04:28:38,159 equal to request 6844 04:28:38,159 --> 04:28:40,000 dot headers 6845 04:28:40,000 --> 04:28:42,319 oh and then we need a bracket not a dot 6846 04:28:42,319 --> 04:28:43,600 and then 6847 04:28:43,600 --> 04:28:46,000 authorization 6848 04:28:46,000 --> 04:28:47,680 and of course we need to check to see if 6849 04:28:47,680 --> 04:28:49,680 that is actually received so now we can 6850 04:28:49,680 --> 04:28:51,199 say if 6851 04:28:51,199 --> 04:28:53,439 no auth header 6852 04:28:53,439 --> 04:28:55,680 we're going to send a response which we 6853 04:28:55,680 --> 04:28:56,960 need to return 6854 04:28:56,960 --> 04:28:58,640 and then just send 6855 04:28:58,640 --> 04:29:01,359 the status 401 which stands for 6856 04:29:01,359 --> 04:29:02,880 unauthorized 6857 04:29:02,880 --> 04:29:05,040 after that i'm going to go ahead and put 6858 04:29:05,040 --> 04:29:08,239 a console log statement to just go ahead 6859 04:29:08,239 --> 04:29:10,479 and log the auth header for when we test 6860 04:29:10,479 --> 04:29:11,920 this we'll be able to see that in the 6861 04:29:11,920 --> 04:29:14,560 console you'll see a line that looks 6862 04:29:14,560 --> 04:29:17,439 like this and it will say bearer 6863 04:29:17,439 --> 04:29:19,439 and there will be a space and then there 6864 04:29:19,439 --> 04:29:20,479 will be 6865 04:29:20,479 --> 04:29:23,439 the token after that so we can define 6866 04:29:23,439 --> 04:29:25,040 our token now 6867 04:29:25,040 --> 04:29:27,520 and we can set this equal to 6868 04:29:27,520 --> 04:29:29,920 the auth header and then we can call 6869 04:29:29,920 --> 04:29:31,920 split on that space that i just 6870 04:29:31,920 --> 04:29:33,760 described and we'll put the space in 6871 04:29:33,760 --> 04:29:36,080 here on the split and then we need the 6872 04:29:36,080 --> 04:29:38,000 token so it wouldn't be in the zero 6873 04:29:38,000 --> 04:29:40,720 position it's in the one position after 6874 04:29:40,720 --> 04:29:41,680 that 6875 04:29:41,680 --> 04:29:44,080 and now we can verify the token with the 6876 04:29:44,080 --> 04:29:47,840 jwt dot verify 6877 04:29:47,840 --> 04:29:49,600 and here we'll pass in 6878 04:29:49,600 --> 04:29:51,279 the token first 6879 04:29:51,279 --> 04:29:53,199 after the token 6880 04:29:53,199 --> 04:29:55,840 we'll pull back in our secret and this 6881 04:29:55,840 --> 04:29:58,000 will be the access token secret because 6882 04:29:58,000 --> 04:29:59,600 that's what we'll verify with this 6883 04:29:59,600 --> 04:30:02,159 middleware 6884 04:30:02,800 --> 04:30:04,880 and after the access token secret there 6885 04:30:04,880 --> 04:30:07,120 will be a callback that gets an error 6886 04:30:07,120 --> 04:30:08,800 and then we'll call this you could call 6887 04:30:08,800 --> 04:30:10,479 it token if you want we'll call it 6888 04:30:10,479 --> 04:30:12,960 decoded 6889 04:30:12,960 --> 04:30:14,960 because it will have decoded information 6890 04:30:14,960 --> 04:30:17,040 from the jwt 6891 04:30:17,040 --> 04:30:19,680 and now we'll say if we have an error 6892 04:30:19,680 --> 04:30:22,080 let's go ahead and return 6893 04:30:22,080 --> 04:30:24,880 and send a status once again 6894 04:30:24,880 --> 04:30:27,199 this status will be 403 forbidden 6895 04:30:27,199 --> 04:30:28,560 because at this point we'll know we 6896 04:30:28,560 --> 04:30:30,159 received a token 6897 04:30:30,159 --> 04:30:32,080 but something about it wasn't right in 6898 04:30:32,080 --> 04:30:33,920 other words it may have been tampered 6899 04:30:33,920 --> 04:30:35,199 with and so 6900 04:30:35,199 --> 04:30:39,199 this means we have a invalid token 6901 04:30:39,199 --> 04:30:41,680 so we'll send a 403 instead of a 401 6902 04:30:41,680 --> 04:30:44,080 which means you are forbidden from 6903 04:30:44,080 --> 04:30:45,760 accessing this 6904 04:30:45,760 --> 04:30:48,640 and then we'll set the user 6905 04:30:48,640 --> 04:30:50,479 equal to 6906 04:30:50,479 --> 04:30:53,760 decoded dot username remember we passed 6907 04:30:53,760 --> 04:30:56,720 in the username to the jwt so now it's 6908 04:30:56,720 --> 04:30:59,439 been decoded and we can read that 6909 04:30:59,439 --> 04:31:01,279 and then we'll call 6910 04:31:01,279 --> 04:31:03,920 next from our middleware 6911 04:31:03,920 --> 04:31:06,560 and that pretty much wraps up that whole 6912 04:31:06,560 --> 04:31:08,560 middleware function right there except 6913 04:31:08,560 --> 04:31:10,960 we need to remember to export it so 6914 04:31:10,960 --> 04:31:13,520 we'll say module.exports 6915 04:31:13,520 --> 04:31:15,920 set that equal to verify 6916 04:31:15,920 --> 04:31:19,120 jwt and save with the middleware now 6917 04:31:19,120 --> 04:31:22,080 complete we can add it to a route or 6918 04:31:22,080 --> 04:31:24,399 routes that we want to protect 6919 04:31:24,399 --> 04:31:27,279 so let's open up routes and api and go 6920 04:31:27,279 --> 04:31:30,560 to our employees js file that has all of 6921 04:31:30,560 --> 04:31:32,880 the employee routes in it and we need to 6922 04:31:32,880 --> 04:31:35,040 import that middleware here so we'll say 6923 04:31:35,040 --> 04:31:36,319 const 6924 04:31:36,319 --> 04:31:39,040 verify jwt 6925 04:31:39,040 --> 04:31:40,239 equals 6926 04:31:40,239 --> 04:31:41,520 require 6927 04:31:41,520 --> 04:31:43,600 and now we need to go up out of the api 6928 04:31:43,600 --> 04:31:44,880 folder 6929 04:31:44,880 --> 04:31:46,880 up out of the next folder the routes 6930 04:31:46,880 --> 04:31:49,520 folder and then go to middleware and 6931 04:31:49,520 --> 04:31:51,600 then choose 6932 04:31:51,600 --> 04:31:54,239 verify jwt 6933 04:31:54,239 --> 04:31:56,239 and once we have that we can just put it 6934 04:31:56,239 --> 04:31:58,560 let's say in our git route if we only 6935 04:31:58,560 --> 04:32:01,680 wanted it in the get route 6936 04:32:01,680 --> 04:32:03,040 before 6937 04:32:03,040 --> 04:32:05,600 we call the controller and so it will go 6938 04:32:05,600 --> 04:32:08,159 through the middleware first and then go 6939 04:32:08,159 --> 04:32:10,800 to the employees controller here so 6940 04:32:10,800 --> 04:32:13,840 let's save this and now let's test this 6941 04:32:13,840 --> 04:32:16,159 with thunderclient so over here to the 6942 04:32:16,159 --> 04:32:18,960 left i'll open up thunderclient 6943 04:32:18,960 --> 04:32:21,359 and we're going to need to log in first 6944 04:32:21,359 --> 04:32:23,359 so i'll go to our collections and i have 6945 04:32:23,359 --> 04:32:25,920 an auth collection that i created and i 6946 04:32:25,920 --> 04:32:28,560 want to go to the auth route 6947 04:32:28,560 --> 04:32:30,159 i'll click this and now i'm going to 6948 04:32:30,159 --> 04:32:32,080 click over here again to hide the tree 6949 04:32:32,080 --> 04:32:34,800 so we can see better and i'm also going 6950 04:32:34,800 --> 04:32:36,800 to press control and backtick to open 6951 04:32:36,800 --> 04:32:39,439 our terminal window we haven't started 6952 04:32:39,439 --> 04:32:41,359 the server yet so that's something else 6953 04:32:41,359 --> 04:32:44,159 we need so we need to type npm 6954 04:32:44,159 --> 04:32:49,479 run dev to go ahead and start our server 6955 04:32:49,920 --> 04:32:53,120 and it's running on port 3500 and so you 6956 04:32:53,120 --> 04:32:55,120 can see the route we're going to go to 6957 04:32:55,120 --> 04:32:59,279 is a post route it's localhost 3500 6958 04:32:59,279 --> 04:33:02,000 auth that we had previously set up to 6959 04:33:02,000 --> 04:33:05,438 log in with the password so in the body 6960 04:33:05,438 --> 04:33:09,278 we're sending wall 1 and password 6961 04:33:09,278 --> 04:33:10,958 and the password that we had in there 6962 04:33:10,958 --> 04:33:13,599 previously and this is in the users file 6963 04:33:13,599 --> 04:33:15,919 that i have if you haven't done this you 6964 04:33:15,919 --> 04:33:18,400 need to go ahead and either create this 6965 04:33:18,400 --> 04:33:20,958 in the file or use the register route 6966 04:33:20,958 --> 04:33:24,160 that we previously created and create a 6967 04:33:24,160 --> 04:33:26,561 new user and then this will work but 6968 04:33:26,561 --> 04:33:28,639 we're right now we're going to log in 6969 04:33:28,639 --> 04:33:30,639 with user walt 1 6970 04:33:30,639 --> 04:33:33,438 and this password using this route so 6971 04:33:33,438 --> 04:33:35,919 i'll go ahead and send 6972 04:33:35,919 --> 04:33:38,000 and you can see over here on the right 6973 04:33:38,000 --> 04:33:40,160 the response we get is a 200 and we get 6974 04:33:40,160 --> 04:33:43,039 this access token this access token will 6975 04:33:43,039 --> 04:33:45,199 only be good for 30 seconds though so 6976 04:33:45,199 --> 04:33:47,118 let's see if i'm fast enough to come 6977 04:33:47,118 --> 04:33:49,199 back here to our collections 6978 04:33:49,199 --> 04:33:52,320 open the employees api 6979 04:33:52,320 --> 04:33:55,039 and now let's look at this and i need to 6980 04:33:55,039 --> 04:33:58,320 go ahead and in auth i need to send a 6981 04:33:58,320 --> 04:33:59,599 new 6982 04:33:59,599 --> 04:34:02,400 access token now we can send 6983 04:34:02,400 --> 04:34:04,639 and we got our employees back but this 6984 04:34:04,639 --> 04:34:07,599 will expire and notice down here we also 6985 04:34:07,599 --> 04:34:09,520 got the console log statement i put in 6986 04:34:09,520 --> 04:34:10,879 here's bear 6987 04:34:10,879 --> 04:34:13,438 and then the space and then the token so 6988 04:34:13,438 --> 04:34:15,438 now if i send this again the token 6989 04:34:15,438 --> 04:34:17,359 should have expired 6990 04:34:17,359 --> 04:34:19,680 and now we get the 403 forbidden because 6991 04:34:19,680 --> 04:34:22,400 the access token that we sent was only 6992 04:34:22,400 --> 04:34:24,719 good for 30 seconds now let's look at 6993 04:34:24,719 --> 04:34:27,919 the employees.js file again and i will 6994 04:34:27,919 --> 04:34:29,520 go ahead and click the file tree so we 6995 04:34:29,520 --> 04:34:32,000 can see our files also we're only 6996 04:34:32,000 --> 04:34:35,118 applying this verify jwt middleware to 6997 04:34:35,118 --> 04:34:36,879 the get route right now 6998 04:34:36,879 --> 04:34:39,520 and this is a good way to do this if you 6999 04:34:39,520 --> 04:34:41,840 have select routes that you want to 7000 04:34:41,840 --> 04:34:44,320 protect or verify but not all of them 7001 04:34:44,320 --> 04:34:47,039 however if you know you want to protect 7002 04:34:47,039 --> 04:34:49,520 all of the routes in your api 7003 04:34:49,520 --> 04:34:51,359 there is an easier way to do this so 7004 04:34:51,359 --> 04:34:55,118 let's go ahead and remove this and we'll 7005 04:34:55,118 --> 04:34:57,599 pull this out of here as well 7006 04:34:57,599 --> 04:34:59,599 and i don't think we'll just retype it 7007 04:34:59,599 --> 04:35:01,438 in the server file because that's where 7008 04:35:01,438 --> 04:35:03,199 we're headed next so we've removed the 7009 04:35:03,199 --> 04:35:06,799 verify jwt from the employees js file 7010 04:35:06,799 --> 04:35:10,480 all together let's go to server js our 7011 04:35:10,480 --> 04:35:11,840 main file 7012 04:35:11,840 --> 04:35:13,039 and here 7013 04:35:13,039 --> 04:35:15,919 we can just pull this middleware in 7014 04:35:15,919 --> 04:35:17,759 so i'm going to do it right underneath 7015 04:35:17,759 --> 04:35:19,759 the error handler that we had middleware 7016 04:35:19,759 --> 04:35:21,359 for so we'll say 7017 04:35:21,359 --> 04:35:24,561 verify jwt 7018 04:35:24,561 --> 04:35:27,599 set this equal to require 7019 04:35:27,599 --> 04:35:29,438 and here we want to go into we don't 7020 04:35:29,438 --> 04:35:31,359 need to go up at all actually we just 7021 04:35:31,359 --> 04:35:33,759 want to go into dot slash 7022 04:35:33,759 --> 04:35:35,599 middleware slash 7023 04:35:35,599 --> 04:35:38,480 verify jwt 7024 04:35:38,480 --> 04:35:40,480 and now that we have that we can use 7025 04:35:40,480 --> 04:35:42,080 this just like we've used other 7026 04:35:42,080 --> 04:35:44,958 middleware in our file but we probably 7027 04:35:44,958 --> 04:35:47,599 don't want to use this verified jwt 7028 04:35:47,599 --> 04:35:49,599 middleware for all the routes probably 7029 04:35:49,599 --> 04:35:52,480 not the root route that we have here 7030 04:35:52,480 --> 04:35:54,719 definitely not to register a new user or 7031 04:35:54,719 --> 04:35:56,320 we'd never be able to register in the 7032 04:35:56,320 --> 04:35:58,799 first place and not even to auth because 7033 04:35:58,799 --> 04:36:00,639 they have to visit auth first to get the 7034 04:36:00,639 --> 04:36:02,799 token but we do want to put it before 7035 04:36:02,799 --> 04:36:05,118 the employees route so i'll put an extra 7036 04:36:05,118 --> 04:36:06,561 line there 7037 04:36:06,561 --> 04:36:07,599 and then 7038 04:36:07,599 --> 04:36:09,840 right here we'll say use 7039 04:36:09,840 --> 04:36:11,118 verify 7040 04:36:11,118 --> 04:36:12,639 jwt 7041 04:36:12,639 --> 04:36:14,320 and if you remember 7042 04:36:14,320 --> 04:36:16,639 this works like a waterfall so in this 7043 04:36:16,639 --> 04:36:19,199 order everything after this line will 7044 04:36:19,199 --> 04:36:23,919 use the verified jwt middleware so 7045 04:36:23,919 --> 04:36:26,958 any route we do not want to have 7046 04:36:26,958 --> 04:36:30,000 verified with the jwt we need to have 7047 04:36:30,000 --> 04:36:33,118 above this line right here so let's go 7048 04:36:33,118 --> 04:36:35,840 ahead and save this once again 7049 04:36:35,840 --> 04:36:38,639 and now we can go back 7050 04:36:38,639 --> 04:36:42,080 and let's log in again and get another 7051 04:36:42,080 --> 04:36:44,240 well we close up the api there there we 7052 04:36:44,240 --> 04:36:46,879 go here's our offline let's log in and 7053 04:36:46,879 --> 04:36:51,199 get another 30 second access token 7054 04:36:51,438 --> 04:36:54,079 here we are 7055 04:36:54,240 --> 04:36:57,438 okay now let's go back to the employees 7056 04:36:57,438 --> 04:36:59,278 and once again we can just use the get 7057 04:36:59,278 --> 04:37:02,000 route before it was forbidden because it 7058 04:37:02,000 --> 04:37:04,080 expired but now let's go to the auth tab 7059 04:37:04,080 --> 04:37:05,919 here in thunder client 7060 04:37:05,919 --> 04:37:08,561 select all delete that paste in the new 7061 04:37:08,561 --> 04:37:11,039 token and submit 7062 04:37:11,039 --> 04:37:13,359 and we've got our employees list once 7063 04:37:13,359 --> 04:37:16,799 again but if i continue to submit 7064 04:37:16,799 --> 04:37:19,599 it will expire 7065 04:37:19,599 --> 04:37:21,520 as soon as the 30 seconds is up and 7066 04:37:21,520 --> 04:37:24,080 there is our 403 and just to show that 7067 04:37:24,080 --> 04:37:26,160 this is on all routes now let's go back 7068 04:37:26,160 --> 04:37:29,520 to our employees api to the post route 7069 04:37:29,520 --> 04:37:31,438 for posting an employee 7070 04:37:31,438 --> 04:37:32,799 here in the body 7071 04:37:32,799 --> 04:37:36,080 we've got john doe so that looks like we 7072 04:37:36,080 --> 04:37:38,320 could go ahead and post to the employees 7073 04:37:38,320 --> 04:37:40,400 for john doe let's see if we can send 7074 04:37:40,400 --> 04:37:42,080 this 7075 04:37:42,080 --> 04:37:45,359 and we get unauthorized so our verify 7076 04:37:45,359 --> 04:37:48,480 jwt middleware is working on all routes 7077 04:37:48,480 --> 04:37:50,799 okay let's go back to the file tree we 7078 04:37:50,799 --> 04:37:53,039 can close the terminal once again we can 7079 04:37:53,039 --> 04:37:56,400 close out of thunderclient for now and 7080 04:37:56,400 --> 04:37:58,240 let's close up the middleware what we do 7081 04:37:58,240 --> 04:38:00,400 want is to go back to the controllers 7082 04:38:00,400 --> 04:38:02,000 and we're going to create a new 7083 04:38:02,000 --> 04:38:04,719 controller here called 7084 04:38:04,719 --> 04:38:07,118 refresh 7085 04:38:07,118 --> 04:38:08,840 token 7086 04:38:08,840 --> 04:38:11,039 controller.js because we're going to 7087 04:38:11,039 --> 04:38:13,919 have a refresh token route and before we 7088 04:38:13,919 --> 04:38:15,680 get started on our refresh token 7089 04:38:15,680 --> 04:38:17,118 controller because we're going to need 7090 04:38:17,118 --> 04:38:19,520 it let's go back to the server.js 7091 04:38:19,520 --> 04:38:20,561 quickly 7092 04:38:20,561 --> 04:38:22,879 and at the top of the server.js we 7093 04:38:22,879 --> 04:38:25,599 pulled in the verified jwt 7094 04:38:25,599 --> 04:38:28,480 we also need to go ahead and define 7095 04:38:28,480 --> 04:38:31,039 cookie parser the other package that we 7096 04:38:31,039 --> 04:38:32,879 had required at the beginning of the 7097 04:38:32,879 --> 04:38:34,080 tutorial 7098 04:38:34,080 --> 04:38:36,561 and let's require 7099 04:38:36,561 --> 04:38:39,520 cookie dash parser there we go but we'll 7100 04:38:39,520 --> 04:38:42,719 define it with cookie parser capital p 7101 04:38:42,719 --> 04:38:45,359 camel case right here so i can go ahead 7102 04:38:45,359 --> 04:38:46,879 and copy that 7103 04:38:46,879 --> 04:38:49,480 now much like where we used the 7104 04:38:49,480 --> 04:38:52,320 express.json and express.url 7105 04:38:52,320 --> 04:38:55,359 encoded that we use for form data and of 7106 04:38:55,359 --> 04:38:56,039 course 7107 04:38:56,039 --> 04:38:59,278 express.json is for json data we just 7108 04:38:59,278 --> 04:39:00,958 want to add 7109 04:39:00,958 --> 04:39:02,639 middleware 7110 04:39:02,639 --> 04:39:05,359 or cookies right here so we'll say 7111 04:39:05,359 --> 04:39:07,199 app.use 7112 04:39:07,199 --> 04:39:09,438 and now we'll pass in 7113 04:39:09,438 --> 04:39:11,919 cookie parser and we want to call that 7114 04:39:11,919 --> 04:39:14,320 into action right there 7115 04:39:14,320 --> 04:39:17,039 and save our server.js file now we're 7116 04:39:17,039 --> 04:39:19,118 ready to go right back to that refresh 7117 04:39:19,118 --> 04:39:22,240 token controller and we'll begin by 7118 04:39:22,240 --> 04:39:25,599 copying the auth controller 7119 04:39:25,599 --> 04:39:27,919 control c i'll just copy everything and 7120 04:39:27,919 --> 04:39:29,759 paste it in but we will make some 7121 04:39:29,759 --> 04:39:31,438 modifications 7122 04:39:31,438 --> 04:39:34,080 so we need the user's db we will not 7123 04:39:34,080 --> 04:39:37,360 need bcrypt because we are not doing 7124 04:39:37,360 --> 04:39:40,240 user password authentication there we do 7125 04:39:40,240 --> 04:39:42,400 need jwt 7126 04:39:42,400 --> 04:39:44,160 and dot env 7127 04:39:44,160 --> 04:39:47,120 but we won't need fs promises or the 7128 04:39:47,120 --> 04:39:49,200 path in this either so we can remove 7129 04:39:49,200 --> 04:39:51,680 those dependencies now instead of handle 7130 04:39:51,680 --> 04:39:55,040 login let's call this handle 7131 04:39:55,040 --> 04:39:57,040 refresh 7132 04:39:57,040 --> 04:39:58,240 token 7133 04:39:58,240 --> 04:40:00,718 and this does not need to be async so 7134 04:40:00,718 --> 04:40:02,878 we'll just have the request and response 7135 04:40:02,878 --> 04:40:04,160 here 7136 04:40:04,160 --> 04:40:06,798 after that we won't be looking for a 7137 04:40:06,798 --> 04:40:08,560 password 7138 04:40:08,560 --> 04:40:11,280 but we will be looking for a cookie so 7139 04:40:11,280 --> 04:40:13,360 let's say const 7140 04:40:13,360 --> 04:40:16,080 cookies and set this equal to request 7141 04:40:16,080 --> 04:40:18,798 dot cookies and so we will have a 7142 04:40:18,798 --> 04:40:20,240 similar if statement here at the 7143 04:40:20,240 --> 04:40:21,200 beginning 7144 04:40:21,200 --> 04:40:23,280 but we'll be checking for a couple of 7145 04:40:23,280 --> 04:40:26,240 things one is to make sure that we have 7146 04:40:26,240 --> 04:40:28,560 cookies but now let's use optional 7147 04:40:28,560 --> 04:40:30,958 chaining with a question mark and a dot 7148 04:40:30,958 --> 04:40:33,200 the optional chaining operator and put 7149 04:40:33,200 --> 04:40:36,480 jwt after it so we're checking to see 7150 04:40:36,480 --> 04:40:39,920 if we have cookies and then if we do 7151 04:40:39,920 --> 04:40:41,920 have the cookies we're also checking to 7152 04:40:41,920 --> 04:40:45,760 see if there is a jwt property and this 7153 04:40:45,760 --> 04:40:47,520 is saying of course if these do not 7154 04:40:47,520 --> 04:40:48,480 exist 7155 04:40:48,480 --> 04:40:50,000 then we're going to return something 7156 04:40:50,000 --> 04:40:53,440 here and we're going to return a 401 and 7157 04:40:53,440 --> 04:40:56,160 we do not need a json message after that 7158 04:40:56,160 --> 04:40:58,160 so i'll just eliminate that portion of 7159 04:40:58,160 --> 04:40:59,280 it 7160 04:40:59,280 --> 04:41:01,360 and save this it looks like i may need 7161 04:41:01,360 --> 04:41:04,240 to go ahead and press alt z to wrap any 7162 04:41:04,240 --> 04:41:06,320 lines that exceed 7163 04:41:06,320 --> 04:41:08,160 the edge of the screen so there we've 7164 04:41:08,160 --> 04:41:12,080 got a 401 if the cookies do not exist 7165 04:41:12,080 --> 04:41:13,120 and 7166 04:41:13,120 --> 04:41:15,600 maybe we have cookies but no jwt either 7167 04:41:15,600 --> 04:41:17,520 way that gets a 401 which is 7168 04:41:17,520 --> 04:41:20,160 unauthorized now let's go ahead after 7169 04:41:20,160 --> 04:41:22,320 we've confirmed that at least and let's 7170 04:41:22,320 --> 04:41:24,560 log to the console 7171 04:41:24,560 --> 04:41:26,480 cookies.jw 7172 04:41:26,480 --> 04:41:28,718 so when we run this you'll be able to 7173 04:41:28,718 --> 04:41:31,760 see what values we get from that and 7174 04:41:31,760 --> 04:41:34,400 likewise let's go ahead and define the 7175 04:41:34,400 --> 04:41:36,480 refresh token now that we've received 7176 04:41:36,480 --> 04:41:38,320 now that we know it exists within the 7177 04:41:38,320 --> 04:41:41,360 cookie and we'll set that equal to 7178 04:41:41,360 --> 04:41:43,440 cookies.jwt 7179 04:41:43,440 --> 04:41:46,320 and once again we do want to find a user 7180 04:41:46,320 --> 04:41:49,280 but now we're receiving a refresh token 7181 04:41:49,280 --> 04:41:52,000 and not a username or password for this 7182 04:41:52,000 --> 04:41:54,560 so we'll say person and there is a 7183 04:41:54,560 --> 04:41:56,798 refresh token that should be saved with 7184 04:41:56,798 --> 04:41:59,440 each user if they have created one 7185 04:41:59,440 --> 04:42:01,520 and so here we'll just match that to the 7186 04:42:01,520 --> 04:42:03,680 refresh token 7187 04:42:03,680 --> 04:42:06,400 that we have now defined here so this 7188 04:42:06,400 --> 04:42:08,718 would be our found user then if that 7189 04:42:08,718 --> 04:42:11,520 user exists and once again we can say if 7190 04:42:11,520 --> 04:42:14,638 we do not find a user let's now send the 7191 04:42:14,638 --> 04:42:17,120 status forbidden here and i'll just 7192 04:42:17,120 --> 04:42:19,680 highlight that 7193 04:42:19,680 --> 04:42:22,160 okay we're not evaluating a password 7194 04:42:22,160 --> 04:42:25,760 here we are evaluating a jwt though so 7195 04:42:25,760 --> 04:42:27,680 these lines will change as well so we 7196 04:42:27,680 --> 04:42:30,958 can get rid of the const match line 7197 04:42:30,958 --> 04:42:32,958 and instead well we can even get rid of 7198 04:42:32,958 --> 04:42:36,400 the if match here as well 7199 04:42:36,400 --> 04:42:38,400 and instead of that we'll type 7200 04:42:38,400 --> 04:42:41,200 jwt dot verify 7201 04:42:41,200 --> 04:42:43,040 and now we'll pass in 7202 04:42:43,040 --> 04:42:46,160 the refresh token that was received 7203 04:42:46,160 --> 04:42:49,200 now we'll say process dot env 7204 04:42:49,200 --> 04:42:50,878 dot 7205 04:42:50,878 --> 04:42:54,400 refresh token secret 7206 04:42:54,560 --> 04:42:57,280 and then we'll have 7207 04:42:57,280 --> 04:42:59,120 an anonymous function that once again 7208 04:42:59,120 --> 04:43:01,760 has error and decoded its callback 7209 04:43:01,760 --> 04:43:03,280 function 7210 04:43:03,280 --> 04:43:05,040 and before i type the rest i think i'm 7211 04:43:05,040 --> 04:43:08,160 just going to get rid of this because 7212 04:43:08,160 --> 04:43:09,840 it doesn't need to be there at this 7213 04:43:09,840 --> 04:43:12,798 point either so let's just go ahead and 7214 04:43:12,798 --> 04:43:15,360 clear it out now 7215 04:43:15,360 --> 04:43:17,120 and with that cleared out we can finish 7216 04:43:17,120 --> 04:43:19,120 our function here for the token 7217 04:43:19,120 --> 04:43:21,120 verification and 7218 04:43:21,120 --> 04:43:24,000 if we do not have a valid token we can 7219 04:43:24,000 --> 04:43:26,320 say if we have an error 7220 04:43:26,320 --> 04:43:27,520 or 7221 04:43:27,520 --> 04:43:31,520 we can also check the found user dot 7222 04:43:31,520 --> 04:43:33,360 username because we did have the 7223 04:43:33,360 --> 04:43:35,840 username encoded in the token 7224 04:43:35,840 --> 04:43:39,200 so if it's not equal to the decoded 7225 04:43:39,200 --> 04:43:41,360 dot username in other words maybe it was 7226 04:43:41,360 --> 04:43:43,040 tampered with again 7227 04:43:43,040 --> 04:43:44,840 then we can 7228 04:43:44,840 --> 04:43:49,840 return a response with a send status 7229 04:43:49,840 --> 04:43:52,560 and say 403 once again 7230 04:43:52,560 --> 04:43:54,638 otherwise everything is good and we're 7231 04:43:54,638 --> 04:43:57,040 ready to create a new access token 7232 04:43:57,040 --> 04:43:59,680 descend because the refresh token has 7233 04:43:59,680 --> 04:44:02,080 verified so we'll set this access token 7234 04:44:02,080 --> 04:44:04,878 equal to jwt dot sign 7235 04:44:04,878 --> 04:44:07,360 and then inside of the access token will 7236 04:44:07,360 --> 04:44:11,040 once again have a username 7237 04:44:11,040 --> 04:44:13,520 and this username will be the 7238 04:44:13,520 --> 04:44:15,200 dot username because it should be the 7239 04:44:15,200 --> 04:44:18,480 same username that was verified before 7240 04:44:18,480 --> 04:44:21,040 and after that we can say process dot 7241 04:44:21,040 --> 04:44:22,560 env 7242 04:44:22,560 --> 04:44:23,520 dot 7243 04:44:23,520 --> 04:44:26,320 access token secret 7244 04:44:26,320 --> 04:44:30,080 if we can find that access 7245 04:44:30,080 --> 04:44:32,480 token 7246 04:44:32,480 --> 04:44:34,000 secret there we go 7247 04:44:34,000 --> 04:44:36,000 and now we need to put an expires in 7248 04:44:36,000 --> 04:44:37,120 once again 7249 04:44:37,120 --> 04:44:39,440 let's put expires in 7250 04:44:39,440 --> 04:44:41,760 and let's set this to 30 seconds as well 7251 04:44:41,760 --> 04:44:43,600 however remember you might want to set 7252 04:44:43,600 --> 04:44:45,920 this longer in your production app this 7253 04:44:45,920 --> 04:44:48,000 is just for the demonstration of this 7254 04:44:48,000 --> 04:44:51,120 tutorial so i can show it expire and 7255 04:44:51,120 --> 04:44:53,200 then of course after we've created this 7256 04:44:53,200 --> 04:44:55,920 access token we need to go ahead and 7257 04:44:55,920 --> 04:44:57,760 send the access token 7258 04:44:57,760 --> 04:45:00,080 so we'll send this json down and it'll 7259 04:45:00,080 --> 04:45:02,638 say access 7260 04:45:02,638 --> 04:45:04,160 token 7261 04:45:04,160 --> 04:45:06,560 and once we've created that much all we 7262 04:45:06,560 --> 04:45:08,638 need to do is export it 7263 04:45:08,638 --> 04:45:11,600 so now instead of handle login we once 7264 04:45:11,600 --> 04:45:13,920 again have handle i believe it was 7265 04:45:13,920 --> 04:45:17,200 refresh token yep 7266 04:45:20,638 --> 04:45:22,958 and we should be able to save that 7267 04:45:22,958 --> 04:45:24,718 and i don't know why i put a semicolon 7268 04:45:24,718 --> 04:45:26,560 there it's not really needed so 7269 04:45:26,560 --> 04:45:28,480 once we have saved that we should be 7270 04:45:28,480 --> 04:45:30,160 good to go with our refresh token 7271 04:45:30,160 --> 04:45:32,400 controller and we can create the refresh 7272 04:45:32,400 --> 04:45:36,080 token route okay in our routes file now 7273 04:45:36,080 --> 04:45:39,040 we can open that up and we can look at 7274 04:45:39,040 --> 04:45:41,840 our auth route i'll just highlight all 7275 04:45:41,840 --> 04:45:44,638 and copy and we'll create a new route 7276 04:45:44,638 --> 04:45:46,878 called refresh 7277 04:45:46,878 --> 04:45:48,718 dot js 7278 04:45:48,718 --> 04:45:51,120 i'll go ahead and paste that in but 7279 04:45:51,120 --> 04:45:54,000 instead of the auth controller we need 7280 04:45:54,000 --> 04:45:57,040 the refresh token controller so we'll 7281 04:45:57,040 --> 04:45:59,520 spell all of that out 7282 04:45:59,520 --> 04:46:02,560 and instead of handle login we need 7283 04:46:02,560 --> 04:46:03,920 handle 7284 04:46:03,920 --> 04:46:06,320 refresh token and before we're finished 7285 04:46:06,320 --> 04:46:08,958 with our refresh route let's change the 7286 04:46:08,958 --> 04:46:11,920 http method from post 7287 04:46:11,920 --> 04:46:14,798 to git as well so this is now a get 7288 04:46:14,798 --> 04:46:18,718 route for refresh okay now we need to 7289 04:46:18,718 --> 04:46:21,040 insert this route into the server along 7290 04:46:21,040 --> 04:46:23,040 with the other routes so we'll go to the 7291 04:46:23,040 --> 04:46:26,400 server.js file and you can see we've got 7292 04:46:26,400 --> 04:46:29,120 root register auth and of course we want 7293 04:46:29,120 --> 04:46:32,798 this to also be before we verify the jwt 7294 04:46:32,798 --> 04:46:34,718 because the refresh route actually 7295 04:46:34,718 --> 04:46:38,160 issues a new access token and that's 7296 04:46:38,160 --> 04:46:41,600 what verified jwt verifies so what we 7297 04:46:41,600 --> 04:46:43,120 want to do now 7298 04:46:43,120 --> 04:46:46,000 is just copy a line down i pressed shift 7299 04:46:46,000 --> 04:46:48,320 alt and the down arrow in windows to do 7300 04:46:48,320 --> 04:46:51,200 that and i'm going to change off 7301 04:46:51,200 --> 04:46:54,160 to refresh 7302 04:46:54,240 --> 04:46:56,958 and now the refresh endpoint will 7303 04:46:56,958 --> 04:46:59,520 receive the cookie that has the refresh 7304 04:46:59,520 --> 04:47:02,798 token and then that will issue a new 7305 04:47:02,798 --> 04:47:05,360 access token once the access token has 7306 04:47:05,360 --> 04:47:08,798 expired so we'll save this and we should 7307 04:47:08,798 --> 04:47:11,040 be ready to try it out so i need to open 7308 04:47:11,040 --> 04:47:13,280 up the terminal window again and we can 7309 04:47:13,280 --> 04:47:16,878 see our server is running on port 3500 7310 04:47:16,878 --> 04:47:19,520 now let's open up thunder client 7311 04:47:19,520 --> 04:47:22,000 and we're ready to check out the refresh 7312 04:47:22,000 --> 04:47:23,520 route but first we need to go to the 7313 04:47:23,520 --> 04:47:24,798 auth route 7314 04:47:24,798 --> 04:47:28,080 and here it looks like the off path 7315 04:47:28,080 --> 04:47:29,840 i'll hide the tree over there and so now 7316 04:47:29,840 --> 04:47:32,958 we're going to localhost 3500 auth we're 7317 04:47:32,958 --> 04:47:35,040 once again going to authorize 7318 04:47:35,040 --> 04:47:37,760 walt here with his password so let's 7319 04:47:37,760 --> 04:47:39,440 send this 7320 04:47:39,440 --> 04:47:41,600 and we got a new access token but what 7321 04:47:41,600 --> 04:47:44,958 we also get from our auth route is a 7322 04:47:44,958 --> 04:47:47,440 cookie and this cookie has a refresh 7323 04:47:47,440 --> 04:47:49,920 token and here's the jwt name for the 7324 04:47:49,920 --> 04:47:52,400 cookie and here's the refresh token now 7325 04:47:52,400 --> 04:47:54,718 a cookie is sent every time we don't 7326 04:47:54,718 --> 04:47:57,840 need to paste it in in an auth area like 7327 04:47:57,840 --> 04:48:00,560 we did with our access token 7328 04:48:00,560 --> 04:48:03,440 the jwt cookie is sent every time we 7329 04:48:03,440 --> 04:48:05,760 make a request to the domain that it's 7330 04:48:05,760 --> 04:48:08,080 associated with so we'll go back over 7331 04:48:08,080 --> 04:48:10,080 here to thunder client we'll come down 7332 04:48:10,080 --> 04:48:12,560 to our refresh route and i'll hide that 7333 04:48:12,560 --> 04:48:15,920 tree now we're going to localhost 3500 7334 04:48:15,920 --> 04:48:17,760 refresh and you can see we don't need to 7335 04:48:17,760 --> 04:48:20,320 post anything in the body we don't need 7336 04:48:20,320 --> 04:48:22,638 to post anything in the auth because we 7337 04:48:22,638 --> 04:48:24,400 have the cookie that we're going to send 7338 04:48:24,400 --> 04:48:26,638 so let's send this cookie 7339 04:48:26,638 --> 04:48:28,638 and now we got a new access token and 7340 04:48:28,638 --> 04:48:30,798 you can see we also logged our 7341 04:48:30,798 --> 04:48:33,200 information as we set it up in our path 7342 04:48:33,200 --> 04:48:34,320 to log 7343 04:48:34,320 --> 04:48:36,160 the refresh token that we received from 7344 04:48:36,160 --> 04:48:38,240 the cookie down here so 7345 04:48:38,240 --> 04:48:39,360 we've got 7346 04:48:39,360 --> 04:48:42,000 a new access token and our refresh token 7347 04:48:42,000 --> 04:48:44,320 is used to do that so now every time we 7348 04:48:44,320 --> 04:48:46,400 hit this refresh route and we send our 7349 04:48:46,400 --> 04:48:48,638 cookie with the refresh token we should 7350 04:48:48,638 --> 04:48:50,958 get a new access token and you can see 7351 04:48:50,958 --> 04:48:52,560 every time we're getting a different 7352 04:48:52,560 --> 04:48:56,080 access token over there so that is what 7353 04:48:56,080 --> 04:48:58,480 refreshes our access and that's why it's 7354 04:48:58,480 --> 04:49:00,718 called a refresh token and so now we 7355 04:49:00,718 --> 04:49:02,000 have a new 7356 04:49:02,000 --> 04:49:04,718 limited time access token to use again 7357 04:49:04,718 --> 04:49:06,240 in production you might want to set it 7358 04:49:06,240 --> 04:49:08,638 to 5 minutes or 15 minutes instead of 30 7359 04:49:08,638 --> 04:49:10,480 seconds or something like that 7360 04:49:10,480 --> 04:49:12,240 but then our refresh token has the 7361 04:49:12,240 --> 04:49:15,600 longer duration that we store in an http 7362 04:49:15,600 --> 04:49:18,718 only cookie and that's because we don't 7363 04:49:18,718 --> 04:49:21,120 want it to be available via javascript 7364 04:49:21,120 --> 04:49:23,440 so it increases our security nothing's 7365 04:49:23,440 --> 04:49:25,680 100 perfect but it's the best way to go 7366 04:49:25,680 --> 04:49:27,760 i believe for that so we're sending the 7367 04:49:27,760 --> 04:49:30,240 http only cookie that has the refresh 7368 04:49:30,240 --> 04:49:32,878 token and that issues us the new access 7369 04:49:32,878 --> 04:49:34,878 token now that we've finished testing 7370 04:49:34,878 --> 04:49:37,760 the refresh route one extra measure of 7371 04:49:37,760 --> 04:49:40,400 security or one extra step we can take 7372 04:49:40,400 --> 04:49:43,200 is to offer a log out route and with the 7373 04:49:43,200 --> 04:49:45,600 log out route we could actually then 7374 04:49:45,600 --> 04:49:48,798 delete the refresh token and not let it 7375 04:49:48,798 --> 04:49:51,520 last for the full duration and that just 7376 04:49:51,520 --> 04:49:55,520 gives our users the opportunity to log 7377 04:49:55,520 --> 04:49:58,160 out and of course delete any existing 7378 04:49:58,160 --> 04:50:00,240 tokens and of course the access token 7379 04:50:00,240 --> 04:50:02,160 should also be erased on the front end 7380 04:50:02,160 --> 04:50:04,480 when the login or log out 7381 04:50:04,480 --> 04:50:06,718 link or button is clicked as well of 7382 04:50:06,718 --> 04:50:08,400 course we're working on the back end so 7383 04:50:08,400 --> 04:50:10,560 we won't be doing that today but let's 7384 04:50:10,560 --> 04:50:13,600 go ahead and add this new controller for 7385 04:50:13,600 --> 04:50:17,840 the logout so we'll have logout 7386 04:50:18,200 --> 04:50:19,840 controller.js 7387 04:50:19,840 --> 04:50:21,840 and now that we have that let's go ahead 7388 04:50:21,840 --> 04:50:25,040 and copy our refresh token controller 7389 04:50:25,040 --> 04:50:27,440 and we will just make some modifications 7390 04:50:27,440 --> 04:50:30,560 to handle the logout because we need the 7391 04:50:30,560 --> 04:50:32,878 same requirements at the top we've 7392 04:50:32,878 --> 04:50:34,958 pulling in the users so we can check our 7393 04:50:34,958 --> 04:50:38,000 database we've got the jwt requirement 7394 04:50:38,000 --> 04:50:40,798 and we've also got dot env i'm going to 7395 04:50:40,798 --> 04:50:42,718 close the terminal window 7396 04:50:42,718 --> 04:50:44,560 and now i said we had the same 7397 04:50:44,560 --> 04:50:46,080 requirements we don't quite have the 7398 04:50:46,080 --> 04:50:47,840 same requirements i'm sorry we don't 7399 04:50:47,840 --> 04:50:50,878 need the jwt or the dot emv 7400 04:50:50,878 --> 04:50:52,480 what we do need 7401 04:50:52,480 --> 04:50:55,360 is the fs promises that goes with 7402 04:50:55,360 --> 04:50:57,200 accessing our database because we're 7403 04:50:57,200 --> 04:50:59,920 just using a json file so we'll require 7404 04:50:59,920 --> 04:51:01,280 fs 7405 04:51:01,280 --> 04:51:02,160 dot 7406 04:51:02,160 --> 04:51:06,160 promises and we once again need path so 7407 04:51:06,160 --> 04:51:08,560 we'll require 7408 04:51:08,560 --> 04:51:09,760 the path 7409 04:51:09,760 --> 04:51:12,480 and again these are only to access the 7410 04:51:12,480 --> 04:51:15,040 current json file we're using for our 7411 04:51:15,040 --> 04:51:17,600 users database this is a mock database 7412 04:51:17,600 --> 04:51:19,760 this will be replaced of course 7413 04:51:19,760 --> 04:51:22,400 with or postgres or whichever 7414 04:51:22,400 --> 04:51:24,958 database technology we work on in a 7415 04:51:24,958 --> 04:51:26,878 tutorial in the future 7416 04:51:26,878 --> 04:51:28,798 so we've got this 7417 04:51:28,798 --> 04:51:31,040 down for the requirements and let's move 7418 04:51:31,040 --> 04:51:33,280 forward with the log out so instead of 7419 04:51:33,280 --> 04:51:36,560 handle refresh token now we're going to 7420 04:51:36,560 --> 04:51:38,320 handle the logout 7421 04:51:38,320 --> 04:51:40,240 and we'll have a request and response 7422 04:51:40,240 --> 04:51:42,798 and then let's just add a note here for 7423 04:51:42,798 --> 04:51:44,798 the front end if you're doing full stack 7424 04:51:44,798 --> 04:51:47,040 development like on 7425 04:51:47,040 --> 04:51:48,560 client 7426 04:51:48,560 --> 04:51:50,000 also 7427 04:51:50,000 --> 04:51:51,280 delete 7428 04:51:51,280 --> 04:51:53,040 the access 7429 04:51:53,040 --> 04:51:54,240 token 7430 04:51:54,240 --> 04:51:56,080 we can't do that here on the back end 7431 04:51:56,080 --> 04:51:57,600 you need to do that 7432 04:51:57,600 --> 04:52:00,400 in the memory of the client application 7433 04:52:00,400 --> 04:52:01,200 just 7434 04:52:01,200 --> 04:52:03,360 zero it out or set it to blank or 7435 04:52:03,360 --> 04:52:05,600 whatever when that log out button is 7436 04:52:05,600 --> 04:52:08,400 clicked what we can do though is take 7437 04:52:08,400 --> 04:52:11,040 care of the refresh token so here once 7438 04:52:11,040 --> 04:52:13,200 again we need to get the cookies as they 7439 04:52:13,200 --> 04:52:15,680 come in now let's erase our logs here we 7440 04:52:15,680 --> 04:52:17,280 were doing for the cookies and the 7441 04:52:17,280 --> 04:52:19,760 cookies jwt before if you wanted to see 7442 04:52:19,760 --> 04:52:21,440 those in the console 7443 04:52:21,440 --> 04:52:24,638 and we'll once again verify that we have 7444 04:52:24,638 --> 04:52:27,120 got cookies and then that we also have a 7445 04:52:27,120 --> 04:52:30,878 jwt in the cookies and if we don't 7446 04:52:30,878 --> 04:52:32,718 well that's okay because we were just 7447 04:52:32,718 --> 04:52:34,958 going to erase it anyway so let's send a 7448 04:52:34,958 --> 04:52:38,080 status 204 that means it's successful 7449 04:52:38,080 --> 04:52:39,600 and there's essentially 7450 04:52:39,600 --> 04:52:40,560 no 7451 04:52:40,560 --> 04:52:42,560 content to send back that's what that 7452 04:52:42,560 --> 04:52:44,560 status is so you've had a successful 7453 04:52:44,560 --> 04:52:46,080 request and we're not sending any 7454 04:52:46,080 --> 04:52:47,120 content 7455 04:52:47,120 --> 04:52:49,360 and this needs to be send status instead 7456 04:52:49,360 --> 04:52:52,638 of just status there we go response.send 7457 04:52:52,638 --> 04:52:54,958 status got that now let's go ahead and 7458 04:52:54,958 --> 04:52:56,958 define our refresh token and we'll do 7459 04:52:56,958 --> 04:52:59,040 that in the same way setting it equal to 7460 04:52:59,040 --> 04:53:01,760 cookies.jwt 7461 04:53:01,760 --> 04:53:04,560 now let's see if we have that refresh 7462 04:53:04,560 --> 04:53:06,400 token in the database so that's what 7463 04:53:06,400 --> 04:53:07,840 we're checking here 7464 04:53:07,840 --> 04:53:09,600 is 7465 04:53:09,600 --> 04:53:12,798 refresh token in db 7466 04:53:12,798 --> 04:53:14,240 so once again 7467 04:53:14,240 --> 04:53:16,560 we try to find a user that has this 7468 04:53:16,560 --> 04:53:18,560 token so setting this equal to the 7469 04:53:18,560 --> 04:53:20,718 refresh token just as we did 7470 04:53:20,718 --> 04:53:22,798 in the previous refresh token controller 7471 04:53:22,798 --> 04:53:24,878 is exactly what we need 7472 04:53:24,878 --> 04:53:28,000 and if we don't have a found user at 7473 04:53:28,000 --> 04:53:31,280 this point we can go ahead and clear the 7474 04:53:31,280 --> 04:53:33,120 cookie so we're going to do something 7475 04:53:33,120 --> 04:53:34,878 just a little different here and it 7476 04:53:34,878 --> 04:53:36,798 won't be forbidden either so we'll get 7477 04:53:36,798 --> 04:53:38,320 rid of that 7478 04:53:38,320 --> 04:53:39,200 and 7479 04:53:39,200 --> 04:53:41,120 let's go ahead and look at how we clear 7480 04:53:41,120 --> 04:53:42,958 the cookie first so if we don't have a 7481 04:53:42,958 --> 04:53:45,120 found user but we did have a cookie to 7482 04:53:45,120 --> 04:53:47,840 get to this point we can just go ahead 7483 04:53:47,840 --> 04:53:50,400 and erase the cookie that was sent 7484 04:53:50,400 --> 04:53:53,200 and that's going to be with response dot 7485 04:53:53,200 --> 04:53:54,480 clear 7486 04:53:54,480 --> 04:53:55,440 cookie 7487 04:53:55,440 --> 04:53:58,320 and then inside there we'll look for the 7488 04:53:58,320 --> 04:54:00,798 jwt cookie 7489 04:54:00,798 --> 04:54:02,878 and then you have to pass in the same 7490 04:54:02,878 --> 04:54:04,638 options that it was set with so once 7491 04:54:04,638 --> 04:54:07,200 again we'll say http only 7492 04:54:07,200 --> 04:54:09,280 set that to true 7493 04:54:09,280 --> 04:54:11,280 and that should be all we need to do 7494 04:54:11,280 --> 04:54:13,760 there except for sending the proper 7495 04:54:13,760 --> 04:54:15,600 status so once again we're going to 7496 04:54:15,600 --> 04:54:16,560 return 7497 04:54:16,560 --> 04:54:19,920 and send the status not a 403 but of 204 7498 04:54:19,920 --> 04:54:21,200 which once again means this was 7499 04:54:21,200 --> 04:54:24,320 successful but no content okay i'm going 7500 04:54:24,320 --> 04:54:26,080 to scroll up just a little bit and now 7501 04:54:26,080 --> 04:54:28,718 we can pretty much erase everything here 7502 04:54:28,718 --> 04:54:30,240 and we're going to create some new 7503 04:54:30,240 --> 04:54:31,360 content 7504 04:54:31,360 --> 04:54:33,040 below because if we've reached this 7505 04:54:33,040 --> 04:54:34,560 point that means 7506 04:54:34,560 --> 04:54:37,120 we did find the same refresh token in 7507 04:54:37,120 --> 04:54:39,520 the database so now we need to 7508 04:54:39,520 --> 04:54:42,160 delete the refresh 7509 04:54:42,160 --> 04:54:44,798 token in the database right here and 7510 04:54:44,798 --> 04:54:47,120 we're using file system of course for 7511 04:54:47,120 --> 04:54:49,440 that instead of or postgres at 7512 04:54:49,440 --> 04:54:51,520 this point but we will change that in 7513 04:54:51,520 --> 04:54:53,120 the future right now 7514 04:54:53,120 --> 04:54:55,520 let's define other users which would be 7515 04:54:55,520 --> 04:54:57,520 all of the other users that are not the 7516 04:54:57,520 --> 04:55:00,798 user that we found and set that equal to 7517 04:55:00,798 --> 04:55:03,200 users db 7518 04:55:03,200 --> 04:55:05,920 dot users 7519 04:55:05,920 --> 04:55:08,240 dot filter and we'll just filter this 7520 04:55:08,240 --> 04:55:10,638 out we'll pass in a person 7521 04:55:10,638 --> 04:55:14,000 and now the person dot 7522 04:55:14,000 --> 04:55:16,240 refresh token 7523 04:55:16,240 --> 04:55:18,958 is not equal to our 7524 04:55:18,958 --> 04:55:22,320 found user dot 7525 04:55:22,320 --> 04:55:25,360 refresh token as well there we go 7526 04:55:25,360 --> 04:55:27,920 okay so i'm going to press alt z to wrap 7527 04:55:27,920 --> 04:55:29,600 that line of code so we can see all of 7528 04:55:29,600 --> 04:55:32,400 it and that's all the other users so now 7529 04:55:32,400 --> 04:55:33,970 our current user 7530 04:55:33,970 --> 04:55:35,920 [Music] 7531 04:55:35,920 --> 04:55:40,638 is going to be equal to our found user 7532 04:55:40,638 --> 04:55:42,400 and then we're going to set the refresh 7533 04:55:42,400 --> 04:55:45,360 token to blank so 7534 04:55:45,360 --> 04:55:47,840 we'll leave the refresh token property 7535 04:55:47,840 --> 04:55:50,080 but now we're just erasing it from the 7536 04:55:50,080 --> 04:55:53,600 user and now we can update our users db 7537 04:55:53,600 --> 04:55:56,240 with our set users 7538 04:55:56,240 --> 04:55:59,120 and we'll go ahead and pass in our 7539 04:55:59,120 --> 04:56:00,718 other users 7540 04:56:00,718 --> 04:56:04,240 and then we'll pass in our found user 7541 04:56:04,240 --> 04:56:06,480 oh and i'm sorry that's not found user 7542 04:56:06,480 --> 04:56:08,718 that should be the current user that we 7543 04:56:08,718 --> 04:56:11,040 just defined there we go so found user 7544 04:56:11,040 --> 04:56:13,600 was before we erased the fresh token now 7545 04:56:13,600 --> 04:56:16,160 the current user has the updated refresh 7546 04:56:16,160 --> 04:56:19,040 token that is zeroed out or blank if you 7547 04:56:19,040 --> 04:56:21,040 will now let's just write this to the 7548 04:56:21,040 --> 04:56:23,600 file so we'll await fs 7549 04:56:23,600 --> 04:56:25,920 promises dot 7550 04:56:25,920 --> 04:56:27,520 write file 7551 04:56:27,520 --> 04:56:29,520 and now on a separate line here i'm 7552 04:56:29,520 --> 04:56:32,080 going to say path dot join 7553 04:56:32,080 --> 04:56:34,240 pass in the directory name 7554 04:56:34,240 --> 04:56:36,480 after that we'll need to come up one 7555 04:56:36,480 --> 04:56:37,520 folder 7556 04:56:37,520 --> 04:56:40,480 and then we'll say in the model folder 7557 04:56:40,480 --> 04:56:43,080 and then we'll name the 7558 04:56:43,080 --> 04:56:45,360 users.json file 7559 04:56:45,360 --> 04:56:47,840 okay then we need to go ahead and use 7560 04:56:47,840 --> 04:56:50,718 json.stringify 7561 04:56:50,718 --> 04:56:53,280 and write in the usersdb 7562 04:56:53,280 --> 04:56:55,440 dot users 7563 04:56:55,440 --> 04:56:58,400 and that should update our users file 7564 04:56:58,400 --> 04:57:00,958 and so after that we need to go ahead 7565 04:57:00,958 --> 04:57:03,680 and delete the cookie which we saw how 7566 04:57:03,680 --> 04:57:05,600 to do before as well so we use clear 7567 04:57:05,600 --> 04:57:06,638 cookie 7568 04:57:06,638 --> 04:57:09,040 and we reference a jwt 7569 04:57:09,040 --> 04:57:11,040 and we need to pass in the same options 7570 04:57:11,040 --> 04:57:12,798 as well 7571 04:57:12,798 --> 04:57:16,480 http only set to true now i will add in 7572 04:57:16,480 --> 04:57:19,600 production both when we send the cookie 7573 04:57:19,600 --> 04:57:22,160 and when we delete the cookie you also 7574 04:57:22,160 --> 04:57:24,480 want to add the flag 7575 04:57:24,480 --> 04:57:25,760 secure 7576 04:57:25,760 --> 04:57:29,840 true and that will make it only serve 7577 04:57:29,840 --> 04:57:32,798 so it only serves 7578 04:57:32,798 --> 04:57:33,718 on 7579 04:57:33,718 --> 04:57:36,798 https we're just using a dev server that 7580 04:57:36,798 --> 04:57:39,520 uses http but you want a secure 7581 04:57:39,520 --> 04:57:41,000 connection with 7582 04:57:41,000 --> 04:57:42,958 https so 7583 04:57:42,958 --> 04:57:45,120 we don't add this in development but we 7584 04:57:45,120 --> 04:57:48,240 would in production the option of secure 7585 04:57:48,240 --> 04:57:50,320 and set that to true 7586 04:57:50,320 --> 04:57:52,560 okay and after that we want to send our 7587 04:57:52,560 --> 04:57:56,080 status again which once again is a 204 7588 04:57:56,080 --> 04:57:58,080 all as well but we have no content to 7589 04:57:58,080 --> 04:57:59,440 send back 7590 04:57:59,440 --> 04:58:02,080 and after that we need to change our 7591 04:58:02,080 --> 04:58:04,638 export from handle refresh token 7592 04:58:04,638 --> 04:58:07,760 to handle logout and save okay with the 7593 04:58:07,760 --> 04:58:10,080 controller complete let's create our 7594 04:58:10,080 --> 04:58:11,440 route file 7595 04:58:11,440 --> 04:58:13,520 and we can just go here to the refresh 7596 04:58:13,520 --> 04:58:15,440 file we created 7597 04:58:15,440 --> 04:58:17,840 select all and copy 7598 04:58:17,840 --> 04:58:20,480 and now i'm going to create a new file 7599 04:58:20,480 --> 04:58:23,600 in the routes called logout.js 7600 04:58:23,600 --> 04:58:25,520 and now i'll just paste everything in 7601 04:58:25,520 --> 04:58:27,440 from the refresh 7602 04:58:27,440 --> 04:58:30,320 route and we'll change that so we need 7603 04:58:30,320 --> 04:58:32,718 express we need the router but instead 7604 04:58:32,718 --> 04:58:35,520 of refresh token controller we'll select 7605 04:58:35,520 --> 04:58:38,240 all three of those with control d 7606 04:58:38,240 --> 04:58:40,080 and i'll just type 7607 04:58:40,080 --> 04:58:41,280 log out 7608 04:58:41,280 --> 04:58:43,760 and this will be a get route again and 7609 04:58:43,760 --> 04:58:46,638 the export to router is correct but we 7610 04:58:46,638 --> 04:58:49,600 do not have handle refresh token here we 7611 04:58:49,600 --> 04:58:52,400 also have handle 7612 04:58:52,400 --> 04:58:54,160 logout as 7613 04:58:54,160 --> 04:58:56,718 the function for the controller and with 7614 04:58:56,718 --> 04:58:58,560 that complete now we can go to the 7615 04:58:58,560 --> 04:59:00,878 server and add that route as well we 7616 04:59:00,878 --> 04:59:04,160 won't need to verify a jwt to log out so 7617 04:59:04,160 --> 04:59:06,320 i will just once again shift alt and the 7618 04:59:06,320 --> 04:59:08,798 down arrow to copy a line down 7619 04:59:08,798 --> 04:59:11,840 and i'll change refresh 7620 04:59:11,840 --> 04:59:14,718 to log out and save now let's open a 7621 04:59:14,718 --> 04:59:16,958 terminal window again oh and we've got 7622 04:59:16,958 --> 04:59:19,600 an error so let's see what our error is 7623 04:59:19,600 --> 04:59:21,360 here in node 7624 04:59:21,360 --> 04:59:23,760 a weight is only valid in an async 7625 04:59:23,760 --> 04:59:26,080 function ah we did not do that in our 7626 04:59:26,080 --> 04:59:28,320 controller let's go back to the logout 7627 04:59:28,320 --> 04:59:31,440 controller and we used await when we 7628 04:59:31,440 --> 04:59:34,718 used fs promises so let's make the 7629 04:59:34,718 --> 04:59:38,240 handle logout an async function 7630 04:59:38,240 --> 04:59:40,798 and save and let's see if node restarts 7631 04:59:40,798 --> 04:59:44,560 and yes the server is running on 3500 7632 04:59:44,560 --> 04:59:47,120 all looks good there so now we can go to 7633 04:59:47,120 --> 04:59:48,798 thunder client 7634 04:59:48,798 --> 04:59:50,798 and we'll hit our auth route to make 7635 04:59:50,798 --> 04:59:53,200 sure everything is logged in we're going 7636 04:59:53,200 --> 04:59:57,520 to log in our user walt one 7637 04:59:57,760 --> 05:00:00,160 and we've got an access token 7638 05:00:00,160 --> 05:00:01,920 and we've got a cookie 7639 05:00:01,920 --> 05:00:04,638 so now let's go back to thunder client 7640 05:00:04,638 --> 05:00:07,840 and i have a log out route that is a get 7641 05:00:07,840 --> 05:00:10,718 route as well so we have get localhost 7642 05:00:10,718 --> 05:00:12,320 3500 7643 05:00:12,320 --> 05:00:14,718 log out you don't need to pass anything 7644 05:00:14,718 --> 05:00:17,120 in the body or the auth when we log out 7645 05:00:17,120 --> 05:00:20,000 but let's go ahead and do that 7646 05:00:20,000 --> 05:00:22,480 and we hit log out and everything seems 7647 05:00:22,480 --> 05:00:24,400 to be fine 7648 05:00:24,400 --> 05:00:26,400 we can see our cookie has now been 7649 05:00:26,400 --> 05:00:27,680 erased 7650 05:00:27,680 --> 05:00:29,520 so now if we go to any of the other 7651 05:00:29,520 --> 05:00:30,638 routes 7652 05:00:30,638 --> 05:00:33,200 like refresh that would use our refresh 7653 05:00:33,200 --> 05:00:34,638 token cookie 7654 05:00:34,638 --> 05:00:37,200 we don't have a cookie to refresh with 7655 05:00:37,200 --> 05:00:40,320 so now let's try localhost 3500 refresh 7656 05:00:40,320 --> 05:00:43,040 and it's a git route and see what we get 7657 05:00:43,040 --> 05:00:46,240 well we can see in the console we logged 7658 05:00:46,240 --> 05:00:48,240 or attempted to log a cookie but it 7659 05:00:48,240 --> 05:00:50,560 didn't exist here and we're not getting 7660 05:00:50,560 --> 05:00:52,718 a response so let's look at that i can 7661 05:00:52,718 --> 05:00:55,120 cancel this here in thunder client let's 7662 05:00:55,120 --> 05:00:57,760 go ahead and look at our 7663 05:00:57,760 --> 05:01:00,320 refresh controller 7664 05:01:00,320 --> 05:01:02,798 and see what's going on there so refresh 7665 05:01:02,798 --> 05:01:05,440 token controller 7666 05:01:05,440 --> 05:01:07,200 and here is where we're logging the 7667 05:01:07,200 --> 05:01:09,040 cookies we can get rid of these lines 7668 05:01:09,040 --> 05:01:11,600 actually now so we don't need those 7669 05:01:11,600 --> 05:01:13,680 but let's see if we can figure out what 7670 05:01:13,680 --> 05:01:16,480 is going on and yes here's the error i 7671 05:01:16,480 --> 05:01:19,360 said response dot status 401 it needs to 7672 05:01:19,360 --> 05:01:21,280 be response dot 7673 05:01:21,280 --> 05:01:23,760 send status 401 7674 05:01:23,760 --> 05:01:25,360 just like we had down here with send 7675 05:01:25,360 --> 05:01:27,200 status 403 7676 05:01:27,200 --> 05:01:29,520 so if i've typoed any of those others 7677 05:01:29,520 --> 05:01:31,600 throughout this code it looks like i had 7678 05:01:31,600 --> 05:01:34,000 send status everywhere else but just in 7679 05:01:34,000 --> 05:01:35,920 case you see that to actually send the 7680 05:01:35,920 --> 05:01:38,000 status it needs to be send status 7681 05:01:38,000 --> 05:01:40,080 instead of just status status is 7682 05:01:40,080 --> 05:01:42,000 chainable if we're sending a response 7683 05:01:42,000 --> 05:01:43,120 after that 7684 05:01:43,120 --> 05:01:44,958 but now this should work so let's once 7685 05:01:44,958 --> 05:01:47,280 again go back to thunderclient 7686 05:01:47,280 --> 05:01:49,440 and we'll hit our refresh token route 7687 05:01:49,440 --> 05:01:51,600 although we don't have a cookie 7688 05:01:51,600 --> 05:01:53,520 and we'll see what happens 7689 05:01:53,520 --> 05:01:55,840 ah we immediately got our 401 7690 05:01:55,840 --> 05:01:57,840 unauthorized because now it sends the 7691 05:01:57,840 --> 05:02:00,480 status so that's correct let's go back 7692 05:02:00,480 --> 05:02:03,680 and log in again with our auth route 7693 05:02:03,680 --> 05:02:07,040 localhost 3500 auth send that we have an 7694 05:02:07,040 --> 05:02:08,560 access token 7695 05:02:08,560 --> 05:02:11,120 we have a cookie and everything seems 7696 05:02:11,120 --> 05:02:13,200 good so all routes are working as 7697 05:02:13,200 --> 05:02:15,600 expected okay i thought i was at the end 7698 05:02:15,600 --> 05:02:17,440 of the tutorial but after playing around 7699 05:02:17,440 --> 05:02:19,760 with some front end code there were a 7700 05:02:19,760 --> 05:02:21,680 few things i thought i needed to go 7701 05:02:21,680 --> 05:02:25,360 ahead and clear up or include one is 7702 05:02:25,360 --> 05:02:27,440 here this is front end code by the way 7703 05:02:27,440 --> 05:02:29,200 this is not what we were working on in 7704 05:02:29,200 --> 05:02:33,280 express if you use fetch to access 7705 05:02:33,280 --> 05:02:36,320 what we had set up say our auth route 7706 05:02:36,320 --> 05:02:38,560 well you're going to need to include 7707 05:02:38,560 --> 05:02:40,718 a credentials option 7708 05:02:40,718 --> 05:02:43,840 again on the front end to have fetch 7709 05:02:43,840 --> 05:02:46,320 send the cookie now if you use axios i 7710 05:02:46,320 --> 05:02:48,958 believe there is a with credentials flag 7711 05:02:48,958 --> 05:02:50,878 that needs to be set as well 7712 05:02:50,878 --> 05:02:52,638 and we could do that in a future 7713 05:02:52,638 --> 05:02:54,400 tutorial but right now 7714 05:02:54,400 --> 05:02:57,040 just looking at credentials for use with 7715 05:02:57,040 --> 05:02:59,760 fetch so you have to set that to include 7716 05:02:59,760 --> 05:03:02,320 but that sets off a chain of events so 7717 05:03:02,320 --> 05:03:04,080 even though this is required here to 7718 05:03:04,080 --> 05:03:05,600 send the cookie 7719 05:03:05,600 --> 05:03:07,200 then what happens 7720 05:03:07,200 --> 05:03:10,958 is i'll pull up chrome here 7721 05:03:10,958 --> 05:03:13,200 we get a course error 7722 05:03:13,200 --> 05:03:15,840 and you will be blocked by cores because 7723 05:03:15,840 --> 05:03:19,120 the value of access control allow 7724 05:03:19,120 --> 05:03:21,440 credentials in the header 7725 05:03:21,440 --> 05:03:23,280 of the response 7726 05:03:23,280 --> 05:03:24,638 is blank 7727 05:03:24,638 --> 05:03:27,200 and it needs to be set to true 7728 05:03:27,200 --> 05:03:29,280 so cores will block this 7729 05:03:29,280 --> 05:03:31,760 if you don't have that set before you 7730 05:03:31,760 --> 05:03:34,400 reach the course check now what is 7731 05:03:34,400 --> 05:03:37,520 happening is a pre-flight options check 7732 05:03:37,520 --> 05:03:41,200 it's an options http method request but 7733 05:03:41,200 --> 05:03:43,200 then you also need to have the same 7734 05:03:43,200 --> 05:03:45,920 access control allow credentials 7735 05:03:45,920 --> 05:03:49,200 set to true when you do send the cookie 7736 05:03:49,200 --> 05:03:51,040 back as well because that's what's 7737 05:03:51,040 --> 05:03:52,798 expected with fetch 7738 05:03:52,798 --> 05:03:55,360 so there's a way to fix that of course 7739 05:03:55,360 --> 05:03:57,120 and now let's look at some of our back 7740 05:03:57,120 --> 05:03:58,480 end code 7741 05:03:58,480 --> 05:04:02,000 and let's start in the config folder 7742 05:04:02,000 --> 05:04:05,280 and i went to coors options first 7743 05:04:05,280 --> 05:04:07,600 and we did have what was called a white 7744 05:04:07,600 --> 05:04:09,920 list in here which 7745 05:04:09,920 --> 05:04:11,760 that's traditionally what it's called 7746 05:04:11,760 --> 05:04:14,000 and i've seen that in articles as well i 7747 05:04:14,000 --> 05:04:16,798 don't like the name so much either 7748 05:04:16,798 --> 05:04:18,958 just maybe in modern day it sounds a 7749 05:04:18,958 --> 05:04:21,040 little racist or something so we've 7750 05:04:21,040 --> 05:04:23,440 switched that to allowed origins which 7751 05:04:23,440 --> 05:04:25,680 is what it is it's a list of allowed 7752 05:04:25,680 --> 05:04:29,040 origins and i put it in its own file 7753 05:04:29,040 --> 05:04:30,320 because we're going to use it in 7754 05:04:30,320 --> 05:04:32,240 middleware also but i only want to 7755 05:04:32,240 --> 05:04:34,480 update it in one place so we have our 7756 05:04:34,480 --> 05:04:36,240 allowed origins 7757 05:04:36,240 --> 05:04:38,480 formerly called the white list in the 7758 05:04:38,480 --> 05:04:39,680 tutorial 7759 05:04:39,680 --> 05:04:42,480 and we're exporting allowed origins so 7760 05:04:42,480 --> 05:04:45,520 we import that into our cores options 7761 05:04:45,520 --> 05:04:48,400 and use it here but then we also create 7762 05:04:48,400 --> 05:04:51,360 middleware called credentials 7763 05:04:51,360 --> 05:04:53,440 and now this credentials middleware of 7764 05:04:53,440 --> 05:04:56,560 course has a request response and next 7765 05:04:56,560 --> 05:04:59,600 and we import allowed origins here also 7766 05:04:59,600 --> 05:05:03,280 all we're really doing is to say if 7767 05:05:03,280 --> 05:05:06,400 the origin that is sending the request 7768 05:05:06,400 --> 05:05:09,280 is in our allowed origins list so it's 7769 05:05:09,280 --> 05:05:11,680 just a little bit of extra security if 7770 05:05:11,680 --> 05:05:14,080 that is the case go ahead and set this 7771 05:05:14,080 --> 05:05:16,080 header on the response because that's 7772 05:05:16,080 --> 05:05:18,000 what cores is looking for 7773 05:05:18,000 --> 05:05:20,878 access control allow credentials and we 7774 05:05:20,878 --> 05:05:22,638 set that to true 7775 05:05:22,638 --> 05:05:24,320 so now that we have the middleware 7776 05:05:24,320 --> 05:05:27,280 created we go to the server file and we 7777 05:05:27,280 --> 05:05:29,440 pull that middleware in 7778 05:05:29,440 --> 05:05:32,160 and then you want to use the middleware 7779 05:05:32,160 --> 05:05:34,480 we're using credentials right here and 7780 05:05:34,480 --> 05:05:36,400 of course i'm importing it right up here 7781 05:05:36,400 --> 05:05:37,600 above 7782 05:05:37,600 --> 05:05:40,638 you want to use that before cores 7783 05:05:40,638 --> 05:05:42,480 because of course sees that that 7784 05:05:42,480 --> 05:05:44,958 response header is not set it throws 7785 05:05:44,958 --> 05:05:46,080 that error 7786 05:05:46,080 --> 05:05:47,040 so 7787 05:05:47,040 --> 05:05:49,440 that will fix that issue and i would 7788 05:05:49,440 --> 05:05:51,680 hope that would be the end of the issues 7789 05:05:51,680 --> 05:05:54,400 but no there was one other issue 7790 05:05:54,400 --> 05:05:56,320 and let's take a look at what that was 7791 05:05:56,320 --> 05:05:58,400 i'm going to open chrome again and i had 7792 05:05:58,400 --> 05:06:01,280 to do a screen capture now i was in the 7793 05:06:01,280 --> 05:06:04,560 dev tools in the network tab and i was 7794 05:06:04,560 --> 05:06:07,600 looking at the auth request and then i 7795 05:06:07,600 --> 05:06:09,520 could pull up the cookie 7796 05:06:09,520 --> 05:06:11,360 and then there was this little triangle 7797 05:06:11,360 --> 05:06:12,958 and the reason i had to do this screen 7798 05:06:12,958 --> 05:06:15,040 capture is because i had to mouse over 7799 05:06:15,040 --> 05:06:16,400 the triangle 7800 05:06:16,400 --> 05:06:19,120 for this message to pop up in this very 7801 05:06:19,120 --> 05:06:21,520 small print i'm zoomed in 7802 05:06:21,520 --> 05:06:24,480 quite a bit already just to see this and 7803 05:06:24,480 --> 05:06:27,120 it said the set cookie header didn't 7804 05:06:27,120 --> 05:06:30,400 specify a same site attribute so that 7805 05:06:30,400 --> 05:06:33,920 defaulted to same site equals lacks and 7806 05:06:33,920 --> 05:06:36,240 then it was blocked because it came from 7807 05:06:36,240 --> 05:06:38,798 a cross site response which means 7808 05:06:38,798 --> 05:06:39,760 our 7809 05:06:39,760 --> 05:06:41,760 front end application was not on the 7810 05:06:41,760 --> 05:06:45,200 same domain as our back end api and 7811 05:06:45,200 --> 05:06:47,040 that's often the case 7812 05:06:47,040 --> 05:06:48,000 so 7813 05:06:48,000 --> 05:06:49,600 there's nothing wrong with that it's 7814 05:06:49,600 --> 05:06:51,920 just that's why they blocked it says the 7815 05:06:51,920 --> 05:06:54,638 set cookie had to have been set with 7816 05:06:54,638 --> 05:06:56,878 same site equals none 7817 05:06:56,878 --> 05:07:00,560 to enable cross site usage so that tells 7818 05:07:00,560 --> 05:07:03,120 us something else another option 7819 05:07:03,120 --> 05:07:06,160 we need to go and put in our set cookie 7820 05:07:06,160 --> 05:07:09,280 response so let's look back at the back 7821 05:07:09,280 --> 05:07:11,040 end code here 7822 05:07:11,040 --> 05:07:11,840 and 7823 05:07:11,840 --> 05:07:14,480 then where did we set the cookies well 7824 05:07:14,480 --> 05:07:16,240 let's look at the controllers that's 7825 05:07:16,240 --> 05:07:18,560 where the cookies were being set 7826 05:07:18,560 --> 05:07:21,200 and i believe the auth controller is 7827 05:07:21,200 --> 05:07:25,200 what sets the cookie specifically and so 7828 05:07:25,200 --> 05:07:28,638 not only do we have http only true 7829 05:07:28,638 --> 05:07:31,760 but then we went ahead and set same site 7830 05:07:31,760 --> 05:07:34,560 to none as they request here 7831 05:07:34,560 --> 05:07:36,958 and then after you do that 7832 05:07:36,958 --> 05:07:38,798 they give you another message in the 7833 05:07:38,798 --> 05:07:42,320 same spot and that says secure must be 7834 05:07:42,320 --> 05:07:43,680 set to true 7835 05:07:43,680 --> 05:07:46,718 now previously i didn't have secure set 7836 05:07:46,718 --> 05:07:48,480 to true there 7837 05:07:48,480 --> 05:07:50,240 because i thought you could only use it 7838 05:07:50,240 --> 05:07:53,600 with https even during development but 7839 05:07:53,600 --> 05:07:56,160 no it's working in the dev server even 7840 05:07:56,160 --> 05:07:58,718 though we're just using http 7841 05:07:58,718 --> 05:08:00,560 then after that i went ahead i've got 7842 05:08:00,560 --> 05:08:02,400 the code wrapping by the way because 7843 05:08:02,400 --> 05:08:03,840 down here on the next line you still 7844 05:08:03,840 --> 05:08:06,160 have the max age option 7845 05:08:06,160 --> 05:08:08,240 now when you delete a cookie 7846 05:08:08,240 --> 05:08:11,280 you need to set the same options however 7847 05:08:11,280 --> 05:08:13,760 and i'll put a link to the documentation 7848 05:08:13,760 --> 05:08:16,958 in the description below however max age 7849 05:08:16,958 --> 05:08:20,160 actually does not need to be set 7850 05:08:20,160 --> 05:08:22,480 or in there when you delete the cookie 7851 05:08:22,480 --> 05:08:24,080 it's one of only a couple like the 7852 05:08:24,080 --> 05:08:26,400 expiration and max age are the only two 7853 05:08:26,400 --> 05:08:28,958 options that don't need to be identical 7854 05:08:28,958 --> 05:08:31,040 when you delete the cookie so when we go 7855 05:08:31,040 --> 05:08:33,680 to the log out controller 7856 05:08:33,680 --> 05:08:36,798 and we have our res.clear cookie we need 7857 05:08:36,798 --> 05:08:41,520 to have http only same site and secure 7858 05:08:41,520 --> 05:08:44,240 all set as before but max age does not 7859 05:08:44,240 --> 05:08:45,920 need to be in here and we've got that in 7860 05:08:45,920 --> 05:08:47,760 here a couple of times 7861 05:08:47,760 --> 05:08:50,878 in the log out controller as well so we 7862 05:08:50,878 --> 05:08:54,000 did make those changes but after you do 7863 05:08:54,000 --> 05:08:54,798 that 7864 05:08:54,798 --> 05:08:57,920 everything should work as intended so 7865 05:08:57,920 --> 05:08:59,280 it's just a couple of things i 7866 05:08:59,280 --> 05:09:01,520 discovered as i was working with this 7867 05:09:01,520 --> 05:09:04,400 front end code and of course it is good 7868 05:09:04,400 --> 05:09:06,798 to test it out with actual code and a 7869 05:09:06,798 --> 05:09:09,280 browser besides using an extension like 7870 05:09:09,280 --> 05:09:11,280 thunder client because thunder client 7871 05:09:11,280 --> 05:09:12,798 showed everything was working as we 7872 05:09:12,798 --> 05:09:15,680 expected it to but of course we ran into 7873 05:09:15,680 --> 05:09:18,480 a few issues with chrome and now 7874 05:09:18,480 --> 05:09:20,400 i've walked you through how to fix those 7875 05:09:20,400 --> 05:09:22,480 as well 7876 05:09:22,480 --> 05:09:24,480 since creating the last tutorial i 7877 05:09:24,480 --> 05:09:26,400 realized there's a lot of confusion 7878 05:09:26,400 --> 05:09:28,040 concerning the concepts of 7879 05:09:28,040 --> 05:09:31,280 authentication and authorization they're 7880 05:09:31,280 --> 05:09:33,600 often used interchangeably or simply 7881 05:09:33,600 --> 05:09:36,718 abbreviated as off but they are not the 7882 05:09:36,718 --> 05:09:38,280 same things 7883 05:09:38,280 --> 05:09:40,798 authentication refers to the process of 7884 05:09:40,798 --> 05:09:43,520 verifying who someone is 7885 05:09:43,520 --> 05:09:45,920 authorization is the process of 7886 05:09:45,920 --> 05:09:49,520 verifying what specific resources a user 7887 05:09:49,520 --> 05:09:52,160 has access to when we log in with the 7888 05:09:52,160 --> 05:09:54,958 username and password we are verifying 7889 05:09:54,958 --> 05:09:57,920 who we are and that is considered to be 7890 05:09:57,920 --> 05:09:59,600 authentication 7891 05:09:59,600 --> 05:10:02,878 after logging in our express api issues 7892 05:10:02,878 --> 05:10:08,000 users jwt tokens jwt stands for json web 7893 05:10:08,000 --> 05:10:10,718 tokens while it's true that the tokens 7894 05:10:10,718 --> 05:10:13,120 confirm the authentication process has 7895 05:10:13,120 --> 05:10:16,000 already taken place these tokens also 7896 05:10:16,000 --> 05:10:18,878 allow access to our api endpoints which 7897 05:10:18,878 --> 05:10:21,280 provide our api data 7898 05:10:21,280 --> 05:10:24,320 this is authorization a hint towards 7899 05:10:24,320 --> 05:10:27,680 this fact is that a jwt token uses the 7900 05:10:27,680 --> 05:10:30,160 authorization header today we will 7901 05:10:30,160 --> 05:10:32,480 expand the authorization process by 7902 05:10:32,480 --> 05:10:34,878 adding user roles with specific 7903 05:10:34,878 --> 05:10:37,360 permissions to our api authorization 7904 05:10:37,360 --> 05:10:39,280 process we're going to start with the 7905 05:10:39,280 --> 05:10:41,680 code repository from the last tutorial 7906 05:10:41,680 --> 05:10:43,360 but if you don't have it you should be 7907 05:10:43,360 --> 05:10:45,680 able to easily follow along without it 7908 05:10:45,680 --> 05:10:48,000 or you can download or clone the starter 7909 05:10:48,000 --> 05:10:50,080 source code from the link i've provided 7910 05:10:50,080 --> 05:10:51,920 in the description below we're going to 7911 05:10:51,920 --> 05:10:54,638 start by going to the config folder in 7912 05:10:54,638 --> 05:10:57,440 the file tree and creating a new file 7913 05:10:57,440 --> 05:10:59,520 and we'll call this file roles 7914 05:10:59,520 --> 05:11:02,718 underscore list dot js 7915 05:11:02,718 --> 05:11:04,798 in the roles list we're going to create 7916 05:11:04,798 --> 05:11:07,760 the user roles now this could be in a 7917 05:11:07,760 --> 05:11:09,920 data table in a database this is just 7918 05:11:09,920 --> 05:11:12,080 how we're going to do this and you'll 7919 05:11:12,080 --> 05:11:14,400 find that user permissions can be 7920 05:11:14,400 --> 05:11:17,040 constructed in an assortment of ways and 7921 05:11:17,040 --> 05:11:19,680 we're going to apply a fairly simple 7922 05:11:19,680 --> 05:11:21,360 structure today with three different 7923 05:11:21,360 --> 05:11:24,000 user roles so we'll start out with our 7924 05:11:24,000 --> 05:11:26,240 roles list constant 7925 05:11:26,240 --> 05:11:28,080 and we're going to set this equal to an 7926 05:11:28,080 --> 05:11:29,120 object 7927 05:11:29,120 --> 05:11:31,520 and the keys will actually be the names 7928 05:11:31,520 --> 05:11:34,560 of the roles so we'll have admin and 7929 05:11:34,560 --> 05:11:38,160 then we'll have a code that identifies 7930 05:11:38,160 --> 05:11:39,280 the role 7931 05:11:39,280 --> 05:11:41,280 and then after that let's create an 7932 05:11:41,280 --> 05:11:44,878 editor role and we'll call this 1984 and 7933 05:11:44,878 --> 05:11:47,680 then let's have just a user role and 7934 05:11:47,680 --> 05:11:50,320 we'll give that 2001. okay now that 7935 05:11:50,320 --> 05:11:52,560 we've created our roles list object all 7936 05:11:52,560 --> 05:11:54,920 we need to do here is 7937 05:11:54,920 --> 05:11:56,560 module.exports 7938 05:11:56,560 --> 05:11:59,360 and set that equal to roles underscore 7939 05:11:59,360 --> 05:12:00,480 list 7940 05:12:00,480 --> 05:12:03,280 okay we've saved that now we're ready to 7941 05:12:03,280 --> 05:12:05,280 go ahead and modify 7942 05:12:05,280 --> 05:12:08,240 our users model that we have in the 7943 05:12:08,240 --> 05:12:10,320 users.json 7944 05:12:10,320 --> 05:12:11,200 file 7945 05:12:11,200 --> 05:12:13,680 and you might find that 7946 05:12:13,680 --> 05:12:15,680 once it has been written 7947 05:12:15,680 --> 05:12:19,200 by the read and write file process that 7948 05:12:19,200 --> 05:12:21,280 we use in node we're using this user 7949 05:12:21,280 --> 05:12:23,920 jsons file it might all be in one line 7950 05:12:23,920 --> 05:12:27,040 but i believe if we go ahead and just 7951 05:12:27,040 --> 05:12:30,000 put a return and edit and then save in 7952 05:12:30,000 --> 05:12:32,958 visual studio code it should go ahead 7953 05:12:32,958 --> 05:12:35,760 and format that file for you so it's a 7954 05:12:35,760 --> 05:12:37,920 little easier to read now this is what 7955 05:12:37,920 --> 05:12:40,400 we had after the last tutorial we have a 7956 05:12:40,400 --> 05:12:43,840 username walt1 and a username walt2 they 7957 05:12:43,840 --> 05:12:45,840 all have their own passwords and of 7958 05:12:45,840 --> 05:12:48,080 course we're tracking the refresh tokens 7959 05:12:48,080 --> 05:12:50,798 i'm going to copy in the file i've 7960 05:12:50,798 --> 05:12:54,160 created it adds one more user and so you 7961 05:12:54,160 --> 05:12:56,400 can copy this file 7962 05:12:56,400 --> 05:12:58,958 out of the completed source code that 7963 05:12:58,958 --> 05:13:00,718 i'm going to link to there's a starter 7964 05:13:00,718 --> 05:13:02,798 source code link and then a completed 7965 05:13:02,798 --> 05:13:05,520 source code link or you can go ahead and 7966 05:13:05,520 --> 05:13:07,760 use the register route create your own 7967 05:13:07,760 --> 05:13:10,638 new user and you can see i've added dave 7968 05:13:10,638 --> 05:13:13,760 one as well as our walt one and walt ii 7969 05:13:13,760 --> 05:13:17,520 dave one just has the role of user while 7970 05:13:17,520 --> 05:13:22,798 walt 2 has the roles of user and editor 7971 05:13:22,798 --> 05:13:24,480 and walt 1 7972 05:13:24,480 --> 05:13:26,798 is not only a user but also an editor 7973 05:13:26,798 --> 05:13:28,798 and admin so you can see we're going to 7974 05:13:28,798 --> 05:13:32,000 support multiple roles i'll go ahead and 7975 05:13:32,000 --> 05:13:34,560 save this file once again you can copy 7976 05:13:34,560 --> 05:13:36,560 it from the completed source code or 7977 05:13:36,560 --> 05:13:38,718 just create your own third user if you 7978 05:13:38,718 --> 05:13:39,680 want to 7979 05:13:39,680 --> 05:13:41,760 and we'll just edit these manually now 7980 05:13:41,760 --> 05:13:43,120 there could be 7981 05:13:43,120 --> 05:13:45,440 a admin area 7982 05:13:45,440 --> 05:13:47,920 for whatever service you were creating 7983 05:13:47,920 --> 05:13:50,560 and that is where these additional roles 7984 05:13:50,560 --> 05:13:53,520 could be created but what we will do is 7985 05:13:53,520 --> 05:13:56,240 just ensure when everyone registers 7986 05:13:56,240 --> 05:13:58,400 they're given the user role and then of 7987 05:13:58,400 --> 05:14:00,480 course later on you'd think an admin 7988 05:14:00,480 --> 05:14:01,760 that was in charge could add the 7989 05:14:01,760 --> 05:14:04,240 additional roles if those roles were to 7990 05:14:04,240 --> 05:14:06,400 be granted to other users 7991 05:14:06,400 --> 05:14:09,280 so now we can collapse the model folder 7992 05:14:09,280 --> 05:14:11,280 and the config folder but let's look 7993 05:14:11,280 --> 05:14:13,680 inside the controller folder and let's 7994 05:14:13,680 --> 05:14:15,920 look at that register controller that we 7995 05:14:15,920 --> 05:14:18,320 have what we want to do 7996 05:14:18,320 --> 05:14:23,120 is add that role of user with the 2001 7997 05:14:23,120 --> 05:14:24,080 code 7998 05:14:24,080 --> 05:14:26,958 when any user registers you can see 7999 05:14:26,958 --> 05:14:29,280 we're creating our new user right here 8000 05:14:29,280 --> 05:14:32,320 on line 19 and we have the username and 8001 05:14:32,320 --> 05:14:34,638 the hashed password so let's just go 8002 05:14:34,638 --> 05:14:37,120 ahead and add this extra roll i'll break 8003 05:14:37,120 --> 05:14:39,440 this out on separate lines 8004 05:14:39,440 --> 05:14:41,680 and that way we can see 8005 05:14:41,680 --> 05:14:44,000 the new part that we add and what we 8006 05:14:44,000 --> 05:14:46,798 want to do is just in between username 8007 05:14:46,798 --> 05:14:48,560 and password 8008 05:14:48,560 --> 05:14:51,680 add roles let's keep that lowercase 8009 05:14:51,680 --> 05:14:54,000 and after that let's put this in an 8010 05:14:54,000 --> 05:14:56,400 object so then we have 8011 05:14:56,400 --> 05:14:57,760 user 8012 05:14:57,760 --> 05:15:00,878 and then we have the code 2001 and then 8013 05:15:00,878 --> 05:15:04,320 remember to put a comma after roles 8014 05:15:04,320 --> 05:15:07,520 so it goes before the password 8015 05:15:07,520 --> 05:15:10,240 and save now let's go to the auth 8016 05:15:10,240 --> 05:15:11,600 controller 8017 05:15:11,600 --> 05:15:14,400 and when we authorize and create the 8018 05:15:14,400 --> 05:15:15,920 access token 8019 05:15:15,920 --> 05:15:18,240 we will want to send this information in 8020 05:15:18,240 --> 05:15:20,000 the access token so we're going to 8021 05:15:20,000 --> 05:15:23,120 change our payload for the jwts 8022 05:15:23,120 --> 05:15:27,120 now first when we know we have a match 8023 05:15:27,120 --> 05:15:30,160 and we have verified our user so 8024 05:15:30,160 --> 05:15:32,958 everything is good we want to go ahead 8025 05:15:32,958 --> 05:15:35,520 and grab the roles that we put in our 8026 05:15:35,520 --> 05:15:38,958 users json file so let's go ahead and 8027 05:15:38,958 --> 05:15:40,798 define roles 8028 05:15:40,798 --> 05:15:44,000 and set this equal to object 8029 05:15:44,000 --> 05:15:47,200 dot values and then we can pass in the 8030 05:15:47,200 --> 05:15:50,240 found user that we have above 8031 05:15:50,240 --> 05:15:53,200 and we want found user dot 8032 05:15:53,200 --> 05:15:57,920 roles and so now we'll get those values 8033 05:15:57,920 --> 05:16:00,878 inside of roles and now let's change our 8034 05:16:00,878 --> 05:16:02,958 access token payload just a little bit 8035 05:16:02,958 --> 05:16:04,798 since we're not just going to send the 8036 05:16:04,798 --> 05:16:08,080 username let's create a new namespace 8037 05:16:08,080 --> 05:16:10,840 here with user 8038 05:16:10,840 --> 05:16:14,320 info and then this will be an object 8039 05:16:14,320 --> 05:16:16,958 and inside this object we'll have 8040 05:16:16,958 --> 05:16:18,240 username 8041 05:16:18,240 --> 05:16:21,200 let me go ahead and add the closing 8042 05:16:21,200 --> 05:16:24,000 curly brace for the object here so we'll 8043 05:16:24,000 --> 05:16:26,080 have the username 8044 05:16:26,080 --> 05:16:28,320 but then oh we don't need that closing 8045 05:16:28,320 --> 05:16:30,958 curly brace but then we also want to add 8046 05:16:30,958 --> 05:16:33,520 some more information here inside of the 8047 05:16:33,520 --> 05:16:35,760 user info object 8048 05:16:35,760 --> 05:16:38,160 and the next one will be 8049 05:16:38,160 --> 05:16:39,360 roles 8050 05:16:39,360 --> 05:16:42,638 with quote and we'll just pass 8051 05:16:42,638 --> 05:16:44,958 the rolls that we've created it looks 8052 05:16:44,958 --> 05:16:46,878 like i actually did need that extra 8053 05:16:46,878 --> 05:16:48,638 curly brace or at least we need to 8054 05:16:48,638 --> 05:16:50,400 indent here 8055 05:16:50,400 --> 05:16:52,798 and then we're going to need to close 8056 05:16:52,798 --> 05:16:55,520 out our payload that looks correct i 8057 05:16:55,520 --> 05:16:57,680 believe and if you want to break this 8058 05:16:57,680 --> 05:16:59,920 onto another line you can do that as 8059 05:16:59,920 --> 05:17:00,718 well 8060 05:17:00,718 --> 05:17:02,480 go ahead and save and there we go it 8061 05:17:02,480 --> 05:17:04,400 gets formatted a little bit better 8062 05:17:04,400 --> 05:17:07,920 already so we're using this user info as 8063 05:17:07,920 --> 05:17:10,400 a different name space and that's good 8064 05:17:10,400 --> 05:17:12,400 because this is considered to be a 8065 05:17:12,400 --> 05:17:15,920 private jwt claim because there are some 8066 05:17:15,920 --> 05:17:16,718 res 8067 05:17:16,718 --> 05:17:19,680 reserved abbreviations and words for 8068 05:17:19,680 --> 05:17:22,718 public jwt claims and you can find out 8069 05:17:22,718 --> 05:17:24,400 more about that at 8070 05:17:24,400 --> 05:17:28,160 jwt dot io i believe i'll confirm that 8071 05:17:28,160 --> 05:17:29,840 and leave a link in the description 8072 05:17:29,840 --> 05:17:31,520 below so you can check out more 8073 05:17:31,520 --> 05:17:34,400 information on the jwt claims if you're 8074 05:17:34,400 --> 05:17:36,878 interested now there is no reason to 8075 05:17:36,878 --> 05:17:39,520 send the roles in the refresh token and 8076 05:17:39,520 --> 05:17:41,760 ideally the access token will only be 8077 05:17:41,760 --> 05:17:44,400 stored in memory on the front end but we 8078 05:17:44,400 --> 05:17:46,240 don't have control over that so when we 8079 05:17:46,240 --> 05:17:48,320 do send the roles we're just sending the 8080 05:17:48,320 --> 05:17:51,360 codes and not actually the word admin or 8081 05:17:51,360 --> 05:17:53,600 editor or anything like that 8082 05:17:53,600 --> 05:17:57,120 so we just are kind of hiding what each 8083 05:17:57,120 --> 05:17:59,840 one is by using codes but at the same 8084 05:17:59,840 --> 05:18:03,120 time ideally that access token would 8085 05:18:03,120 --> 05:18:05,440 only be stored in memory anyway but 8086 05:18:05,440 --> 05:18:07,520 there is no need whatsoever to send the 8087 05:18:07,520 --> 05:18:11,040 roles in the refresh token refresh token 8088 05:18:11,040 --> 05:18:14,320 is only there to verify that you can get 8089 05:18:14,320 --> 05:18:16,480 a new access token 8090 05:18:16,480 --> 05:18:18,798 okay now speaking of the refresh token 8091 05:18:18,798 --> 05:18:20,718 we need to go to the refresh token 8092 05:18:20,718 --> 05:18:23,600 controller and add some of the same code 8093 05:18:23,600 --> 05:18:26,958 because that refresh token does issue 8094 05:18:26,958 --> 05:18:29,440 a new access token so 8095 05:18:29,440 --> 05:18:32,878 right underneath where we have verified 8096 05:18:32,878 --> 05:18:35,280 the or we've actually decoded the 8097 05:18:35,280 --> 05:18:37,360 refresh token here and we have the 8098 05:18:37,360 --> 05:18:40,080 decoded and now we have no error and 8099 05:18:40,080 --> 05:18:42,000 everything is good so we want to come 8100 05:18:42,000 --> 05:18:44,160 down to this line right before we create 8101 05:18:44,160 --> 05:18:46,400 the token and here once again let's 8102 05:18:46,400 --> 05:18:50,240 define roles and set equal to object 8103 05:18:50,240 --> 05:18:53,280 dot values and we'll pass in our found 8104 05:18:53,280 --> 05:18:56,400 user once again and the roles that are 8105 05:18:56,400 --> 05:18:58,958 associated with that user and now we can 8106 05:18:58,958 --> 05:19:02,080 use the name new namespace as well so 8107 05:19:02,080 --> 05:19:04,798 we'll use user info again 8108 05:19:04,798 --> 05:19:07,680 and then we'll create an object 8109 05:19:07,680 --> 05:19:10,718 and inside this object 8110 05:19:10,718 --> 05:19:12,718 we'll have the username which is the 8111 05:19:12,718 --> 05:19:14,560 decoded username 8112 05:19:14,560 --> 05:19:16,320 but then we'll also 8113 05:19:16,320 --> 05:19:19,280 want to put the roles once again so 8114 05:19:19,280 --> 05:19:21,520 now we'll have roles 8115 05:19:21,520 --> 05:19:24,480 and here we can just pass in roles once 8116 05:19:24,480 --> 05:19:28,000 again i need a comma it looks like and 8117 05:19:28,000 --> 05:19:29,760 now let's save and it will format a 8118 05:19:29,760 --> 05:19:31,680 little bit better and that looks correct 8119 05:19:31,680 --> 05:19:34,878 so we have user info with username 8120 05:19:34,878 --> 05:19:37,360 and roles now that we've updated our 8121 05:19:37,360 --> 05:19:39,920 tokens to include the roles our access 8122 05:19:39,920 --> 05:19:42,160 token specifically we're ready to go to 8123 05:19:42,160 --> 05:19:43,840 middleware because we're going to have 8124 05:19:43,840 --> 05:19:46,080 to create some new middleware to verify 8125 05:19:46,080 --> 05:19:48,240 those roles but first let's look at the 8126 05:19:48,240 --> 05:19:51,520 verified jwt file that we already have 8127 05:19:51,520 --> 05:19:53,120 i'd like to make a couple of quick 8128 05:19:53,120 --> 05:19:55,360 updates one is when we define the auth 8129 05:19:55,360 --> 05:19:58,000 header this works and especially when we 8130 05:19:58,000 --> 05:20:00,240 have control of the front end as well 8131 05:20:00,240 --> 05:20:01,680 and we know we would define 8132 05:20:01,680 --> 05:20:04,400 authorization with a lower case a 8133 05:20:04,400 --> 05:20:06,160 however it can also come in with an 8134 05:20:06,160 --> 05:20:09,920 upper case so instead of this let's go 8135 05:20:09,920 --> 05:20:11,840 ahead and just say 8136 05:20:11,840 --> 05:20:14,718 dot authorization which is essentially 8137 05:20:14,718 --> 05:20:16,320 the same thing that we had but then we 8138 05:20:16,320 --> 05:20:18,718 can also say or and then we could have 8139 05:20:18,718 --> 05:20:21,120 request dot headers 8140 05:20:21,120 --> 05:20:24,080 dot authorization with a capital a as 8141 05:20:24,080 --> 05:20:26,958 well just in case it comes in with a 8142 05:20:26,958 --> 05:20:28,958 capital now we know we're going to grab 8143 05:20:28,958 --> 05:20:30,400 it either way 8144 05:20:30,400 --> 05:20:34,000 after that let's go ahead and change our 8145 05:20:34,000 --> 05:20:36,400 if auth header as well 8146 05:20:36,400 --> 05:20:39,440 and let's look for 8147 05:20:39,440 --> 05:20:42,160 an optional chaining method here so we 8148 05:20:42,160 --> 05:20:45,760 say if we have or if we do not have the 8149 05:20:45,760 --> 05:20:47,920 auth header actually are saying 8150 05:20:47,920 --> 05:20:50,320 and also 8151 05:20:50,320 --> 05:20:51,680 starts 8152 05:20:51,680 --> 05:20:53,280 with 8153 05:20:53,280 --> 05:20:55,360 and here we want to have 8154 05:20:55,360 --> 05:20:57,600 bearer and 8155 05:20:57,600 --> 05:21:01,360 this is supposed to be a capital b by 8156 05:21:01,360 --> 05:21:03,520 the standard so we don't really have to 8157 05:21:03,520 --> 05:21:05,600 look for lowercase and uppercase like we 8158 05:21:05,600 --> 05:21:08,638 were here so we're verifying 8159 05:21:08,638 --> 05:21:09,840 that 8160 05:21:09,840 --> 05:21:12,638 first of all if we do have an off header 8161 05:21:12,638 --> 05:21:14,878 or if it starts with this and we're 8162 05:21:14,878 --> 05:21:16,638 actually looking to say if we don't have 8163 05:21:16,638 --> 05:21:18,558 an auth header and then this optional 8164 05:21:18,558 --> 05:21:20,480 chaining says okay well even if we do 8165 05:21:20,480 --> 05:21:23,360 have an auth header if it doesn't start 8166 05:21:23,360 --> 05:21:25,920 with bearer with a capital b and then 8167 05:21:25,920 --> 05:21:28,718 the space after bear then we're going to 8168 05:21:28,718 --> 05:21:31,120 return a 401 because it is not a 8169 05:21:31,120 --> 05:21:32,840 correctly formed 8170 05:21:32,840 --> 05:21:34,958 authorization header that starts with 8171 05:21:34,958 --> 05:21:36,878 bearer and then the token as it's 8172 05:21:36,878 --> 05:21:39,040 supposed to it looks like we left in a 8173 05:21:39,040 --> 05:21:41,280 console log from the last tutorial let's 8174 05:21:41,280 --> 05:21:42,878 go ahead and take that out where we were 8175 05:21:42,878 --> 05:21:45,040 just viewing the bearer token in the 8176 05:21:45,040 --> 05:21:46,080 console 8177 05:21:46,080 --> 05:21:48,798 and now after we decode 8178 05:21:48,798 --> 05:21:52,558 this token when we're verifying the jwt 8179 05:21:52,558 --> 05:21:54,798 we also want to set the roles here not 8180 05:21:54,798 --> 05:21:57,120 just the user on the request so let's 8181 05:21:57,120 --> 05:21:58,718 have request 8182 05:21:58,718 --> 05:22:02,240 dot roles and we'll set this equal to 8183 05:22:02,240 --> 05:22:03,760 decoded 8184 05:22:03,760 --> 05:22:05,680 and now we've got a different namespace 8185 05:22:05,680 --> 05:22:07,040 so it's dot 8186 05:22:07,040 --> 05:22:09,120 user info 8187 05:22:09,120 --> 05:22:11,760 dot roles and we need to make the same 8188 05:22:11,760 --> 05:22:13,680 change up here when we get the username 8189 05:22:13,680 --> 05:22:17,840 because now it's decoded dot user info 8190 05:22:17,840 --> 05:22:19,520 dot username 8191 05:22:19,520 --> 05:22:21,280 and now we've got the correct namespace 8192 05:22:21,280 --> 05:22:24,480 for both and we can save our verify jwt 8193 05:22:24,480 --> 05:22:26,798 middleware and now it's time to create 8194 05:22:26,798 --> 05:22:28,798 our new middleware i'm going to collapse 8195 05:22:28,798 --> 05:22:30,638 the open editors so we have a little 8196 05:22:30,638 --> 05:22:32,958 more room and in the same middleware 8197 05:22:32,958 --> 05:22:35,200 folder let's create a new file and let's 8198 05:22:35,200 --> 05:22:36,320 call this 8199 05:22:36,320 --> 05:22:39,760 verify roles.js 8200 05:22:39,760 --> 05:22:41,600 inside of verify roles we're going to 8201 05:22:41,600 --> 05:22:45,760 create middleware named verify roles 8202 05:22:45,760 --> 05:22:48,798 and it's going to accept 8203 05:22:48,798 --> 05:22:50,878 a lot of parameters if we want it to 8204 05:22:50,878 --> 05:22:53,920 it's however many roles we want to pass 8205 05:22:53,920 --> 05:22:56,638 in and the way we do that is with the 8206 05:22:56,638 --> 05:22:58,718 rest operator it looks just like the 8207 05:22:58,718 --> 05:23:01,760 spread operator but it lets us pass in 8208 05:23:01,760 --> 05:23:04,798 as many parameters as we wish and we're 8209 05:23:04,798 --> 05:23:07,680 just going to call them allowed 8210 05:23:07,680 --> 05:23:09,840 roles 8211 05:23:09,840 --> 05:23:12,480 now from there we need to go ahead and 8212 05:23:12,480 --> 05:23:15,280 have a middleware function and you know 8213 05:23:15,280 --> 05:23:16,638 that takes 8214 05:23:16,638 --> 05:23:19,680 a request response and next so what we 8215 05:23:19,680 --> 05:23:22,638 need to do and this allows us to pass in 8216 05:23:22,638 --> 05:23:25,040 the allowed roles by having this on the 8217 05:23:25,040 --> 05:23:28,320 outside but we need to return 8218 05:23:28,320 --> 05:23:30,000 a middleware function essentially an 8219 05:23:30,000 --> 05:23:33,360 anonymous function here with request 8220 05:23:33,360 --> 05:23:34,878 response 8221 05:23:34,878 --> 05:23:37,440 and next 8222 05:23:37,440 --> 05:23:39,440 and now inside of this function we'll 8223 05:23:39,440 --> 05:23:41,040 have everything that our middleware 8224 05:23:41,040 --> 05:23:42,958 would do so the first thing we need to 8225 05:23:42,958 --> 05:23:45,280 do is say if 8226 05:23:45,280 --> 05:23:48,480 we do not have a request which we should 8227 05:23:48,480 --> 05:23:51,200 because our verified jwt will come 8228 05:23:51,200 --> 05:23:52,798 before this 8229 05:23:52,798 --> 05:23:55,520 but let's go ahead and do that just to 8230 05:23:55,520 --> 05:23:57,840 be thorough and then let's use optional 8231 05:23:57,840 --> 05:24:00,958 chaining to say okay even if we do have 8232 05:24:00,958 --> 05:24:04,320 a request it needs to have roles 8233 05:24:04,320 --> 05:24:05,120 or 8234 05:24:05,120 --> 05:24:07,440 this should not be valid and if it's not 8235 05:24:07,440 --> 05:24:08,400 valid 8236 05:24:08,400 --> 05:24:10,878 we're just going to return a response 8237 05:24:10,878 --> 05:24:12,400 and send 8238 05:24:12,400 --> 05:24:13,840 the status 8239 05:24:13,840 --> 05:24:17,040 401 which is unauthorized now we need to 8240 05:24:17,040 --> 05:24:19,600 define a roles array let's keep that 8241 05:24:19,600 --> 05:24:22,718 lowercase camelcase actually rolls array 8242 05:24:22,718 --> 05:24:24,958 and set this equal to 8243 05:24:24,958 --> 05:24:28,000 the allowed roles that were passed in 8244 05:24:28,000 --> 05:24:29,760 now we're spreading those into a new 8245 05:24:29,760 --> 05:24:31,040 array here 8246 05:24:31,040 --> 05:24:33,360 now that we have this let's go ahead and 8247 05:24:33,360 --> 05:24:36,080 log this to the console these will be 8248 05:24:36,080 --> 05:24:38,080 the different roles that we're passing 8249 05:24:38,080 --> 05:24:39,920 in of course there will be the codes 8250 05:24:39,920 --> 05:24:42,400 associated with the roles but let's go 8251 05:24:42,400 --> 05:24:45,280 ahead and log roles array so when we go 8252 05:24:45,280 --> 05:24:47,120 ahead and test this we can see 8253 05:24:47,120 --> 05:24:49,200 everything that we expect to and we'll 8254 05:24:49,200 --> 05:24:52,400 be comparing this to the request dot 8255 05:24:52,400 --> 05:24:54,798 roles that we just set 8256 05:24:54,798 --> 05:24:58,320 inside of the verified jwt that will be 8257 05:24:58,320 --> 05:25:00,798 executed as middleware before the 8258 05:25:00,798 --> 05:25:03,200 verifies roles middleware 8259 05:25:03,200 --> 05:25:05,200 okay so we'll log both of those just so 8260 05:25:05,200 --> 05:25:07,360 we can see what's going on but now we 8261 05:25:07,360 --> 05:25:08,240 know 8262 05:25:08,240 --> 05:25:10,798 we have an array 8263 05:25:10,798 --> 05:25:13,280 of user roles that are coming from the 8264 05:25:13,280 --> 05:25:15,440 jwt 8265 05:25:15,440 --> 05:25:17,840 and then we have the roles that are 8266 05:25:17,840 --> 05:25:20,160 passed in that will be allowed and 8267 05:25:20,160 --> 05:25:21,680 that's what we're going to compare so 8268 05:25:21,680 --> 05:25:24,558 we're comparing arrays and i'm going to 8269 05:25:24,558 --> 05:25:27,120 define just a result here 8270 05:25:27,120 --> 05:25:30,080 and let's set this equal to request 8271 05:25:30,080 --> 05:25:31,760 dot 8272 05:25:31,760 --> 05:25:35,840 dot map which map creates a new array 8273 05:25:35,840 --> 05:25:38,240 and we'll have a role 8274 05:25:38,240 --> 05:25:41,040 and for each role essentially we'll 8275 05:25:41,040 --> 05:25:43,040 compare to the roles 8276 05:25:43,040 --> 05:25:44,400 array 8277 05:25:44,400 --> 05:25:46,558 and we'll see if the roles array 8278 05:25:46,558 --> 05:25:48,320 includes 8279 05:25:48,320 --> 05:25:50,878 the role that we're passing in and if it 8280 05:25:50,878 --> 05:25:53,840 does it will return true 8281 05:25:53,840 --> 05:25:56,000 that's what includes does and if not it 8282 05:25:56,000 --> 05:25:58,240 will return false it's a boolean so 8283 05:25:58,240 --> 05:26:00,878 we'll have a new array and we'll have a 8284 05:26:00,878 --> 05:26:03,440 true or false for every 8285 05:26:03,440 --> 05:26:05,360 thing that was in 8286 05:26:05,360 --> 05:26:07,680 the request role so 8287 05:26:07,680 --> 05:26:09,360 if we had three different roles here 8288 05:26:09,360 --> 05:26:11,600 we'll have possibly true true true or 8289 05:26:11,600 --> 05:26:13,200 true false true 8290 05:26:13,200 --> 05:26:15,040 who knows for sure but that's what we'll 8291 05:26:15,040 --> 05:26:17,600 have so we need to filter this array 8292 05:26:17,600 --> 05:26:20,558 and all we need is one true to know that 8293 05:26:20,558 --> 05:26:21,600 the role 8294 05:26:21,600 --> 05:26:23,280 can access 8295 05:26:23,280 --> 05:26:25,040 the route that we're 8296 05:26:25,040 --> 05:26:27,520 verifying so what we want to do then is 8297 05:26:27,520 --> 05:26:29,280 chain 8298 05:26:29,280 --> 05:26:33,520 find and we can just say for each value 8299 05:26:33,520 --> 05:26:36,000 the true and false in the new array that 8300 05:26:36,000 --> 05:26:38,638 was mapped 8301 05:26:38,718 --> 05:26:42,000 each value will check the value to see 8302 05:26:42,000 --> 05:26:45,040 if it's equal to true 8303 05:26:45,040 --> 05:26:47,280 and if it is 8304 05:26:47,280 --> 05:26:49,280 it will return if it finds a match it 8305 05:26:49,280 --> 05:26:51,200 will return the very first one it finds 8306 05:26:51,200 --> 05:26:53,520 or if it finds no matches then we won't 8307 05:26:53,520 --> 05:26:55,760 have a result and so that's how this 8308 05:26:55,760 --> 05:26:57,600 works i'm using two higher order 8309 05:26:57,600 --> 05:26:59,840 functions here essentially so we're 8310 05:26:59,840 --> 05:27:01,600 mapping over 8311 05:27:01,600 --> 05:27:04,480 the roles that are sent 8312 05:27:04,480 --> 05:27:07,440 from the jwt and they're assigned in the 8313 05:27:07,440 --> 05:27:11,040 verified jwt to request roles and we're 8314 05:27:11,040 --> 05:27:13,120 mapping those comparing them getting 8315 05:27:13,120 --> 05:27:14,798 true and false 8316 05:27:14,798 --> 05:27:17,280 results back to the roles array that 8317 05:27:17,280 --> 05:27:20,080 will be passed into this route so we'll 8318 05:27:20,080 --> 05:27:21,600 pass in 8319 05:27:21,600 --> 05:27:24,080 however many roles we want to this array 8320 05:27:24,080 --> 05:27:26,080 could possibly only have one role or it 8321 05:27:26,080 --> 05:27:28,240 might have three or four or however many 8322 05:27:28,240 --> 05:27:30,240 we're checking for or want to allow to 8323 05:27:30,240 --> 05:27:31,440 this route 8324 05:27:31,440 --> 05:27:33,200 so once we compare those and get all the 8325 05:27:33,200 --> 05:27:36,080 true false results we're just using find 8326 05:27:36,080 --> 05:27:38,958 to say hey find the first true 8327 05:27:38,958 --> 05:27:41,200 and if there is any truths it will be 8328 05:27:41,200 --> 05:27:43,680 good and if there's not it won't and 8329 05:27:43,680 --> 05:27:46,160 that's what we'll do next so we'll just 8330 05:27:46,160 --> 05:27:47,440 say if 8331 05:27:47,440 --> 05:27:49,600 there's no result 8332 05:27:49,600 --> 05:27:51,200 essentially we did not find a true 8333 05:27:51,200 --> 05:27:53,600 result we're going to return 8334 05:27:53,600 --> 05:27:55,600 and then we'll say res 8335 05:27:55,600 --> 05:27:57,760 send status 8336 05:27:57,760 --> 05:28:01,120 and again we'll send a 401 unauthorized 8337 05:28:01,120 --> 05:28:04,000 otherwise we'll just call next because 8338 05:28:04,000 --> 05:28:05,920 everything's good and we're ready to 8339 05:28:05,920 --> 05:28:07,840 move on and we're going to let the route 8340 05:28:07,840 --> 05:28:10,558 be accessed so let's save our middleware 8341 05:28:10,558 --> 05:28:12,240 and we'll know when it runs we'll see 8342 05:28:12,240 --> 05:28:14,638 the arrays that we're comparing 8343 05:28:14,638 --> 05:28:17,360 in the console and before i forget we 8344 05:28:17,360 --> 05:28:21,840 also need to add module dot exports 8345 05:28:21,840 --> 05:28:24,718 and set that equal to verify 8346 05:28:24,718 --> 05:28:26,718 roles and save 8347 05:28:26,718 --> 05:28:28,878 now let's collapse the middleware folder 8348 05:28:28,878 --> 05:28:31,040 and open up the routes folder and then 8349 05:28:31,040 --> 05:28:34,120 the api folder and let's go to our 8350 05:28:34,120 --> 05:28:36,400 employees.js that has the different 8351 05:28:36,400 --> 05:28:39,600 routes get post put and delete we have a 8352 05:28:39,600 --> 05:28:42,160 couple of imports to make here so 8353 05:28:42,160 --> 05:28:44,638 let's go ahead and define our 8354 05:28:44,638 --> 05:28:46,400 roles list 8355 05:28:46,400 --> 05:28:49,120 and set this equal to require 8356 05:28:49,120 --> 05:28:51,200 and now we need to go up 8357 05:28:51,200 --> 05:28:53,760 and up again and then we'll look in the 8358 05:28:53,760 --> 05:28:56,400 config folder and then we'll find our 8359 05:28:56,400 --> 05:28:58,958 roles list 8360 05:28:58,958 --> 05:29:00,718 and after that 8361 05:29:00,718 --> 05:29:04,000 we need to go ahead and define our 8362 05:29:04,000 --> 05:29:06,718 verify roles middleware 8363 05:29:06,718 --> 05:29:09,360 set this equal to 8364 05:29:09,360 --> 05:29:12,480 up a folder up another folder middleware 8365 05:29:12,480 --> 05:29:17,280 and now we're going to verify roles 8366 05:29:17,280 --> 05:29:19,280 and i'll save just because i've 8367 05:29:19,280 --> 05:29:21,680 completed the imports but now we need to 8368 05:29:21,680 --> 05:29:24,638 add this to the different routes now 8369 05:29:24,638 --> 05:29:27,120 let's just leave the get route open 8370 05:29:27,120 --> 05:29:28,080 because 8371 05:29:28,080 --> 05:29:28,878 any 8372 05:29:28,878 --> 05:29:31,040 one could access that or if we wanted to 8373 05:29:31,040 --> 05:29:33,280 put verify roles and at least verify 8374 05:29:33,280 --> 05:29:36,320 their user but they already have to have 8375 05:29:36,320 --> 05:29:39,920 the jwt because we required that ahead 8376 05:29:39,920 --> 05:29:41,360 of time so 8377 05:29:41,360 --> 05:29:43,600 they'll have to have the jwt to access 8378 05:29:43,600 --> 05:29:45,920 the git route so kind of the user is the 8379 05:29:45,920 --> 05:29:48,718 default but after that 8380 05:29:48,718 --> 05:29:52,160 in the post route let's put in 8381 05:29:52,160 --> 05:29:53,360 verify 8382 05:29:53,360 --> 05:29:55,280 roles 8383 05:29:55,280 --> 05:29:57,680 and now we can pass in 8384 05:29:57,680 --> 05:29:59,040 the different 8385 05:29:59,040 --> 05:30:00,240 roles list 8386 05:30:00,240 --> 05:30:03,280 values that we want to let access 8387 05:30:03,280 --> 05:30:06,480 this route so we'll type 8388 05:30:06,480 --> 05:30:09,840 roles underscore list and now dot 8389 05:30:09,840 --> 05:30:11,680 admin will work 8390 05:30:11,680 --> 05:30:12,718 remember 8391 05:30:12,718 --> 05:30:15,040 this is the key and then the code is the 8392 05:30:15,040 --> 05:30:17,520 value so admin is the key 8393 05:30:17,520 --> 05:30:19,840 and then we could also put in 8394 05:30:19,840 --> 05:30:23,360 roles underscore list dot 8395 05:30:23,360 --> 05:30:24,878 editor 8396 05:30:24,878 --> 05:30:27,040 and that makes sense because you could 8397 05:30:27,040 --> 05:30:28,958 have an editor that could post a new 8398 05:30:28,958 --> 05:30:31,360 value as well okay then i'm going to 8399 05:30:31,360 --> 05:30:34,558 copy the verify roles that we just added 8400 05:30:34,558 --> 05:30:35,840 here 8401 05:30:35,840 --> 05:30:36,638 and 8402 05:30:36,638 --> 05:30:39,040 it's like maybe i'll put a space here 8403 05:30:39,040 --> 05:30:42,718 and then input i'll do the same thing 8404 05:30:42,718 --> 05:30:45,600 and add the space but now in delete 8405 05:30:45,600 --> 05:30:47,360 let's say only 8406 05:30:47,360 --> 05:30:50,958 an admin can delete anything from our 8407 05:30:50,958 --> 05:30:53,280 database so we'll save that which gives 8408 05:30:53,280 --> 05:30:55,840 it just a little bit of a change so now 8409 05:30:55,840 --> 05:30:58,320 everyone can access the get route 8410 05:30:58,320 --> 05:31:01,520 but the post route should be limited to 8411 05:31:01,520 --> 05:31:04,400 any user that's an admin or an editor 8412 05:31:04,400 --> 05:31:07,280 the same for the put route but then the 8413 05:31:07,280 --> 05:31:10,400 delete route could only be used by an 8414 05:31:10,400 --> 05:31:12,878 admin i think we're ready to test this 8415 05:31:12,878 --> 05:31:15,920 out now so let's go ahead and open a new 8416 05:31:15,920 --> 05:31:17,600 terminal window you can do that from the 8417 05:31:17,600 --> 05:31:20,320 terminal menu or i'll just use control 8418 05:31:20,320 --> 05:31:22,718 and backtick i'm on windows and i'll 8419 05:31:22,718 --> 05:31:24,638 type npn 8420 05:31:24,638 --> 05:31:25,760 run 8421 05:31:25,760 --> 05:31:28,400 dev to get our dev server up and running 8422 05:31:28,400 --> 05:31:30,000 with our api 8423 05:31:30,000 --> 05:31:31,760 and then we'll test this out with 8424 05:31:31,760 --> 05:31:33,840 thunder client okay i'm going to drag 8425 05:31:33,840 --> 05:31:35,120 the terminal 8426 05:31:35,120 --> 05:31:36,798 window up just a little give it some 8427 05:31:36,798 --> 05:31:39,040 more room so we can see the console log 8428 05:31:39,040 --> 05:31:41,520 notices that we get and from there i'm 8429 05:31:41,520 --> 05:31:43,440 going to click on thunder client here on 8430 05:31:43,440 --> 05:31:45,040 the left if you don't have thunder 8431 05:31:45,040 --> 05:31:47,600 client installed you can get it through 8432 05:31:47,600 --> 05:31:50,080 the extension over here and then just 8433 05:31:50,080 --> 05:31:52,240 search for 8434 05:31:52,240 --> 05:31:54,878 under client 8435 05:31:54,878 --> 05:31:57,040 and there you see it 8436 05:31:57,040 --> 05:31:58,878 and now that you have it or once you 8437 05:31:58,878 --> 05:32:00,480 have it you'll be able to click on it 8438 05:32:00,480 --> 05:32:02,160 over here on the left circle with the 8439 05:32:02,160 --> 05:32:03,520 lightning bolt 8440 05:32:03,520 --> 05:32:06,000 and you can create collections and of 8441 05:32:06,000 --> 05:32:07,600 course we've done this in some previous 8442 05:32:07,600 --> 05:32:09,840 tutorials if you have followed along in 8443 05:32:09,840 --> 05:32:12,480 the series i've got an auth collection 8444 05:32:12,480 --> 05:32:13,920 here and the very first thing we'll need 8445 05:32:13,920 --> 05:32:16,958 to do is authenticate a user let's look 8446 05:32:16,958 --> 05:32:18,160 at which user we're going to 8447 05:32:18,160 --> 05:32:21,040 authenticate first let's just do dave 8448 05:32:21,040 --> 05:32:22,798 one remember 8449 05:32:22,798 --> 05:32:26,638 this user only has the user permissions 8450 05:32:26,638 --> 05:32:28,638 it does not have an editor 8451 05:32:28,638 --> 05:32:32,400 or an admin permission so i'll send 8452 05:32:32,400 --> 05:32:35,200 and i get the access token 8453 05:32:35,200 --> 05:32:37,600 i'll copy this access token 8454 05:32:37,600 --> 05:32:39,920 and now when i go to 8455 05:32:39,920 --> 05:32:42,718 the employees api and go to get 8456 05:32:42,718 --> 05:32:44,958 employees 8457 05:32:44,958 --> 05:32:46,718 i'll go to auth here 8458 05:32:46,718 --> 05:32:50,320 paste in the new access token and send 8459 05:32:50,320 --> 05:32:52,638 and everything is good but if i do this 8460 05:32:52,638 --> 05:32:55,520 for post if i'm in time within the 30 8461 05:32:55,520 --> 05:32:57,280 seconds it was given 8462 05:32:57,280 --> 05:33:00,798 and send i got a 401 unauthorized so 8463 05:33:00,798 --> 05:33:03,200 that means our verify rolls is working 8464 05:33:03,200 --> 05:33:05,120 as we look down here 8465 05:33:05,120 --> 05:33:06,798 we've got the two different arrays 8466 05:33:06,798 --> 05:33:09,440 logged to the console too so 8467 05:33:09,440 --> 05:33:10,558 this 8468 05:33:10,558 --> 05:33:12,798 is the array that has the roles that we 8469 05:33:12,798 --> 05:33:15,760 were looking for that we passed in 5150 8470 05:33:15,760 --> 05:33:19,520 is the admin 1984 is the editor but you 8471 05:33:19,520 --> 05:33:22,558 can see our user only had role 2001 8472 05:33:22,558 --> 05:33:25,440 which is just a user okay now let's go 8473 05:33:25,440 --> 05:33:28,240 back to my auth collection here and the 8474 05:33:28,240 --> 05:33:30,558 auth route and let's change 8475 05:33:30,558 --> 05:33:32,798 who we're getting a token for so now 8476 05:33:32,798 --> 05:33:34,000 this will be 8477 05:33:34,000 --> 05:33:36,480 walt 2 he is an editor 8478 05:33:36,480 --> 05:33:37,600 and 8479 05:33:37,600 --> 05:33:40,718 also a user but he is not an admin so we 8480 05:33:40,718 --> 05:33:43,600 can send this we get a new token i'll 8481 05:33:43,600 --> 05:33:45,920 copy the token 8482 05:33:45,920 --> 05:33:48,000 and now we can go back to the employees 8483 05:33:48,000 --> 05:33:51,040 api and we can post a new employee or we 8484 05:33:51,040 --> 05:33:52,958 should be able to we'll put in the new 8485 05:33:52,958 --> 05:33:54,400 token here 8486 05:33:54,400 --> 05:33:58,718 and send and yes we posted john doe so 8487 05:33:58,718 --> 05:34:01,120 he's now number three has the id of 8488 05:34:01,120 --> 05:34:03,760 three in the employees list but if we go 8489 05:34:03,760 --> 05:34:05,840 to the delete route 8490 05:34:05,840 --> 05:34:08,320 and go to auth i'll post in the same 8491 05:34:08,320 --> 05:34:11,200 token and attempt to delete 8492 05:34:11,200 --> 05:34:13,040 and oh we're out of time we got 8493 05:34:13,040 --> 05:34:16,520 forbidden so let me go ahead and try to 8494 05:34:16,520 --> 05:34:19,040 re-verify here 8495 05:34:19,040 --> 05:34:21,600 we once again got walt 2 8496 05:34:21,600 --> 05:34:25,320 we get a new token 8497 05:34:25,360 --> 05:34:27,440 and get that new token once again i 8498 05:34:27,440 --> 05:34:28,878 didn't copy it right the first time 8499 05:34:28,878 --> 05:34:32,000 there we go back to employees now we'll 8500 05:34:32,000 --> 05:34:34,480 attempt to delete i'll pass in the auth 8501 05:34:34,480 --> 05:34:35,760 token 8502 05:34:35,760 --> 05:34:39,520 and send and we got a 401 unauthorized 8503 05:34:39,520 --> 05:34:41,360 and that is because the role did not 8504 05:34:41,360 --> 05:34:44,638 verify now you can see you had to have 8505 05:34:44,638 --> 05:34:48,000 the 5150 code which is admin that was 8506 05:34:48,000 --> 05:34:49,680 passed into the route 8507 05:34:49,680 --> 05:34:50,558 but 8508 05:34:50,558 --> 05:34:55,040 walt 2 only had the 2001 and the 1984 8509 05:34:55,040 --> 05:34:58,000 code in his roles list okay once again 8510 05:34:58,000 --> 05:35:00,958 let's go back to the auth route now and 8511 05:35:00,958 --> 05:35:02,958 we will change to where we are 8512 05:35:02,958 --> 05:35:04,320 authorizing 8513 05:35:04,320 --> 05:35:07,520 walt one walt one should be an admin as 8514 05:35:07,520 --> 05:35:09,920 well as a user and an editor 8515 05:35:09,920 --> 05:35:14,718 and so we'll send that get a new token 8516 05:35:14,718 --> 05:35:16,798 and now we can go back to our employees 8517 05:35:16,798 --> 05:35:19,280 delete route that was just denied to 8518 05:35:19,280 --> 05:35:20,638 walt to 8519 05:35:20,638 --> 05:35:22,480 paste in this new token 8520 05:35:22,480 --> 05:35:24,638 and attempt to send the delete request 8521 05:35:24,638 --> 05:35:27,360 and employee id 3 is not found that's 8522 05:35:27,360 --> 05:35:30,320 because nodemon restarted i believe and 8523 05:35:30,320 --> 05:35:32,400 of course employee id 3 isn't in there 8524 05:35:32,400 --> 05:35:34,718 so let me go ahead and request another 8525 05:35:34,718 --> 05:35:37,840 token and we'll see if we can create id3 8526 05:35:37,840 --> 05:35:41,120 again quickly and then delete it on the 8527 05:35:41,120 --> 05:35:43,680 same so here's our new token for our 8528 05:35:43,680 --> 05:35:46,638 admin user 8529 05:35:47,600 --> 05:35:52,718 copy that go to the employees post route 8530 05:35:52,878 --> 05:35:56,000 paste in the auth and created now let's 8531 05:35:56,000 --> 05:35:58,878 go to delete 8532 05:35:59,040 --> 05:36:01,440 paste in the new auth and we deleted 8533 05:36:01,440 --> 05:36:03,760 with an okay so i just had to be quick 8534 05:36:03,760 --> 05:36:05,760 enough to beat the 30 seconds with that 8535 05:36:05,760 --> 05:36:09,040 token but here you can see the user 8536 05:36:09,040 --> 05:36:11,280 our walt one user had 8537 05:36:11,280 --> 05:36:17,120 2001 1984 and 5150 in his roles and 8538 05:36:17,120 --> 05:36:20,400 the api route needed the 5150 so when it 8539 05:36:20,400 --> 05:36:22,718 matched everything was good hey just a 8540 05:36:22,718 --> 05:36:24,718 quick note on testing with thunder 8541 05:36:24,718 --> 05:36:27,120 client and i want to point this out in 8542 05:36:27,120 --> 05:36:28,958 the controllers 8543 05:36:28,958 --> 05:36:31,600 and then under the auth controller but 8544 05:36:31,600 --> 05:36:33,680 when you're testing with thunder client 8545 05:36:33,680 --> 05:36:37,440 it honors the cookie setting for secure 8546 05:36:37,440 --> 05:36:40,080 true or not so if we were to set a 8547 05:36:40,080 --> 05:36:42,558 cookie and then use the refresh token 8548 05:36:42,558 --> 05:36:45,520 here you would need to remove 8549 05:36:45,520 --> 05:36:47,760 this or the cookie would not work with 8550 05:36:47,760 --> 05:36:50,400 thunder client however this is required 8551 05:36:50,400 --> 05:36:53,120 as i noted in a previous tutorial when 8552 05:36:53,120 --> 05:36:55,280 working with chrome so 8553 05:36:55,280 --> 05:36:57,280 just a note if you're testing the 8554 05:36:57,280 --> 05:37:00,160 refresh endpoint with a refresh cookie 8555 05:37:00,160 --> 05:37:02,558 you'll have to comment this part out or 8556 05:37:02,558 --> 05:37:04,080 at least take it out for testing 8557 05:37:04,080 --> 05:37:06,240 purposes with thunder client but then 8558 05:37:06,240 --> 05:37:08,320 when you work with chrome and in 8559 05:37:08,320 --> 05:37:11,520 production you both want that secure 8560 05:37:11,520 --> 05:37:13,600 true back in here when you create that 8561 05:37:13,600 --> 05:37:15,840 refresh token that is saved in a cookie 8562 05:37:15,840 --> 05:37:18,160 we did not use that today and the 8563 05:37:18,160 --> 05:37:20,798 refresh token does not store any 8564 05:37:20,798 --> 05:37:22,958 information about the user roles and it 8565 05:37:22,958 --> 05:37:25,360 shouldn't however just wanted to note 8566 05:37:25,360 --> 05:37:27,360 that for you in case you were testing 8567 05:37:27,360 --> 05:37:29,200 out that refresh token with thunder 8568 05:37:29,200 --> 05:37:31,360 client so there you have it we created 8569 05:37:31,360 --> 05:37:34,160 our verify roles middleware and you can 8570 05:37:34,160 --> 05:37:34,958 see 8571 05:37:34,958 --> 05:37:36,638 both of those that we've been logging to 8572 05:37:36,638 --> 05:37:38,320 the console i'll go ahead and delete 8573 05:37:38,320 --> 05:37:40,400 those now before i commit it to github 8574 05:37:40,400 --> 05:37:42,638 and get the console logs out of there 8575 05:37:42,638 --> 05:37:45,440 there's our verify roles middleware and 8576 05:37:45,440 --> 05:37:47,760 you of course saw how we applied it to 8577 05:37:47,760 --> 05:37:49,920 the routes in our api 8578 05:37:49,920 --> 05:37:52,558 for our employees and you can just pass 8579 05:37:52,558 --> 05:37:53,360 in 8580 05:37:53,360 --> 05:37:56,400 the different roles that you want to let 8581 05:37:56,400 --> 05:37:57,600 access 8582 05:37:57,600 --> 05:38:00,400 that given route 8583 05:38:00,400 --> 05:38:03,360 mongodb is the m in the mern stack it 8584 05:38:03,360 --> 05:38:05,840 represents the database in the stack the 8585 05:38:05,840 --> 05:38:08,160 front end application of the mirn stack 8586 05:38:08,160 --> 05:38:10,958 is handled by react along with nodejs 8587 05:38:10,958 --> 05:38:13,760 and express mongodb completes the 8588 05:38:13,760 --> 05:38:15,760 backend rest api 8589 05:38:15,760 --> 05:38:18,638 traditional sql databases are built in a 8590 05:38:18,638 --> 05:38:20,480 relational structure 8591 05:38:20,480 --> 05:38:22,798 related tables reference each other with 8592 05:38:22,798 --> 05:38:25,200 joins as data is queried these 8593 05:38:25,200 --> 05:38:27,680 relational tables also normalize the 8594 05:38:27,680 --> 05:38:30,400 data that means data is not duplicated 8595 05:38:30,400 --> 05:38:32,638 in the tables that's the dry principle 8596 05:38:32,638 --> 05:38:34,958 which stands for don't repeat yourself 8597 05:38:34,958 --> 05:38:36,958 and that's applied to the structure 8598 05:38:36,958 --> 05:38:39,280 however with no sql databases like 8599 05:38:39,280 --> 05:38:42,638 mongodb you can throw all of that out 8600 05:38:42,638 --> 05:38:45,840 mongodb stores data in collections the 8601 05:38:45,840 --> 05:38:47,600 individual records in the collections 8602 05:38:47,600 --> 05:38:50,160 are called documents the documents have 8603 05:38:50,160 --> 05:38:52,240 a key value structure and look a lot 8604 05:38:52,240 --> 05:38:55,280 like json a collection holds all of the 8605 05:38:55,280 --> 05:38:58,000 data about a user for example instead of 8606 05:38:58,000 --> 05:39:00,480 breaking it into related tables and 8607 05:39:00,480 --> 05:39:02,638 likewise duplicating and distributing 8608 05:39:02,638 --> 05:39:04,638 the data where deemed necessary in a 8609 05:39:04,638 --> 05:39:07,040 nosql structure is permitted 8610 05:39:07,040 --> 05:39:09,760 so why choose nosql databases and what 8611 05:39:09,760 --> 05:39:12,878 are the advantages of using mongodb 8612 05:39:12,878 --> 05:39:14,798 performance is key 8613 05:39:14,798 --> 05:39:16,558 the speed at which a collection is 8614 05:39:16,558 --> 05:39:18,400 queried is very fast 8615 05:39:18,400 --> 05:39:21,280 flexibility it's very easy to make 8616 05:39:21,280 --> 05:39:23,360 structural changes like adding a new 8617 05:39:23,360 --> 05:39:25,520 field without wreaking havoc in your 8618 05:39:25,520 --> 05:39:28,320 total structure it is much like adding a 8619 05:39:28,320 --> 05:39:31,040 new property to an object 8620 05:39:31,040 --> 05:39:32,400 scalability 8621 05:39:32,400 --> 05:39:34,958 nosql can support large databases with 8622 05:39:34,958 --> 05:39:38,000 high request rates at a very low latency 8623 05:39:38,000 --> 05:39:40,558 and finally usability as you'll see 8624 05:39:40,558 --> 05:39:42,638 today we can get up and running with 8625 05:39:42,638 --> 05:39:45,760 mongodb in the cloud very fast 8626 05:39:45,760 --> 05:39:47,200 let's get started by going to 8627 05:39:47,200 --> 05:39:49,440 mongodb.com 8628 05:39:49,440 --> 05:39:50,680 okay we're at 8629 05:39:50,680 --> 05:39:52,798 mongodb.com and i already have an 8630 05:39:52,798 --> 05:39:54,958 account so i'm going to choose sign in 8631 05:39:54,958 --> 05:39:56,558 if you don't have an account you'll want 8632 05:39:56,558 --> 05:39:58,878 to sign up for a free account so you can 8633 05:39:58,878 --> 05:40:01,520 just click try free i'm going to click 8634 05:40:01,520 --> 05:40:02,638 sign in 8635 05:40:02,638 --> 05:40:04,878 and then it will take me to a page that 8636 05:40:04,878 --> 05:40:08,000 probably uses my google id or allows me 8637 05:40:08,000 --> 05:40:09,840 to log in with an email address yes 8638 05:40:09,840 --> 05:40:12,320 there it is so you get those options i'm 8639 05:40:12,320 --> 05:40:14,638 going to log in with my google email 8640 05:40:14,638 --> 05:40:16,878 address or my google account 8641 05:40:16,878 --> 05:40:19,440 and then we'll meet back up after you 8642 05:40:19,440 --> 05:40:21,680 have your account or you've signed into 8643 05:40:21,680 --> 05:40:22,878 your account 8644 05:40:22,878 --> 05:40:25,840 okay i'm signed into my account and i'm 8645 05:40:25,840 --> 05:40:27,920 on the projects page where i can create 8646 05:40:27,920 --> 05:40:30,000 a new project and you see i already have 8647 05:40:30,000 --> 05:40:32,638 one project here now if you're not on 8648 05:40:32,638 --> 05:40:34,638 this page once you're signed in just 8649 05:40:34,638 --> 05:40:36,638 click the little leaf in the top left 8650 05:40:36,638 --> 05:40:38,480 and it says view the organization home 8651 05:40:38,480 --> 05:40:40,958 because that's where this is so once 8652 05:40:40,958 --> 05:40:43,200 you're there you'll want to create a new 8653 05:40:43,200 --> 05:40:45,120 project 8654 05:40:45,120 --> 05:40:47,440 and now you want to name the project i'm 8655 05:40:47,440 --> 05:40:49,360 just going to name this one 8656 05:40:49,360 --> 05:40:50,798 8657 05:40:50,798 --> 05:40:54,160 tuts short for tutorial and click next 8658 05:40:54,160 --> 05:40:57,040 and then it asks you to go ahead and set 8659 05:40:57,040 --> 05:40:59,200 permissions or members and it'll 8660 05:40:59,200 --> 05:41:01,360 probably assign that to your default 8661 05:41:01,360 --> 05:41:02,958 account to start out with as you can see 8662 05:41:02,958 --> 05:41:04,878 i have project owner right here so i'm 8663 05:41:04,878 --> 05:41:06,878 going to click create project with the 8664 05:41:06,878 --> 05:41:09,120 project created you can see i now have 8665 05:41:09,120 --> 05:41:11,120 tuts up here above database 8666 05:41:11,120 --> 05:41:13,840 deployments we need to build a database 8667 05:41:13,840 --> 05:41:15,360 and of course it gives you the big shiny 8668 05:41:15,360 --> 05:41:16,958 button right in the middle to do so so 8669 05:41:16,958 --> 05:41:18,400 please click that 8670 05:41:18,400 --> 05:41:20,718 and then it gives you choices and we're 8671 05:41:20,718 --> 05:41:22,958 just going to go with free today if you 8672 05:41:22,958 --> 05:41:24,798 want to get one that you pay for that's 8673 05:41:24,798 --> 05:41:27,360 fine i'm going to choose free over here 8674 05:41:27,360 --> 05:41:29,440 on the right 8675 05:41:29,440 --> 05:41:31,760 and then it will say create a shared 8676 05:41:31,760 --> 05:41:32,958 cluster 8677 05:41:32,958 --> 05:41:35,440 and right now i'm just going to keep all 8678 05:41:35,440 --> 05:41:39,280 the defaults so free shared it has aws 8679 05:41:39,280 --> 05:41:41,520 it has one of the usa 8680 05:41:41,520 --> 05:41:43,920 regions for me because i'm in the u.s 8681 05:41:43,920 --> 05:41:45,360 you may want to pick a different one if 8682 05:41:45,360 --> 05:41:47,600 you're not and maybe it already defaults 8683 05:41:47,600 --> 05:41:49,520 to something close to you 8684 05:41:49,520 --> 05:41:51,200 and then it just has these other default 8685 05:41:51,200 --> 05:41:52,638 settings and i'm going to go with all of 8686 05:41:52,638 --> 05:41:54,638 that even the default name here cluster 8687 05:41:54,638 --> 05:41:57,840 0 and click create cluster at the bottom 8688 05:41:57,840 --> 05:42:00,558 now it says new clusters take between 1 8689 05:42:00,558 --> 05:42:02,878 to 3 minutes to provision so i'll come 8690 05:42:02,878 --> 05:42:05,840 back when this is finished my cluster 0 8691 05:42:05,840 --> 05:42:08,320 has now been created and we're given 8692 05:42:08,320 --> 05:42:10,160 this screen so what we want to do is 8693 05:42:10,160 --> 05:42:12,878 click browse collections 8694 05:42:12,878 --> 05:42:14,798 we don't have any collections yet so we 8695 05:42:14,798 --> 05:42:17,040 get this and it says load a sample data 8696 05:42:17,040 --> 05:42:19,200 set or add my own data we're going to 8697 05:42:19,200 --> 05:42:22,000 choose add my own data 8698 05:42:22,000 --> 05:42:24,638 with that it asks for a database name 8699 05:42:24,638 --> 05:42:26,878 and a collection name so let's just call 8700 05:42:26,878 --> 05:42:29,200 this 8701 05:42:29,200 --> 05:42:31,360 company db 8702 05:42:31,360 --> 05:42:33,920 and then we can call the collection name 8703 05:42:33,920 --> 05:42:35,440 let's go with 8704 05:42:35,440 --> 05:42:37,440 employees and i'll just keep that all 8705 05:42:37,440 --> 05:42:39,920 lowercase companydb 8706 05:42:39,920 --> 05:42:43,200 is capital company and capitaldb at the 8707 05:42:43,200 --> 05:42:47,638 end and i'll click create 8708 05:42:49,120 --> 05:42:52,000 and with that has created our 8709 05:42:52,000 --> 05:42:55,280 companydb database and our empty 8710 05:42:55,280 --> 05:42:58,240 employees collection right here what we 8711 05:42:58,240 --> 05:43:00,320 should do now is concern ourselves with 8712 05:43:00,320 --> 05:43:03,120 database access so let's create a user 8713 05:43:03,120 --> 05:43:04,718 that can access 8714 05:43:04,718 --> 05:43:07,120 this new collection and database that we 8715 05:43:07,120 --> 05:43:10,000 have so create a database user we click 8716 05:43:10,000 --> 05:43:11,600 the big button that says add new 8717 05:43:11,600 --> 05:43:13,200 database user 8718 05:43:13,200 --> 05:43:15,200 and then it gives us password 8719 05:43:15,200 --> 05:43:16,558 certificate and all of that we'll just 8720 05:43:16,558 --> 05:43:18,400 stick with password 8721 05:43:18,400 --> 05:43:20,558 and it put in some old information for 8722 05:43:20,558 --> 05:43:22,798 me let's put in something different here 8723 05:43:22,798 --> 05:43:25,680 let me go with 8724 05:43:26,638 --> 05:43:27,920 8725 05:43:27,920 --> 05:43:29,600 tut once again 8726 05:43:29,600 --> 05:43:32,160 and then for a password 8727 05:43:32,160 --> 05:43:33,840 i'm just going to and we'll go ahead and 8728 05:43:33,840 --> 05:43:36,080 show whatever the password is i'll go 8729 05:43:36,080 --> 05:43:37,040 with 8730 05:43:37,040 --> 05:43:39,680 testing one two three kind of like a mic 8731 05:43:39,680 --> 05:43:40,480 check 8732 05:43:40,480 --> 05:43:42,760 okay so we've got tut testing two 8733 05:43:42,760 --> 05:43:45,600 testing123 i think we'll keep all of the 8734 05:43:45,600 --> 05:43:48,320 default options here and we want the 8735 05:43:48,320 --> 05:43:50,320 read and write to any database and of 8736 05:43:50,320 --> 05:43:53,040 course i'll come back later and 8737 05:43:53,040 --> 05:43:55,280 delete this user but for now 8738 05:43:55,280 --> 05:43:57,120 we'll use it for the tutorial and we'll 8739 05:43:57,120 --> 05:43:58,798 click add user 8740 05:43:58,798 --> 05:44:01,040 and once we have the user we need to go 8741 05:44:01,040 --> 05:44:03,520 back to our cluster so 8742 05:44:03,520 --> 05:44:06,798 let's click tuts 8743 05:44:06,798 --> 05:44:09,920 and that takes us back to our cluster 8744 05:44:09,920 --> 05:44:11,520 and it says we are deploying your 8745 05:44:11,520 --> 05:44:13,360 changes current action configuring 8746 05:44:13,360 --> 05:44:14,718 mongodb 8747 05:44:14,718 --> 05:44:17,600 so i'll give this just a second and then 8748 05:44:17,600 --> 05:44:19,440 we're going to click connect with the 8749 05:44:19,440 --> 05:44:21,920 configuration now complete let's go 8750 05:44:21,920 --> 05:44:24,480 ahead and click the connect button 8751 05:44:24,480 --> 05:44:26,400 it tells us we need to set up some 8752 05:44:26,400 --> 05:44:29,120 security this part is required and what 8753 05:44:29,120 --> 05:44:31,440 we're going to do is allow access from 8754 05:44:31,440 --> 05:44:33,440 anywhere we don't really know where 8755 05:44:33,440 --> 05:44:36,160 we're going to host our back end yet so 8756 05:44:36,160 --> 05:44:38,080 this is good for development right now 8757 05:44:38,080 --> 05:44:40,320 until we actually know what i p address 8758 05:44:40,320 --> 05:44:43,520 we had so let's just add in the zeros 8759 05:44:43,520 --> 05:44:45,280 and that means it's good from anywhere 8760 05:44:45,280 --> 05:44:47,600 we already created a user so now let's 8761 05:44:47,600 --> 05:44:49,760 click choose a connection method and 8762 05:44:49,760 --> 05:44:52,160 we're going to connect our application 8763 05:44:52,160 --> 05:44:53,920 this gives us a connection string and 8764 05:44:53,920 --> 05:44:55,680 this is what we want notice it's already 8765 05:44:55,680 --> 05:44:58,400 put in the tut user i created now 8766 05:44:58,400 --> 05:45:00,480 i'm going to have to put in the password 8767 05:45:00,480 --> 05:45:02,798 including getting rid of the 8768 05:45:02,798 --> 05:45:04,558 less than and greater than 8769 05:45:04,558 --> 05:45:07,520 around the password and then also i need 8770 05:45:07,520 --> 05:45:10,638 to replace my first database with the 8771 05:45:10,638 --> 05:45:13,280 companydb which was the name we gave the 8772 05:45:13,280 --> 05:45:15,840 database so i'm going to copy this 8773 05:45:15,840 --> 05:45:18,798 and we're ready to close out of this now 8774 05:45:18,798 --> 05:45:21,200 and open up visual studio code we're 8775 05:45:21,200 --> 05:45:23,280 going to start with the code repository 8776 05:45:23,280 --> 05:45:26,080 from the last tutorial in the nodejs and 8777 05:45:26,080 --> 05:45:29,040 express and now mongodb series but if 8778 05:45:29,040 --> 05:45:30,558 you don't have it you should be able to 8779 05:45:30,558 --> 05:45:32,798 easily follow along without it or you 8780 05:45:32,798 --> 05:45:35,040 can download or clone the starter source 8781 05:45:35,040 --> 05:45:36,878 code from the link that i'm providing in 8782 05:45:36,878 --> 05:45:38,798 the description below so let's get 8783 05:45:38,798 --> 05:45:42,480 started by going to our dot env file 8784 05:45:42,480 --> 05:45:44,718 and inside the dot env file it looks 8785 05:45:44,718 --> 05:45:47,120 like i need to go to the end of the line 8786 05:45:47,120 --> 05:45:50,000 and hit return so we have another line 8787 05:45:50,000 --> 05:45:51,600 i'm going to create 8788 05:45:51,600 --> 05:45:53,280 a database 8789 05:45:53,280 --> 05:45:55,360 underscore uri 8790 05:45:55,360 --> 05:45:57,440 and set it equal to our connection 8791 05:45:57,440 --> 05:45:59,600 string but now we just need to replace a 8792 05:45:59,600 --> 05:46:02,080 few things in the connection string and 8793 05:46:02,080 --> 05:46:04,480 i'm going to press alt z here i'm using 8794 05:46:04,480 --> 05:46:06,558 windows and it will wrap 8795 05:46:06,558 --> 05:46:09,440 the code in visual studio code it may be 8796 05:46:09,440 --> 05:46:12,000 different for you on mac or linux if you 8797 05:46:12,000 --> 05:46:14,638 have those okay so we're replacing the 8798 05:46:14,638 --> 05:46:18,320 password and if you remember i put in 8799 05:46:18,320 --> 05:46:20,160 testing lowercase 8800 05:46:20,160 --> 05:46:21,760 one two three 8801 05:46:21,760 --> 05:46:24,400 and then instead of my first database we 8802 05:46:24,400 --> 05:46:26,240 named the database 8803 05:46:26,240 --> 05:46:27,360 company 8804 05:46:27,360 --> 05:46:28,400 db 8805 05:46:28,400 --> 05:46:31,360 now we can save this file because we're 8806 05:46:31,360 --> 05:46:34,000 using the process.env 8807 05:46:34,000 --> 05:46:36,000 to pull this value out when we need it 8808 05:46:36,000 --> 05:46:38,638 to connect and speaking of dot e and v 8809 05:46:38,638 --> 05:46:40,480 we actually put it in about three 8810 05:46:40,480 --> 05:46:42,798 different files before and really we 8811 05:46:42,798 --> 05:46:44,240 could have just put it at the beginning 8812 05:46:44,240 --> 05:46:46,160 of the project it's not like when we 8813 05:46:46,160 --> 05:46:47,920 require other 8814 05:46:47,920 --> 05:46:50,240 things like the jwt here that need to go 8815 05:46:50,240 --> 05:46:52,638 in individual files as they're needed 8816 05:46:52,638 --> 05:46:54,638 this can just go at the beginning of the 8817 05:46:54,638 --> 05:46:56,480 server.js so i'm going to pull it out of 8818 05:46:56,480 --> 05:46:59,360 our middleware verify 8819 05:46:59,360 --> 05:47:02,480 dot jwt.js file and save that and i 8820 05:47:02,480 --> 05:47:04,160 believe it's in a couple of controllers 8821 05:47:04,160 --> 05:47:07,040 that we can remove it from as well but 8822 05:47:07,040 --> 05:47:09,120 in the server.js file i'm going to put 8823 05:47:09,120 --> 05:47:11,120 it at the very top you just want it in 8824 05:47:11,120 --> 05:47:13,040 there as soon as you can get it 8825 05:47:13,040 --> 05:47:15,840 and we'll save it and now i believe 8826 05:47:15,840 --> 05:47:17,600 let's check these controllers real quick 8827 05:47:17,600 --> 05:47:20,000 is it in the auth controller 8828 05:47:20,000 --> 05:47:22,958 yes it is we can remove that 8829 05:47:22,958 --> 05:47:25,040 and it's in one other controller if i 8830 05:47:25,040 --> 05:47:27,840 remember right 8831 05:47:27,840 --> 05:47:28,718 not 8832 05:47:28,718 --> 05:47:31,600 the logout controller 8833 05:47:31,600 --> 05:47:33,520 see if it's the refresh controller there 8834 05:47:33,520 --> 05:47:35,920 it is and we can remove it from that as 8835 05:47:35,920 --> 05:47:38,080 well and i think that will take care of 8836 05:47:38,080 --> 05:47:40,718 it so let's close out of the controllers 8837 05:47:40,718 --> 05:47:42,718 it didn't save the auth controller need 8838 05:47:42,718 --> 05:47:44,080 to do that too 8839 05:47:44,080 --> 05:47:46,160 okay close out of those and we're going 8840 05:47:46,160 --> 05:47:48,400 to do some work here in the server well 8841 05:47:48,400 --> 05:47:50,080 really before we do anything in the 8842 05:47:50,080 --> 05:47:52,080 server we need to go to package json 8843 05:47:52,080 --> 05:47:55,200 because we're going to install mongoose 8844 05:47:55,200 --> 05:47:57,760 and i'm going to press control and the 8845 05:47:57,760 --> 05:47:59,200 back tick you can also go to the 8846 05:47:59,200 --> 05:48:01,840 terminal window or menu to open a new 8847 05:48:01,840 --> 05:48:04,240 terminal window and here we need to 8848 05:48:04,240 --> 05:48:06,798 install the mongoose package so it's npm 8849 05:48:06,798 --> 05:48:08,240 i 8850 05:48:08,240 --> 05:48:11,920 mongoose and return now mongoose.js is a 8851 05:48:11,920 --> 05:48:14,558 library that makes working with 8852 05:48:14,558 --> 05:48:16,798 much easier kind of like you could 8853 05:48:16,798 --> 05:48:18,718 consider react makes working with 8854 05:48:18,718 --> 05:48:20,320 javascript easier than vanilla 8855 05:48:20,320 --> 05:48:23,280 javascript it's just a helpful library 8856 05:48:23,280 --> 05:48:25,120 so let's take a look at their page real 8857 05:48:25,120 --> 05:48:26,320 quick it's 8858 05:48:26,320 --> 05:48:30,080 mon goose there we go js.com 8859 05:48:30,080 --> 05:48:32,400 we bring up that page and you can see it 8860 05:48:32,400 --> 05:48:35,440 says an elegant mongodb object modeling 8861 05:48:35,440 --> 05:48:38,718 for node.js perfect exactly what we need 8862 05:48:38,718 --> 05:48:41,040 and it has some great documentation as 8863 05:48:41,040 --> 05:48:43,760 well so we can refer to this as we go 8864 05:48:43,760 --> 05:48:46,480 today we'll be working with a connection 8865 05:48:46,480 --> 05:48:49,680 let's go back to vs code and see if our 8866 05:48:49,680 --> 05:48:51,920 package installed it looks like it has 8867 05:48:51,920 --> 05:48:54,558 so let's check our dependencies and we 8868 05:48:54,558 --> 05:48:57,280 see mongoose listed right here on line 8869 05:48:57,280 --> 05:48:59,920 20 so we are ready to begin working with 8870 05:48:59,920 --> 05:49:01,840 mongoose so now let's go to the 8871 05:49:01,840 --> 05:49:04,000 server.js file and underneath 8872 05:49:04,000 --> 05:49:06,160 credentials let's go ahead and require 8873 05:49:06,160 --> 05:49:09,280 mongoose so we define const 8874 05:49:09,280 --> 05:49:11,680 mongoose and set it equal to 8875 05:49:11,680 --> 05:49:14,718 require and then we just bring in 8876 05:49:14,718 --> 05:49:17,040 mongoose 8877 05:49:17,040 --> 05:49:18,480 and save 8878 05:49:18,480 --> 05:49:21,360 now after that we need to go ahead and 8879 05:49:21,360 --> 05:49:24,958 create a connection configuration so as 8880 05:49:24,958 --> 05:49:26,878 you might guess we're going to do that 8881 05:49:26,878 --> 05:49:29,200 in our config folder that we have up 8882 05:49:29,200 --> 05:49:32,160 here so i'll highlight that and click 8883 05:49:32,160 --> 05:49:34,000 create new file and i'm going to call 8884 05:49:34,000 --> 05:49:38,160 this lowercase db and then uppercase 8885 05:49:38,160 --> 05:49:41,760 con so it's camelcase really db con 8886 05:49:41,760 --> 05:49:44,400 dot js now let's start this file by 8887 05:49:44,400 --> 05:49:46,798 requiring mongoose as well so we'll once 8888 05:49:46,798 --> 05:49:49,520 again bring in mongoose 8889 05:49:49,520 --> 05:49:51,520 and require 8890 05:49:51,520 --> 05:49:52,958 mongoose 8891 05:49:52,958 --> 05:49:54,400 and now we need to create a function 8892 05:49:54,400 --> 05:49:55,840 that we're going to export and let's 8893 05:49:55,840 --> 05:49:57,680 call it connect 8894 05:49:57,680 --> 05:50:00,160 db and set this equal to an async 8895 05:50:00,160 --> 05:50:05,360 function mongoose is async let's go try 8896 05:50:05,360 --> 05:50:09,440 and after try we'll have our catch 8897 05:50:09,440 --> 05:50:11,440 with an error block here 8898 05:50:11,440 --> 05:50:14,000 and we'll just 8899 05:50:14,280 --> 05:50:17,600 console.error and pass in the air 8900 05:50:17,600 --> 05:50:20,400 and then up above we need to go ahead 8901 05:50:20,400 --> 05:50:23,120 and try to connect a mongoose so how 8902 05:50:23,120 --> 05:50:25,760 we'll do that is a weight we've already 8903 05:50:25,760 --> 05:50:28,840 got a sink above and then we use 8904 05:50:28,840 --> 05:50:32,160 mongoose and we choose connect 8905 05:50:32,160 --> 05:50:34,718 and from there we need to bring in 8906 05:50:34,718 --> 05:50:38,558 that db uri that we defined back in our 8907 05:50:38,558 --> 05:50:42,080 dot env so we can just refer to process 8908 05:50:42,080 --> 05:50:42,840 dot 8909 05:50:42,840 --> 05:50:45,040 env dot 8910 05:50:45,040 --> 05:50:47,920 database underscore uri 8911 05:50:47,920 --> 05:50:50,718 and then there is an object 8912 05:50:50,718 --> 05:50:52,480 and what we need to do is pass in a 8913 05:50:52,480 --> 05:50:54,558 couple of options that will just prevent 8914 05:50:54,558 --> 05:50:57,040 warnings that we would get from mongodb 8915 05:50:57,040 --> 05:50:58,718 otherwise one is 8916 05:50:58,718 --> 05:51:00,400 use 8917 05:51:00,400 --> 05:51:04,400 unified if i spelled that right 8918 05:51:04,400 --> 05:51:07,520 topology and we set that to true 8919 05:51:07,520 --> 05:51:10,840 and the other one is use new 8920 05:51:10,840 --> 05:51:14,320 url parser and we'll set that to true as 8921 05:51:14,320 --> 05:51:16,638 well 8922 05:51:16,798 --> 05:51:18,320 and then we should be pretty much 8923 05:51:18,320 --> 05:51:22,440 finished except we once again need to do 8924 05:51:22,440 --> 05:51:24,638 module.exports as we have many times 8925 05:51:24,638 --> 05:51:26,878 before four functions we've created and 8926 05:51:26,878 --> 05:51:28,400 exported 8927 05:51:28,400 --> 05:51:31,280 and it is connect db and we can save the 8928 05:51:31,280 --> 05:51:33,760 file with our function created let's go 8929 05:51:33,760 --> 05:51:36,320 back to server.js now and we need to 8930 05:51:36,320 --> 05:51:38,160 import that so 8931 05:51:38,160 --> 05:51:43,600 say const connect db is going to equal 8932 05:51:43,600 --> 05:51:45,280 require 8933 05:51:45,280 --> 05:51:48,080 and then we need to go into config 8934 05:51:48,080 --> 05:51:52,958 and from there we need to pull in db con 8935 05:51:52,958 --> 05:51:55,440 and save and now let's go ahead and 8936 05:51:55,440 --> 05:51:58,240 connect to the db the very first thing 8937 05:51:58,240 --> 05:52:00,400 if it fails we don't want to listen for 8938 05:52:00,400 --> 05:52:02,638 any other connections anyway so this is 8939 05:52:02,638 --> 05:52:05,680 connect to mongodb and all we do is call 8940 05:52:05,680 --> 05:52:07,200 connect 8941 05:52:07,200 --> 05:52:09,440 db right here 8942 05:52:09,440 --> 05:52:12,080 and it's ready to connect however there 8943 05:52:12,080 --> 05:52:14,558 is one issue to address here so let's 8944 05:52:14,558 --> 05:52:17,360 scroll all the way down to where our app 8945 05:52:17,360 --> 05:52:20,558 listens for requests now we don't want 8946 05:52:20,558 --> 05:52:22,958 to listen for requests if we don't 8947 05:52:22,958 --> 05:52:25,120 connect and so 8948 05:52:25,120 --> 05:52:27,280 if our connection fails for whatever 8949 05:52:27,280 --> 05:52:30,958 reason we need to avoid doing this 8950 05:52:30,958 --> 05:52:33,360 and we can do that let's go back to the 8951 05:52:33,360 --> 05:52:36,718 documentation for mongoose and where we 8952 05:52:36,718 --> 05:52:39,040 had connections here in the 8953 05:52:39,040 --> 05:52:41,440 documentation we also have connection 8954 05:52:41,440 --> 05:52:42,558 events 8955 05:52:42,558 --> 05:52:45,200 and let's look we can listen for the 8956 05:52:45,200 --> 05:52:47,280 connected event 8957 05:52:47,280 --> 05:52:50,080 and there's also the open event which is 8958 05:52:50,080 --> 05:52:52,878 equivalent to connected so let's go back 8959 05:52:52,878 --> 05:52:55,440 to our code and use that 8960 05:52:55,440 --> 05:52:57,280 and we can do that with mongoose which 8961 05:52:57,280 --> 05:52:59,520 is why we required it for this file in 8962 05:52:59,520 --> 05:53:00,958 particular 8963 05:53:00,958 --> 05:53:03,200 and we'll have mongoose 8964 05:53:03,200 --> 05:53:04,718 dot 8965 05:53:04,718 --> 05:53:05,920 connection 8966 05:53:05,920 --> 05:53:08,718 and then we want wants instead of on 8967 05:53:08,718 --> 05:53:10,798 because we'll just listen for this event 8968 05:53:10,798 --> 05:53:12,320 one time 8969 05:53:12,320 --> 05:53:14,240 and we'll listen for the open event it's 8970 05:53:14,240 --> 05:53:17,120 a little shorter to type than connected 8971 05:53:17,120 --> 05:53:18,718 and now we'll have 8972 05:53:18,718 --> 05:53:22,000 an anonymous function and inside this we 8973 05:53:22,000 --> 05:53:24,878 can console log first 8974 05:53:24,878 --> 05:53:27,120 and inside there we'll just say 8975 05:53:27,120 --> 05:53:28,400 connected 8976 05:53:28,400 --> 05:53:29,280 to 8977 05:53:29,280 --> 05:53:31,440 mongodb 8978 05:53:31,440 --> 05:53:33,760 and after we've connected to mongodb 8979 05:53:33,760 --> 05:53:37,360 let's just go ahead and cut and paste in 8980 05:53:37,360 --> 05:53:39,520 our app listen 8981 05:53:39,520 --> 05:53:41,520 and we'll put it right there 8982 05:53:41,520 --> 05:53:43,760 and now we're only going to listen for 8983 05:53:43,760 --> 05:53:44,958 requests 8984 05:53:44,958 --> 05:53:46,958 if we have successfully connected 8985 05:53:46,958 --> 05:53:49,520 because that is when the open event will 8986 05:53:49,520 --> 05:53:52,718 be emitted by the connection and so then 8987 05:53:52,718 --> 05:53:54,480 our console will let us know we've 8988 05:53:54,480 --> 05:53:56,400 connected to mongodb 8989 05:53:56,400 --> 05:53:58,480 and we're listening for requests on 8990 05:53:58,480 --> 05:54:00,400 whatever port and of course here in 8991 05:54:00,400 --> 05:54:02,798 development we are defaulting to port 8992 05:54:02,798 --> 05:54:05,920 3500 as set at the top okay all of that 8993 05:54:05,920 --> 05:54:07,760 said i've left the terminal window open 8994 05:54:07,760 --> 05:54:10,400 let's go ahead and type npm 8995 05:54:10,400 --> 05:54:12,000 run dev 8996 05:54:12,000 --> 05:54:13,840 and i'll drag it up so we have a little 8997 05:54:13,840 --> 05:54:17,200 more room and let's see if we connect 8998 05:54:17,200 --> 05:54:19,760 and we're connected to mongodb 8999 05:54:19,760 --> 05:54:22,958 and the server is running on port 3500. 9000 05:54:22,958 --> 05:54:25,120 okay in the next tutorial we'll create 9001 05:54:25,120 --> 05:54:28,558 schemas and data models with mongoose 9002 05:54:28,558 --> 05:54:31,280 and relate that back to our mongodb 9003 05:54:31,280 --> 05:54:33,760 collections 9004 05:54:33,760 --> 05:54:35,760 in the previous tutorial we went over 9005 05:54:35,760 --> 05:54:39,200 what mongodb is and we used the mongoose 9006 05:54:39,200 --> 05:54:43,280 js library to connect our app to mongodb 9007 05:54:43,280 --> 05:54:45,520 today we're going to create mongoose 9008 05:54:45,520 --> 05:54:48,240 schemas and data models that will allow 9009 05:54:48,240 --> 05:54:50,480 us to perform crud operations on our 9010 05:54:50,480 --> 05:54:53,440 mongodb data collections we're going to 9011 05:54:53,440 --> 05:54:55,280 start with the code repository from the 9012 05:54:55,280 --> 05:54:56,480 last tutorial 9013 05:54:56,480 --> 05:54:58,320 but if you don't have it you can 9014 05:54:58,320 --> 05:55:00,320 download or clone the starter source 9015 05:55:00,320 --> 05:55:02,240 code from the link i've provided in the 9016 05:55:02,240 --> 05:55:04,400 description below let's get started 9017 05:55:04,400 --> 05:55:06,760 today at 9018 05:55:06,760 --> 05:55:08,798 mongoosejs.com and from there we're 9019 05:55:08,798 --> 05:55:11,200 going to click read the docs and it 9020 05:55:11,200 --> 05:55:13,840 instantly takes us to the schemas in the 9021 05:55:13,840 --> 05:55:16,480 docs and that is because as you see on 9022 05:55:16,480 --> 05:55:18,480 the page all highlighted it says 9023 05:55:18,480 --> 05:55:20,718 everything in mongoose starts with a 9024 05:55:20,718 --> 05:55:21,760 schema 9025 05:55:21,760 --> 05:55:25,360 each schema maps to a mongodb collection 9026 05:55:25,360 --> 05:55:27,360 and defines the shape of the documents 9027 05:55:27,360 --> 05:55:29,120 within that collection so you can see 9028 05:55:29,120 --> 05:55:32,160 why they're very important and here you 9029 05:55:32,160 --> 05:55:34,400 see an example schema they have a blog 9030 05:55:34,400 --> 05:55:37,680 schema and they're declaring data types 9031 05:55:37,680 --> 05:55:40,240 for the different fields inside of the 9032 05:55:40,240 --> 05:55:42,240 documents that will be created with the 9033 05:55:42,240 --> 05:55:43,520 data model 9034 05:55:43,520 --> 05:55:46,160 and they also have some options here 9035 05:55:46,160 --> 05:55:48,638 such as date they're not just saying 9036 05:55:48,638 --> 05:55:50,798 it's a string for the body they're also 9037 05:55:50,798 --> 05:55:53,200 saying it's going to be a date they can 9038 05:55:53,200 --> 05:55:55,360 have default data and there's other 9039 05:55:55,360 --> 05:55:57,280 options available as well and if we 9040 05:55:57,280 --> 05:55:59,440 scroll down just a little bit further 9041 05:55:59,440 --> 05:56:01,600 you can see the permitted schema types 9042 05:56:01,600 --> 05:56:03,520 and here we have string number date 9043 05:56:03,520 --> 05:56:06,638 buffer boolean and a few others object 9044 05:56:06,638 --> 05:56:08,958 id is an important one that will be 9045 05:56:08,958 --> 05:56:11,680 created automatically for us so we will 9046 05:56:11,680 --> 05:56:14,320 not have to specify an id as you don't 9047 05:56:14,320 --> 05:56:16,320 see one specified in their example 9048 05:56:16,320 --> 05:56:18,558 either so now let's go to visual studio 9049 05:56:18,558 --> 05:56:21,120 code and get started creating our 9050 05:56:21,120 --> 05:56:24,240 schemas for both our employees data and 9051 05:56:24,240 --> 05:56:26,878 our users data okay i'm in visual studio 9052 05:56:26,878 --> 05:56:28,638 code and i've got the source code from 9053 05:56:28,638 --> 05:56:30,958 the last tutorial it's linked to below 9054 05:56:30,958 --> 05:56:33,040 as the starter source code if you want 9055 05:56:33,040 --> 05:56:35,120 to download or clone that right now 9056 05:56:35,120 --> 05:56:37,440 we're going to go to the model folder 9057 05:56:37,440 --> 05:56:38,920 and we've got 9058 05:56:38,920 --> 05:56:41,760 employees.json and users.json because we 9059 05:56:41,760 --> 05:56:44,798 were just using the nodejs file system 9060 05:56:44,798 --> 05:56:47,200 to write to both of these files and 9061 05:56:47,200 --> 05:56:48,958 we're going to get rid of that 9062 05:56:48,958 --> 05:56:50,878 eventually here as we replace everything 9063 05:56:50,878 --> 05:56:53,120 with mongodb for now just keep those 9064 05:56:53,120 --> 05:56:54,798 files and we'll create 9065 05:56:54,798 --> 05:56:57,520 new files for our schemas and the first 9066 05:56:57,520 --> 05:57:00,638 one will be called employee 9067 05:57:00,638 --> 05:57:03,280 and i'll spell that with a capital e and 9068 05:57:03,280 --> 05:57:06,000 then just js and that's a naming 9069 05:57:06,000 --> 05:57:08,320 standard it won't really have an impact 9070 05:57:08,320 --> 05:57:10,638 if you don't use the capital e however 9071 05:57:10,638 --> 05:57:12,958 it's pretty much the consistent standard 9072 05:57:12,958 --> 05:57:15,360 that i've seen so first let's define 9073 05:57:15,360 --> 05:57:16,958 mongoose 9074 05:57:16,958 --> 05:57:20,320 and require that 9075 05:57:20,798 --> 05:57:22,638 there we go 9076 05:57:22,638 --> 05:57:24,480 and now that we've got mongoose pulled 9077 05:57:24,480 --> 05:57:26,718 in we also need 9078 05:57:26,718 --> 05:57:29,200 a schema with a capital s and that's 9079 05:57:29,200 --> 05:57:30,400 going to equal 9080 05:57:30,400 --> 05:57:32,320 mongoose dot 9081 05:57:32,320 --> 05:57:35,040 schema once again with a capital s 9082 05:57:35,040 --> 05:57:37,280 now that we have that we can define our 9083 05:57:37,280 --> 05:57:39,360 schema and let's call this 9084 05:57:39,360 --> 05:57:40,718 employee 9085 05:57:40,718 --> 05:57:43,280 schema and that can use camel case 9086 05:57:43,280 --> 05:57:46,240 we'll set this equal to a new 9087 05:57:46,240 --> 05:57:49,360 schema once again capital s on schema 9088 05:57:49,360 --> 05:57:52,480 and now we can map out our data and if 9089 05:57:52,480 --> 05:57:54,320 you remember our employee's data was 9090 05:57:54,320 --> 05:57:56,240 very simple it just had a first name and 9091 05:57:56,240 --> 05:57:58,798 a last name so that's what we'll declare 9092 05:57:58,798 --> 05:58:00,480 here now remember 9093 05:58:00,480 --> 05:58:02,878 uh we will automatically have an object 9094 05:58:02,878 --> 05:58:05,680 id created for us so we don't need an id 9095 05:58:05,680 --> 05:58:07,040 field here 9096 05:58:07,040 --> 05:58:11,040 and now we can say first name is type 9097 05:58:11,040 --> 05:58:14,718 string and then we can also say it's 9098 05:58:14,718 --> 05:58:17,440 required and that accepts a boolean so 9099 05:58:17,440 --> 05:58:19,840 we'll say true that is absolutely 9100 05:58:19,840 --> 05:58:22,718 required and then we'll do the same for 9101 05:58:22,718 --> 05:58:24,480 last name and know what we could just 9102 05:58:24,480 --> 05:58:26,958 copy this down 9103 05:58:26,958 --> 05:58:29,200 shift alt and the down arrow for me on 9104 05:58:29,200 --> 05:58:31,680 windows and visual studio code 9105 05:58:31,680 --> 05:58:33,840 and make that the last name 9106 05:58:33,840 --> 05:58:36,160 and let's get rid of the comma there and 9107 05:58:36,160 --> 05:58:39,360 that's basically our schema very simple 9108 05:58:39,360 --> 05:58:41,680 we've got two string fields first name 9109 05:58:41,680 --> 05:58:44,638 and last name now at the end of the file 9110 05:58:44,638 --> 05:58:47,680 we need module dot exports 9111 05:58:47,680 --> 05:58:49,520 and we're going to set this equal to 9112 05:58:49,520 --> 05:58:50,878 mongoose 9113 05:58:50,878 --> 05:58:52,558 dot model 9114 05:58:52,558 --> 05:58:54,878 now we're creating a data model right 9115 05:58:54,878 --> 05:58:57,040 here and we'll set this equal to 9116 05:58:57,040 --> 05:58:58,798 employee 9117 05:58:58,798 --> 05:59:01,200 and that is uppercase 9118 05:59:01,200 --> 05:59:03,520 first letter and then not plural once 9119 05:59:03,520 --> 05:59:05,680 again just like the name of the file 9120 05:59:05,680 --> 05:59:07,200 employee 9121 05:59:07,200 --> 05:59:11,120 and then we'll use the employee schema 9122 05:59:11,120 --> 05:59:13,520 now by default 9123 05:59:13,520 --> 05:59:16,400 mongoose when it creates this model 9124 05:59:16,400 --> 05:59:19,920 will set this to lowercase and plural so 9125 05:59:19,920 --> 05:59:22,240 it will look for an employee's 9126 05:59:22,240 --> 05:59:24,958 collection in mongodb 9127 05:59:24,958 --> 05:59:27,680 and the employees collection will be all 9128 05:59:27,680 --> 05:59:29,760 lower case and once again it will be 9129 05:59:29,760 --> 05:59:32,320 plural and we can see that if we go back 9130 05:59:32,320 --> 05:59:36,878 to the docs and we go to the models link 9131 05:59:36,878 --> 05:59:38,958 in the docs there we go 9132 05:59:38,958 --> 05:59:41,840 it says this in bold right here mongoose 9133 05:59:41,840 --> 05:59:44,718 automatically looks for the plural 9134 05:59:44,718 --> 05:59:46,638 lowercase version 9135 05:59:46,638 --> 05:59:49,440 of your model name so in the example 9136 05:59:49,440 --> 05:59:52,000 they give they have the word tank they 9137 05:59:52,000 --> 05:59:53,840 defined here for the model and they 9138 05:59:53,840 --> 05:59:55,040 passed in 9139 05:59:55,040 --> 05:59:58,080 the string tank with the capital t 9140 05:59:58,080 --> 06:00:00,958 but it says for the example above the 9141 06:00:00,958 --> 06:00:04,320 model tank is for the tanks collection 9142 06:00:04,320 --> 06:00:06,558 in the mongodb database and that's all 9143 06:00:06,558 --> 06:00:09,760 lowercase and plural all right back to 9144 06:00:09,760 --> 06:00:12,320 visual studio code let's save this file 9145 06:00:12,320 --> 06:00:14,638 and let's go ahead and create a user 9146 06:00:14,638 --> 06:00:16,958 schema as well now our user data had 9147 06:00:16,958 --> 06:00:19,600 just a little more to it so this will be 9148 06:00:19,600 --> 06:00:21,360 a little more interesting than just the 9149 06:00:21,360 --> 06:00:23,200 first name and last name that we had in 9150 06:00:23,200 --> 06:00:25,440 the employee schema i'm just going to 9151 06:00:25,440 --> 06:00:27,360 copy these imports here because we need 9152 06:00:27,360 --> 06:00:30,000 the exact same thing at the beginning of 9153 06:00:30,000 --> 06:00:32,558 basically every schema we create 9154 06:00:32,558 --> 06:00:35,760 and now i'm going to say user 9155 06:00:35,760 --> 06:00:37,760 schema camel case 9156 06:00:37,760 --> 06:00:40,558 this is equal to new schema capital s on 9157 06:00:40,558 --> 06:00:41,920 schema 9158 06:00:41,920 --> 06:00:44,958 and now we have a username in our data 9159 06:00:44,958 --> 06:00:47,120 and this is kind of like the first name 9160 06:00:47,120 --> 06:00:49,120 last name in the previous schema it's 9161 06:00:49,120 --> 06:00:50,480 type 9162 06:00:50,480 --> 06:00:51,840 string 9163 06:00:51,840 --> 06:00:54,558 and then required 9164 06:00:54,558 --> 06:00:56,000 is true 9165 06:00:56,000 --> 06:00:58,480 after the username 9166 06:00:58,480 --> 06:01:02,240 we have roles 9167 06:01:02,240 --> 06:01:04,320 now the roles data is a little bit 9168 06:01:04,320 --> 06:01:06,958 different so we had 9169 06:01:06,958 --> 06:01:10,160 user which was a possible role 9170 06:01:10,160 --> 06:01:12,000 and now let's specify 9171 06:01:12,000 --> 06:01:14,878 some data or some details about the user 9172 06:01:14,878 --> 06:01:18,240 role here inside of an object and now we 9173 06:01:18,240 --> 06:01:20,320 can set this to type 9174 06:01:20,320 --> 06:01:21,920 number 9175 06:01:21,920 --> 06:01:25,920 and we can put a default value so if not 9176 06:01:25,920 --> 06:01:27,360 specified 9177 06:01:27,360 --> 06:01:29,040 any user that's created will 9178 06:01:29,040 --> 06:01:31,680 automatically be assigned the value of 9179 06:01:31,680 --> 06:01:34,480 2001 and that was our basic user value 9180 06:01:34,480 --> 06:01:36,480 that we had previously 9181 06:01:36,480 --> 06:01:38,000 applied 9182 06:01:38,000 --> 06:01:40,878 and then the next role was editor 9183 06:01:40,878 --> 06:01:43,040 and that had a number 9184 06:01:43,040 --> 06:01:45,840 and then the next role was admin and 9185 06:01:45,840 --> 06:01:47,440 that had a number notice we're not 9186 06:01:47,440 --> 06:01:50,080 providing default values and we're not 9187 06:01:50,080 --> 06:01:51,600 even saying they're required not 9188 06:01:51,600 --> 06:01:53,760 everybody's an editor not everybody's an 9189 06:01:53,760 --> 06:01:56,480 admin so these will only be added to the 9190 06:01:56,480 --> 06:01:59,280 data that we decide to add them to as an 9191 06:01:59,280 --> 06:02:02,480 admin could make those decisions okay 9192 06:02:02,480 --> 06:02:04,638 now that we've defined the roles let's 9193 06:02:04,638 --> 06:02:07,920 put in the password field 9194 06:02:07,920 --> 06:02:10,400 this is much like we had before type 9195 06:02:10,400 --> 06:02:12,160 will be string 9196 06:02:12,160 --> 06:02:14,400 and required 9197 06:02:14,400 --> 06:02:15,760 will be true 9198 06:02:15,760 --> 06:02:18,798 and then after that if you remember 9199 06:02:18,798 --> 06:02:21,920 we also store a refresh token 9200 06:02:21,920 --> 06:02:23,760 and when a user is created they don't 9201 06:02:23,760 --> 06:02:26,160 have a refresh token but after they're 9202 06:02:26,160 --> 06:02:28,958 authenticated they get one so this is a 9203 06:02:28,958 --> 06:02:29,920 string 9204 06:02:29,920 --> 06:02:32,558 but it does not have a default value and 9205 06:02:32,558 --> 06:02:34,480 it's not required because it's not 9206 06:02:34,480 --> 06:02:35,760 always there 9207 06:02:35,760 --> 06:02:38,480 so now we've created our schema 9208 06:02:38,480 --> 06:02:40,240 for our users 9209 06:02:40,240 --> 06:02:42,958 and let's go ahead and export this 9210 06:02:42,958 --> 06:02:44,440 we'll say 9211 06:02:44,440 --> 06:02:46,160 module.exports 9212 06:02:46,160 --> 06:02:47,878 set it equal to 9213 06:02:47,878 --> 06:02:50,160 mongoose.model so we're creating a data 9214 06:02:50,160 --> 06:02:51,120 model 9215 06:02:51,120 --> 06:02:54,080 and this will be user singular and then 9216 06:02:54,080 --> 06:02:56,718 mongoose will once again look for users 9217 06:02:56,718 --> 06:02:58,718 all lowercase plural 9218 06:02:58,718 --> 06:03:01,200 but we'll match our file name so user 9219 06:03:01,200 --> 06:03:04,160 and then we'll use the user schema that 9220 06:03:04,160 --> 06:03:06,558 we just created 9221 06:03:06,558 --> 06:03:09,840 okay we've created two basic schemas for 9222 06:03:09,840 --> 06:03:12,000 our data user and employee and then we 9223 06:03:12,000 --> 06:03:14,000 created models to be associated with 9224 06:03:14,000 --> 06:03:16,480 those let's go ahead and implement the 9225 06:03:16,480 --> 06:03:18,878 user schema and model 9226 06:03:18,878 --> 06:03:21,760 and let's attach that to one of our 9227 06:03:21,760 --> 06:03:23,200 controllers 9228 06:03:23,200 --> 06:03:24,718 and let's do it with the register 9229 06:03:24,718 --> 06:03:26,480 controller first that makes sense where 9230 06:03:26,480 --> 06:03:29,200 we'd create a new user and now you'll 9231 06:03:29,200 --> 06:03:31,760 see how much easier mongoose makes 9232 06:03:31,760 --> 06:03:35,040 interacting with the mongodb collection 9233 06:03:35,040 --> 06:03:36,958 than it is to write all of this stuff 9234 06:03:36,958 --> 06:03:39,760 that we have been doing with the file 9235 06:03:39,760 --> 06:03:42,718 system module and interacting with json 9236 06:03:42,718 --> 06:03:45,280 files so let's simplify this file and 9237 06:03:45,280 --> 06:03:48,000 just switch it over to using mongodb so 9238 06:03:48,000 --> 06:03:50,958 we're going to get rid of the user db 9239 06:03:50,958 --> 06:03:52,878 that we had up here 9240 06:03:52,878 --> 06:03:54,638 and let's just bring in 9241 06:03:54,638 --> 06:03:56,958 the user model so i'll define user with 9242 06:03:56,958 --> 06:03:58,798 a capital u and set this equal to 9243 06:03:58,798 --> 06:04:00,878 require 9244 06:04:00,878 --> 06:04:02,718 and now we need to go up out of the 9245 06:04:02,718 --> 06:04:05,680 controller folder and into the model 9246 06:04:05,680 --> 06:04:06,718 folder 9247 06:04:06,718 --> 06:04:08,558 and then we need 9248 06:04:08,558 --> 06:04:10,958 the user model 9249 06:04:10,958 --> 06:04:13,520 after we do that whoa i lost something 9250 06:04:13,520 --> 06:04:14,718 there 9251 06:04:14,718 --> 06:04:17,840 user model 9252 06:04:18,638 --> 06:04:20,798 there we go after we do that we can 9253 06:04:20,798 --> 06:04:23,200 really get rid of the fs promises and 9254 06:04:23,200 --> 06:04:25,120 path we're not going to be right into a 9255 06:04:25,120 --> 06:04:27,280 file anymore or need that we do still 9256 06:04:27,280 --> 06:04:30,480 need to keep the bcrypt import and now 9257 06:04:30,480 --> 06:04:32,798 let's start making changes as we handle 9258 06:04:32,798 --> 06:04:34,400 the new user here 9259 06:04:34,400 --> 06:04:37,360 and it'll start out the same we need the 9260 06:04:37,360 --> 06:04:39,600 user and password to come in from the 9261 06:04:39,600 --> 06:04:42,320 request and if we don't have those we're 9262 06:04:42,320 --> 06:04:44,240 going to send the same information back 9263 06:04:44,240 --> 06:04:46,000 that's a bad request 9264 06:04:46,000 --> 06:04:48,000 but now when we check for duplicates 9265 06:04:48,000 --> 06:04:49,760 we're interacting with a different 9266 06:04:49,760 --> 06:04:52,400 database so now this will change just a 9267 06:04:52,400 --> 06:04:53,440 little 9268 06:04:53,440 --> 06:04:56,240 and we'll still define duplicate but how 9269 06:04:56,240 --> 06:04:58,480 we get the duplicate is completely 9270 06:04:58,480 --> 06:04:59,600 different 9271 06:04:59,600 --> 06:05:02,480 notice we already have handled a user as 9272 06:05:02,480 --> 06:05:04,638 an async function so right here we're 9273 06:05:04,638 --> 06:05:07,040 going to await 9274 06:05:07,040 --> 06:05:09,200 and then use our user model 9275 06:05:09,200 --> 06:05:10,320 and call 9276 06:05:10,320 --> 06:05:14,320 find one and now we'll pass in some 9277 06:05:14,320 --> 06:05:16,000 information here we're looking for a 9278 06:05:16,000 --> 06:05:18,080 username that matches 9279 06:05:18,080 --> 06:05:20,320 the user that we defined from our 9280 06:05:20,320 --> 06:05:21,440 request 9281 06:05:21,440 --> 06:05:24,718 and after that we need to call 9282 06:05:24,718 --> 06:05:27,840 exec here at the end now not every 9283 06:05:27,840 --> 06:05:30,958 mongoose method needs that on the data 9284 06:05:30,958 --> 06:05:33,680 model but this one in particular does 9285 06:05:33,680 --> 06:05:36,240 and that is because we could pass in a 9286 06:05:36,240 --> 06:05:39,520 callback afterwards like a error result 9287 06:05:39,520 --> 06:05:41,920 for example but if you don't do that and 9288 06:05:41,920 --> 06:05:45,600 you're using the async a weight 9289 06:05:45,600 --> 06:05:46,958 pattern here 9290 06:05:46,958 --> 06:05:50,480 then you need to put exec at the end of 9291 06:05:50,480 --> 06:05:52,638 find one and you can check that in the 9292 06:05:52,638 --> 06:05:56,320 documentation under find one to 9293 06:05:56,320 --> 06:05:57,360 reference 9294 06:05:57,360 --> 06:05:59,200 if you need that or when you use any 9295 06:05:59,200 --> 06:06:01,120 method that you're not sure of and 9296 06:06:01,120 --> 06:06:03,040 you're using async await you should 9297 06:06:03,040 --> 06:06:06,240 check that but this is going to return 9298 06:06:06,240 --> 06:06:09,360 any user that matches the user that was 9299 06:06:09,360 --> 06:06:11,680 passed in and of course we don't want a 9300 06:06:11,680 --> 06:06:14,400 duplicate so this is the same if there 9301 06:06:14,400 --> 06:06:17,440 is a duplicate we need to send this 409 9302 06:06:17,440 --> 06:06:19,360 conflict there 9303 06:06:19,360 --> 06:06:20,638 after that 9304 06:06:20,638 --> 06:06:22,558 we need to handle the password in the 9305 06:06:22,558 --> 06:06:24,958 same way but after the password is 9306 06:06:24,958 --> 06:06:26,958 created our code is going to get much 9307 06:06:26,958 --> 06:06:29,120 simpler let's go ahead and keep the new 9308 06:06:29,120 --> 06:06:30,958 user even though we will define this a 9309 06:06:30,958 --> 06:06:32,320 little bit differently 9310 06:06:32,320 --> 06:06:34,718 but we're not going to use this or the 9311 06:06:34,718 --> 06:06:37,200 fs promises we don't need that console 9312 06:06:37,200 --> 06:06:39,120 log there let's go ahead and keep the 9313 06:06:39,120 --> 06:06:41,040 status 201 though because that is what 9314 06:06:41,040 --> 06:06:43,040 we want to send when we create the new 9315 06:06:43,040 --> 06:06:45,760 user with mongoose we can create and 9316 06:06:45,760 --> 06:06:48,160 store so i'll put create and store the 9317 06:06:48,160 --> 06:06:50,958 new user all at once and so what we'll 9318 06:06:50,958 --> 06:06:51,680 do 9319 06:06:51,680 --> 06:06:53,760 instead of defining a new user here i'm 9320 06:06:53,760 --> 06:06:55,200 going to define 9321 06:06:55,200 --> 06:06:56,400 result 9322 06:06:56,400 --> 06:07:00,558 and i'm going to set result equal to 9323 06:07:00,638 --> 06:07:01,840 a weight 9324 06:07:01,840 --> 06:07:03,680 then our user model 9325 06:07:03,680 --> 06:07:04,718 dot 9326 06:07:04,718 --> 06:07:06,160 create 9327 06:07:06,160 --> 06:07:09,600 and now we are creating a new user and 9328 06:07:09,600 --> 06:07:11,280 let's look inside to see what we're 9329 06:07:11,280 --> 06:07:13,360 passing into the create to make sure we 9330 06:07:13,360 --> 06:07:16,160 have everything we want or we don't want 9331 06:07:16,160 --> 06:07:19,360 what we do want the username 9332 06:07:19,360 --> 06:07:21,840 and then we don't really need the roles 9333 06:07:21,840 --> 06:07:23,440 here because remember we have the 9334 06:07:23,440 --> 06:07:26,080 default data in our schema so it will be 9335 06:07:26,080 --> 06:07:28,558 added automatically so we can remove 9336 06:07:28,558 --> 06:07:31,760 that and then we pass the password and 9337 06:07:31,760 --> 06:07:33,840 an object id will be created 9338 06:07:33,840 --> 06:07:36,000 automatically also so this is all we 9339 06:07:36,000 --> 06:07:38,000 really need to send 9340 06:07:38,000 --> 06:07:39,840 through our data model 9341 06:07:39,840 --> 06:07:42,400 and the username and password 9342 06:07:42,400 --> 06:07:44,878 will be sent by us and then a role will 9343 06:07:44,878 --> 06:07:46,958 automatically be created a roll value 9344 06:07:46,958 --> 06:07:48,480 and an id 9345 06:07:48,480 --> 06:07:51,040 for the document will also be created 9346 06:07:51,040 --> 06:07:54,320 now result will return the record that 9347 06:07:54,320 --> 06:07:57,200 is created so we could go ahead and log 9348 06:07:57,200 --> 06:07:59,040 the result just to view the record in 9349 06:07:59,040 --> 06:08:01,920 the console and i'll save that i do want 9350 06:08:01,920 --> 06:08:04,718 to discuss a couple of other ways a 9351 06:08:04,718 --> 06:08:06,798 record could be created that you might 9352 06:08:06,798 --> 06:08:08,878 see somewhere i prefer to do it this way 9353 06:08:08,878 --> 06:08:10,878 because it happens all at once just the 9354 06:08:10,878 --> 06:08:13,600 user create and it is created and we get 9355 06:08:13,600 --> 06:08:16,240 the result back but you might also see 9356 06:08:16,240 --> 06:08:17,440 something like 9357 06:08:17,440 --> 06:08:18,638 const 9358 06:08:18,638 --> 06:08:22,798 new user equals and then a new user with 9359 06:08:22,798 --> 06:08:25,120 the model and then of course you could 9360 06:08:25,120 --> 06:08:26,160 set 9361 06:08:26,160 --> 06:08:30,240 new user dot i'm sorry user name there 9362 06:08:30,240 --> 06:08:32,400 we go and set that equal to whatever 9363 06:08:32,400 --> 06:08:35,840 data you had and use uh dot notation 9364 06:08:35,840 --> 06:08:38,240 that way to set the values and at the 9365 06:08:38,240 --> 06:08:40,638 end of all of that you would want to 9366 06:08:40,638 --> 06:08:44,638 save so then you would say const result 9367 06:08:44,638 --> 06:08:46,878 equals and then you would have your 9368 06:08:46,878 --> 06:08:49,440 await 9369 06:08:49,680 --> 06:08:51,680 new user 9370 06:08:51,680 --> 06:08:54,000 and you would call save and that would 9371 06:08:54,000 --> 06:08:56,400 also work however that's just a little 9372 06:08:56,400 --> 06:08:58,160 bit longer process 9373 06:08:58,160 --> 06:09:00,878 you might also see something like the 9374 06:09:00,878 --> 06:09:03,600 new user being created and then passing 9375 06:09:03,600 --> 06:09:06,320 that data in like we did inside of user 9376 06:09:06,320 --> 06:09:09,360 create so instead of using dot notation 9377 06:09:09,360 --> 06:09:11,520 you might see something like 9378 06:09:11,520 --> 06:09:12,878 new user 9379 06:09:12,878 --> 06:09:14,240 equals 9380 06:09:14,240 --> 06:09:15,840 well here we define new user but then 9381 06:09:15,840 --> 06:09:18,000 it's equal to a new user and you're 9382 06:09:18,000 --> 06:09:20,240 passing in the values inside of the 9383 06:09:20,240 --> 06:09:22,480 parentheses there and then you would 9384 06:09:22,480 --> 06:09:25,120 still need to have the result equals 9385 06:09:25,120 --> 06:09:28,718 await new user.save after you did this 9386 06:09:28,718 --> 06:09:30,878 and so this happens 9387 06:09:30,878 --> 06:09:33,600 all at once when i use user create and 9388 06:09:33,600 --> 06:09:35,360 if i do it this other way and create a 9389 06:09:35,360 --> 06:09:38,638 new user then you have to have another 9390 06:09:38,638 --> 06:09:41,760 line to actually save the new user so i 9391 06:09:41,760 --> 06:09:44,080 prefer the user.create 9392 06:09:44,080 --> 06:09:46,558 let's go ahead and save this 9393 06:09:46,558 --> 06:09:48,878 now we're ready to test our register 9394 06:09:48,878 --> 06:09:50,480 controller route 9395 06:09:50,480 --> 06:09:52,320 so let's open up a terminal you can do 9396 06:09:52,320 --> 06:09:54,160 that from the terminal menu or press 9397 06:09:54,160 --> 06:09:56,480 control back tick at least i can on 9398 06:09:56,480 --> 06:10:00,080 windows and now i'm going to type npm 9399 06:10:00,080 --> 06:10:01,280 run 9400 06:10:01,280 --> 06:10:03,600 dev 9401 06:10:03,920 --> 06:10:06,718 and that should start up nodemon and 9402 06:10:06,718 --> 06:10:09,600 start our api server here just on 9403 06:10:09,600 --> 06:10:12,638 localhost port 3500. we've got our 9404 06:10:12,638 --> 06:10:15,120 message connected to mongodb 9405 06:10:15,120 --> 06:10:18,000 server's running on port 3500. i'm going 9406 06:10:18,000 --> 06:10:20,240 to use thunderclient to test everything 9407 06:10:20,240 --> 06:10:21,840 out like i have in some previous 9408 06:10:21,840 --> 06:10:24,080 tutorials if you don't have it you can 9409 06:10:24,080 --> 06:10:26,240 install thunder client as an extension 9410 06:10:26,240 --> 06:10:28,080 and then it's right here after you have 9411 06:10:28,080 --> 06:10:29,280 on the left 9412 06:10:29,280 --> 06:10:30,878 i'll click that i've got some 9413 06:10:30,878 --> 06:10:33,760 collections and under auth i should have 9414 06:10:33,760 --> 06:10:36,480 a registration route to test and now i'm 9415 06:10:36,480 --> 06:10:39,760 going to hide the left-hand 9416 06:10:39,760 --> 06:10:41,440 menu over here because it looks much 9417 06:10:41,440 --> 06:10:42,958 better and it's easier to read when it's 9418 06:10:42,958 --> 06:10:44,798 the full screen so here's the register 9419 06:10:44,798 --> 06:10:45,760 route 9420 06:10:45,760 --> 06:10:47,440 see what we're sending in the body okay 9421 06:10:47,440 --> 06:10:52,080 i've got a user named steve1 to create 9422 06:10:52,080 --> 06:10:54,320 and then it's got a password and that's 9423 06:10:54,320 --> 06:10:56,958 really all we need to send let's see if 9424 06:10:56,958 --> 06:11:00,320 the register route works 9425 06:11:00,718 --> 06:11:03,040 new user steve one created and here's 9426 06:11:03,040 --> 06:11:05,200 our object in the console so now we can 9427 06:11:05,200 --> 06:11:06,878 see what we've got 9428 06:11:06,878 --> 06:11:09,920 username steve1 the role was created 9429 06:11:09,920 --> 06:11:13,200 with user 2001 we've got our encrypted 9430 06:11:13,200 --> 06:11:16,480 password and we've got a new object id 9431 06:11:16,480 --> 06:11:18,320 and then notice this other field as well 9432 06:11:18,320 --> 06:11:20,480 that's always added the two underscores 9433 06:11:20,480 --> 06:11:23,040 and the v and the zero you should always 9434 06:11:23,040 --> 06:11:25,600 see that as well if you're curious the v 9435 06:11:25,600 --> 06:11:28,480 is the version key and it keeps track of 9436 06:11:28,480 --> 06:11:30,718 this and we can also increment this 9437 06:11:30,718 --> 06:11:33,040 manually if we want to okay now that we 9438 06:11:33,040 --> 06:11:35,520 created steve one let's create another 9439 06:11:35,520 --> 06:11:38,000 user and i'm going to go back to my wall 9440 06:11:38,000 --> 06:11:40,320 one that was an admin previously so 9441 06:11:40,320 --> 06:11:42,480 let's create him as well 9442 06:11:42,480 --> 06:11:45,040 and now new user walt one was created 9443 06:11:45,040 --> 06:11:47,360 and if we look here in the console we 9444 06:11:47,360 --> 06:11:49,200 can also see 9445 06:11:49,200 --> 06:11:50,320 that 9446 06:11:50,320 --> 06:11:52,080 walt one was created down here and 9447 06:11:52,080 --> 06:11:54,718 here's his information now let's go to 9448 06:11:54,718 --> 06:11:56,798 mongodb 9449 06:11:56,798 --> 06:11:59,120 and inside of mongodb 9450 06:11:59,120 --> 06:12:00,558 we can check 9451 06:12:00,558 --> 06:12:03,280 our company db here and we should be 9452 06:12:03,280 --> 06:12:05,200 able to refresh 9453 06:12:05,200 --> 06:12:07,600 and now we have a user's collection as 9454 06:12:07,600 --> 06:12:09,200 well and you can see it has two 9455 06:12:09,200 --> 06:12:11,200 documents so let's look at the user's 9456 06:12:11,200 --> 06:12:13,200 collection all right we load the 9457 06:12:13,200 --> 06:12:15,600 documents up and here are the 9458 06:12:15,600 --> 06:12:17,760 two records or we could call them 9459 06:12:17,760 --> 06:12:19,920 documents really i'm used to saying 9460 06:12:19,920 --> 06:12:22,320 records with sql this is a nosql 9461 06:12:22,320 --> 06:12:24,798 database and it has collections instead 9462 06:12:24,798 --> 06:12:26,558 of tables and it has 9463 06:12:26,558 --> 06:12:29,440 documents instead of records so in 9464 06:12:29,440 --> 06:12:32,798 either one of these we can expand 9465 06:12:32,798 --> 06:12:35,600 the object and see our user 2001 that 9466 06:12:35,600 --> 06:12:38,400 was created so we want to make walt an 9467 06:12:38,400 --> 06:12:42,480 editor and an admin so inside of mongodb 9468 06:12:42,480 --> 06:12:44,958 here we're mongodb.com and logged back 9469 06:12:44,958 --> 06:12:46,480 into our account and we're looking at 9470 06:12:46,480 --> 06:12:47,920 our collection 9471 06:12:47,920 --> 06:12:50,638 inside of the companydb database that 9472 06:12:50,638 --> 06:12:52,840 was created and we'd created the 9473 06:12:52,840 --> 06:12:56,080 employees collection before 9474 06:12:56,080 --> 06:12:58,878 but we had not created users so when we 9475 06:12:58,878 --> 06:13:02,240 created the first user it also created 9476 06:13:02,240 --> 06:13:04,000 the users collection which is 9477 06:13:04,000 --> 06:13:06,558 interesting but after that 9478 06:13:06,558 --> 06:13:09,200 we want to edit walt here and we can do 9479 06:13:09,200 --> 06:13:11,160 that right inside of 9480 06:13:11,160 --> 06:13:14,480 mongodb.com so let's edit and now let's 9481 06:13:14,480 --> 06:13:16,718 go ahead and add 9482 06:13:16,718 --> 06:13:20,160 another role add field after user 9483 06:13:20,160 --> 06:13:23,280 and we'll put in editor and the value 9484 06:13:23,280 --> 06:13:25,120 for the editor 9485 06:13:25,120 --> 06:13:28,638 was 1984. okay now we've completed that 9486 06:13:28,638 --> 06:13:32,080 now let's add field after editor 9487 06:13:32,080 --> 06:13:33,360 and we'll add 9488 06:13:33,360 --> 06:13:34,480 admin 9489 06:13:34,480 --> 06:13:37,920 and the value for the admin was 51.50 if 9490 06:13:37,920 --> 06:13:39,920 i remember correctly 9491 06:13:39,920 --> 06:13:42,638 and so now we've gone ahead and changed 9492 06:13:42,638 --> 06:13:44,080 that information but we still need to 9493 06:13:44,080 --> 06:13:45,920 click update here 9494 06:13:45,920 --> 06:13:46,718 to 9495 06:13:46,718 --> 06:13:48,718 update the document now the document is 9496 06:13:48,718 --> 06:13:50,480 updated and walt 9497 06:13:50,480 --> 06:13:54,400 has all three roles in his document oh 9498 06:13:54,400 --> 06:13:56,400 but notice what happened here and you 9499 06:13:56,400 --> 06:13:58,000 want to catch this if you do it because 9500 06:13:58,000 --> 06:13:59,840 i certainly just made the mistake this 9501 06:13:59,840 --> 06:14:01,920 was entered as a string and this should 9502 06:14:01,920 --> 06:14:04,718 be numeric data so we want to get rid of 9503 06:14:04,718 --> 06:14:07,120 that we don't really need to remove that 9504 06:14:07,120 --> 06:14:09,200 we need to change the data type over 9505 06:14:09,200 --> 06:14:11,120 here on the right notice how 9506 06:14:11,120 --> 06:14:14,958 user 2001 is int 32 so we want to choose 9507 06:14:14,958 --> 06:14:16,958 that type here on the right 9508 06:14:16,958 --> 06:14:18,400 and make sure 9509 06:14:18,400 --> 06:14:21,520 we have the right data type as we make 9510 06:14:21,520 --> 06:14:25,200 changes here on mongodb.com to any given 9511 06:14:25,200 --> 06:14:27,120 document okay those changes have been 9512 06:14:27,120 --> 06:14:29,760 made now let's update and now we have 9513 06:14:29,760 --> 06:14:32,718 the correct data type in waltz roles 9514 06:14:32,718 --> 06:14:37,440 2001 1984 and 5150. okay going back to 9515 06:14:37,440 --> 06:14:39,040 vs code 9516 06:14:39,040 --> 06:14:41,840 and i'll close out of thunder client 9517 06:14:41,840 --> 06:14:43,920 and close out of the terminal as well 9518 06:14:43,920 --> 06:14:46,000 but let's go ahead and show the file 9519 06:14:46,000 --> 06:14:47,520 tree because 9520 06:14:47,520 --> 06:14:49,520 we have changes to make in the 9521 06:14:49,520 --> 06:14:52,558 controllers we need to go ahead and 9522 06:14:52,558 --> 06:14:55,760 implement our user model instead of the 9523 06:14:55,760 --> 06:14:57,600 json file we've been using in the 9524 06:14:57,600 --> 06:14:59,920 refresh token controller the logout 9525 06:14:59,920 --> 06:15:02,958 controller and the auth controller and 9526 06:15:02,958 --> 06:15:06,080 then we need to implement the employee 9527 06:15:06,080 --> 06:15:09,280 model in the employees controller and 9528 06:15:09,280 --> 06:15:11,920 change how some of this works just like 9529 06:15:11,920 --> 06:15:14,080 we did in the register controller and 9530 06:15:14,080 --> 06:15:15,760 that's what we'll be doing in the next 9531 06:15:15,760 --> 06:15:18,000 tutorial but my challenge to you is 9532 06:15:18,000 --> 06:15:20,240 before then go ahead and make the 9533 06:15:20,240 --> 06:15:24,080 changes on your own and then compare to 9534 06:15:24,080 --> 06:15:26,240 the tutorial that i release that makes 9535 06:15:26,240 --> 06:15:28,958 the same changes 9536 06:15:28,958 --> 06:15:31,520 today we're going to apply the user and 9537 06:15:31,520 --> 06:15:34,240 employee models to the remaining api 9538 06:15:34,240 --> 06:15:36,958 routes and update the asynchronous crud 9539 06:15:36,958 --> 06:15:39,120 operations accordingly and before we 9540 06:15:39,120 --> 06:15:41,280 finish i'll show you an easy way to 9541 06:15:41,280 --> 06:15:43,840 deploy your rest api to the web we're 9542 06:15:43,840 --> 06:15:45,360 going to start with the code from the 9543 06:15:45,360 --> 06:15:47,520 last tutorial if you don't have it i 9544 06:15:47,520 --> 06:15:49,360 have a link to the starter source code 9545 06:15:49,360 --> 06:15:51,040 in the description below let's get 9546 06:15:51,040 --> 06:15:53,840 started we're looking at the user schema 9547 06:15:53,840 --> 06:15:55,760 and then of course we create a model 9548 06:15:55,760 --> 06:15:57,520 here at the bottom that's where we're 9549 06:15:57,520 --> 06:16:00,080 starting and we applied it to the 9550 06:16:00,080 --> 06:16:02,080 register controller near the end of the 9551 06:16:02,080 --> 06:16:03,600 last tutorial 9552 06:16:03,600 --> 06:16:05,920 and you can see we imported in the user 9553 06:16:05,920 --> 06:16:08,240 model and this replaced what we had 9554 06:16:08,240 --> 06:16:10,638 previously in the tutorial series which 9555 06:16:10,638 --> 06:16:11,600 were 9556 06:16:11,600 --> 06:16:15,040 json files now we're using mongodb 9557 06:16:15,040 --> 06:16:18,080 and we're interacting with mongodb by 9558 06:16:18,080 --> 06:16:20,638 using the mongoose schemas and models 9559 06:16:20,638 --> 06:16:23,440 that we've created so now let's apply 9560 06:16:23,440 --> 06:16:27,360 our user model to the next route and 9561 06:16:27,360 --> 06:16:30,080 instead of register let's just move up 9562 06:16:30,080 --> 06:16:32,000 one and go to the refresh token 9563 06:16:32,000 --> 06:16:34,798 controller okay you saw i copied the 9564 06:16:34,798 --> 06:16:36,798 user model import from the register 9565 06:16:36,798 --> 06:16:38,480 controller file and i'm going to paste 9566 06:16:38,480 --> 06:16:41,120 that right over the user's db that we 9567 06:16:41,120 --> 06:16:42,958 had previously defined to interact with 9568 06:16:42,958 --> 06:16:45,600 the json file so now we're bringing in 9569 06:16:45,600 --> 06:16:48,320 the user model and now we can update the 9570 06:16:48,320 --> 06:16:50,798 handle refresh token function we'll 9571 06:16:50,798 --> 06:16:52,798 start out the same here where we define 9572 06:16:52,798 --> 06:16:53,920 the cookie 9573 06:16:53,920 --> 06:16:56,400 and of course get the jwt from the 9574 06:16:56,400 --> 06:16:59,120 cookie if it exists but then as far as 9575 06:16:59,120 --> 06:17:01,840 checking to find a user we have to do 9576 06:17:01,840 --> 06:17:03,600 that differently and let's go back to 9577 06:17:03,600 --> 06:17:05,200 that register controller for just a 9578 06:17:05,200 --> 06:17:07,680 second because we did a very similar 9579 06:17:07,680 --> 06:17:11,200 thing we used user dot find one and we 9580 06:17:11,200 --> 06:17:13,760 passed in the name of the user well this 9581 06:17:13,760 --> 06:17:15,600 time we won't have the username but 9582 06:17:15,600 --> 06:17:18,718 besides that it will be very similar so 9583 06:17:18,718 --> 06:17:20,878 let's highlight all of this where we 9584 06:17:20,878 --> 06:17:23,360 were finding the refresh token 9585 06:17:23,360 --> 06:17:26,160 inside of the json and let's paste in 9586 06:17:26,160 --> 06:17:29,600 our user find one but now instead of the 9587 06:17:29,600 --> 06:17:33,280 username we can just put refresh token 9588 06:17:33,280 --> 06:17:35,520 here and that's because 9589 06:17:35,520 --> 06:17:38,160 the property name and the value name or 9590 06:17:38,160 --> 06:17:38,958 the 9591 06:17:38,958 --> 06:17:41,120 variable name are the same so instead of 9592 06:17:41,120 --> 06:17:43,440 refresh token colon refresh token we can 9593 06:17:43,440 --> 06:17:45,840 just put refresh token there we still 9594 06:17:45,840 --> 06:17:46,638 need 9595 06:17:46,638 --> 06:17:49,840 the exec here to execute and that is 9596 06:17:49,840 --> 06:17:52,638 because we're using async a weight and 9597 06:17:52,638 --> 06:17:54,558 that means we need to put async up here 9598 06:17:54,558 --> 06:17:57,520 above which i almost forgot so let's put 9599 06:17:57,520 --> 06:18:00,160 async up here by handle refresh token 9600 06:18:00,160 --> 06:18:02,080 and make this an async function and 9601 06:18:02,080 --> 06:18:04,958 we'll use await here and then we search 9602 06:18:04,958 --> 06:18:06,958 for the refresh token 9603 06:18:06,958 --> 06:18:09,440 inside of a document within the user 9604 06:18:09,440 --> 06:18:11,440 collection after that everything 9605 06:18:11,440 --> 06:18:13,600 essentially remains the same because we 9606 06:18:13,600 --> 06:18:16,878 have defined the found user if we found 9607 06:18:16,878 --> 06:18:18,878 the refresh token or not and of course 9608 06:18:18,878 --> 06:18:21,680 if not it's a 403 forbidden but after 9609 06:18:21,680 --> 06:18:24,638 that we're using the same definitions as 9610 06:18:24,638 --> 06:18:27,280 before so there's nothing else to change 9611 06:18:27,280 --> 06:18:29,360 in the refresh token route so we can 9612 06:18:29,360 --> 06:18:30,558 save the file 9613 06:18:30,558 --> 06:18:33,040 and move on to the logout controller 9614 06:18:33,040 --> 06:18:35,120 okay we'll start the same in the logout 9615 06:18:35,120 --> 06:18:37,680 controller and we will not need the fs 9616 06:18:37,680 --> 06:18:40,080 promises or path either so we can just 9617 06:18:40,080 --> 06:18:43,840 highlight all of that and paste our 9618 06:18:43,840 --> 06:18:46,320 that's not the user model is it so let's 9619 06:18:46,320 --> 06:18:49,120 change that control z to undo that i'll 9620 06:18:49,120 --> 06:18:51,440 go back here to the register controller 9621 06:18:51,440 --> 06:18:52,878 where i copied 9622 06:18:52,878 --> 06:18:55,200 the user model import before i'll copy 9623 06:18:55,200 --> 06:18:56,558 that again 9624 06:18:56,558 --> 06:18:58,638 back to the log out controller 9625 06:18:58,638 --> 06:19:01,120 highlight all of that and paste because 9626 06:19:01,120 --> 06:19:03,360 that's what we need is the user model 9627 06:19:03,360 --> 06:19:06,320 imported after that we already have the 9628 06:19:06,320 --> 06:19:08,558 note here on client also delete the 9629 06:19:08,558 --> 06:19:10,558 access token again this is the log out 9630 06:19:10,558 --> 06:19:12,400 route and that's fine 9631 06:19:12,400 --> 06:19:15,520 the next three lines just as before 9632 06:19:15,520 --> 06:19:18,320 are fine and then we need to say is the 9633 06:19:18,320 --> 06:19:20,478 refresh token in the database well that 9634 06:19:20,478 --> 06:19:22,718 sounds familiar that's just like we used 9635 06:19:22,718 --> 06:19:25,200 in the refresh token controller so we 9636 06:19:25,200 --> 06:19:26,638 come back to the refresh token 9637 06:19:26,638 --> 06:19:28,718 controller i'll scroll up where we had 9638 06:19:28,718 --> 06:19:31,440 our await user find one and passed in 9639 06:19:31,440 --> 06:19:33,200 the refresh token 9640 06:19:33,200 --> 06:19:35,520 copy that come back to the logout this 9641 06:19:35,520 --> 06:19:38,400 is already an async function that's good 9642 06:19:38,400 --> 06:19:41,200 so now we can once again define the fine 9643 06:19:41,200 --> 06:19:42,958 found user 9644 06:19:42,958 --> 06:19:44,638 in the same way 9645 06:19:44,638 --> 06:19:47,760 and now after that this remains the same 9646 06:19:47,760 --> 06:19:49,600 but now this gets much simpler where we 9647 06:19:49,600 --> 06:19:51,680 delete the refresh token we're no longer 9648 06:19:51,680 --> 06:19:53,680 interacting with any of this json here 9649 06:19:53,680 --> 06:19:56,240 so let's just highlight all of this 9650 06:19:56,240 --> 06:19:58,798 and delete and we can make a few changes 9651 06:19:58,798 --> 06:20:01,120 we can say found user 9652 06:20:01,120 --> 06:20:04,080 dot refresh token 9653 06:20:04,080 --> 06:20:06,638 and we can erase it simply by setting it 9654 06:20:06,638 --> 06:20:08,478 to an empty string 9655 06:20:08,478 --> 06:20:11,040 and then we can set a result and we'll 9656 06:20:11,040 --> 06:20:13,760 set that equal to await 9657 06:20:13,760 --> 06:20:15,680 found user 9658 06:20:15,680 --> 06:20:19,360 dot save and that will save our changes 9659 06:20:19,360 --> 06:20:22,000 back to our mongodb document that is 9660 06:20:22,000 --> 06:20:24,558 stored in the user collection and after 9661 06:20:24,558 --> 06:20:27,040 we do that we could go ahead and log the 9662 06:20:27,040 --> 06:20:29,360 result if we'd like to see that because 9663 06:20:29,360 --> 06:20:30,958 that will return 9664 06:20:30,958 --> 06:20:33,680 the newly updated information 9665 06:20:33,680 --> 06:20:35,520 and that's just for our purposes there 9666 06:20:35,520 --> 06:20:37,840 you would of course delete that before 9667 06:20:37,840 --> 06:20:40,000 you put it in production and that's all 9668 06:20:40,000 --> 06:20:42,400 the changes so we can save that and 9669 06:20:42,400 --> 06:20:43,440 again 9670 06:20:43,440 --> 06:20:45,760 remember that found user this is a 9671 06:20:45,760 --> 06:20:48,840 document now that we have found if it 9672 06:20:48,840 --> 06:20:52,160 existed and therefore we're able to 9673 06:20:52,160 --> 06:20:54,638 update that document with the save okay 9674 06:20:54,638 --> 06:20:56,400 i'll scroll back up to the top and i'm 9675 06:20:56,400 --> 06:20:59,040 going to copy that user model import one 9676 06:20:59,040 --> 06:21:01,600 more time because we need it in the auth 9677 06:21:01,600 --> 06:21:02,878 controller 9678 06:21:02,878 --> 06:21:05,280 once again i'll highlight the user's db 9679 06:21:05,280 --> 06:21:07,360 that we previously had and paste in the 9680 06:21:07,360 --> 06:21:09,520 user model instead i'm going to go ahead 9681 06:21:09,520 --> 06:21:11,840 and delete this line between bcrypt and 9682 06:21:11,840 --> 06:21:14,558 jwt we need both of those but we can go 9683 06:21:14,558 --> 06:21:17,440 ahead and once again delete fs promises 9684 06:21:17,440 --> 06:21:19,760 and path which we should no longer need 9685 06:21:19,760 --> 06:21:22,160 these first two lines are the same we'll 9686 06:21:22,160 --> 06:21:24,638 keep both of them but then once again 9687 06:21:24,638 --> 06:21:26,958 with found user i think you see the 9688 06:21:26,958 --> 06:21:28,958 pattern now now we're back to looking at 9689 06:21:28,958 --> 06:21:32,000 a user name though so we can take this 9690 06:21:32,000 --> 06:21:34,080 and go back to the register controller 9691 06:21:34,080 --> 06:21:36,958 where we had the user name with find one 9692 06:21:36,958 --> 06:21:39,920 and copy that line go back to the auth 9693 06:21:39,920 --> 06:21:43,200 controller and then we can paste that in 9694 06:21:43,200 --> 06:21:45,600 as we find the user 9695 06:21:45,600 --> 06:21:48,400 and save because the next line is fine 9696 06:21:48,400 --> 06:21:50,000 as well and then 9697 06:21:50,000 --> 06:21:52,320 the password is evaluated and we're not 9698 06:21:52,320 --> 06:21:54,718 changing anything about that and really 9699 06:21:54,718 --> 06:21:56,958 we won't make any changes until we come 9700 06:21:56,958 --> 06:21:58,478 down here once again to where we were 9701 06:21:58,478 --> 06:22:00,400 interacting with the file 9702 06:22:00,400 --> 06:22:02,638 and we will be saving the refresh token 9703 06:22:02,638 --> 06:22:04,878 with the current user so once again we 9704 06:22:04,878 --> 06:22:07,840 can delete everything we had here down 9705 06:22:07,840 --> 06:22:09,840 to the response cookie 9706 06:22:09,840 --> 06:22:12,160 and let's leave a blank line in between 9707 06:22:12,160 --> 06:22:14,240 and then we can once again say found 9708 06:22:14,240 --> 06:22:16,638 user which is our document that we have 9709 06:22:16,638 --> 06:22:17,680 found 9710 06:22:17,680 --> 06:22:20,320 and set the refresh token 9711 06:22:20,320 --> 06:22:22,718 we're going to set that equal to 9712 06:22:22,718 --> 06:22:25,120 the refresh token that we now have here 9713 06:22:25,120 --> 06:22:26,958 in the auth controller 9714 06:22:26,958 --> 06:22:29,520 and then let's define result again 9715 06:22:29,520 --> 06:22:32,400 and let's set result equal to await 9716 06:22:32,400 --> 06:22:34,400 found user 9717 06:22:34,400 --> 06:22:36,558 dot save and that will update the 9718 06:22:36,558 --> 06:22:38,718 document back in mongodb and let's go 9719 06:22:38,718 --> 06:22:40,718 ahead and log that result again as well 9720 06:22:40,718 --> 06:22:43,200 so that should also look very familiar 9721 06:22:43,200 --> 06:22:45,680 and that completes the auth controller 9722 06:22:45,680 --> 06:22:47,680 so i know we tested the register 9723 06:22:47,680 --> 06:22:49,920 controller the register route in the 9724 06:22:49,920 --> 06:22:51,840 last tutorial but now we'll want to go 9725 06:22:51,840 --> 06:22:54,320 back and test all the rest of these so 9726 06:22:54,320 --> 06:22:57,040 we've got the refresh token route we've 9727 06:22:57,040 --> 06:23:00,000 got a logout route and of course we've 9728 06:23:00,000 --> 06:23:02,878 got the auth route as well so now let's 9729 06:23:02,878 --> 06:23:05,760 test those once again with thunderclient 9730 06:23:05,760 --> 06:23:07,120 if you don't have thunderclient 9731 06:23:07,120 --> 06:23:09,120 installed it's an extension for visual 9732 06:23:09,120 --> 06:23:10,558 studio code 9733 06:23:10,558 --> 06:23:12,080 and you get that right here under 9734 06:23:12,080 --> 06:23:14,000 extensions and search for thunder client 9735 06:23:14,000 --> 06:23:16,400 and then you'll have it on the left 9736 06:23:16,400 --> 06:23:18,718 and this is thunder client it allows you 9737 06:23:18,718 --> 06:23:20,878 to save collections which i've already 9738 06:23:20,878 --> 06:23:23,040 saved to match up to this but before we 9739 06:23:23,040 --> 06:23:25,520 can test it we actually need to start 9740 06:23:25,520 --> 06:23:27,280 our dev server i'm going to press 9741 06:23:27,280 --> 06:23:29,840 control back tick you could also go to 9742 06:23:29,840 --> 06:23:33,040 the terminal menu at the top and choose 9743 06:23:33,040 --> 06:23:35,360 a new terminal window that way then we 9744 06:23:35,360 --> 06:23:39,520 type npm run dev to launch our dev 9745 06:23:39,520 --> 06:23:40,478 server 9746 06:23:40,478 --> 06:23:43,200 and we should be running on 3500 and 9747 06:23:43,200 --> 06:23:45,440 connected to mongodb here in a second 9748 06:23:45,440 --> 06:23:47,280 when it gives us our information and 9749 06:23:47,280 --> 06:23:50,080 there we see both connected to mongodb 9750 06:23:50,080 --> 06:23:53,040 server running on port 3500 so it's now 9751 06:23:53,040 --> 06:23:55,760 ready to test so i've saved a collection 9752 06:23:55,760 --> 06:23:58,000 named auth and then here we have 9753 06:23:58,000 --> 06:23:58,958 different 9754 06:23:58,958 --> 06:24:01,440 routes if i can see which route we have 9755 06:24:01,440 --> 06:24:03,200 here looks like this is the register 9756 06:24:03,200 --> 06:24:04,878 route let's go ahead and try that i'm 9757 06:24:04,878 --> 06:24:06,558 going to click thunder client to hide 9758 06:24:06,558 --> 06:24:07,760 the left hand 9759 06:24:07,760 --> 06:24:10,320 side so we can see better and let's look 9760 06:24:10,320 --> 06:24:11,920 at the body to see what we're going to 9761 06:24:11,920 --> 06:24:14,638 register we're going to register a user 9762 06:24:14,638 --> 06:24:16,798 wall one with the password i believe 9763 06:24:16,798 --> 06:24:18,718 he's already registered so we should get 9764 06:24:18,718 --> 06:24:21,360 a 409 conflict back let's see what we 9765 06:24:21,360 --> 06:24:22,160 get 9766 06:24:22,160 --> 06:24:25,120 that's exactly right 409 conflicts so 9767 06:24:25,120 --> 06:24:26,878 let's change this 9768 06:24:26,878 --> 06:24:29,920 to tom one i don't believe i have tom 9769 06:24:29,920 --> 06:24:33,040 one in my collection of users so now 9770 06:24:33,040 --> 06:24:34,638 we'll send this 9771 06:24:34,638 --> 06:24:38,240 and 201 created so we have a new user 9772 06:24:38,240 --> 06:24:40,718 created our register route is working 9773 06:24:40,718 --> 06:24:43,360 now let's go back to thunder client and 9774 06:24:43,360 --> 06:24:44,878 look at the other routes that was the 9775 06:24:44,878 --> 06:24:46,718 register route now that we've registered 9776 06:24:46,718 --> 06:24:50,080 let's test the auth route now let's see 9777 06:24:50,080 --> 06:24:52,160 who we want to authorize well i believe 9778 06:24:52,160 --> 06:24:53,760 walt one was already created we 9779 06:24:53,760 --> 06:24:55,680 confirmed that so we can just send in 9780 06:24:55,680 --> 06:24:58,878 this information to authorize and we 9781 06:24:58,878 --> 06:25:01,440 send it in and we've got a new access 9782 06:25:01,440 --> 06:25:02,798 token back 9783 06:25:02,798 --> 06:25:05,520 and everything is okay he is authorized 9784 06:25:05,520 --> 06:25:07,680 and of course you see down here 9785 06:25:07,680 --> 06:25:10,160 we're logging these responses that we're 9786 06:25:10,160 --> 06:25:12,638 also getting back these console log 9787 06:25:12,638 --> 06:25:14,638 statements that had the result 9788 06:25:14,638 --> 06:25:16,958 and that's what we see here is the user 9789 06:25:16,958 --> 06:25:19,280 that we've pulled in is walt one okay 9790 06:25:19,280 --> 06:25:20,958 i'm going to pull this down and i'm 9791 06:25:20,958 --> 06:25:23,280 going to authorize again because if you 9792 06:25:23,280 --> 06:25:25,200 remember right 9793 06:25:25,200 --> 06:25:27,520 we only get an access token the way we 9794 06:25:27,520 --> 06:25:30,080 have it set that works for about 30 9795 06:25:30,080 --> 06:25:32,400 seconds but really i guess we're not 9796 06:25:32,400 --> 06:25:34,320 testing that so wait a minute when we're 9797 06:25:34,320 --> 06:25:36,000 not too worried about that because we're 9798 06:25:36,000 --> 06:25:38,000 going to test either the log out or 9799 06:25:38,000 --> 06:25:40,080 refresh token route let's test that 9800 06:25:40,080 --> 06:25:41,840 refresh token route that i have in a 9801 06:25:41,840 --> 06:25:43,520 separate collection 9802 06:25:43,520 --> 06:25:45,920 and so with the refresh token we're 9803 06:25:45,920 --> 06:25:48,558 sending a cookie it says no cookies 9804 06:25:48,558 --> 06:25:50,558 available but i think we have a cookie 9805 06:25:50,558 --> 06:25:53,040 let's try it out 9806 06:25:53,040 --> 06:25:56,240 401 unauthorized we did not get a cookie 9807 06:25:56,240 --> 06:25:58,878 i think we're running into our secure 9808 06:25:58,878 --> 06:26:01,200 site error where thunder client doesn't 9809 06:26:01,200 --> 06:26:03,360 let us test even though we need that 9810 06:26:03,360 --> 06:26:06,000 secure site option in there 9811 06:26:06,000 --> 06:26:08,558 for chrome so let's take this out 9812 06:26:08,558 --> 06:26:11,600 temporarily i'm in the auth controller 9813 06:26:11,600 --> 06:26:14,320 as we issue the refresh token 9814 06:26:14,320 --> 06:26:16,160 let's take this out and put it in a 9815 06:26:16,160 --> 06:26:19,120 comment near the end in production once 9816 06:26:19,120 --> 06:26:22,000 again you would want this back in there 9817 06:26:22,000 --> 06:26:24,958 however we can't test it with thunder 9818 06:26:24,958 --> 06:26:27,040 client with it in there so let's go 9819 06:26:27,040 --> 06:26:29,760 ahead and save now 9820 06:26:29,760 --> 06:26:32,558 and once we've got the server updated 9821 06:26:32,558 --> 06:26:35,520 let's go ahead and run the auth again 9822 06:26:35,520 --> 06:26:38,000 and then we should be able to test the 9823 06:26:38,000 --> 06:26:40,638 refresh and now we're up and running 9824 06:26:40,638 --> 06:26:42,718 connected to mongodb server running on 9825 06:26:42,718 --> 06:26:45,280 port 3500 let's try this again in 9826 06:26:45,280 --> 06:26:47,840 thunder client we'll go to the auth 9827 06:26:47,840 --> 06:26:49,200 first 9828 06:26:49,200 --> 06:26:51,760 hide this over here i'll go ahead and 9829 06:26:51,760 --> 06:26:53,680 log 9830 06:26:53,680 --> 06:26:56,320 and we've got a cookie let's go to the 9831 06:26:56,320 --> 06:26:58,638 refresh now 9832 06:26:58,638 --> 06:27:00,320 and come back before we were 9833 06:27:00,320 --> 06:27:02,558 unauthorized but let's see if this works 9834 06:27:02,558 --> 06:27:04,878 now and yes it does 9835 06:27:04,878 --> 06:27:06,558 our cookie works you just have to take 9836 06:27:06,558 --> 06:27:08,958 that secure out while testing with 9837 06:27:08,958 --> 06:27:11,520 thunder client so the refresh route is 9838 06:27:11,520 --> 06:27:14,160 working we have tested the register 9839 06:27:14,160 --> 06:27:16,638 route we have tested the auth route 9840 06:27:16,638 --> 06:27:18,558 we've tested the refresh route what is 9841 06:27:18,558 --> 06:27:22,000 left is the log out route so once again 9842 06:27:22,000 --> 06:27:24,798 we'll probably need a fresh login to 9843 06:27:24,798 --> 06:27:27,040 ensure we are logging out correctly so 9844 06:27:27,040 --> 06:27:29,040 let's go to the auth 9845 06:27:29,040 --> 06:27:31,200 and now let's see who we're logging in 9846 06:27:31,200 --> 06:27:34,638 it looks like walt won that's fine 9847 06:27:34,638 --> 06:27:37,440 we'll send that and now let's go to the 9848 06:27:37,440 --> 06:27:39,360 log out route 9849 06:27:39,360 --> 06:27:40,240 and 9850 06:27:40,240 --> 06:27:42,558 come over here in thunder client and it 9851 06:27:42,558 --> 06:27:43,840 looks like we actually don't need to 9852 06:27:43,840 --> 06:27:45,600 send anything it's just going to log out 9853 06:27:45,600 --> 06:27:47,760 the refresh token that's right 9854 06:27:47,760 --> 06:27:50,478 so we'll just send 9855 06:27:50,478 --> 06:27:53,840 and 204 no content that's fine we didn't 9856 06:27:53,840 --> 06:27:55,920 expect any content to come back but we 9857 06:27:55,920 --> 06:27:58,080 can see down here because we logged the 9858 06:27:58,080 --> 06:27:59,120 result 9859 06:27:59,120 --> 06:28:01,280 we logged out 9860 06:28:01,280 --> 06:28:03,840 there's the route we went ahead and 9861 06:28:03,840 --> 06:28:06,320 logged out and we logged out our user 9862 06:28:06,320 --> 06:28:09,040 walt 1 and we set the refresh token to 9863 06:28:09,040 --> 06:28:11,520 an empty string okay those updates using 9864 06:28:11,520 --> 06:28:14,000 the user model were not too bad at all 9865 06:28:14,000 --> 06:28:16,080 let's go back to the file and what we 9866 06:28:16,080 --> 06:28:18,558 want to do now is update the employees 9867 06:28:18,558 --> 06:28:19,840 controller 9868 06:28:19,840 --> 06:28:23,520 with the employee model and this will be 9869 06:28:23,520 --> 06:28:25,280 just a little bit more work than what we 9870 06:28:25,280 --> 06:28:27,440 had previously had but we'll start out 9871 06:28:27,440 --> 06:28:28,798 once again 9872 06:28:28,798 --> 06:28:31,520 by eliminating the json file and let's 9873 06:28:31,520 --> 06:28:33,600 go ahead and import our employee model 9874 06:28:33,600 --> 06:28:35,440 so we'll have const 9875 06:28:35,440 --> 06:28:38,320 employee and we'll set that equal to 9876 06:28:38,320 --> 06:28:40,240 require 9877 06:28:40,240 --> 06:28:42,958 and then we need to go up one folder 9878 06:28:42,958 --> 06:28:45,760 find the model folder and then employee 9879 06:28:45,760 --> 06:28:47,360 that is correct 9880 06:28:47,360 --> 06:28:49,200 and now let's look at our get all 9881 06:28:49,200 --> 06:28:51,760 employees route well before we just grab 9882 06:28:51,760 --> 06:28:53,760 the json and return that now this will 9883 06:28:53,760 --> 06:28:56,798 be just a little bit more 9884 06:28:56,798 --> 06:28:58,320 to do with 9885 06:28:58,320 --> 06:29:00,400 mongodb and of course this will be an 9886 06:29:00,400 --> 06:29:02,958 async operation so let's remove this 9887 06:29:02,958 --> 06:29:05,120 response json for now 9888 06:29:05,120 --> 06:29:08,320 and we'll define employees 9889 06:29:08,320 --> 06:29:10,160 and let's set this equal to 9890 06:29:10,160 --> 06:29:12,600 await 9891 06:29:12,600 --> 06:29:15,280 employee.find and by calling find like 9892 06:29:15,280 --> 06:29:17,680 this it will return all of the employees 9893 06:29:17,680 --> 06:29:19,440 then we can say if 9894 06:29:19,440 --> 06:29:21,920 there are no employees 9895 06:29:21,920 --> 06:29:23,680 we're going to return and then we'll 9896 06:29:23,680 --> 06:29:25,200 send a response 9897 06:29:25,200 --> 06:29:28,478 with a status 204 which is no content 9898 06:29:28,478 --> 06:29:29,440 and then 9899 06:29:29,440 --> 06:29:32,798 json and we'll send a message here 9900 06:29:32,798 --> 06:29:35,040 and in our json message we'll just say 9901 06:29:35,040 --> 06:29:38,400 it's a string no employees 9902 06:29:38,400 --> 06:29:41,120 found and after that we'll assume we do 9903 06:29:41,120 --> 06:29:43,120 have employees if we've made it this far 9904 06:29:43,120 --> 06:29:45,600 so we'll say response.json 9905 06:29:45,600 --> 06:29:47,840 and we'll send the employees and i can 9906 06:29:47,840 --> 06:29:49,920 see i've got an error i put brackets 9907 06:29:49,920 --> 06:29:52,240 instead of curly braces 9908 06:29:52,240 --> 06:29:54,958 go ahead and eliminate that bracket and 9909 06:29:54,958 --> 06:29:57,200 replace this one as well and we should 9910 06:29:57,200 --> 06:30:00,080 be good so let's go ahead and save this 9911 06:30:00,080 --> 06:30:01,680 now let's look at the create new 9912 06:30:01,680 --> 06:30:04,160 employee function which will also be 9913 06:30:04,160 --> 06:30:07,040 async so let's start here 9914 06:30:07,040 --> 06:30:08,478 and with that 9915 06:30:08,478 --> 06:30:10,478 we can probably go ahead and change 9916 06:30:10,478 --> 06:30:12,638 quite a few things that we have in here 9917 06:30:12,638 --> 06:30:14,558 so i'm just going to highlight all of 9918 06:30:14,558 --> 06:30:15,600 this 9919 06:30:15,600 --> 06:30:17,520 and remove it 9920 06:30:17,520 --> 06:30:19,840 and after that we'll start out with an 9921 06:30:19,840 --> 06:30:21,920 if statement but different than we have 9922 06:30:21,920 --> 06:30:25,520 below this will be if a request and now 9923 06:30:25,520 --> 06:30:27,840 we can use optional chaining to say as a 9924 06:30:27,840 --> 06:30:28,958 body 9925 06:30:28,958 --> 06:30:31,440 and then if it has a first name because 9926 06:30:31,440 --> 06:30:33,280 we need all of that 9927 06:30:33,280 --> 06:30:34,160 or 9928 06:30:34,160 --> 06:30:36,080 we can say the same thing 9929 06:30:36,080 --> 06:30:37,600 about the last name essentially we're 9930 06:30:37,600 --> 06:30:40,240 saying if there is no first name or last 9931 06:30:40,240 --> 06:30:41,440 name 9932 06:30:41,440 --> 06:30:43,520 and we would expect the request to have 9933 06:30:43,520 --> 06:30:45,440 a body but for some reason 9934 06:30:45,440 --> 06:30:47,520 if it doesn't we'll catch that as well 9935 06:30:47,520 --> 06:30:50,558 so if we don't have any of those 9936 06:30:50,558 --> 06:30:52,478 let's go ahead and send a response here 9937 06:30:52,478 --> 06:30:54,000 and we'll return 9938 06:30:54,000 --> 06:30:56,000 response dot status and this would be a 9939 06:30:56,000 --> 06:30:58,878 400 because it would be a bad request 9940 06:30:58,878 --> 06:31:02,000 there's json curly braces this time 9941 06:31:02,000 --> 06:31:03,040 message 9942 06:31:03,040 --> 06:31:06,478 and our message will be first 9943 06:31:06,478 --> 06:31:08,638 and last names 9944 06:31:08,638 --> 06:31:10,400 are required and you can see we had 9945 06:31:10,400 --> 06:31:12,558 something very similar below but it was 9946 06:31:12,558 --> 06:31:14,958 just a little different than that so now 9947 06:31:14,958 --> 06:31:17,120 let's eliminate that as well let's just 9948 06:31:17,120 --> 06:31:19,200 go ahead and delete these lines too and 9949 06:31:19,200 --> 06:31:21,200 we'll start over just a little bit and 9950 06:31:21,200 --> 06:31:23,280 put an extra line there and we'll do a 9951 06:31:23,280 --> 06:31:25,120 try catch block 9952 06:31:25,120 --> 06:31:29,360 and of course we have the catch after 9953 06:31:29,360 --> 06:31:31,520 there's our error and if we catch an 9954 06:31:31,520 --> 06:31:33,600 error let's just console 9955 06:31:33,600 --> 06:31:35,040 error the 9956 06:31:35,040 --> 06:31:38,000 error okay but in the try block 9957 06:31:38,000 --> 06:31:40,638 now let's define a result 9958 06:31:40,638 --> 06:31:42,558 and set this equal to 9959 06:31:42,558 --> 06:31:44,080 a weight 9960 06:31:44,080 --> 06:31:46,958 use our employee model and now we create 9961 06:31:46,958 --> 06:31:49,040 we'll create a record here 9962 06:31:49,040 --> 06:31:50,718 and we'll say 9963 06:31:50,718 --> 06:31:51,920 first name 9964 06:31:51,920 --> 06:31:55,160 is request.body.firstname 9965 06:31:56,000 --> 06:31:58,080 and lastname would be much the same 9966 06:31:58,080 --> 06:32:00,400 pattern 9967 06:32:00,958 --> 06:32:01,478 last request.body.lastname 9968 06:32:02,958 --> 06:32:05,680 and i'll put my semicolon here and save 9969 06:32:05,680 --> 06:32:07,600 but we still need to send a response so 9970 06:32:07,600 --> 06:32:09,600 we'll say response.json 9971 06:32:09,600 --> 06:32:11,840 oh let's put a status first 9972 06:32:11,840 --> 06:32:14,718 and the status here would be a 201 which 9973 06:32:14,718 --> 06:32:16,558 stands for created 9974 06:32:16,558 --> 06:32:18,558 and then we'll send the result back with 9975 06:32:18,558 --> 06:32:20,718 the record that we have created okay 9976 06:32:20,718 --> 06:32:22,878 let's scroll down now and look at the 9977 06:32:22,878 --> 06:32:25,680 update employee once again it will be an 9978 06:32:25,680 --> 06:32:27,600 async function 9979 06:32:27,600 --> 06:32:29,840 and after that we'll start out a little 9980 06:32:29,840 --> 06:32:33,200 differently so let's go ahead and delete 9981 06:32:33,200 --> 06:32:34,080 and 9982 06:32:34,080 --> 06:32:35,680 well we don't need to delete that line 9983 06:32:35,680 --> 06:32:37,520 let's just delete the top line to begin 9984 06:32:37,520 --> 06:32:41,040 with okay so we'll start out with if and 9985 06:32:41,040 --> 06:32:42,638 once again 9986 06:32:42,638 --> 06:32:45,280 request and let's use optional chaining 9987 06:32:45,280 --> 06:32:48,638 body and now we're looking for 9988 06:32:48,638 --> 06:32:50,240 an id 9989 06:32:50,240 --> 06:32:53,520 and if we don't have the id we'll 9990 06:32:53,520 --> 06:32:56,000 return response status 9991 06:32:56,000 --> 06:32:57,600 400 9992 06:32:57,600 --> 06:32:59,680 and after that we can just give a 9993 06:32:59,680 --> 06:33:02,240 message in json 9994 06:33:02,240 --> 06:33:03,840 and let's see if it's the same message 9995 06:33:03,840 --> 06:33:05,760 that we have below 9996 06:33:05,760 --> 06:33:08,320 not quite the same so here let's add a 9997 06:33:08,320 --> 06:33:11,200 little message different and we'll say 9998 06:33:11,200 --> 06:33:12,958 message 9999 06:33:12,958 --> 06:33:15,280 an id 10000 06:33:15,280 --> 06:33:19,280 parameter is required 10001 06:33:20,080 --> 06:33:22,080 okay so we've confirmed we now have an 10002 06:33:22,080 --> 06:33:24,798 id if we make it to the next spot 10003 06:33:24,798 --> 06:33:29,920 and here we can define our employee 10004 06:33:30,000 --> 06:33:33,440 so we'll say const 10005 06:33:33,920 --> 06:33:36,878 employee and set this equal to 10006 06:33:36,878 --> 06:33:40,160 a weight call our employee model and 10007 06:33:40,160 --> 06:33:43,200 we'll use find one here 10008 06:33:43,200 --> 06:33:46,558 and now remember mongodb uses an 10009 06:33:46,558 --> 06:33:49,200 underscore id it automatically generates 10010 06:33:49,200 --> 06:33:52,040 that and we're going to compare that to 10011 06:33:52,040 --> 06:33:54,478 request.body.id that is passed in as a 10012 06:33:54,478 --> 06:33:55,840 parameter 10013 06:33:55,840 --> 06:33:58,718 and after that we need to call the exec 10014 06:33:58,718 --> 06:34:00,478 at the end to call that function into 10015 06:34:00,478 --> 06:34:02,638 action now let's go ahead and delete the 10016 06:34:02,638 --> 06:34:05,440 next line here and we can say if there's 10017 06:34:05,440 --> 06:34:06,958 no employee 10018 06:34:06,958 --> 06:34:10,160 return status let's change this to a 204 10019 06:34:10,160 --> 06:34:12,240 because it's not really a bad request at 10020 06:34:12,240 --> 06:34:14,558 this point it just means that they 10021 06:34:14,558 --> 06:34:17,040 requested an id that doesn't exist they 10022 06:34:17,040 --> 06:34:18,000 might have 10023 06:34:18,000 --> 06:34:20,320 issued the request properly but now 10024 06:34:20,320 --> 06:34:21,520 let's say 10025 06:34:21,520 --> 06:34:23,440 something just a little different we'll 10026 06:34:23,440 --> 06:34:27,360 say no employee matches 10027 06:34:29,520 --> 06:34:32,320 and id and now let's get rid of this at 10028 06:34:32,320 --> 06:34:34,718 the end and get rid of the not found 10029 06:34:34,718 --> 06:34:37,520 part no employee matches whatever id was 10030 06:34:37,520 --> 06:34:39,040 passed in there 10031 06:34:39,040 --> 06:34:41,520 okay so we're sending our status 204 10032 06:34:41,520 --> 06:34:43,360 we'll save that much now let's look at 10033 06:34:43,360 --> 06:34:46,240 this if the request dot body and let's 10034 06:34:46,240 --> 06:34:49,520 go ahead and optionally chain 10035 06:34:49,520 --> 06:34:51,600 first name 10036 06:34:51,600 --> 06:34:53,760 and last name 10037 06:34:53,760 --> 06:34:55,920 and then we can say employee first name 10038 06:34:55,920 --> 06:34:57,840 equals the request.body first name and 10039 06:34:57,840 --> 06:35:00,478 last name is the same that's fine 10040 06:35:00,478 --> 06:35:02,718 so we're just optionally chaining that's 10041 06:35:02,718 --> 06:35:04,240 all we change there that's different 10042 06:35:04,240 --> 06:35:06,400 than we had before and now this gets 10043 06:35:06,400 --> 06:35:09,120 much simpler here we're not sorting any 10044 06:35:09,120 --> 06:35:11,520 json data or 10045 06:35:11,520 --> 06:35:13,760 creating new ids or anything there so 10046 06:35:13,760 --> 06:35:15,760 let's remove all of that and we'll just 10047 06:35:15,760 --> 06:35:17,520 say const 10048 06:35:17,520 --> 06:35:19,600 result equals 10049 06:35:19,600 --> 06:35:21,040 a weight 10050 06:35:21,040 --> 06:35:23,600 employee notice this is the employee 10051 06:35:23,600 --> 06:35:26,320 document we've created or we've found 10052 06:35:26,320 --> 06:35:27,680 actually we didn't create it's the 10053 06:35:27,680 --> 06:35:30,558 employee here not the employee model but 10054 06:35:30,558 --> 06:35:33,360 employee dot save 10055 06:35:33,360 --> 06:35:35,680 and now we've saved any changes we've 10056 06:35:35,680 --> 06:35:37,920 made to this employee document and then 10057 06:35:37,920 --> 06:35:40,000 instead of data employees 10058 06:35:40,000 --> 06:35:41,920 we'll just send the result we've defined 10059 06:35:41,920 --> 06:35:42,798 here 10060 06:35:42,798 --> 06:35:45,760 and save and that is our update employee 10061 06:35:45,760 --> 06:35:48,000 function okay let's scroll up and take a 10062 06:35:48,000 --> 06:35:50,400 look at delete employee which should 10063 06:35:50,400 --> 06:35:52,798 also get a little simpler but we're 10064 06:35:52,798 --> 06:35:54,320 going to start out much like we did 10065 06:35:54,320 --> 06:35:57,680 before so we'll say if there is no 10066 06:35:57,680 --> 06:36:00,400 request and will optionally chain 10067 06:36:00,400 --> 06:36:02,558 body and then we'll optionally chain the 10068 06:36:02,558 --> 06:36:04,000 id 10069 06:36:04,000 --> 06:36:06,400 and we're going to return response dot 10070 06:36:06,400 --> 06:36:07,840 status 10071 06:36:07,840 --> 06:36:09,360 400 10072 06:36:09,360 --> 06:36:12,638 and that should also be json 10073 06:36:12,638 --> 06:36:15,760 and here we'll have a message 10074 06:36:15,760 --> 06:36:18,000 and our json will say 10075 06:36:18,000 --> 06:36:19,680 employee 10076 06:36:19,680 --> 06:36:21,680 id required 10077 06:36:21,680 --> 06:36:23,760 and put the semicolon there and now we 10078 06:36:23,760 --> 06:36:26,000 need to define our employee again which 10079 06:36:26,000 --> 06:36:28,000 would be much the same as we had before 10080 06:36:28,000 --> 06:36:30,798 really it's the find one with the body 10081 06:36:30,798 --> 06:36:34,000 id so let's just scroll up here and copy 10082 06:36:34,000 --> 06:36:36,718 exactly what we had before 10083 06:36:36,718 --> 06:36:39,360 and come down 10084 06:36:39,360 --> 06:36:42,240 and here we have defined the employee id 10085 06:36:42,240 --> 06:36:44,878 again and so then our 10086 06:36:44,878 --> 06:36:47,040 employee not found will probably be the 10087 06:36:47,040 --> 06:36:50,080 same as well so we can copy that 10088 06:36:50,080 --> 06:36:52,320 and come down here as well and just copy 10089 06:36:52,320 --> 06:36:55,280 this over and paste 10090 06:36:55,280 --> 06:36:57,360 and then after that we will not be 10091 06:36:57,360 --> 06:36:59,600 filtering so let's just remove these 10092 06:36:59,600 --> 06:37:01,840 lines here once again 10093 06:37:01,840 --> 06:37:04,000 and we'll say const 10094 06:37:04,000 --> 06:37:05,200 result 10095 06:37:05,200 --> 06:37:07,440 equals 10096 06:37:07,440 --> 06:37:11,280 await employee dot delete one now 10097 06:37:11,280 --> 06:37:13,200 instead of find one 10098 06:37:13,200 --> 06:37:14,638 and here once again this will be 10099 06:37:14,638 --> 06:37:16,798 underscore id and then it will be the 10100 06:37:16,798 --> 06:37:20,160 request dot body dot id that was passed 10101 06:37:20,160 --> 06:37:21,120 in 10102 06:37:21,120 --> 06:37:24,478 we do not need an exec after delete one 10103 06:37:24,478 --> 06:37:28,000 and then we will put in 10104 06:37:28,000 --> 06:37:30,958 the result and if you're wondering why 10105 06:37:30,958 --> 06:37:32,958 we don't need the exec here it's just 10106 06:37:32,958 --> 06:37:35,280 all based on the documents so once again 10107 06:37:35,280 --> 06:37:37,680 look at mongoosejs.com 10108 06:37:37,680 --> 06:37:40,798 and those docs and you'll see which 10109 06:37:40,798 --> 06:37:42,958 different methods you need to put that 10110 06:37:42,958 --> 06:37:45,040 after and which ones you don't and 10111 06:37:45,040 --> 06:37:47,200 finally let's check the get employee 10112 06:37:47,200 --> 06:37:49,600 route as well and we need to put in a 10113 06:37:49,600 --> 06:37:51,840 check as we had before with the others 10114 06:37:51,840 --> 06:37:53,680 let's check the request if there's no 10115 06:37:53,680 --> 06:37:55,440 request 10116 06:37:55,440 --> 06:37:56,558 dot 10117 06:37:56,558 --> 06:37:59,440 params this time and then of course 10118 06:37:59,440 --> 06:38:01,760 we're looking for an id once again 10119 06:38:01,760 --> 06:38:04,320 and we have a similar message at the end 10120 06:38:04,320 --> 06:38:06,798 so let's just copy this over 10121 06:38:06,798 --> 06:38:09,840 and paste it in because that is required 10122 06:38:09,840 --> 06:38:11,840 and now we need to find an employee 10123 06:38:11,840 --> 06:38:14,478 again so we can copy the same line as 10124 06:38:14,478 --> 06:38:17,040 we've defined the employee before 10125 06:38:17,040 --> 06:38:19,120 and we'll come back down here and set 10126 06:38:19,120 --> 06:38:22,080 employee equal to the find one statement 10127 06:38:22,080 --> 06:38:24,000 if we don't find the employee well 10128 06:38:24,000 --> 06:38:26,000 that's a repeat of this i'll just copy 10129 06:38:26,000 --> 06:38:28,558 this return line here and paste it in 10130 06:38:28,558 --> 06:38:29,680 here 10131 06:38:29,680 --> 06:38:31,600 and then we're already set because we 10132 06:38:31,600 --> 06:38:33,600 just want to return the employee as we 10133 06:38:33,600 --> 06:38:37,040 were before as this is the get employee 10134 06:38:37,040 --> 06:38:39,120 route or handler just for the one 10135 06:38:39,120 --> 06:38:40,798 employee and of course we're expecting 10136 06:38:40,798 --> 06:38:43,840 that id parameter to be sent in so let's 10137 06:38:43,840 --> 06:38:46,798 save that and we have completed 10138 06:38:46,798 --> 06:38:48,638 our async 10139 06:38:48,638 --> 06:38:51,360 functions let's put async with that as 10140 06:38:51,360 --> 06:38:53,680 well and if we didn't with the others it 10141 06:38:53,680 --> 06:38:55,920 looks like i also forgot to do that with 10142 06:38:55,920 --> 06:38:58,080 the delete employee let me check the 10143 06:38:58,080 --> 06:38:59,440 rest 10144 06:38:59,440 --> 06:39:01,440 we need to have all of these functions 10145 06:39:01,440 --> 06:39:04,160 as async functions and they complete the 10146 06:39:04,160 --> 06:39:06,400 different crud operations create read 10147 06:39:06,400 --> 06:39:08,160 update and delete 10148 06:39:08,160 --> 06:39:10,718 for the employees collection before we 10149 06:39:10,718 --> 06:39:13,280 test the routes there is one error to 10150 06:39:13,280 --> 06:39:16,240 fix notice i was using params as we're 10151 06:39:16,240 --> 06:39:18,558 using a git route to pull that in but i 10152 06:39:18,558 --> 06:39:21,600 put request body dot id 10153 06:39:21,600 --> 06:39:24,190 in the function and this needs to be 10154 06:39:24,190 --> 06:39:25,638 [Music] 10155 06:39:25,638 --> 06:39:27,440 params.id 10156 06:39:27,440 --> 06:39:30,000 to access that value that we get from 10157 06:39:30,000 --> 06:39:32,798 the url in the route okay so now that we 10158 06:39:32,798 --> 06:39:35,200 have fixed this and we're now using 10159 06:39:35,200 --> 06:39:38,798 params let's test the routes and we need 10160 06:39:38,798 --> 06:39:42,718 a jwt to be authorized so let's go ahead 10161 06:39:42,718 --> 06:39:45,120 and use the auth route you might want to 10162 06:39:45,120 --> 06:39:49,200 change your code to timeout or for a 10163 06:39:49,200 --> 06:39:52,320 longer timeout duration than 30 seconds 10164 06:39:52,320 --> 06:39:54,160 mine is set to 30 seconds so i've got to 10165 06:39:54,160 --> 06:39:56,160 be a little fast to test these reps but 10166 06:39:56,160 --> 06:39:58,558 let's go ahead and get authorized 10167 06:39:58,558 --> 06:40:01,440 now we've got a new access token 10168 06:40:01,440 --> 06:40:03,760 we can jump back over here i'll open the 10169 06:40:03,760 --> 06:40:06,080 employees routes i want to get all 10170 06:40:06,080 --> 06:40:07,520 employees 10171 06:40:07,520 --> 06:40:09,760 and now for the authorization i need to 10172 06:40:09,760 --> 06:40:12,798 paste in the new token and we get our 10173 06:40:12,798 --> 06:40:15,680 one employee walt walters 10174 06:40:15,680 --> 06:40:17,920 i'm going to copy his id 10175 06:40:17,920 --> 06:40:19,920 open the terminal window 10176 06:40:19,920 --> 06:40:21,920 and just paste his id down there because 10177 06:40:21,920 --> 06:40:23,520 i'm going to need it to test the 10178 06:40:23,520 --> 06:40:26,400 individual get employee route 10179 06:40:26,400 --> 06:40:28,400 so we'll come back now and get 10180 06:40:28,400 --> 06:40:30,320 authorized once again 10181 06:40:30,320 --> 06:40:33,120 collapse the employees api 10182 06:40:33,120 --> 06:40:36,240 routes and check the authorization get a 10183 06:40:36,240 --> 06:40:38,478 new access token 10184 06:40:38,478 --> 06:40:42,400 once we've got the new access token 10185 06:40:42,400 --> 06:40:45,360 go back to the employees api go to the 10186 06:40:45,360 --> 06:40:48,320 get an employee route an individual 10187 06:40:48,320 --> 06:40:49,600 employee 10188 06:40:49,600 --> 06:40:50,958 and 10189 06:40:50,958 --> 06:40:53,120 paste that token in here and i need the 10190 06:40:53,120 --> 06:40:56,160 id to put in the url 10191 06:40:56,160 --> 06:40:58,798 i'll paste that in and send 10192 06:40:58,798 --> 06:41:01,520 and we get walt walters back once again 10193 06:41:01,520 --> 06:41:04,280 now that we're using the 10194 06:41:04,280 --> 06:41:07,360 request.params.id for this route because 10195 06:41:07,360 --> 06:41:10,000 the user id is read directly from the 10196 06:41:10,000 --> 06:41:13,280 url as it is a get request okay let's 10197 06:41:13,280 --> 06:41:16,478 get authorized once again and then we'll 10198 06:41:16,478 --> 06:41:19,360 come back and test the post 10199 06:41:19,360 --> 06:41:22,878 here is the auth route again we'll send 10200 06:41:22,878 --> 06:41:26,080 get a new token 10201 06:41:27,040 --> 06:41:28,798 back to thunder client open the 10202 06:41:28,798 --> 06:41:30,958 employees apis 10203 06:41:30,958 --> 06:41:33,680 look at the posted employee 10204 06:41:33,680 --> 06:41:34,840 here's the new 10205 06:41:34,840 --> 06:41:37,600 auth paste that in look at the body 10206 06:41:37,600 --> 06:41:39,840 we've already got walt walters let's 10207 06:41:39,840 --> 06:41:41,840 change this to dave 10208 06:41:41,840 --> 06:41:44,958 walters is good now we have dave walters 10209 06:41:44,958 --> 06:41:47,120 as an employee that was created a status 10210 06:41:47,120 --> 06:41:49,680 201 which is created 10211 06:41:49,680 --> 06:41:52,240 one more authorization to go so we can 10212 06:41:52,240 --> 06:41:54,320 test the put i guess two more and then 10213 06:41:54,320 --> 06:41:56,240 the delete as well 10214 06:41:56,240 --> 06:41:59,040 so let's go here and authorize once 10215 06:41:59,040 --> 06:42:01,360 again 10216 06:42:02,160 --> 06:42:05,840 new access token 10217 06:42:06,400 --> 06:42:10,798 come back to the put route 10218 06:42:10,798 --> 06:42:12,558 and let's see what we need here let's 10219 06:42:12,558 --> 06:42:15,360 put in the access token 10220 06:42:15,360 --> 06:42:17,760 and in the body oh we need to specify 10221 06:42:17,760 --> 06:42:20,638 the id once again well let's and we 10222 06:42:20,638 --> 06:42:23,280 change a first name to david 10223 06:42:23,280 --> 06:42:25,600 let's go back and i believe in the get 10224 06:42:25,600 --> 06:42:28,320 employee route i still have that id that 10225 06:42:28,320 --> 06:42:30,558 we used before 10226 06:42:30,558 --> 06:42:33,360 oh and it's still down here in the node 10227 06:42:33,360 --> 06:42:35,600 terminal as well so we can pull it out 10228 06:42:35,600 --> 06:42:39,920 of there so let's reauthorize 10229 06:42:44,638 --> 06:42:47,840 new access token 10230 06:42:48,958 --> 06:42:51,360 we will come back to the employees 10231 06:42:51,360 --> 06:42:53,920 use put 10232 06:42:53,920 --> 06:42:55,840 and for put we need to put in the new 10233 06:42:55,840 --> 06:42:57,280 access token 10234 06:42:57,280 --> 06:42:59,440 and then in the body we need the 10235 06:42:59,440 --> 06:43:02,080 employee id so i'm going to paste that 10236 06:43:02,080 --> 06:43:03,600 in here and 10237 06:43:03,600 --> 06:43:06,320 change walt to david 10238 06:43:06,320 --> 06:43:08,160 and now we have david walters and of 10239 06:43:08,160 --> 06:43:10,320 course there's already a dave walters as 10240 06:43:10,320 --> 06:43:11,120 well 10241 06:43:11,120 --> 06:43:13,440 so the put route works and now we want 10242 06:43:13,440 --> 06:43:15,520 to delete and of course we'll delete 10243 06:43:15,520 --> 06:43:17,680 this same user that we still have the id 10244 06:43:17,680 --> 06:43:19,040 handy for 10245 06:43:19,040 --> 06:43:23,200 so let's reauthorize one more time 10246 06:43:24,320 --> 06:43:27,680 get a new access token 10247 06:43:27,680 --> 06:43:30,878 we have the new access token 10248 06:43:30,878 --> 06:43:35,120 copy that go to the employees routes 10249 06:43:35,120 --> 06:43:38,240 delete an employee 10250 06:43:38,240 --> 06:43:41,360 paste in our new access token and in the 10251 06:43:41,360 --> 06:43:44,718 body we once again need the employee id 10252 06:43:44,718 --> 06:43:47,600 we paste in that id 10253 06:43:47,600 --> 06:43:48,558 and 10254 06:43:48,558 --> 06:43:50,558 we get returned the employee that was 10255 06:43:50,558 --> 06:43:53,200 deleted so david walters is no longer in 10256 06:43:53,200 --> 06:43:55,680 our collection but it was returned 10257 06:43:55,680 --> 06:43:58,478 by the delete route 10258 06:43:58,478 --> 06:44:01,200 as a confirmation that this employee was 10259 06:44:01,200 --> 06:44:03,760 deleted okay so from here what you 10260 06:44:03,760 --> 06:44:04,878 should do 10261 06:44:04,878 --> 06:44:06,478 is go ahead and 10262 06:44:06,478 --> 06:44:10,958 create a users js in the routes 10263 06:44:10,958 --> 06:44:14,638 and then api folder and allow some user 10264 06:44:14,638 --> 06:44:16,798 information to be requested maybe get 10265 06:44:16,798 --> 06:44:20,000 all users and maybe delete users and set 10266 06:44:20,000 --> 06:44:22,160 that up so it can be accessed in the 10267 06:44:22,160 --> 06:44:25,478 route by admin only such as 10268 06:44:25,478 --> 06:44:28,320 roleslist.admin because that's the users 10269 06:44:28,320 --> 06:44:30,000 and i'll let you do that on your own but 10270 06:44:30,000 --> 06:44:32,320 i will put it in the completed 10271 06:44:32,320 --> 06:44:34,958 repository that i will post the link 10272 06:44:34,958 --> 06:44:36,320 below so there'll be a link to the 10273 06:44:36,320 --> 06:44:38,958 starter code and then a link to the 10274 06:44:38,958 --> 06:44:40,638 completed code 10275 06:44:40,638 --> 06:44:42,878 as well but before i go 10276 06:44:42,878 --> 06:44:45,280 i want to show you how you can easily 10277 06:44:45,280 --> 06:44:47,440 launch a repository 10278 06:44:47,440 --> 06:44:50,558 and if that repository create or has a 10279 06:44:50,558 --> 06:44:54,160 node application like this rest api 10280 06:44:54,160 --> 06:44:56,958 it will easily be deployed okay there 10281 06:44:56,958 --> 06:44:58,958 are several places on the web where you 10282 06:44:58,958 --> 06:45:01,040 can host your node project and even a 10283 06:45:01,040 --> 06:45:03,760 rest api using express in like 10284 06:45:03,760 --> 06:45:05,760 we've created i'm going to show you one 10285 06:45:05,760 --> 06:45:08,558 of the easiest ways to deploy 10286 06:45:08,558 --> 06:45:10,400 a project like this and we're going to 10287 06:45:10,400 --> 06:45:13,120 deploy to glitch.com so i'm going to put 10288 06:45:13,120 --> 06:45:16,400 in another bold statement here and say 10289 06:45:16,400 --> 06:45:18,718 well not start let's just say 10290 06:45:18,718 --> 06:45:20,400 deploy by 10291 06:45:20,400 --> 06:45:21,840 clicking the 10292 06:45:21,840 --> 06:45:23,120 button 10293 06:45:23,120 --> 06:45:24,798 above 10294 06:45:24,798 --> 06:45:26,718 okay now that we've put that in i'm 10295 06:45:26,718 --> 06:45:29,360 going to copy and paste some code but 10296 06:45:29,360 --> 06:45:31,680 i'm going to just change which project 10297 06:45:31,680 --> 06:45:33,280 it was for so 10298 06:45:33,280 --> 06:45:35,760 this code will put a button on the 10299 06:45:35,760 --> 06:45:38,878 readme of the repository that i share to 10300 06:45:38,878 --> 06:45:41,520 glit or not to glitch to github 10301 06:45:41,520 --> 06:45:43,840 and then when the button is clicked it 10302 06:45:43,840 --> 06:45:45,280 will launch 10303 06:45:45,280 --> 06:45:48,160 on glitch so here we have to put in the 10304 06:45:48,160 --> 06:45:51,120 name of the current project and so i'm 10305 06:45:51,120 --> 06:45:52,160 put 10306 06:45:52,160 --> 06:45:53,760 10307 06:45:53,760 --> 06:45:56,638 async crud i believe i named this 10308 06:45:56,638 --> 06:45:58,080 project 10309 06:45:58,080 --> 06:45:59,760 and i'll save 10310 06:45:59,760 --> 06:46:02,638 and we can verify here by i'll go back 10311 06:46:02,638 --> 06:46:05,520 and let's see i named this async 10312 06:46:05,520 --> 06:46:07,600 crud that is correct 10313 06:46:07,600 --> 06:46:08,558 so 10314 06:46:08,558 --> 06:46:10,478 i'll open up the terminal window and 10315 06:46:10,478 --> 06:46:13,680 i'll push my changes to github that i've 10316 06:46:13,680 --> 06:46:16,558 already initialized repository so i'll 10317 06:46:16,558 --> 06:46:18,240 add the file 10318 06:46:18,240 --> 06:46:21,360 and i'll commit 10319 06:46:21,760 --> 06:46:25,040 messages updated 10320 06:46:25,040 --> 06:46:26,718 readme 10321 06:46:26,718 --> 06:46:28,600 and i'll push to 10322 06:46:28,600 --> 06:46:31,120 github and i need 10323 06:46:31,120 --> 06:46:33,360 a passphrase set up with mine and there 10324 06:46:33,360 --> 06:46:35,920 we go i've pushed that to github and so 10325 06:46:35,920 --> 06:46:37,840 now if we go back 10326 06:46:37,840 --> 06:46:40,798 and look at the readme 10327 06:46:40,798 --> 06:46:43,120 for this repository there should be a 10328 06:46:43,120 --> 06:46:45,360 button that's available and it says 10329 06:46:45,360 --> 06:46:46,798 remix this 10330 06:46:46,798 --> 06:46:48,878 and that will allow you to go ahead and 10331 06:46:48,878 --> 06:46:51,520 launch this application on glitch so 10332 06:46:51,520 --> 06:46:53,440 let's go ahead and do that i'll just 10333 06:46:53,440 --> 06:46:55,920 click the button it takes just a minute 10334 06:46:55,920 --> 06:46:57,600 to load the project 10335 06:46:57,600 --> 06:46:59,840 and it will be up and running except for 10336 06:46:59,840 --> 06:47:02,478 one thing and that one thing 10337 06:47:02,478 --> 06:47:05,120 is the values that we 10338 06:47:05,120 --> 06:47:07,600 have decided to put in our dot env 10339 06:47:07,600 --> 06:47:10,000 remember the dot e and v file should be 10340 06:47:10,000 --> 06:47:13,200 listed in your git ignore so it is not 10341 06:47:13,200 --> 06:47:16,000 here and notice the remix button is much 10342 06:47:16,000 --> 06:47:18,080 larger once it gets to glitch that's 10343 06:47:18,080 --> 06:47:19,120 interesting 10344 06:47:19,120 --> 06:47:20,400 okay so 10345 06:47:20,400 --> 06:47:22,320 once again looking at the get ignore you 10346 06:47:22,320 --> 06:47:24,478 should have your enb file here so the 10347 06:47:24,478 --> 06:47:26,558 secrets that you have stored in the dot 10348 06:47:26,558 --> 06:47:27,760 enb 10349 06:47:27,760 --> 06:47:30,558 need to go in the dot e and b that is 10350 06:47:30,558 --> 06:47:32,718 provided here at glitch 10351 06:47:32,718 --> 06:47:34,878 and you can of course click this to 10352 06:47:34,878 --> 06:47:36,558 learn how to use environment variables 10353 06:47:36,558 --> 06:47:37,920 we've already talked about that so you 10354 06:47:37,920 --> 06:47:40,958 can add a variable and here is where you 10355 06:47:40,958 --> 06:47:42,798 would type like 10356 06:47:42,798 --> 06:47:44,638 access 10357 06:47:44,638 --> 06:47:46,400 token 10358 06:47:46,400 --> 06:47:48,718 secret i believe is what we named one of 10359 06:47:48,718 --> 06:47:50,240 ours and then you would paste in your 10360 06:47:50,240 --> 06:47:51,840 value for that you would do the same for 10361 06:47:51,840 --> 06:47:54,478 the refresh token secret and you would 10362 06:47:54,478 --> 06:47:56,798 also want to do the same 10363 06:47:56,798 --> 06:47:58,638 for the 10364 06:47:58,638 --> 06:48:00,478 string you put in the connection string 10365 06:48:00,478 --> 06:48:03,680 that we used for mongoose and mongodb 10366 06:48:03,680 --> 06:48:06,558 but this is just an example to show you 10367 06:48:06,558 --> 06:48:08,558 how you can go ahead and launch that so 10368 06:48:08,558 --> 06:48:10,400 what i'm going to do is leave that 10369 06:48:10,400 --> 06:48:11,280 button 10370 06:48:11,280 --> 06:48:13,040 in the readme and i'll put a little 10371 06:48:13,040 --> 06:48:14,878 extra note in the readme saying remember 10372 06:48:14,878 --> 06:48:17,680 to add your own values in the dot env 10373 06:48:17,680 --> 06:48:19,920 because you can click that button 10374 06:48:19,920 --> 06:48:22,638 right there in my repository 10375 06:48:22,638 --> 06:48:27,040 and from there you can bring back up 10376 06:48:27,040 --> 06:48:28,638 this project 10377 06:48:28,638 --> 06:48:30,558 in glitch and if you create an account a 10378 06:48:30,558 --> 06:48:33,440 glitch you can save it there as well so 10379 06:48:33,440 --> 06:48:35,920 that will help you launch any future 10380 06:48:35,920 --> 06:48:38,638 node application you might create 10381 06:48:38,638 --> 06:48:41,200 and this would allow you to put a button 10382 06:48:41,200 --> 06:48:43,760 in your repositories and launch them as 10383 06:48:43,760 --> 06:48:45,360 well and i think you'll like working 10384 06:48:45,360 --> 06:48:47,760 with glitch also i use it for several 10385 06:48:47,760 --> 06:48:50,000 things and i should add that once you're 10386 06:48:50,000 --> 06:48:52,080 on glitch and you have your project 10387 06:48:52,080 --> 06:48:53,120 launched 10388 06:48:53,120 --> 06:48:55,280 it will give it a name like this and 10389 06:48:55,280 --> 06:48:57,520 then you'll have your live project you 10390 06:48:57,520 --> 06:49:00,400 can click change url here and you'll see 10391 06:49:00,400 --> 06:49:03,280 the url for your project so that would 10392 06:49:03,280 --> 06:49:06,000 be the url for your api 10393 06:49:06,000 --> 06:49:08,638 so that is what you would want to go to 10394 06:49:08,638 --> 06:49:11,280 for your api and then the 10395 06:49:11,280 --> 06:49:13,760 end points would go of course after this 10396 06:49:13,760 --> 06:49:16,000 if you're trying to pull up any specific 10397 06:49:16,000 --> 06:49:18,798 endpoint so we have covered a lot in 10398 06:49:18,798 --> 06:49:21,200 this series and of course there is much 10399 06:49:21,200 --> 06:49:23,200 more depth you could go into as you dive 10400 06:49:23,200 --> 06:49:25,040 into the docs but i feel like we've 10401 06:49:25,040 --> 06:49:27,280 covered the create read update and 10402 06:49:27,280 --> 06:49:28,320 delete 10403 06:49:28,320 --> 06:49:32,160 for and mongodb we've built a full 10404 06:49:32,160 --> 06:49:33,840 rest api 10405 06:49:33,840 --> 06:49:36,958 with node and express and we have added 10406 06:49:36,958 --> 06:49:39,440 user authentication we have talked about 10407 06:49:39,440 --> 06:49:41,680 authorization and 10408 06:49:41,680 --> 06:49:44,080 json web tokens as well to protect those 10409 06:49:44,080 --> 06:49:46,558 routes so if we talk about next steps 10410 06:49:46,558 --> 06:49:48,240 you would want to take what we have 10411 06:49:48,240 --> 06:49:49,200 built 10412 06:49:49,200 --> 06:49:51,680 and what we have built here on the back 10413 06:49:51,680 --> 06:49:54,638 end using express and node could 10414 06:49:54,638 --> 06:49:56,878 be combined with react 10415 06:49:56,878 --> 06:49:59,600 for the mern stack so if you haven't 10416 06:49:59,600 --> 06:50:02,080 learned react you want to check out my 10417 06:50:02,080 --> 06:50:05,600 nine hour beginners tutorial for that 10418 06:50:05,600 --> 06:50:08,000 and you can combine that knowledge with 10419 06:50:08,000 --> 06:50:09,760 this back end course knowledge to 10420 06:50:09,760 --> 06:50:11,920 complete the mern stack however you 10421 06:50:11,920 --> 06:50:13,280 could also 10422 06:50:13,280 --> 06:50:16,240 add whichever front-end technology you 10423 06:50:16,240 --> 06:50:18,958 wanted to this back end with 10424 06:50:18,958 --> 06:50:21,360 express and node for whatever 10425 06:50:21,360 --> 06:50:23,120 application you wanted to create 10426 06:50:23,120 --> 06:50:25,120 remember to keep striving for progress 10427 06:50:25,120 --> 06:50:27,440 over perfection and a little progress 10428 06:50:27,440 --> 06:50:30,320 every day will go a very long way please 10429 06:50:30,320 --> 06:50:32,080 give this video a like if it's helped 10430 06:50:32,080 --> 06:50:34,080 you and thank you for watching and 10431 06:50:34,080 --> 06:50:35,840 subscribing you're helping my channel 10432 06:50:35,840 --> 06:50:38,240 grow have a great day and let's write 10433 06:50:38,240 --> 06:50:42,520 more code together very soon 710021

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.