1 00:00:00,980 --> 00:00:02,900 Hello everybody. 2 00:00:02,900 --> 00:00:04,000 Welcome back. 3 00:00:04,280 --> 00:00:12,170 And in this video, I want to show you something that got me really really really excited when I first started 4 00:00:12,170 --> 00:00:19,040 learning node, and hopefully you get just as excited as I am as we go through this video and then do 5 00:00:19,040 --> 00:00:20,300 a little bit of a challenge. 6 00:00:20,300 --> 00:00:21,310 It's going to blow your mind. 7 00:00:21,330 --> 00:00:25,760 At least it did for me when I first started learning to code. 8 00:00:25,850 --> 00:00:34,250 Now in the previous video we talked about something called the FS module that comes with node, and this 9 00:00:34,250 --> 00:00:39,590 is something that is built into node and the way we get it is we'll just do const. 10 00:00:39,760 --> 00:00:50,360 Well actually we need a file first, so let's do a touch script.js, we'll create a new script perfect. 11 00:00:50,400 --> 00:00:52,920 Alright we can close that because we don't need this. 12 00:00:53,490 --> 00:01:02,190 And in the script.js that we have in this folder or some node we can have 'const fs'. 13 00:01:02,210 --> 00:01:14,990 equals 'require('fs')', so FS here stands for file system and if you have node downloaded you have access 14 00:01:14,990 --> 00:01:15,460 to this. 15 00:01:15,470 --> 00:01:17,680 By just doing this. 16 00:01:17,880 --> 00:01:21,750 Now why is this so exciting and so useful. 17 00:01:22,320 --> 00:01:30,830 Well because FS file system allows you to access our file system, which again doesn't sound that exciting. 18 00:01:30,840 --> 00:01:38,230 But let me demonstrate what happens and why this is useful - so bear with me here. 19 00:01:38,280 --> 00:01:42,080 I'm going to do a bit of coding and then I'm going to explain exactly what is happening. 20 00:01:43,140 --> 00:01:48,470 So we're going to use the 'fs.readFile'. 21 00:01:48,560 --> 00:01:50,720 It's pretty self-explanatory. 22 00:01:51,200 --> 00:02:01,380 We have a blank parameter and then we have err for error, data. 23 00:02:01,760 --> 00:02:03,710 And this is an arrow function. 24 00:02:05,450 --> 00:02:12,270 So the second parameter is a function, and this function has either error or data. 25 00:02:12,310 --> 00:02:24,630 So for the error, I'm going to say if there is an error we'll just throw err, or we can just say console dot 26 00:02:25,020 --> 00:02:30,660 log errrroooorrr. Really shove it into their face. 27 00:02:31,550 --> 00:02:42,680 Alright! Now if there wasn't an error, we'll just say well we'll just say console.log and we'll just log out 28 00:02:42,680 --> 00:02:45,150 whatever data is. 29 00:02:45,370 --> 00:02:45,990 Alright. 30 00:02:46,150 --> 00:02:47,720 Let me add the semi-colons. 31 00:02:48,830 --> 00:02:51,090 And save that. 32 00:02:51,100 --> 00:02:59,440 So what I just do here. I just imported the FS module that comes with node and the FS module has a method 33 00:02:59,740 --> 00:03:01,670 readFile() that 34 00:03:01,720 --> 00:03:05,480 well it looks like it reads some sort of a file. 35 00:03:05,560 --> 00:03:13,300 The first parameter is actual file that we want to do, so the path to the file, which we don't have yet. 36 00:03:13,300 --> 00:03:14,650 So let's create one. 37 00:03:14,650 --> 00:03:25,200 I'm going to say new file and we'll call it 'hello.txt', and with 'hello.txt', we'll do our classic 38 00:03:26,300 --> 00:03:27,850 helllloooooo there!!! 39 00:03:29,090 --> 00:03:32,300 And exciting stuff so exclamation marks. 40 00:03:32,540 --> 00:03:37,120 We saved that and now we can say that we want to read the file. 41 00:03:37,190 --> 00:03:40,340 'hello.txt' from the current directory. 42 00:03:41,140 --> 00:03:42,340 Hello dot 43 00:03:42,790 --> 00:03:45,140 text. 44 00:03:45,280 --> 00:03:50,450 Now this readFile method is going to find the file with the path, 45 00:03:50,500 --> 00:03:52,700 ok, so here it is. 46 00:03:52,840 --> 00:03:59,420 It's going to read it and then if there's an error, it's going to spit out an error. 47 00:03:59,590 --> 00:04:06,670 If there's some sort of data and everything successful, it's going to spit out data. With the 48 00:04:06,670 --> 00:04:08,300 way we can run this file in node. 49 00:04:08,310 --> 00:04:13,720 Well well first I'll have to save it. We'll say node script.js 50 00:04:15,140 --> 00:04:19,740 Enter and I got buffer. 51 00:04:20,920 --> 00:04:22,740 What is that? 52 00:04:22,840 --> 00:04:27,460 And that is because we have to do a little bit of encoding. 53 00:04:27,670 --> 00:04:34,680 And what that means is if I do this 'toString()', let's just try this and show you what I mean. 54 00:04:37,450 --> 00:04:38,530 And I get. 55 00:04:38,770 --> 00:04:41,250 Hello there. 56 00:04:41,260 --> 00:04:49,530 So the 'readFile' reads the file as the name suggests, and it's going to spit out whatever it's reading. 57 00:04:50,010 --> 00:04:50,750 Data. 58 00:04:51,500 --> 00:04:55,490 Now why do we have to put in the 'toString()' here. 59 00:04:56,970 --> 00:05:04,380 Just so you know 'toString()' does something, by default if you don't add anything, it's going to use the 60 00:05:04,380 --> 00:05:11,280 encoding called UTF 8 and UTF 8 is a type of a coding. 61 00:05:11,420 --> 00:05:14,640 Let's just run this to make sure that this is still the same. 62 00:05:14,750 --> 00:05:15,530 Yep. 63 00:05:15,530 --> 00:05:20,300 So UTF 8 is a type of encoding that we have. 64 00:05:20,300 --> 00:05:22,980 That's pretty standard with HTML 5. 65 00:05:23,000 --> 00:05:26,900 And pretty much everybody uses it on the web. 66 00:05:27,290 --> 00:05:37,160 It's a way for us to have characters, whether they're Latin characters, you know ABC's or we have Korean 67 00:05:37,160 --> 00:05:45,770 Japanese, Chinese characters, they're all represented by a number, and UTF 8 is a way of encoding these 68 00:05:45,770 --> 00:05:48,870 numbers to make sure that we can read them. 69 00:05:48,920 --> 00:05:55,910 It's actually quite a complex subject, so I recommend the Wikipedia page that talks about UTF 8. 70 00:05:55,970 --> 00:06:03,420 For now just know that UTF 8 is a type of encoding that is pretty much standard across the web. 71 00:06:03,500 --> 00:06:09,290 You might have also seen ASCII and ASCII was before UTF 8 existed. 72 00:06:09,290 --> 00:06:19,610 ASCII was mainly for Latin based languages, so the ABCs; and UTF expanded that to include many more from 73 00:06:19,860 --> 00:06:22,540 many more languages from different parts of the world. 74 00:06:22,960 --> 00:06:29,780 Alright well a little bit of a tangent that we didn't need to go on, but as you can see over here we 75 00:06:30,530 --> 00:06:30,990 have 76 00:06:31,070 --> 00:06:35,300 'helllloooooo there' and we're able to read the file. 77 00:06:35,300 --> 00:06:36,320 Alright. 78 00:06:36,330 --> 00:06:37,030 Very cool. 79 00:06:38,270 --> 00:06:42,810 Let me just add one other method to your repertoire. 80 00:06:43,250 --> 00:06:57,160 Let's also do 'fs.readFileSync()' and 'fs.readFileSync()' works a little bit different than the previous 81 00:06:57,160 --> 00:07:00,210 one and sync stands for synchronous. 82 00:07:00,580 --> 00:07:01,540 So we'll see what happens. 83 00:07:01,540 --> 00:07:02,500 I'm going to put in 84 00:07:05,340 --> 00:07:12,230 'hello.txt' once again, and this time I'm just going to leave it like that. 85 00:07:14,150 --> 00:07:26,630 And let's just say that this is going to equal the file and we'll just 'console.log(file)'. 86 00:07:26,650 --> 00:07:27,020 Alright. 87 00:07:27,050 --> 00:07:31,710 Let me save that and run this. 88 00:07:31,760 --> 00:07:32,390 Alright. 89 00:07:32,460 --> 00:07:37,370 We know once again that we got this because we need to do 'toString()'. 90 00:07:37,440 --> 00:07:38,610 So let's do that. 91 00:07:38,640 --> 00:07:42,660 I'm going to try 'toString()' 92 00:07:46,060 --> 00:07:51,020 I'm going to save and run this again. Alright I get 93 00:07:51,280 --> 00:07:52,410 'helllloooooo there!!!' 94 00:07:52,510 --> 00:08:05,020 But just to distinguish them, let's just say that this is number one and this is number two. 95 00:08:05,450 --> 00:08:07,660 I save this, run this again. 96 00:08:08,630 --> 00:08:12,510 And whoa, what just happened. 97 00:08:14,050 --> 00:08:21,140 We have one coming after the two. 98 00:08:21,920 --> 00:08:30,830 Now if you've watched the asynchronous synchronous video in this course, then you might understand what's 99 00:08:30,830 --> 00:08:32,090 happening here. 100 00:08:32,090 --> 00:08:34,210 If not I'm going to do a quick synopsis. 101 00:08:34,220 --> 00:08:42,610 But I do recommend you check out that video. Now read file is asynchronous, and that's why it has something 102 00:08:42,610 --> 00:08:45,200 called the callback function. 103 00:08:45,220 --> 00:08:53,290 It is saying: hey I'm going to read this file, and when I'm done with it, you just keep going on with your 104 00:08:53,290 --> 00:08:55,120 business and keep reading. 105 00:08:55,120 --> 00:08:59,010 Line three, line four, line five, line six, all the way to 11. 106 00:08:59,260 --> 00:09:07,010 When I'm done, I'll let you know and I'll either give you an error or some data. 'readFileSync()' which 107 00:09:07,010 --> 00:09:11,740 is synchronous says: I'm going to read this file. 108 00:09:11,780 --> 00:09:14,750 Don't do anything, just wait here when I'm done 109 00:09:14,750 --> 00:09:18,600 I'm going to assign it to 'file' and then you can keep going. 110 00:09:20,210 --> 00:09:30,080 So as you can see here what happens is we read the file and because this is an asynchronous, it's going 111 00:09:30,080 --> 00:09:41,280 to run this and come to line 10 and run the 'fs.readFileSync()' and the 'readFileSync()' because it's saying 112 00:09:41,280 --> 00:09:45,150 hey wait until I'm done this, it is going to print out 113 00:09:45,330 --> 00:09:46,920 'helllloooooo there!!!' 114 00:09:47,160 --> 00:09:52,340 and then the asynchronous code is going to say: hey I finished reading the file, 115 00:09:52,380 --> 00:09:55,770 here's the data and it prints out down over here. 116 00:09:55,770 --> 00:09:59,860 Again I really really recommend you check out the asynchronous synchronous video. 117 00:10:00,330 --> 00:10:03,070 But for now hopefully that makes sense. 118 00:10:03,240 --> 00:10:12,240 Now looking at this, which one should you use? Well for a simple case like this, 119 00:10:12,400 --> 00:10:19,690 we can use whichever we want. But you may see a problem with the second one especially if we're building 120 00:10:19,690 --> 00:10:21,150 something like a server. 121 00:10:21,250 --> 00:10:25,090 If we have a massive file that has something more than 'helllloooooo there!!!', 122 00:10:25,300 --> 00:10:34,360 well if we're doing 'readFileSync()', what happens is we're going to halt or pause the execution of our 123 00:10:34,360 --> 00:10:39,980 file and it's going to read the entire text. 124 00:10:40,370 --> 00:10:47,000 And the program is just going to be waiting for that to finish; versus the read file which says: hey you 125 00:10:47,000 --> 00:10:48,230 can keep going ahead. 126 00:10:48,320 --> 00:10:50,340 I'll let you know when I'm done with this. 127 00:10:50,360 --> 00:10:56,210 So when you're building a server and let's say an express server with routes. 128 00:10:56,360 --> 00:11:03,530 Well in that case we want to use readFile() if we're reading any sort of a file or text file, whatever 129 00:11:03,530 --> 00:11:04,160 it is. 130 00:11:04,310 --> 00:11:08,680 Because that way we're not blocking the execution and the program can keep doing things. 131 00:11:09,700 --> 00:11:10,050 Alright. 132 00:11:10,150 --> 00:11:12,750 So one last time just to clarify things. 133 00:11:12,760 --> 00:11:17,690 I'm going to label this as 'Async' and 'Sync'. 134 00:11:17,710 --> 00:11:22,350 So once again if we run this we have 'Sync' and 'Async'. 135 00:11:22,420 --> 00:11:23,360 'helllloooooo there!!!' 136 00:11:23,790 --> 00:11:32,350 But very cool we're able to read a file and both instances which was kind of cool. 137 00:11:32,350 --> 00:11:38,800 Let me show you a few more of these and then tell you exactly why this is so exciting, and how we can 138 00:11:38,800 --> 00:11:40,880 use it to do some really exciting things. 139 00:11:44,090 --> 00:11:50,770 So let's say the 'helllloooooo there!!!' text is kind of boring and we want to add to it. 140 00:11:51,110 --> 00:11:54,230 Well we can do something like this. 141 00:11:55,280 --> 00:12:01,160 'fs.appendFile()' we give it the file we want to append. 142 00:12:01,430 --> 00:12:05,220 So in this case it's './hello.txt'. 143 00:12:05,780 --> 00:12:10,130 And by the way if this doesn't exist, it'll actually just create the file for us. 144 00:12:10,130 --> 00:12:13,130 And the second parameter will say what we want to add. 145 00:12:13,130 --> 00:12:28,150 So let's say we also wanted to add maybe a space and then say "This is so cool", now there is an exclamation mark. 146 00:12:28,210 --> 00:12:36,020 And finally third parameter takes in an error and this error will just say that if there is an error. 147 00:12:36,040 --> 00:12:37,020 Let me make this smaller 148 00:12:37,040 --> 00:12:43,540 so you can see - let's just say if there is an error, we'll just 'console.log' 149 00:12:47,320 --> 00:12:49,790 'console.log(err)' 150 00:12:49,870 --> 00:12:50,210 Alright. 151 00:12:50,260 --> 00:12:51,780 Perfect. 152 00:12:51,830 --> 00:12:53,900 So let's run this and see what happens. 153 00:12:53,900 --> 00:12:57,520 I'm going to run the script and I get 154 00:12:57,530 --> 00:12:58,360 'helllloooooo there!!!' 155 00:12:58,370 --> 00:12:58,970 'helllloooooo there!!!' 156 00:12:58,970 --> 00:13:10,710 Because these ran first but if I go to 'hello.txt', look at that, we just wrote to a file. And now we have "This 157 00:13:10,710 --> 00:13:11,690 is so cool." 158 00:13:12,000 --> 00:13:17,440 So if I run this again and let me clear this just so we have more space 159 00:13:21,160 --> 00:13:22,180 Look at that. I get 160 00:13:22,270 --> 00:13:22,930 'helllloooooo there!!!' 161 00:13:22,930 --> 00:13:23,860 'This is so cool' 162 00:13:23,860 --> 00:13:24,520 'helllloooooo there!!!' 163 00:13:24,520 --> 00:13:25,990 'This is so cool' 164 00:13:26,010 --> 00:13:30,770 If we go back to the hello.txt file, Oh boy we've add in even more 165 00:13:30,770 --> 00:13:31,850 'This is so cool' 166 00:13:32,490 --> 00:13:33,460 text. 167 00:13:33,680 --> 00:13:35,140 So that's pretty cool. 168 00:13:35,140 --> 00:13:39,740 We just added a few more lines to our file. 169 00:13:39,800 --> 00:13:42,450 Let me remove that just so it doesn't get too annoying. 170 00:13:42,800 --> 00:13:44,780 Going to save it and come back. 171 00:13:46,190 --> 00:13:48,930 All right so we learned the append. 172 00:13:49,090 --> 00:13:57,880 Let me just comment this out so that it doesn't interfere with any of the other ones. 173 00:13:57,900 --> 00:14:04,570 Let's do a 'write' this time - now create some space here so we can see 174 00:14:07,240 --> 00:14:09,220 With the write, as you can imagine, 175 00:14:10,320 --> 00:14:17,020 'fs.writeFile()' and we'll just say that this file will be called 'bye.txt'. 176 00:14:17,020 --> 00:14:19,550 Very original naming by myself. 177 00:14:20,400 --> 00:14:21,540 And will just say. 178 00:14:21,540 --> 00:14:23,750 Sad to see you go. 179 00:14:27,200 --> 00:14:44,280 And once again it'll take an error and this error will say 'if (err)' 'console.log(err)' 180 00:14:44,340 --> 00:14:50,640 Alright, so let's save that, run the file or run the script. 181 00:14:51,890 --> 00:14:56,110 And look at that, 'bye.txt' just got added. 182 00:14:56,240 --> 00:15:00,580 If we click on it, we have "Sad to see you go". 183 00:15:00,650 --> 00:15:03,090 We've just created a new text file. 184 00:15:03,940 --> 00:15:05,870 Very very cool. 185 00:15:05,930 --> 00:15:17,010 By the way, just so you know what happens, if let's say I misspell the read file and now it's 'hell.txt' 186 00:15:17,150 --> 00:15:22,280 Well if I run this, I'll get an error and you can see that. 187 00:15:22,340 --> 00:15:30,610 Because it errors out I get the error message, error with a lot of R's. 188 00:15:30,620 --> 00:15:30,930 Alright. 189 00:15:30,950 --> 00:15:39,260 A little bit of a segue there but just in case people are asking or you were thinking about that. 190 00:15:39,340 --> 00:15:39,990 Alright. 191 00:15:40,160 --> 00:15:45,190 Woo~ I'm getting tired so let's do one last one and then I'm going to tell you exactly why this is so 192 00:15:45,190 --> 00:15:46,670 cool. 193 00:15:46,840 --> 00:15:54,180 The final one we're going to do - again let's make some space here - is going to be the 'delete'. 194 00:15:54,490 --> 00:15:59,240 So with that 'delete' we can do 'fs.unlink()'. 195 00:15:59,270 --> 00:16:08,500 the name is a little bit off here but 'fs.unlink()' and we'll say the 'bye.txt' and notice that I was able to 196 00:16:08,500 --> 00:16:18,730 just do 'bye.txt' but just to keep it consistent, I'll just say './bye.txt' with the './' before 197 00:16:18,730 --> 00:16:18,940 it 198 00:16:23,530 --> 00:16:24,400 and with this one. 199 00:16:24,400 --> 00:16:25,970 Again if there's any errors 200 00:16:28,550 --> 00:16:29,680 just 'console.log(err)' 201 00:16:29,790 --> 00:16:32,720 Whoops I forgot to do the 'if' 202 00:16:37,540 --> 00:16:39,900 perfect, now 203 00:16:40,130 --> 00:16:42,020 Well let's see what happens here. 204 00:16:43,460 --> 00:16:52,940 And just for fun, let's just add a 'console.log()' here after it has done deleting and we'll say 'inception' because 205 00:16:52,940 --> 00:16:58,590 we're saying 'bye' to the './bye.text'. Mind blown. 206 00:16:58,990 --> 00:17:00,290 Alright let's run this. 207 00:17:00,290 --> 00:17:06,220 I'm going to say - and we'll comment out the write file here because you don't need to write it, 208 00:17:06,220 --> 00:17:08,319 we already have the 'bye.text'. 209 00:17:08,420 --> 00:17:09,640 Let me save. 210 00:17:09,810 --> 00:17:13,280 Now if you look at the 'bye.text', let's see what happens here when I run the script 211 00:17:16,579 --> 00:17:20,410 look at that, the 'bye.text' was just removed. 212 00:17:20,829 --> 00:17:22,910 Inception just happened. 213 00:17:25,290 --> 00:17:26,430 Awesome. 214 00:17:26,490 --> 00:17:30,840 So that was very exciting for me hopefully it was for you as well. 215 00:17:30,900 --> 00:17:34,270 But why is this exciting? 216 00:17:34,650 --> 00:17:42,900 Well because now we're able to use programming in javascript outside of just web browsers and outside 217 00:17:42,900 --> 00:17:46,760 of just building websites and servers. 218 00:17:46,890 --> 00:17:52,760 What we can do now with this knowledge is we can make our lives easier. 219 00:17:52,800 --> 00:18:01,540 You can think of cases maybe you want to read an excel file and there are things that you can use, using 220 00:18:01,540 --> 00:18:05,690 read file and using NPM modules that you can read. 221 00:18:05,830 --> 00:18:06,180 Let's say. 222 00:18:06,180 --> 00:18:10,730 Columns in an excel and do some math. 223 00:18:11,140 --> 00:18:18,100 Or maybe you have an excel sheet with all these emails and you want to send out an e-mail to let's say 224 00:18:18,190 --> 00:18:24,160 100 people on this list but you also don't want to be caught spamming people by the google filter saying 225 00:18:24,160 --> 00:18:32,650 that hey this account is spamming because they just sent 100 emails in two minutes. Well using something 226 00:18:32,650 --> 00:18:40,150 like Node, you can create perhaps something that in every six hours, sends out five e-mails. 227 00:18:40,150 --> 00:18:48,520 Another thing you can do if you had a robot for example something similar to a file system instead of 228 00:18:48,520 --> 00:18:50,010 having reading from the file. 229 00:18:50,000 --> 00:18:58,000 You can read from the robot, maybe through wireless maybe through wires and get an input of some data 230 00:18:58,450 --> 00:19:00,040 that the robot has. 231 00:19:00,400 --> 00:19:03,570 Maybe it detected a cat. 232 00:19:03,910 --> 00:19:10,120 Well using that input, you can do something you can respond maybe send you an e-mail saying that the 233 00:19:10,120 --> 00:19:16,420 robot just detected a cat and send that e-mail out or send an output. 234 00:19:16,420 --> 00:19:24,160 And that's what we call input output, where you're getting an input from another source, another machine 235 00:19:24,760 --> 00:19:32,140 and then you are also outputting something for consumption, that is really really exciting because it 236 00:19:32,170 --> 00:19:35,490 opens up the world where you can create tools for yourself. 237 00:19:35,620 --> 00:19:36,730 You can automate things. 238 00:19:36,730 --> 00:19:41,760 You can literally do anything that your mind can think of. 239 00:19:42,310 --> 00:19:48,310 And I know this is still early and you're still at the beginning of your developer career but this is 240 00:19:48,310 --> 00:19:54,370 the first step into understanding how you can use programming to solve problems and make your life 241 00:19:54,610 --> 00:19:58,580 more efficient, or maybe just a little bit more fun. 242 00:19:58,630 --> 00:20:02,040 So in the next video we're going to do something fun. 243 00:20:02,140 --> 00:20:07,650 We're going to use what we've just learned and we're going to help Santa out on a coding challenge. 244 00:20:08,720 --> 00:20:10,190 I'll see you in that one. 245 00:20:10,360 --> 00:20:10,570 Bye-Bye