WEBVTT 0 00:00.690 --> 00:06.360 When you install Node, it already comes bundled with a whole bunch of built in modules. 1 00:06.360 --> 00:11.910 These are essentially libraries of code that the Node team wrote so that they can help you with day to 2 00:11.910 --> 00:14.790 day things that you might want to do using Node. 3 00:14.790 --> 00:22.230 Now, we mentioned that by liberating Javascript from the browser using Node, we can now use Node.js to interact 4 00:22.320 --> 00:24.180 with the computer directly. 5 00:24.330 --> 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 00:32.190 --> 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 00:37.740 --> 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 00:45.300 --> 00:47.070 of how you can use it. 9 00:47.090 --> 00:51.850 Now if we have a look in the left hand bar here, there's something called the file system. 10 00:52.150 --> 00:58.140 And, as you might predict, this helps you interact with the local file system using Node. 11 00:58.140 --> 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 01:04.440 --> 01:07.530 we first have to require it. 13 01:07.530 --> 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 01:14.060 --> 01:20.700 so intro-to-node, and we can delete our console.log in our index.js. Now instead of having that, 15 01:20.790 --> 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 01:27.210 --> 01:28.210 this module. 17 01:28.230 --> 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 01:34.500 --> 01:35.250 at all. 19 01:35.250 --> 01:37.640 We've been using something called var. 20 01:37.770 --> 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 01:44.730 --> 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 01:52.950 --> 01:55.770 Now const stands for constant, 23 01:55.890 --> 02:02.450 and if I create a constant and I assign it a value, later on I can't change the value. 24 02:02.460 --> 02:04.590 I can't then say a now equals 5. 25 02:04.640 --> 02:09.310 It'll actually give me an error, and it'll tell me that attempting to override a, 26 02:09.360 --> 02:11.250 which is a constant. 27 02:11.370 --> 02:17.010 That means that once you've assigned it a value, you can't change constants any more. 28 02:17.070 --> 02:23.340 And this is useful when you're creating containers that are not going to be changed or meddled with later 29 02:23.340 --> 02:23.650 on. 30 02:23.820 --> 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 02:30.030 --> 02:37.770 So I might just create a constant called pi and set it to 3.14159, and that's about all 32 02:37.770 --> 02:38.600 I can remember. 33 02:38.730 --> 02:44.040 So, in this case, we're creating a const called fs, and that stands for file system. 34 02:44.040 --> 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 02:49.500 --> 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 02:55.530 --> 03:03.300 libraries of code, or modules, or packages, inside our project, we have to require the module. And the module 37 03:03.390 --> 03:05.340 is called fs. 38 03:05.340 --> 03:09.200 So let's go ahead and add that in here. 39 03:09.390 --> 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 03:16.830 --> 03:17.430 to it 41 03:17.550 --> 03:20.480 using this require method from Node. 42 03:20.760 --> 03:24.410 Now, at the moment Atom is giving me a warning here, 43 03:24.420 --> 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 03:30.840 --> 03:37.320 to keep my Javascript code tidy and consistent with other programmers, is giving me a warning saying that 45 03:37.320 --> 03:45.060 const is only available in ES6, it’s the latest stable version of Javascript at the time of recording. 46 03:45.510 --> 03:52.730 And I can switch off these warnings by simply writing a comment that says jshint 47 03:53.310 --> 03:56.680 esversion:6, 48 03:56.760 --> 04:00.610 and this will tell the linter that I'm actually using ES Version 6, 49 04:00.630 --> 04:02.250 don’t worry about the weird syntax, 50 04:02.310 --> 04:04.990 this is simply a new feature of our 51 04:05.280 --> 04:05.810 ES 52 04:05.850 --> 04:08.190 Version 6 of Javascript. 53 04:08.190 --> 04:15.300 So now that we've required fs, that means that we've incorporated this file system module into our project, 54 04:15.630 --> 04:18.530 and we're now able to use it in our project. 55 04:18.540 --> 04:22.080 Now, what are the sort of things that you can do using file system? 56 04:22.110 --> 04:25.040 Well, there's actually a whole bunch of things that you can do, 57 04:25.050 --> 04:32.010 for example, you can specify paths, you can open files, you can change files, you can access and read and 58 04:32.010 --> 04:34.410 write to the local file system. 59 04:34.560 --> 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 04:41.130 --> 04:45.710 So let's just use one of these methods to show you that this is possible. 61 04:45.720 --> 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 04:53.460 --> 04:56.230 So this is just a pure simple text file. 63 04:56.310 --> 04:59.850 And inside here I'm just going to write a little bit of text, 64 05:00.660 --> 05:03.480 “I am file 1.”, hit save, 65 05:03.510 --> 05:06.340 and now I'm going to go into my index.js, and 66 05:06.420 --> 05:11.910 I'm going to use this method called copyFileSync in order to create a copy of my file, 67 05:11.910 --> 05:12.870 file1. 68 05:12.990 --> 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 05:19.110 --> 05:23.470 and then we're going to call the method and we're going to pass in two parameters. 70 05:23.550 --> 05:26.360 So the first parameter is the source, 71 05:26.370 --> 05:29.090 so that's the source filename to copy. 72 05:29.340 --> 05:34.850 And the second one is the destination, which is the destination filename to copy to. 73 05:34.860 --> 05:36.270 So that seems pretty easy. 74 05:36.270 --> 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 05:47.550 --> 05:48.000 file, 76 05:48.000 --> 05:51.250 we’ll just call it file2.txt. 77 05:51.300 --> 05:57.160 So, what this line of code will do is look within the current directory, 78 05:57.300 --> 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 06:03.270 --> 06:06.240 it to something called file2.txt. 80 06:06.240 --> 06:08.850 Now currently file2.txt doesn't exist. 81 06:08.850 --> 06:11.860 So this is going to be the name of our copy. 82 06:12.150 --> 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 06:19.410 --> 06:23.910 our index.js is contained, which I've previously called intro-to-node. 84 06:23.910 --> 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 06:30.960 --> 06:36.280 So now that I see my prompt again, so my name plus the dollar sign, 86 06:36.330 --> 06:39.450 that means that this execution has completed. 87 06:39.450 --> 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 06:45.990 --> 06:52.830 file1, which still has the same content as before, and also file2, which now has the copied content 89 06:52.890 --> 07:01.850 from file1. So if file2 already existed, then this copyFileSync will simply replace the existing content. 90 07:02.130 --> 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 07:11.100 --> 07:13.090 run our script again, 92 07:13.260 --> 07:19.910 then it will again try to replace what's inside file2 with a copy of what's inside file1. 93 07:19.920 --> 07:23.230 So file2 currently reads “I am file 2.txt”. 94 07:23.290 --> 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 07:29.160 --> 07:29.520 it will, 96 07:29.550 --> 07:33.690 and you can cycle through all the previous commands that you've entered. 97 07:33.690 --> 07:36.110 And this is just a quicker way of repeating commands. 98 07:36.240 --> 07:37.970 So we're going to run that file again, 99 07:38.010 --> 07:39.190 and now it's done. 100 07:39.290 --> 07:40.710 And let's head over here. 101 07:40.890 --> 07:44.880 So file2 now reads “I am file 1.” It’s cool, right? 102 07:44.880 --> 07:53.070 So we're able to use Node.js to write Javascript code and execute it to affect our computer, including 103 07:53.100 --> 07:57.900 manipulating and reading and writing to our local file system. 104 07:57.900 --> 08:02.190 Now in the next module, we're going to talk about how we can use external modules, 105 08:02.220 --> 08:07.630 so not just the modules that come bundled with Node, which are available here, 106 08:07.830 --> 08:13.680 but actually there's a whole bunch of libraries that other people have written which we can tap into, 107 08:13.920 --> 08:16.640 and we can use, using something called NPM. 108 08:16.800 --> 08:19.650 So for all of that and more, I’ll see you on the next lesson.