1 00:00:00,270 --> 00:00:03,780 Welcome to your very first programming video for the class. 2 00:00:03,780 --> 00:00:09,330 From here on out we're going to be spending our time writing code and building note applications in 3 00:00:09,330 --> 00:00:11,130 Visual Studio code. 4 00:00:11,130 --> 00:00:16,560 Now in this video in particular to kick off the course we're going to talk about one of the most fundamental 5 00:00:16,560 --> 00:00:17,980 features of node. 6 00:00:18,060 --> 00:00:24,060 This is the module system and this is what's going to allow us to take advantage of all of the cool 7 00:00:24,060 --> 00:00:26,640 and interesting things that node provides. 8 00:00:26,640 --> 00:00:31,290 So as an example over here in the docs there are all sorts of modules listed. 9 00:00:31,560 --> 00:00:38,340 Now some as we saw with console are available globally which means we don't have to do anything special 10 00:00:38,340 --> 00:00:40,840 to use them right here as an example. 11 00:00:40,860 --> 00:00:44,190 We just accessed console dot log to use it. 12 00:00:44,190 --> 00:00:50,070 Others require us to actually load them in before they can be used in our scripts. 13 00:00:50,070 --> 00:00:52,050 That's exactly what we're going to explore. 14 00:00:52,110 --> 00:00:58,480 And the module we're going to use to explore this is the file system module listed down below. 15 00:00:58,500 --> 00:01:03,960 This is the module that's going to allow us to access the operating systems file system. 16 00:01:03,960 --> 00:01:10,070 We'll be able to read and write append files and figure out if a given file or directory exists. 17 00:01:10,170 --> 00:01:16,080 All sorts of interesting utilities related to the file system at the table of contents here contains 18 00:01:16,080 --> 00:01:20,970 all of the interesting ways we can use this module inside of our node applications. 19 00:01:21,030 --> 00:01:26,290 And if we start to scroll down we can see there are quite literally hundreds of different ways. 20 00:01:26,310 --> 00:01:30,720 Now at some point we're going to get to a part in the Table of Contents where everything starts with 21 00:01:30,780 --> 00:01:38,430 F. s dot followed by some method name here as an example I have f s dot access all these methods are 22 00:01:38,430 --> 00:01:44,130 listed in alphabetical order and we're going to scroll down to the method we'll be using which is F 23 00:01:44,130 --> 00:01:46,380 S dot right file. 24 00:01:46,500 --> 00:01:52,770 This is the method that's going to allow us to write some data to a file on our file system from the 25 00:01:52,770 --> 00:01:55,170 node j s application. 26 00:01:55,170 --> 00:02:01,860 Now we have two versions of this I have f s dot right file and F S dot right file sync. 27 00:02:02,010 --> 00:02:07,040 The first one is an asynchronous version and the second one is a synchronous version. 28 00:02:07,110 --> 00:02:10,050 And for the moment we're going to stick with the synchronous version. 29 00:02:10,050 --> 00:02:16,170 Don't worry though we will be exploring asynchronous know j s in detail in just a couple of sections. 30 00:02:16,200 --> 00:02:23,000 For now though the goal is to learn about this module system so F Scott right file sync takes into arguments 31 00:02:23,010 --> 00:02:27,110 the name of the file you want to write to and the data you want to write. 32 00:02:27,150 --> 00:02:28,630 So let's go ahead and use it. 33 00:02:28,650 --> 00:02:34,190 I'm gonna head over to the Visual Studio code editor and I will take a moment to delete Hello Dot J 34 00:02:34,200 --> 00:02:38,240 S as this is not gonna make it into any of our applications. 35 00:02:38,340 --> 00:02:44,010 Let's go ahead and trash it and we'll make a brand new folder for our first project which is going to 36 00:02:44,010 --> 00:02:50,400 be a little notes app so I'll call the folder Notes app and in there we're going to make a single j 37 00:02:50,400 --> 00:02:53,540 ust file right here app dot J. 38 00:02:53,580 --> 00:02:56,640 S now down below inside of the terminal. 39 00:02:56,640 --> 00:03:02,980 I am going to switch directories from node course to Notes app by using d space. 40 00:03:03,120 --> 00:03:06,180 Then the folder name notes hyphen app. 41 00:03:06,240 --> 00:03:10,920 Now we're actually running commands from that directory and that's great because we're going to run 42 00:03:10,980 --> 00:03:12,950 node in just a few seconds. 43 00:03:12,990 --> 00:03:20,670 Let's go ahead and actually use F S dot right file sync exactly like we saw in the docs it is F S which 44 00:03:20,670 --> 00:03:25,110 is an object dot the method name is right. 45 00:03:25,110 --> 00:03:28,190 File sync it is indeed a function. 46 00:03:28,200 --> 00:03:30,310 So I am going to call it as such. 47 00:03:30,310 --> 00:03:37,010 Setting up my parentheses and as I mentioned this takes two arguments in this case both are strings. 48 00:03:37,020 --> 00:03:39,140 The first is the name of the file. 49 00:03:39,150 --> 00:03:40,390 The second is the data. 50 00:03:40,400 --> 00:03:45,630 All right let's go ahead and pick a name for our file and I'm going to call this something like notes 51 00:03:45,660 --> 00:03:47,060 Dot T X T. 52 00:03:47,100 --> 00:03:52,800 So for the moment we're going to stick with a simple text file though later on we'll learn how to work 53 00:03:52,800 --> 00:03:54,960 with files of other types. 54 00:03:54,960 --> 00:03:59,330 Now the second argument is going to be the contents to put in that file. 55 00:03:59,340 --> 00:04:03,630 So right here as a string let's go ahead and write out a little message. 56 00:04:03,630 --> 00:04:09,680 This file was created by node j s perfect. 57 00:04:09,720 --> 00:04:15,930 So now we actually have a call to write file sync and we can go ahead and run this script. 58 00:04:15,930 --> 00:04:19,650 The problem is that it's not going to work down below. 59 00:04:19,650 --> 00:04:27,900 Let's prove that I'm going to use node to run a script after a space the script name is app dot J S 60 00:04:28,110 --> 00:04:33,710 which is indeed in the directory we're running commands from the notes app directory. 61 00:04:33,810 --> 00:04:39,840 If I go ahead and hit enter we are not going to see any sort of output that suggests that things went 62 00:04:39,840 --> 00:04:40,470 well. 63 00:04:40,500 --> 00:04:44,000 Right here we have a very long error message. 64 00:04:44,010 --> 00:04:49,920 It contains information about exactly where the error occurred and what exactly is going wrong. 65 00:04:49,950 --> 00:04:55,260 Now later in the course when we explored debugging we'll really break down errors like this for the 66 00:04:55,260 --> 00:04:55,740 moment. 67 00:04:55,740 --> 00:05:01,020 The only important piece is this little message right here F S is not defined. 68 00:05:01,020 --> 00:05:08,670 So the problem is that we're using F S F S though needs to be loaded in and define the file system module. 69 00:05:08,670 --> 00:05:14,310 As I mentioned has to be required by you in the script you're using it in. 70 00:05:14,400 --> 00:05:20,140 So right here we're gonna add a second line before we use the file system module. 71 00:05:20,190 --> 00:05:26,890 We're gonna go ahead and load it in and this is done using the require function that node provides. 72 00:05:26,970 --> 00:05:30,430 And this is at the very core of the module system. 73 00:05:30,450 --> 00:05:37,470 The require function is how we load in other things whether it's a core node module a another file we 74 00:05:37,470 --> 00:05:45,030 created or an NPM module we've chosen to install into use in our projects to load in the file system 75 00:05:45,030 --> 00:05:52,380 module we have to call the require function which is indeed how we load in modules and we pass to it 76 00:05:52,500 --> 00:05:54,150 a single string. 77 00:05:54,150 --> 00:06:00,420 Now when we're looking to load in a core node module we just provide the name of the module and for 78 00:06:00,420 --> 00:06:03,410 the file system it is f s. 79 00:06:03,440 --> 00:06:09,660 Now they require function returns all of the stuff from that module and we have to store that on a variable 80 00:06:09,840 --> 00:06:12,750 right now f s still doesn't exist. 81 00:06:12,750 --> 00:06:20,730 So right here we're going to create a constant called F S and it's value is going to come from the return 82 00:06:20,730 --> 00:06:22,840 value of calling require. 83 00:06:22,860 --> 00:06:24,000 So let's break this down. 84 00:06:24,180 --> 00:06:30,330 We're calling require which is going to load in the F S module the FSA module is built right into node. 85 00:06:30,330 --> 00:06:33,150 So this is going to work for any node script. 86 00:06:33,270 --> 00:06:40,110 What do we do with the return value we store it on the F S variable then down below we call f s dot 87 00:06:40,170 --> 00:06:45,480 right file sync to write some text to the notes Dot T X T file. 88 00:06:45,480 --> 00:06:48,380 Let's go ahead and see if this works down below. 89 00:06:48,450 --> 00:06:52,430 I'm gonna start by clearing the terminal output using the clear command. 90 00:06:52,560 --> 00:06:58,350 If you're on Windows and you're using these standard command prompt you're gonna have to use S.L. s 91 00:06:58,380 --> 00:07:00,330 which is the command for you. 92 00:07:00,330 --> 00:07:03,800 And now we're gonna go ahead and run the app script again. 93 00:07:03,840 --> 00:07:05,900 So node space app that J. 94 00:07:05,910 --> 00:07:08,130 S this time what do we see. 95 00:07:08,130 --> 00:07:14,010 We see nothing it brings us right back to the command prompt asking us to do something else but you'll 96 00:07:14,010 --> 00:07:20,520 notice that over here in the preview for the notes app directory we have a brand new file right alongside 97 00:07:20,520 --> 00:07:26,670 of app dot J S we have notes Dot T X T which got its name from this argument. 98 00:07:26,670 --> 00:07:28,530 And if I crack it open what do we have. 99 00:07:28,530 --> 00:07:34,530 We have our little message in this file was created with node j s which is fantastic. 100 00:07:34,560 --> 00:07:38,840 Now let's go ahead and switch up the text where writing and run the script again. 101 00:07:38,910 --> 00:07:44,520 So over here I'm gonna change the text value for that second argument and go ahead and do the same. 102 00:07:44,520 --> 00:07:46,980 I'll switch it up to something like an introduction. 103 00:07:47,070 --> 00:07:50,420 I'll say my name is Andrew. 104 00:07:50,460 --> 00:07:51,240 Period. 105 00:07:51,240 --> 00:07:52,200 Perfect. 106 00:07:52,200 --> 00:07:57,960 Now we can rerun the script to write some new contents to that file down below. 107 00:07:57,960 --> 00:08:02,330 I'm gonna use the up arrow key to cycle through my previous commands here. 108 00:08:02,340 --> 00:08:09,090 The previous one we ran was node running app at J S then I'll hit enter to run the script again once 109 00:08:09,090 --> 00:08:11,340 again nothing output it down below. 110 00:08:11,370 --> 00:08:17,410 But if I open up notes Dot T X T we have our new text content showing up which is great. 111 00:08:17,460 --> 00:08:24,020 So the right file and the right file sync methods those are responsible for writing data to a file. 112 00:08:24,030 --> 00:08:31,140 If the file doesn't exist as we saw it will be created if the file does exist as we just saw its text 113 00:08:31,140 --> 00:08:35,230 content will be overwritten with the new provided message. 114 00:08:35,280 --> 00:08:40,850 I want to take a quick moment to note that this variable F S it could be called anything we wanted. 115 00:08:40,860 --> 00:08:47,160 The only important thing is that the string we passed to require matches up with what node calls the 116 00:08:47,160 --> 00:08:47,720 module. 117 00:08:47,760 --> 00:08:52,000 Otherwise node is not going to know which module you're trying to load. 118 00:08:52,050 --> 00:08:57,840 So as an example I could call this something like a file system and that would be perfectly valid as 119 00:08:57,840 --> 00:09:02,330 long as I changed my usage down below to file system as well. 120 00:09:02,340 --> 00:09:06,870 Now we have a different name for the variable and the program would work though it's common to stick 121 00:09:06,870 --> 00:09:13,260 with the convention for all node modules we load in there's typically a variable name that everyone 122 00:09:13,260 --> 00:09:19,290 uses and it makes life easier if we all stick with that variable name so we can know what exactly we're 123 00:09:19,290 --> 00:09:21,330 working with across projects. 124 00:09:21,330 --> 00:09:28,500 Now if you're wondering how I knew to load an F S or what the common variable name is or whether the 125 00:09:28,500 --> 00:09:31,250 module even needs to be loaded in at all. 126 00:09:31,320 --> 00:09:33,930 Once again the docs are your best friend. 127 00:09:34,080 --> 00:09:39,870 If we go back over to the documentation and scroll down past the table of contents one of the first 128 00:09:39,870 --> 00:09:43,660 things we're going to see is what we have on line one. 129 00:09:43,680 --> 00:09:49,380 Here we can see exactly what node calls the module based off of the fact that this code is here. 130 00:09:49,380 --> 00:09:55,920 We know it needs to be loaded in unlike console and then they have a common used a variable name which 131 00:09:55,920 --> 00:09:58,360 we can choose to use or choose to ignore. 132 00:09:58,380 --> 00:10:04,200 Though once again it is a best practice to use the name in this case f S.. 133 00:10:04,240 --> 00:10:10,560 So at this point we have loaded in our first core node module and we've used it to do something interesting. 134 00:10:10,570 --> 00:10:15,530 Now I want to take a moment to move on to our first challenge for the class. 135 00:10:15,550 --> 00:10:19,660 Whenever I write code in a video you should be following along as well. 136 00:10:19,660 --> 00:10:24,490 I film all of the videos in real time so you can actually write the code I'm writing. 137 00:10:24,520 --> 00:10:30,010 That is a great way to learn something but it's not the best way to make sure you can actually use that 138 00:10:30,010 --> 00:10:32,300 information to solve problems. 139 00:10:32,320 --> 00:10:37,390 So throughout the course in almost every single video I have a challenge where you're going to need 140 00:10:37,390 --> 00:10:41,830 to solve a programming problem based off of what you just learned. 141 00:10:41,830 --> 00:10:48,190 The goal here is to give you as much real world experience as possible actually using node j s on your 142 00:10:48,190 --> 00:10:49,990 own by the end of the course. 143 00:10:50,080 --> 00:10:55,300 You'll actually have a ton of experience writing your own code and building your own apps and you'll 144 00:10:55,300 --> 00:11:01,480 find it much more comfortable to go off after the class and actually use no J.S. whether you're building 145 00:11:01,480 --> 00:11:07,420 your own side project trying to get into freelancing or trying to switch careers and become a node j 146 00:11:07,420 --> 00:11:08,560 ask developer. 147 00:11:08,590 --> 00:11:11,310 So it's important that you follow along with the lessons. 148 00:11:11,320 --> 00:11:14,820 For example you should have this code in your editor. 149 00:11:14,860 --> 00:11:18,700 It's also equally important that you take the challenges seriously. 150 00:11:18,700 --> 00:11:19,750 So here's how they work. 151 00:11:19,750 --> 00:11:23,410 I give you a prompt I give you a second to pause the video. 152 00:11:23,410 --> 00:11:25,110 You go off and solve the problem. 153 00:11:25,150 --> 00:11:28,000 Then you come back and I walk you through the solution. 154 00:11:28,000 --> 00:11:31,590 So let's go ahead and jump into our very first one as always. 155 00:11:31,600 --> 00:11:35,270 I'm going to paste in a little description of what I'd like you to do. 156 00:11:35,290 --> 00:11:37,410 So this is a pretty standard challenge. 157 00:11:37,420 --> 00:11:43,540 Now they're gonna start off easy and they're going to get harder over time as we actually know more 158 00:11:43,540 --> 00:11:47,270 about node and we can do more things right here challenge. 159 00:11:47,350 --> 00:11:51,860 Your goal is to append a message to notes Dot T X T. 160 00:11:51,970 --> 00:11:54,550 So I don't want to override what's here. 161 00:11:54,550 --> 00:11:57,250 I want to add something new on it to the end. 162 00:11:57,250 --> 00:11:58,700 How are you going to do that. 163 00:11:58,750 --> 00:12:05,140 You're going to use a new method from the file system module called append file sync to append to the 164 00:12:05,140 --> 00:12:05,810 file. 165 00:12:05,980 --> 00:12:09,000 Once you've added that in you're going to run the script. 166 00:12:09,100 --> 00:12:14,560 And finally you're going to check your work making sure that the new text you append is showing up on 167 00:12:14,560 --> 00:12:16,200 the end right here. 168 00:12:16,330 --> 00:12:22,490 Now to do that we are going to comment out our existing F S dot in a right file sync call. 169 00:12:22,570 --> 00:12:29,440 We're going to add just that one call in the call to append a file sync and if we head over to the documentation 170 00:12:29,470 --> 00:12:31,380 you can learn more about that. 171 00:12:31,480 --> 00:12:36,940 I'm going to scroll up back to all of those modules and methods we were looking at earlier and if I 172 00:12:36,940 --> 00:12:43,810 go all the way up to F S Dot and I go to the A's I have append file sync right here. 173 00:12:43,810 --> 00:12:50,170 Now append file sync works much the same way as right file sync it needs the name of the file you're 174 00:12:50,170 --> 00:12:56,170 trying to append and it needs the text content you want to append so in the end of the day we want to 175 00:12:56,170 --> 00:13:01,840 append to the file whose name is right here and you can pick whatever message you want as the text to 176 00:13:01,840 --> 00:13:02,510 append. 177 00:13:02,590 --> 00:13:07,960 You could add to the introduction saying something maybe about where you live or how old you are. 178 00:13:08,110 --> 00:13:11,500 If the concept of a challenge it seems a little foreign to you. 179 00:13:11,500 --> 00:13:15,850 Don't worry we're gonna go through this about a hundred different times throughout the course. 180 00:13:15,940 --> 00:13:21,130 So you'll be pretty comfortable with it by the end and as I mentioned things are gonna start off easier 181 00:13:21,160 --> 00:13:27,010 as we know less about node and the challenges will get more complex requiring you to write many lines 182 00:13:27,010 --> 00:13:27,720 of code. 183 00:13:27,820 --> 00:13:33,520 As we progressed through the class for now though it takes some time to knock out all three steps down 184 00:13:33,520 --> 00:13:34,040 below. 185 00:13:34,120 --> 00:13:37,990 You can pause the video when you're done come back and click play 186 00:13:41,600 --> 00:13:42,440 how'd that go. 187 00:13:42,440 --> 00:13:44,480 Very first challenge down. 188 00:13:44,480 --> 00:13:45,810 Hopefully it went well. 189 00:13:45,830 --> 00:13:46,480 If it didn't. 190 00:13:46,490 --> 00:13:47,710 That's OK too. 191 00:13:47,720 --> 00:13:52,520 This is really an introduction to challenges and once again we'll get more comfortable as we do more 192 00:13:52,520 --> 00:13:54,080 of them right here. 193 00:13:54,080 --> 00:13:57,720 The goal was to use f data append file sync. 194 00:13:57,740 --> 00:14:02,670 Now this is already a module we have loaded in so there's no need to load it in again. 195 00:14:02,720 --> 00:14:10,700 Right here we can call f s dot append file and you'll notice that since we've loaded in the FSM module 196 00:14:10,790 --> 00:14:16,310 with visual studio code at least I am getting some helpful suggestions about which methods I have access 197 00:14:16,310 --> 00:14:16,970 to. 198 00:14:16,970 --> 00:14:21,980 Right here I will be using append file sync and calling it as such. 199 00:14:21,980 --> 00:14:25,190 Now what file am I trying to append to. 200 00:14:25,250 --> 00:14:32,030 Well I only have one it is notes dot t t so that will be the value I provide for the first argument 201 00:14:32,330 --> 00:14:38,240 and for the second argument I'm gonna provide the text I want to append I'll say something like I live 202 00:14:38,300 --> 00:14:40,420 in Philadelphia. 203 00:14:40,430 --> 00:14:41,480 Perfect. 204 00:14:41,600 --> 00:14:46,760 Now since we're appending one string right on to the end of the other I'm gonna put a space at the beginning 205 00:14:46,760 --> 00:14:47,450 of the string. 206 00:14:47,480 --> 00:14:52,130 So I have a space between the period up here and the letter I down below. 207 00:14:52,850 --> 00:14:58,130 Now we can go ahead and save app not J S and run our script to test things out. 208 00:14:58,220 --> 00:15:04,340 I'll use the up arrow key and enter to rerun the previous command and if I open up notes dot t t what 209 00:15:04,340 --> 00:15:05,140 do we have. 210 00:15:05,240 --> 00:15:09,900 I have my old message My name is Andrew and I have my new message. 211 00:15:09,920 --> 00:15:13,190 I live in Philadelphia which is excellent. 212 00:15:13,190 --> 00:15:14,260 And there we go. 213 00:15:14,270 --> 00:15:17,070 You've completed your very first challenge. 214 00:15:17,120 --> 00:15:22,430 Once again these are essential to getting real world experience solving problems and working through 215 00:15:22,430 --> 00:15:28,580 your misunderstandings so it's okay if you don't get them all right or it takes you a lot of time to 216 00:15:28,580 --> 00:15:30,110 figure out what's going on. 217 00:15:30,170 --> 00:15:36,260 It's gonna get easier to recall what we covered as we use it more and more now that we're starting to 218 00:15:36,260 --> 00:15:37,340 write code together. 219 00:15:37,340 --> 00:15:44,480 I wanted to remind you that the lecture resources for every lesson of the class contains a zip of the 220 00:15:44,510 --> 00:15:46,470 code up to that point. 221 00:15:46,550 --> 00:15:51,530 So if you're getting stuck and you can't quite figure it out you can always download the zip for that 222 00:15:51,530 --> 00:15:58,750 lesson that contains all of the code changes made and you can compare your code with my code. 223 00:15:58,790 --> 00:16:00,920 You can also head over to links. 224 00:16:00,950 --> 00:16:09,680 Dot need dot I O forward slash node course to view a get Hub Repository that contains every piece of 225 00:16:09,680 --> 00:16:12,210 code created in the class. 226 00:16:12,260 --> 00:16:17,780 That's where we're gonna stop for this one we've now loaded in and used our very first core no J.S. 227 00:16:17,810 --> 00:16:24,220 module in the next video we're gonna continue to talk about the module system and require. 228 00:16:24,220 --> 00:16:25,800 I am excited to get to that. 229 00:16:25,820 --> 00:16:28,000 So let's go ahead and jump right in.