Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,440 --> 00:00:08,630
In the last lesson, we created a very basic node application and all our application did was to console
2
00:00:08,630 --> 00:00:10,460
log a bit of text.
3
00:00:10,490 --> 00:00:17,600
In this lesson I'm going to show you how to use native node modules that will allow you to leverage
4
00:00:17,600 --> 00:00:23,360
existing code that's been built into Node that makes it way more powerful than just what you can do
5
00:00:23,360 --> 00:00:24,620
with JavaScript.
6
00:00:24,830 --> 00:00:27,920
What exactly are these native node modules?
7
00:00:27,920 --> 00:00:30,620
Well, they're kind of like your starting tool set.
8
00:00:30,710 --> 00:00:38,390
Now we mentioned before that Node had a whole bunch of features built into it just so that it was able
9
00:00:38,390 --> 00:00:45,320
to make it easier for you to create applications, especially on the server side And the code that was
10
00:00:45,320 --> 00:00:52,460
written in order to make this easier include things such as a file access, so reading and writing into
11
00:00:52,460 --> 00:00:53,900
the local file system.
12
00:00:53,900 --> 00:01:00,240
So that could be local on your computer or in the case of your web application, it will be onto the
13
00:01:00,240 --> 00:01:07,470
server computer as well as things such as networking in order to reach across the internet and carry
14
00:01:07,470 --> 00:01:09,060
out certain bits of functionality.
15
00:01:09,060 --> 00:01:14,070
That's important for the back end of a web app and a lot more.
16
00:01:14,100 --> 00:01:20,490
Essentially, you can think of these native modules as kind of like the games that come pre-bundled
17
00:01:20,490 --> 00:01:22,560
with your operating system.
18
00:01:22,560 --> 00:01:25,410
One of my favorite is Minesweeper on Windows.
19
00:01:25,410 --> 00:01:29,190
It's sort of endlessly entertaining and don't really know why.
20
00:01:29,190 --> 00:01:36,120
But also remember there was pinball and hearts and solitaire and you can of course install more games
21
00:01:36,120 --> 00:01:42,570
onto your computer, but the native ones are the ones that came pre bundled because they thought maybe
22
00:01:42,570 --> 00:01:49,140
they'd be useful to entertain you or to while away the hours like I did in my childhood.
23
00:01:49,170 --> 00:01:55,200
Now in the course resources for this lesson so you can find that in the curriculum and there'll be a
24
00:01:55,200 --> 00:01:57,000
dropdown for course resources.
25
00:01:57,000 --> 00:02:05,420
You should find a link to this documentation page and this page documents all of the native modules.
26
00:02:05,430 --> 00:02:10,620
Now there's a lot of them and we're not going to go through them each one by one because that would
27
00:02:10,620 --> 00:02:11,460
take years.
28
00:02:11,460 --> 00:02:14,280
But also that's what the documentation is for.
29
00:02:14,310 --> 00:02:17,310
It's more like a dictionary and not a curriculum.
30
00:02:17,460 --> 00:02:24,270
And some of these modules we're going to be using in the future, such as the Http, the Hypertext transfer
31
00:02:24,270 --> 00:02:27,860
Protocol module and maybe the Path module.
32
00:02:27,870 --> 00:02:35,400
But right now the one I want to show you is one of the most important and basic features of Node, and
33
00:02:35,400 --> 00:02:37,170
this is the file system.
34
00:02:37,170 --> 00:02:44,550
Remember with native JavaScript that is run in a browser, you can't access a user's files on their
35
00:02:44,550 --> 00:02:45,360
computer.
36
00:02:45,390 --> 00:02:51,300
Can you imagine if you go on to a website and somebody is able to read and write your local file system?
37
00:02:51,300 --> 00:02:54,840
That is a recipe for disaster.
38
00:02:54,840 --> 00:03:02,490
But in the case of NodeJS, because it liberates JavaScript out of the browser, we're able to use it
39
00:03:02,490 --> 00:03:08,820
to access, to read and write to files on the server or in this case to our local computer.
40
00:03:08,820 --> 00:03:13,890
Now you don't have to worry about it being malicious because you're going to be the one writing all
41
00:03:13,890 --> 00:03:14,670
the code.
42
00:03:14,760 --> 00:03:16,500
So what is the file system?
43
00:03:16,500 --> 00:03:24,890
Well, the file system is the native node module that allows us to access the local storage.
44
00:03:24,900 --> 00:03:31,830
And in order to start using it, we need to either import the code from the file system module or we
45
00:03:31,830 --> 00:03:36,840
can require the bits of code that we need from this module.
46
00:03:37,380 --> 00:03:43,170
In the next lesson, I'm going to talk to you a little bit more about ESM, but for now we're going
47
00:03:43,170 --> 00:03:51,390
to use the Commonjs pattern in order to get hold of the methods that we need from this file system module.
48
00:03:51,840 --> 00:03:59,580
I want you to download the starting files for this module, which is in a zipped folder called 2.2 Native
49
00:03:59,580 --> 00:04:05,370
Modules, and you're going to need to extract that folder and open it inside vs code.
50
00:04:05,400 --> 00:04:10,620
If you take a look inside the Index.js, you'll see it's yet another blank page.
51
00:04:10,770 --> 00:04:16,019
But here is where we're going to get hold of that file system module.
52
00:04:16,019 --> 00:04:23,970
So I'm going to create a const called FS and this is going to be set to equal require and the module
53
00:04:23,970 --> 00:04:27,270
that we need has to be entered in as a string.
54
00:04:27,270 --> 00:04:32,760
And you can see as soon as I put in those double quotes, it shows me all of the native node modules
55
00:04:32,760 --> 00:04:34,080
that are available.
56
00:04:34,170 --> 00:04:42,090
It knows exactly what I'm trying to do and the one we want is called FS, which is short for file system.
57
00:04:42,570 --> 00:04:49,440
So now that we've gotten hold of this module, we're going to use one of the methods in that module.
58
00:04:49,440 --> 00:04:55,080
And if we scroll up you can see that there are many, many methods available, but the one that we want
59
00:04:55,080 --> 00:04:59,940
to use is called FS dot write file because.
60
00:04:59,970 --> 00:05:07,380
This is going to allow us to take a message that say user inputs or from some bit of our website and
61
00:05:07,380 --> 00:05:13,050
we're able to write it into a file to be saved onto the computer.
62
00:05:13,050 --> 00:05:16,140
And in this case, it will be our local computer.
63
00:05:16,830 --> 00:05:24,720
The method that we need in order to do this is called write file, and this is from the node file system
64
00:05:24,720 --> 00:05:25,590
module.
65
00:05:25,620 --> 00:05:29,980
And then in order to use this method, you can see the structure is like this.
66
00:05:30,000 --> 00:05:34,080
The first is the file that we want to create.
67
00:05:34,110 --> 00:05:37,440
Next is the data that we want to put in that file.
68
00:05:37,440 --> 00:05:41,920
And finally we have the callback to handle any errors or any issues.
69
00:05:41,970 --> 00:05:45,690
This is the example code for how this would work.
70
00:05:45,690 --> 00:05:51,480
And again, notice how they've got the import format instead of the require format, which we're going
71
00:05:51,480 --> 00:05:54,600
to go through very, very soon in the next lesson.
72
00:05:54,600 --> 00:05:56,940
For now, don't worry too much about that.
73
00:05:56,940 --> 00:06:03,040
We're going to use this write file method and we're going to pull it out of the FS module.
74
00:06:03,580 --> 00:06:10,660
Heading back over here, we can type FS, dot write file and there we can access this method that we
75
00:06:10,660 --> 00:06:11,260
need.
76
00:06:11,290 --> 00:06:15,190
Now the file I'm going to create is called message dot text.
77
00:06:15,190 --> 00:06:16,750
So it's going to be a text file.
78
00:06:16,750 --> 00:06:23,020
And because I'm not putting in a full path here, it's going to create it in the same parent folder
79
00:06:23,020 --> 00:06:25,180
as my index.js.
80
00:06:25,270 --> 00:06:28,390
Now the next part I need to put in is the data.
81
00:06:28,390 --> 00:06:31,090
So I'm going to just enter a string.
82
00:06:31,090 --> 00:06:33,820
So let's use that previous sentence.
83
00:06:33,820 --> 00:06:36,340
Hello from NodeJS.
84
00:06:37,090 --> 00:06:40,720
And then finally we have our callback.
85
00:06:41,170 --> 00:06:43,360
Our callback is pretty simple.
86
00:06:43,360 --> 00:06:48,370
It can return an error and if there is an error, we can throw the error.
87
00:06:48,370 --> 00:06:53,500
And if not, we can simply console log that the file has been saved.
88
00:06:53,530 --> 00:07:00,940
You can either type this out or you can simply just copy everything from here to the end of that curly
89
00:07:00,940 --> 00:07:05,410
brace and paste in that callback as our last parameter.
90
00:07:05,890 --> 00:07:08,890
This is what it should look like.
91
00:07:08,890 --> 00:07:18,580
And now if we hit save and we open up our terminal, let's CD over to this folder and remember that
92
00:07:18,580 --> 00:07:23,860
your file path will look different depending on whether if you're on Windows or Mac and also depending
93
00:07:23,860 --> 00:07:27,280
on how you organize your project structure.
94
00:07:27,280 --> 00:07:32,890
So don't worry if this doesn't match, just make sure that you grab hold of the right folder in the
95
00:07:32,890 --> 00:07:41,080
left hand pane here and now once you're inside that folder, we're going to use our node to run our
96
00:07:41,080 --> 00:07:46,720
index.js and then let's hit enter and it tells us the file has been saved.
97
00:07:46,720 --> 00:07:47,320
Success.
98
00:07:47,320 --> 00:07:51,610
So there were no errors and we managed to get to our console log.
99
00:07:51,640 --> 00:07:58,060
Now you can see that the message dot text file that we wanted to be created got created inside the same
100
00:07:58,060 --> 00:08:02,160
folder and if we open it up, it says hello from NodeJS.
101
00:08:02,170 --> 00:08:03,280
How cool is that?
102
00:08:03,670 --> 00:08:07,630
So we've managed to use a native node module.
103
00:08:07,630 --> 00:08:13,000
We didn't have to think about how to create these files, save it to the local system or any of that
104
00:08:13,000 --> 00:08:19,510
business, because Node has created the code to enable us to do this functionality.
105
00:08:19,540 --> 00:08:23,500
Now the next thing of course is a challenge for you.
106
00:08:23,650 --> 00:08:28,890
Instead of just writing to file, you can also read from a file.
107
00:08:28,900 --> 00:08:39,700
If you take a look at the FS dot read file documentation, I want you to use the example code along
108
00:08:39,700 --> 00:08:47,800
with what we've written so far, and I want you to change the text inside here so maybe you can say
109
00:08:47,800 --> 00:08:55,150
hello from Angela or whatever your name is and let's hit save on that message.
110
00:08:55,510 --> 00:09:03,700
And coming back here, I want you to write the code that would read that file message dot text, take
111
00:09:03,700 --> 00:09:09,400
the text inside it, and then console log it into the terminal.
112
00:09:09,460 --> 00:09:11,650
That is your challenge.
113
00:09:11,680 --> 00:09:17,860
Pause the video, take a look at the documentation and see if you can figure out how to do this.
114
00:09:17,890 --> 00:09:19,060
Have a go now.
115
00:09:20,990 --> 00:09:21,380
All right.
116
00:09:21,380 --> 00:09:27,290
So the method that we need is called read file, and it takes a couple of options.
117
00:09:27,290 --> 00:09:34,820
One is the path to the file that we want to read, and the next is the callback for what we want to
118
00:09:34,820 --> 00:09:37,600
do once our file has been read.
119
00:09:37,610 --> 00:09:44,810
Now, if you take a look at the example here in the callback, we can get either an error or the data
120
00:09:44,810 --> 00:09:50,460
that was read from the file and we can use that data in a console log.
121
00:09:50,480 --> 00:09:51,560
Pretty simple stuff.
122
00:09:51,560 --> 00:09:59,750
Let's copy this example code and put it into our index.js in order to access that method, of course
123
00:09:59,750 --> 00:10:08,450
we need to write f s dot read file and then we need to provide the path to this particular file now
124
00:10:08,450 --> 00:10:11,480
because it's inside the same parent folder.
125
00:10:11,480 --> 00:10:18,710
All we need to do is to write dot slash and then message dot text, and then the rest of the code is
126
00:10:18,710 --> 00:10:20,910
pretty much already written for us.
127
00:10:21,000 --> 00:10:25,260
Let's hit, save and make sure that you've modified the message dot text.
128
00:10:25,260 --> 00:10:30,930
And now all we have to do is to comment out the right file.
129
00:10:30,930 --> 00:10:37,920
We don't want this message replacing what we've got in here, so we don't want this to be run again.
130
00:10:37,920 --> 00:10:45,090
And then we want to go into our terminal and use node to run our Index.js.
131
00:10:45,090 --> 00:10:50,580
Now you can see we've got something outputted here, but it's in the format of a buffer.
132
00:10:50,610 --> 00:10:54,810
Let's take a look at the documentation and see what's going on.
133
00:10:55,170 --> 00:10:59,850
If we continue reading, it says if no encoding is specified.
134
00:10:59,850 --> 00:11:05,910
So remember encoding is one of the options, then the raw buffer is returned.
135
00:11:06,210 --> 00:11:13,740
But we can specify the type of encoding we want the output to be, and in our case we will want UTF
136
00:11:13,740 --> 00:11:16,350
eight just to be able to output the text.
137
00:11:16,440 --> 00:11:23,330
So let's modify our method and add in this extra option right in here.
138
00:11:23,690 --> 00:11:31,730
So now we've got the location of our file, the output encoding of the contents of that file and finally
139
00:11:31,730 --> 00:11:33,950
the console log and error handling.
140
00:11:33,950 --> 00:11:38,480
Let's hit save and let's run our index.js again.
141
00:11:38,480 --> 00:11:46,460
And you can see almost just like magic, it has read our file, including the text updates that we put
142
00:11:46,460 --> 00:11:50,690
in there and it's console logged it right here into the terminal.
143
00:11:50,690 --> 00:11:52,760
So hopefully you managed to get that right.
144
00:11:52,760 --> 00:11:59,420
If not, take a look inside the solution dot JS to see if there are any differences between your code,
145
00:11:59,420 --> 00:12:03,530
see if you've made any spelling errors or typos, that sort of stuff.
146
00:12:03,890 --> 00:12:09,230
And once you're ready, head over to the next lesson where we're going to learn about the Node package
147
00:12:09,230 --> 00:12:09,920
manager.
148
00:12:09,950 --> 00:12:12,290
So for all of that and more, I'll see you there.
15761
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.