In the last lesson, we created a very basic Node application and all our application did was to console.log()
a bit of text.
In this lesson, I'm going to show you how to use Native Node Modules that will allow you to leverage
existing code that's been built into Node that makes it way more powerful than just what you can do
with JavaScript.
What exactly are these native node modules?
Well, they're kind of like your starting tool set.
Now, we mentioned before that Node had a whole bunch of features built into it just so that it was able
to make it easier for you to create applications, especially on the server side, and the code that was
written in order to make this easier include things such as a File Access, so reading and writing into
the local file system.
So that could be local on your computer or in the case of your web application, it will be onto the
server computer, as well as things such as Networking in order to reach across the internet and carry
out certain bits of functionality
that's important for the backend of a web app, and a lot more.
Essentially, you can think of these native modules as kind of like the games that come pre-bundled
with your operating system.
One of my favorite is Minesweeper on Windows.
It's sort of endlessly entertaining and don't really know why.
But also remember there was pinball and hearts and solitaire and you can of course install more games
onto your computer, but the native ones are the ones that came pre-bundled because they thought maybe
they'd be useful to entertain you or to wallow away the hours like I did in my childhood.
Now in the course resources for this lesson, so you can find that in the curriculum and there'll be a
dropdown for course resources,
you should find a link to this documentation page and this page documents all of the native modules.
Now there's a lot of them, and we're not going to go through them each one by one because that would
take years,
but also that's what the documentation is for.
It's more like a dictionary and not a curriculum.
And some of these modules we're going to be using in the future, such as the HTTP, the HyperText Transfer
Protocol module and maybe the Path module,
but right now the one I want to show you is one of the most important and basic features of Node, and
this is the File System.
Remember with native JavaScript that is run in a browser, you can't access a user's files on their
computer.
Can you imagine if you go on to a website and somebody is able to read and write your local file system?
That is a recipe for disaster.
But in the case of Node.js, because it liberates JavaScript out of the browser, we're able to use it
to access, to read and write to files on the server, or in this case to our local computer.
Now, you don't have to worry about it being malicious because you're going to be the one writing all
the code.
So what is the File System?
Well, the file system is the native node module that allows us to access the local storage.
And in order to start using it, we need to either "import" the code from the file system module or we
can "require" the bits of code that we need from this module.
In the next lesson, I'm going to talk to you a little bit more about ESM, but for now we're going
to use the Commonjs pattern in order to get hold of the methods that we need from this File System module.
I want you to download the starting files for this module, which is in a zipped folder called 2.2 Native
Modules, and you're going to need to extract that folder and open it inside VS Code.
If you take a look inside the Index.js, you'll see it's yet another blank page.
But here is where we're going to get hold of that File System module.
So I'm going to create a const called "fs" and this is going to be set to equal "require" and the module
that we need has to be entered in as a string,
and you can see as soon as I put in those double quotes, it shows me all of the native node modules
that are available.
It knows exactly what I'm trying to do and the one we want is called fs, which is short for file system.
So now that we've gotten hold of this module, we're going to use one of the methods in that module.
And if we scroll up you can see that there are many, many methods available, but the one that we want
to use is called "fs.writeFile", because
this is going to allow us to take a message that say user inputs or from some bit of our website and
we're able to write it into a file to be saved onto the computer.
And in this case, it will be our local computer.
The method that we need in order to do this is called writeFile, and this is from the Node file system
module.
And then in order to use this method, you can see the structure is like this.
The first is the file that we want to create.
Next is the data that we want to put in that file.
And finally, we have the callback to handle any errors or any issues.
This is the example code for how this would work.
And again, notice how they've got the import format instead of the require format, which we're going
to go through very, very soon in the next lesson.
For now, don't worry too much about that.
We're going to use this writeFile method and we're going to pull it out of the fs module.
Heading back over here, we can type fs.writeFile() and there we can access this method that we
need.
Now the file I'm going to create is called "message.txt."
So it's going to be a text file.
And because I'm not putting in a full path here, it's going to create it in the same parent folder
as my index.js.
Now the next part I need to put in is the data.
So I'm going to just enter a string.
So let's use that previous sentence,
"Hello from NodeJS!."
And then finally we have our callback.
Our callback is pretty simple.
It can return an error and if there is an error, we can throw the error,
and if not, we can simply console.log() that the file has been saved.
You can either type this out or you can simply just copy everything from here to the end of that curly
brace and paste in that callback as our last parameter.
This is what it should look like.
And now if we hit Save and we open up our Terminal, let's cd over to this folder, and remember that
your file path will look different depending on whether if you're on Windows or Mac and also depending
on how you organize your project structure.
So don't worry if this doesn't match, just make sure that you grab hold of the right folder in the
left-hand pane here. And now once you're inside that folder, we're going to use our node to run our
index.js and then let's hit Enter and it tells us the file has been saved.
Success.
So there were no errors and we managed to get to our console.log().
Now you can see that the message.txt file that we wanted to be created got created inside the same
folder and if we open it up, it says, "Hello from Node.JS!."
How cool is that?
So we've managed to use a native node module.
We didn't have to think about how to create these files, save it to the local system or any of that
business, because Node has created the code to enable us to do this functionality.
Now, the next thing of course, is a challenge for you.
Instead of just writing to file, you can also read from a file.
If you take a look at the "fs.readFile" documentation, I want you to use the example code along
with what we've written so far, and I want you to change the text inside here so maybe you can say,
"Hello from Angela!" Or whatever your name is and let's hit Save on that message.
And coming back here, I want you to write the code that would read that file, message.txt, take
the text inside it, and then console.log() it into the terminal.
That is your challenge.
Pause the video, take a look at the documentation and see if you can figure out how to do this.
Have a go now.
All right.
So the method that we need is called readFile, and it takes a couple of options.
One is the path to the file that we want to read, and the next is the callback for what we want to
do once our file has been read.
Now, if you take a look at the example here in the callback, we can get either an error or the data
that was read from the file and we can use that data in a console.log().
Pretty simple stuff.
Let's copy this example code and put it into our index.js. In order to access that method, of course
we need to write fs.readFile, and then we need to provide the path to this particular file. Now
because it's inside the same parent folder,
all we need to do is to write "./" and then "message.txt", and then the rest of the code is
pretty much already written for us.
Let's hit Save and make sure that you've modified the message.txt.
And now all we have to do is to comment out the writeFile,
we don't want this message replacing what we've got in here, so we don't want this to be run again.
And then we want to go into our terminal and use Node to run our index.js.
Now you can see we've got something outputted here, but it's in the format of a buffer.
Let's take a look at the documentation and see what's going on.
If we continue reading, it says, "If no encoding is specified..."
So remember encoding is one of the options, "then the raw buffer is returned."
But we can specify the type of encoding we want the output to be, and in our case, we will want "utf8"
just to be able to output the text.
So let's modify our method and add in this extra option right in here.
So now we've got the location of our file, the output encoding of the contents of that file and finally,
the console.log() and error handling.
Let's hit Save and let's run our index.js again.
And you can see almost just like magic, it has read our file, including the text updates that we put
in there and it's console logged it right here into the terminal.
So hopefully you managed to get that right.
If not, take a look inside the solution.js to see if there are any differences between your code,
see if you've made any spelling errors or typos, that sort of stuff.
And once you're ready, head over to the next lesson where we're going to learn about the Node Package
Manager.
So for all of that and more, I'll see you there.