0 1 00:00:00,690 --> 00:00:06,360 When you install Node, it already comes bundled with a whole bunch of built in modules. 1 2 00:00:06,360 --> 00:00:11,910 These are essentially libraries of code that the Node team wrote so that they can help you with day to 2 3 00:00:11,910 --> 00:00:14,790 day things that you might want to do using Node. 3 4 00:00:14,790 --> 00:00:22,230 Now, we mentioned that by liberating Javascript from the browser using Node, we can now use Node.js to interact 4 5 00:00:22,320 --> 00:00:24,180 with the computer directly. 5 6 00:00:24,330 --> 00:00:31,970 So a good example of that is we can use Node.js to get access directly to the local files of the computer. 6 7 00:00:32,190 --> 00:00:37,730 So let's take a look at one of the built in modules for Node that helps us to do that. 7 8 00:00:37,740 --> 00:00:45,240 So if you head over to nodejs.org/api, then you can see all of the native modules and the documentation 8 9 00:00:45,300 --> 00:00:47,070 of how you can use it. 9 10 00:00:47,090 --> 00:00:51,850 Now if we have a look in the left hand bar here, there's something called the file system. 10 11 00:00:52,150 --> 00:00:58,140 And, as you might predict, this helps you interact with the local file system using Node. 11 12 00:00:58,140 --> 00:01:04,380 Now there's a whole bunch of methods and properties that you can tap into, but in order to use the module 12 13 00:01:04,440 --> 00:01:07,530 we first have to require it. 13 14 00:01:07,530 --> 00:01:13,950 Now if we head over to Atom and add that new project folder that we created earlier on into our sidebar, 14 15 00:01:14,060 --> 00:01:20,700 so intro-to-node, and we can delete our console.log in our index.js. Now instead of having that, 15 16 00:01:20,790 --> 00:01:27,120 we're going to use the file system module, and we're going to write this line of code in order to use 16 17 00:01:27,210 --> 00:01:28,210 this module. 17 18 00:01:28,230 --> 00:01:34,410 So the first thing we write is a keyword called const, and up till now we've not been using this keyword 18 19 00:01:34,500 --> 00:01:35,250 at all. 19 20 00:01:35,250 --> 00:01:37,640 We've been using something called var. 20 21 00:01:37,770 --> 00:01:44,730 Now var creates a variable where, say, for example, if I said var a = 2, then later on I decide to 21 22 00:01:44,730 --> 00:01:52,950 change the value of a, a now equals 5, then this is how you can use vars to vary your variable. 22 23 00:01:52,950 --> 00:01:55,770 Now const stands for constant, 23 24 00:01:55,890 --> 00:02:02,450 and if I create a constant and I assign it a value, later on I can't change the value. 24 25 00:02:02,460 --> 00:02:04,590 I can't then say a now equals 5. 25 26 00:02:04,640 --> 00:02:09,310 It'll actually give me an error, and it'll tell me that attempting to override a, 26 27 00:02:09,360 --> 00:02:11,250 which is a constant. 27 28 00:02:11,370 --> 00:02:17,010 That means that once you've assigned it a value, you can't change constants any more. 28 29 00:02:17,070 --> 00:02:23,340 And this is useful when you're creating containers that are not going to be changed or meddled with later 29 30 00:02:23,340 --> 00:02:23,650 on. 30 31 00:02:23,820 --> 00:02:30,000 So, for example, if in my web site I needed to use the value of pi, then pi is never going to change, right? 31 32 00:02:30,030 --> 00:02:37,770 So I might just create a constant called pi and set it to 3.14159, and that's about all 32 33 00:02:37,770 --> 00:02:38,600 I can remember. 33 34 00:02:38,730 --> 00:02:44,040 So, in this case, we're creating a const called fs, and that stands for file system. 34 35 00:02:44,040 --> 00:02:49,500 Now you can call this constant whatever you want. You can call it fileSystem. But for simplicity's sake we're 35 36 00:02:49,500 --> 00:02:55,470 just going to call it fs. And we're going to set it to equal the file system module. And in order to use 36 37 00:02:55,530 --> 00:03:03,300 libraries of code, or modules, or packages, inside our project, we have to require the module. And the module 37 38 00:03:03,390 --> 00:03:05,340 is called fs. 38 39 00:03:05,340 --> 00:03:09,200 So let's go ahead and add that in here. 39 40 00:03:09,390 --> 00:03:16,830 So here's our brand new constant called fs, and it is set to equal the module that is fs. And we get access 40 41 00:03:16,830 --> 00:03:17,430 to it 41 42 00:03:17,550 --> 00:03:20,480 using this require method from Node. 42 43 00:03:20,760 --> 00:03:24,410 Now, at the moment Atom is giving me a warning here, 43 44 00:03:24,420 --> 00:03:30,510 as you can see from this little yellow dot, and it's telling me that the JSHint package, that I installed 44 45 00:03:30,840 --> 00:03:37,320 to keep my Javascript code tidy and consistent with other programmers, is giving me a warning saying that 45 46 00:03:37,320 --> 00:03:45,060 const is only available in ES6, it’s the latest stable version of Javascript at the time of recording. 46 47 00:03:45,510 --> 00:03:52,730 And I can switch off these warnings by simply writing a comment that says jshint 47 48 00:03:53,310 --> 00:03:56,680 esversion:6, 48 49 00:03:56,760 --> 00:04:00,610 and this will tell the linter that I'm actually using ES Version 6, 49 50 00:04:00,630 --> 00:04:02,250 don’t worry about the weird syntax, 50 51 00:04:02,310 --> 00:04:04,990 this is simply a new feature of our 51 52 00:04:05,280 --> 00:04:05,810 ES 52 53 00:04:05,850 --> 00:04:08,190 Version 6 of Javascript. 53 54 00:04:08,190 --> 00:04:15,300 So now that we've required fs, that means that we've incorporated this file system module into our project, 54 55 00:04:15,630 --> 00:04:18,530 and we're now able to use it in our project. 55 56 00:04:18,540 --> 00:04:22,080 Now, what are the sort of things that you can do using file system? 56 57 00:04:22,110 --> 00:04:25,040 Well, there's actually a whole bunch of things that you can do, 57 58 00:04:25,050 --> 00:04:32,010 for example, you can specify paths, you can open files, you can change files, you can access and read and 58 59 00:04:32,010 --> 00:04:34,410 write to the local file system. 59 60 00:04:34,560 --> 00:04:40,900 But, as we said before, we should be able to access and manipulate the local files if we're using Node.js. 60 61 00:04:41,130 --> 00:04:45,710 So let's just use one of these methods to show you that this is possible. 61 62 00:04:45,720 --> 00:04:53,430 So inside my intro-to-node, I'm going to create a new file, and I'm going to call it file1.txt. 62 63 00:04:53,460 --> 00:04:56,230 So this is just a pure simple text file. 63 64 00:04:56,310 --> 00:04:59,850 And inside here I'm just going to write a little bit of text, 64 65 00:05:00,660 --> 00:05:03,480 “I am file 1.”, hit save, 65 66 00:05:03,510 --> 00:05:06,340 and now I'm going to go into my index.js, and 66 67 00:05:06,420 --> 00:05:11,910 I'm going to use this method called copyFileSync in order to create a copy of my file, 67 68 00:05:11,910 --> 00:05:12,870 file1. 68 69 00:05:12,990 --> 00:05:18,810 So our docs will tell us how to use it, and what we have to do is first, of course, require the module, 69 70 00:05:19,110 --> 00:05:23,470 and then we're going to call the method and we're going to pass in two parameters. 70 71 00:05:23,550 --> 00:05:26,360 So the first parameter is the source, 71 72 00:05:26,370 --> 00:05:29,090 so that's the source filename to copy. 72 73 00:05:29,340 --> 00:05:34,850 And the second one is the destination, which is the destination filename to copy to. 73 74 00:05:34,860 --> 00:05:36,270 So that seems pretty easy. 74 75 00:05:36,270 --> 00:05:47,550 So all you have to do is say fs.copyFileSync, and the source file is called file1.txt, and the destination 75 76 00:05:47,550 --> 00:05:48,000 file, 76 77 00:05:48,000 --> 00:05:51,250 we’ll just call it file2.txt. 77 78 00:05:51,300 --> 00:05:57,160 So, what this line of code will do is look within the current directory, 78 79 00:05:57,300 --> 00:06:03,270 so intro-to-node folder, it will look for a file that's called file1.txt, and then it will copy 79 80 00:06:03,270 --> 00:06:06,240 it to something called file2.txt. 80 81 00:06:06,240 --> 00:06:08,850 Now currently file2.txt doesn't exist. 81 82 00:06:08,850 --> 00:06:11,860 So this is going to be the name of our copy. 82 83 00:06:12,150 --> 00:06:19,380 So now if I hit save and I head over to Hyper, and make sure that you’re still within the directory that 83 84 00:06:19,410 --> 00:06:23,910 our index.js is contained, which I've previously called intro-to-node. 84 85 00:06:23,910 --> 00:06:30,240 Now here I'm going to use the node command, and then I'm going to run the index.js file using Node.js. 85 86 00:06:30,960 --> 00:06:36,280 So now that I see my prompt again, so my name plus the dollar sign, 86 87 00:06:36,330 --> 00:06:39,450 that means that this execution has completed. 87 88 00:06:39,450 --> 00:06:45,540 So now, if we head into Atom and we look inside our folder, intro-to-node, you can see I now have two files, 88 89 00:06:45,990 --> 00:06:52,830 file1, which still has the same content as before, and also file2, which now has the copied content 89 90 00:06:52,890 --> 00:07:01,850 from file1. So if file2 already existed, then this copyFileSync will simply replace the existing content. 90 91 00:07:02,130 --> 00:07:11,100 So, let's say that in file2 we have some text saying that “I am file 2.txt” and we hit save, and we 91 92 00:07:11,100 --> 00:07:13,090 run our script again, 92 93 00:07:13,260 --> 00:07:19,910 then it will again try to replace what's inside file2 with a copy of what's inside file1. 93 94 00:07:19,920 --> 00:07:23,230 So file2 currently reads “I am file 2.txt”. 94 95 00:07:23,290 --> 00:07:29,100 So, in order to run the last command, all you have to do is just hit the Up key on your keyboard, and 95 96 00:07:29,160 --> 00:07:29,520 it will, 96 97 00:07:29,550 --> 00:07:33,690 and you can cycle through all the previous commands that you've entered. 97 98 00:07:33,690 --> 00:07:36,110 And this is just a quicker way of repeating commands. 98 99 00:07:36,240 --> 00:07:37,970 So we're going to run that file again, 99 100 00:07:38,010 --> 00:07:39,190 and now it's done. 100 101 00:07:39,290 --> 00:07:40,710 And let's head over here. 101 102 00:07:40,890 --> 00:07:44,880 So file2 now reads “I am file 1.” It’s cool, right? 102 103 00:07:44,880 --> 00:07:53,070 So we're able to use Node.js to write Javascript code and execute it to affect our computer, including 103 104 00:07:53,100 --> 00:07:57,900 manipulating and reading and writing to our local file system. 104 105 00:07:57,900 --> 00:08:02,190 Now in the next module, we're going to talk about how we can use external modules, 105 106 00:08:02,220 --> 00:08:07,630 so not just the modules that come bundled with Node, which are available here, 106 107 00:08:07,830 --> 00:08:13,680 but actually there's a whole bunch of libraries that other people have written which we can tap into, 107 108 00:08:13,920 --> 00:08:16,640 and we can use, using something called NPM. 108 109 00:08:16,800 --> 00:08:19,650 So for all of that and more, I’ll see you on the next lesson.