In the last lesson, we created a very basic Node application and all our application did was to console log 1
a bit of text. 2
In this lesson I'm going to show you how to use native node modules that will allow you to leverage 3
existing code that's been built into Node that makes it way more powerful than just what you can do 4
with JavaScript. 5
What exactly are these native node modules? 6
Well, they're kind of like your starting tool set. 7
Now we mentioned before that Node had a whole bunch of features built into it just so that it was able 8
to make it easier for you to create applications, especially on the server side and the code that was 9
written in order to make this easier include things such as a File Access, so reading and writing into 10
the local file system. 11
So that could be local on your computer or in the case of your web application, it will be onto the 12
server computer as well as things such as Networking in order to reach across the internet and carry 13
out certain bits of functionality 14
that's important for the backend of a web app and a lot more. 15
Essentially, you can think of these native modules as kind of like the games that come pre-bundled 16
with your operating system. 17
One of my favorite is Minesweeper on Windows. 18
It's sort of endlessly entertaining and don't really know why. 19
But also remember there was pinball and hearts and solitaire and you can of course install more games 20
onto your computer, but the native ones are the ones that came pre-bundled because they thought maybe 21
they'd be useful to entertain you or to while away the hours like I did in my childhood. 22
Now in the course resources for this lesson, so you can find that in the curriculum and there'll be a 23
dropdown for course resources, 24
you should find a link to this documentation page and this page documents all of the native modules. 25
Now there's a lot of them and we're not going to go through them each one by one because that would 26
take years, 27
but also that's what the documentation is for. 28
It's more like a dictionary and not a curriculum. 29
And some of these modules we're going to be using in the future, such as the HTTP, the HyperText Transfer 30
Protocol module and maybe the Path module, 31
but right now the one I want to show you is one of the most important and basic features of Node, and 32
this is the File system. 33
Remember with native JavaScript that is run in a browser, you can't access a user's files on their 34
computer. 35
Can you imagine if you go on to a website and somebody is able to read and write your local file system? 36
That is a recipe for disaster. 37
But in the case of Node.js, because it liberates JavaScript out of the browser, we're able to use it 38
to access, to read and write to files on the server, or in this case to our local computer. 39
Now you don't have to worry about it being malicious because you're going to be the one writing all 40
the code. 41
So what is the File system? 42
Well, the file system is the native node module that allows us to access the local storage. 43
And in order to start using it, we need to either "import" the code from the file system module or we 44
can "require" the bits of code that we need from this module. 45
In the next lesson, I'm going to talk to you a little bit more about ESM, but for now we're going 46
to use the Commonjs pattern in order to get hold of the methods that we need from this File system module. 47
I want you to download the starting files for this module, which is in a zipped folder called 2.2 Native 48
Modules, and you're going to need to extract that folder and open it inside VS Code. 49
If you take a look inside the Index.js, you'll see it's yet another blank page. 50
But here is where we're going to get hold of that File system module. 51
So I'm going to create a const called "fs" and this is going to be set to equal "require" and the module 52
that we need has to be entered in as a string, 53
and you can see as soon as I put in those double quotes, it shows me all of the native node modules 54
that are available. 55
It knows exactly what I'm trying to do and the one we want is called fs, which is short for file system. 56
So now that we've gotten hold of this module, we're going to use one of the methods in that module. 57
And if we scroll up you can see that there are many, many methods available, but the one that we want 58
to use is called "fs.writeFile", because 59
this is going to allow us to take a message that say user inputs or from some bit of our website and 60
we're able to write it into a file to be saved onto the computer. 61
And in this case, it will be our local computer. 62
The method that we need in order to do this is called writeFile, and this is from the Node file system 63
module. 64
And then in order to use this method, you can see the structure is like this. 65
The first is the file that we want to create. 66
Next is the data that we want to put in that file. 67
And finally, we have the callback to handle any errors or any issues. 68
This is the example code for how this would work. 69
And again, notice how they've got the import format instead of the require format, which we're going 70
to go through very, very soon in the next lesson. 71
For now, don't worry too much about that. 72
We're going to use this writeFile method and we're going to pull it out of the fs module. 73
Heading back over here, we can type fs.writeFile() and there we can access this method that we 74
need. 75
Now the file I'm going to create is called "message.txt." 76
So it's going to be a text file. 77
And because I'm not putting in a full path here, it's going to create it in the same parent folder 78
as my index.js. 79
Now the next part I need to put in is the data. 80
So I'm going to just enter a string. 81
So let's use that previous sentence, 82
"Hello from Node.js." 83
And then finally we have our callback. 84
Our callback is pretty simple. 85
It can return an error and if there is an error, we can throw the error, 86
and if not, we can simply console log that the file has been saved. 87
You can either type this out or you can simply just copy everything from here to the end of that curly 88
brace and paste in that callback as our last parameter. 89
This is what it should look like. 90
And now if we hit Save and we open up our Terminal, let's cd over to this folder and remember that 91
your file path will look different depending on whether if you're on Windows or Mac and also depending 92
on how you organize your project structure. 93
So don't worry if this doesn't match, just make sure that you grab hold of the right folder in the 94
left-hand pane here. And now once you're inside that folder, we're going to use our node to run our 95
index.js and then let's hit Enter and it tells us the file has been saved. 96
Success. 97
So there were no errors and we managed to get to our console log. 98
Now you can see that the message.txt file that we wanted to be created got created inside the same 99
folder and if we open it up, it says, Hello from Node.js." 100
How cool is that? 101
So we've managed to use a native node module. 102
We didn't have to think about how to create these files, save it to the local system or any of that 103
business, because Node has created the code to enable us to do this functionality. 104
Now the next thing of course is a challenge for you. 105
Instead of just writing to file, you can also read from a file. 106
If you take a look at the "fs.readFile" documentation, I want you to use the example code along 107
with what we've written so far, and I want you to change the text inside here so maybe you can say, 108
"Hello from Angela!" Or whatever your name is and let's hit Save on that message. 109
And coming back here, I want you to write the code that would read that file, message.txt, take 110
the text inside it, and then console log it into the terminal. 111
That is your challenge. 112
Pause the video, take a look at the documentation and see if you can figure out how to do this. 113
Have a go now. 114
All right. 115
So the method that we need is called read file, and it takes a couple of options. 116
One is the path to the file that we want to read, and the next is the callback for what we want to 117
do once our file has been read. 118
Now, if you take a look at the example here in the callback, we can get either an error or the data 119
that was read from the file and we can use that data in a console log. 120
Pretty simple stuff. 121
Let's copy this example code and put it into our index.js. In order to access that method, of course 122
we need to write fs.readFile, and then we need to provide the path to this particular file. Now 123
because it's inside the same parent folder, 124
all we need to do is to write "./" and then "message.txt", and then the rest of the code is 125
pretty much already written for us. 126
Let's hit, Save and make sure that you've modified the message.txt. 127
And now all we have to do is to comment out the writeFile, 128
we don't want this message replacing what we've got in here, so we don't want this to be run again. 129
And then we want to go into our terminal and use Node to run our index.js. 130
Now you can see we've got something outputted here, but it's in the format of a buffer. 131
Let's take a look at the documentation and see what's going on. 132
If we continue reading, it says, "If no encoding is specified..." 133
So remember encoding is one of the options, "then the raw buffer is returned." 134
But we can specify the type of encoding we want the output to be, and in our case, we will want "utf8" 135
just to be able to output the text. 136
So let's modify our method and add in this extra option right in here. 137
So now we've got the location of our file, the output encoding of the contents of that file and finally, 138
the console log and error handling. 139
Let's hit Save and let's run our index.js again. 140
And you can see almost just like magic, it has read our file, including the text updates that we put 141
in there and it's console logged it right here into the terminal. 142
So hopefully you managed to get that right. 143
If not, take a look inside the solution.js to see if there are any differences between your code, 144
see if you've made any spelling errors or typos, that sort of stuff. 145
And once you're ready, head over to the next lesson where we're going to learn about the Node Package 146
Manager. 147
So for all of that and more, I'll see you there.