WEBVTT 00:05.800 --> 00:13.480 In this video you're going to get introduced to the file system module or the FS module in no G.S.. 00:13.510 --> 00:20.320 So this is a built in module which you can use to manage files and folders so you can do things like 00:20.320 --> 00:25.850 reading files writing into files creating files or folders and deleting them. 00:25.960 --> 00:28.240 And so many other operations. 00:28.630 --> 00:37.190 So to start I'm going to have a list of files inside our folders which we will manipulate using the 00:37.190 --> 00:38.840 FSA module. 00:38.840 --> 00:45.040 So following is the structure of the letter to make the things easy for you. 00:45.140 --> 00:49.450 So to start we going to look ask the synchronous way of doing things. 00:49.460 --> 00:53.660 So this means that we're going to be writing code which is blocking. 00:54.320 --> 01:01.310 So when you write blocking code in JavaScript then javascript does not move to the next line until the 01:01.310 --> 01:04.270 current one is executed. 01:04.730 --> 01:10.850 So let's go ahead and start writing code and replace these files. 01:11.210 --> 01:15.980 So first of all we will need to use the FSA module. 01:15.980 --> 01:19.620 So we need to require efforts so again to create a constants. 01:19.640 --> 01:25.160 Call these efforts which will be the outputs of require efforts. 01:25.610 --> 01:32.360 Then I'm going to create a couple of variables when we gonna store the names of these two files. 01:32.360 --> 01:34.580 So one variable we call Mars 01:37.440 --> 01:41.190 we're going to store the path of the file mass. 01:41.490 --> 01:42.590 And then another one. 01:42.630 --> 01:46.880 Venus we're going to store the path of Venus. 01:46.920 --> 01:47.560 Okay. 01:47.670 --> 01:52.930 So first of all let's check if this file exists. 01:53.340 --> 01:57.350 So we're going to be doing things the blocking way here. 01:57.370 --> 01:58.130 All right. 01:58.140 --> 02:07.140 So I'm gonna use a method of f s called exists sync so sync for synchronous. 02:07.140 --> 02:08.040 All right. 02:08.190 --> 02:11.800 And we are looking for mass. 02:12.540 --> 02:15.930 So let's log the outputs of these function 02:21.200 --> 02:22.280 and see what we get. 02:24.780 --> 02:26.280 So let's move 02:31.430 --> 02:32.330 to our folder 02:46.290 --> 02:50.000 Okay so let's run up both. 02:50.030 --> 02:54.080 Jess and we getting to. 02:54.260 --> 02:56.630 And that's because Mars does exist. 02:56.750 --> 03:05.800 Now if you look for something else if you look for a file called file or text for example that you would 03:05.800 --> 03:07.510 get false. 03:07.830 --> 03:08.110 Okay. 03:08.110 --> 03:10.750 So that's how you check if a file exists. 03:10.750 --> 03:14.040 Using the blocking way or the synchronous way. 03:14.110 --> 03:17.140 Now let's read the contents of our file. 03:17.200 --> 03:24.540 So the method to use here is going to be read file sync since we're doing it secretly. 03:25.060 --> 03:27.330 So this will take a couple of promises. 03:27.340 --> 03:33.210 So we're gonna start with the name of our files and then we get we're going to say thus we're gonna 03:33.220 --> 03:39.550 be reading our file using the UTF 8 encoding. 03:39.550 --> 03:39.850 All right. 03:40.630 --> 03:46.800 So you can store the outputs of this inside a file data for example. 03:46.840 --> 03:47.440 So 03:51.330 --> 04:01.340 and then you can log data or just log something more readable like 04:07.600 --> 04:08.650 most contents 04:12.110 --> 04:14.800 and then return the data. 04:26.260 --> 04:26.500 Right. 04:26.500 --> 04:29.750 So let's try and see. 04:30.010 --> 04:30.820 Here we go. 04:30.820 --> 04:35.180 So we get the contents of our file Miles. 04:35.190 --> 04:38.880 So if we check inside Mars this is the contents. 04:38.950 --> 04:40.690 Thus we are getting all right. 04:40.990 --> 04:42.340 I've got a land return there. 04:42.340 --> 04:45.850 That's why it's adding another line return. 04:46.690 --> 04:53.370 So if you try again you won't see that's all right. 04:53.400 --> 04:54.810 So let's move to the next one. 04:54.810 --> 04:56.460 Let's create a file. 04:56.470 --> 04:57.890 Jupiter writes 05:00.910 --> 05:07.310 so to do this you need to use a method called right. 05:07.540 --> 05:10.400 File sync. 05:10.470 --> 05:10.730 Okay. 05:10.760 --> 05:14.680 So writes While or writes file sync because you're doing it the synchronous way. 05:14.690 --> 05:20.510 So this method will take as a primary search the name of the file you'd like to create. 05:21.470 --> 05:34.460 So in our case we want to create a file Jupiter which is gonna be Jupiter dot texts to make things tidy. 05:34.460 --> 05:41.270 I'm going to create another variable and I'm going to call it Jupiter. 05:41.410 --> 05:49.050 No text or storage bits of the text inside this bible so we can just use the variable like this. 05:49.440 --> 05:59.540 So if you do this and you pass another parameter or string to the method then this is going to create 05:59.600 --> 06:02.390 a new file named Jupiter. 06:02.390 --> 06:11.010 Those text and it will place whatever content you're right there inside your file. 06:11.320 --> 06:13.240 So let's write something like 06:19.350 --> 06:28.110 Jupiter is the shortest day of all planets. 06:29.050 --> 06:29.400 Okay. 06:29.410 --> 06:30.850 So let's try this 06:36.040 --> 06:37.860 and now you can see that's our file. 06:37.860 --> 06:39.170 Jupiter has been created. 06:39.190 --> 06:44.640 And if we look inside it you'll see the contents that we have just added inside Jupiter. 06:45.210 --> 06:46.960 So the method right. 06:47.090 --> 06:54.370 While we're using all right pilots sync here to make a synchronous synchronous will create a file. 06:54.390 --> 07:01.930 If it does not exist basically and it's well populated with whatever concerns you place there. 07:02.070 --> 07:14.900 Now if you try this same method on a file that exists already then that file will be very recent with 07:14.900 --> 07:17.160 whatever content you pass to the method. 07:17.180 --> 07:27.110 So if you try that's true on Venus then you're going to lose the contents of Venus and you will replace 07:27.110 --> 07:30.600 it with whatever you pass to the function. 07:30.680 --> 07:31.550 So let's try that 07:37.160 --> 07:40.850 so first let's read Jupiter. 07:40.850 --> 07:44.000 The same way we read Mars 07:54.160 --> 07:55.050 so let's use. 07:55.060 --> 08:00.500 Let us instead of cons so that we can reuse our data variable. 08:00.540 --> 08:05.300 So we are reading Jupiter like that's 08:10.730 --> 08:10.880 high. 08:10.950 --> 08:17.910 So basically we are just overwriting Jupiter with this contents because we had already created Jupiter. 08:18.450 --> 08:19.950 So let's do the same thing with 08:26.190 --> 08:32.520 Venus. 08:32.810 --> 08:46.400 And this time we just overriding the contents of Venus. 08:46.520 --> 08:49.950 This time let's just right. 08:50.750 --> 09:00.390 This is some text and let's read Venus afterwards like that's so it is. 09:00.390 --> 09:00.820 Try 09:03.660 --> 09:09.660 so we can see the content of Jupiter first and then we can see the contents of Venus. 09:09.720 --> 09:11.590 It has changed and now we go. 09:11.670 --> 09:15.600 We get only that we used to have a couple of lines there. 09:15.720 --> 09:16.500 Okay. 09:16.710 --> 09:29.040 So now let's append some content to Jupiter so to append contents the method to use is called append 09:29.130 --> 09:29.480 file 09:33.080 --> 09:44.940 like this and we can add sync to make it synchronous so we can pass the path of our file and then let's 09:45.360 --> 09:51.060 just add some text so I'm going to add line return followed by some text 09:54.280 --> 09:54.970 okay. 09:58.100 --> 10:03.970 So right after that let's read the contents of the new contents of Jupiter 10:12.400 --> 10:15.830 series go ahead and do this. 10:15.860 --> 10:17.930 So that's the new contents of Jupiter. 10:17.930 --> 10:19.530 So you got one more line. 10:19.670 --> 10:23.370 If you click on there you can see the new contents. 10:23.540 --> 10:25.460 Perfect. 10:25.460 --> 10:30.110 Now to double check that our code is actually blocking or synchronous. 10:30.110 --> 10:37.580 You can log something right on the end of the code like logging hello and you can see that this line 10:37.610 --> 10:46.940 is going to be printed after all that code has been executed and this will prove that your code is actually 10:47.060 --> 10:47.890 blocking. 10:47.900 --> 10:57.980 Now if you had some asynchronous code then you would have this line executed probably first because 10:59.400 --> 11:05.840 inside those lines you had callbacks which were awaited to be executed. 11:05.910 --> 11:12.270 So we'd start with that line first because it's ready and then move to everything else. 11:12.270 --> 11:17.200 Once you get or you arrive to the callbacks and that's something we're going to be covering. 11:17.230 --> 11:17.570 All right. 11:17.640 --> 11:27.170 Now if you do a quality check on this code then you should find something that is not 100 percent correct. 11:27.210 --> 11:32.030 So you code like this would not pass a quality check. 11:32.100 --> 11:38.360 So I suggest you pause the video and try to find that. 11:38.440 --> 11:45.480 All right so hopefully you managed to spot the issue so to make things clear what I'm gonna do. 11:45.580 --> 11:52.780 I'm going to try to read a file for example that does not exist. 11:53.000 --> 12:00.350 So let's read file dot text and run this code like this. 12:00.750 --> 12:06.970 Well we're getting an error because it does file as it does not exist. 12:06.990 --> 12:13.010 So our code stops right there and none of these lines will be executed. 12:13.410 --> 12:24.260 So you will need to manage these errors by using a try catch statements. 12:24.390 --> 12:33.690 So whenever you use a method like read file sync this method does not have a callback meaning that if 12:33.770 --> 12:38.610 a if a if an error occurs then there is no way to catch this error. 12:38.610 --> 12:45.940 So he's going to be your responsibility to catch this error and do whatever you like with that error. 12:46.020 --> 12:57.370 So we will need to wrap this code inside a try catch statements 13:01.310 --> 13:13.560 like this say try that code and then if you catch an error then you can do things like log the error 13:19.970 --> 13:20.570 like this 13:29.110 --> 15:03.010 and now let's wrap all the other ones inside try catch statements. 15:03.160 --> 15:04.450 All right. 15:04.720 --> 15:13.090 So if you are wondering how I do the formatting in the keyboard so you just press option command in 15:13.090 --> 15:24.610 a Mac or window or control out on windows and that's how you do the formatting in brackets right. 15:24.640 --> 15:26.700 So now let's save. 15:27.370 --> 15:34.050 Now if you try to read a file that does not exist 15:40.540 --> 15:43.710 you get the error thrown like that. 15:43.910 --> 15:44.260 Okay. 15:44.290 --> 15:47.110 And then your code carries on as normal. 15:47.110 --> 15:47.440 All right. 15:47.440 --> 15:53.680 In the next video you're going to work with the FS module using the asynchronous way using callback 15:53.680 --> 15:54.310 functions.