Hello everybody.
Welcome back.
And in this video, I want to show you something that got me really really really excited when I first started
learning node, and hopefully you get just as excited as I am as we go through this video and then do
a little bit of a challenge.
It's going to blow your mind.
At least it did for me when I first started learning to code.
Now in the previous video we talked about something called the FS module that comes with node, and this
is something that is built into node and the way we get it is we'll just do const.
Well actually we need a file first, so let's do a touch script.js, we'll create a new script perfect.
Alright we can close that because we don't need this.
And in the script.js that we have in this folder or some node we can have 'const fs'.
equals 'require('fs')', so FS here stands for file system and if you have node downloaded you have access
to this.
By just doing this.
Now why is this so exciting and so useful.
Well because FS file system allows you to access our file system, which again doesn't sound that exciting.
But let me demonstrate what happens and why this is useful - so bear with me here.
I'm going to do a bit of coding and then I'm going to explain exactly what is happening.
So we're going to use the 'fs.readFile'.
It's pretty self-explanatory.
We have a blank parameter and then we have err for error, data.
And this is an arrow function.
So the second parameter is a function, and this function has either error or data.
So for the error, I'm going to say if there is an error we'll just throw err, or we can just say console dot
log errrroooorrr. Really shove it into their face.
Alright! Now if there wasn't an error, we'll just say well we'll just say console.log and we'll just log out
whatever data is.
Alright.
Let me add the semi-colons.
And save that.
So what I just do here. I just imported the FS module that comes with node and the FS module has a method
readFile() that
well it looks like it reads some sort of a file.
The first parameter is actual file that we want to do, so the path to the file, which we don't have yet.
So let's create one.
I'm going to say new file and we'll call it 'hello.txt', and with 'hello.txt', we'll do our classic
helllloooooo there!!!
And exciting stuff so exclamation marks.
We saved that and now we can say that we want to read the file.
'hello.txt' from the current directory.
Hello dot
text.
Now this readFile method is going to find the file with the path,
ok, so here it is.
It's going to read it and then if there's an error, it's going to spit out an error.
If there's some sort of data and everything successful, it's going to spit out data. With the
way we can run this file in node.
Well well first I'll have to save it. We'll say node script.js
Enter and I got buffer.
What is that?
And that is because we have to do a little bit of encoding.
And what that means is if I do this 'toString()', let's just try this and show you what I mean.
And I get.
Hello there.
So the 'readFile' reads the file as the name suggests, and it's going to spit out whatever it's reading.
Data.
Now why do we have to put in the 'toString()' here.
Just so you know 'toString()' does something, by default if you don't add anything, it's going to use the
encoding called UTF 8 and UTF 8 is a type of a coding.
Let's just run this to make sure that this is still the same.
Yep.
So UTF 8 is a type of encoding that we have.
That's pretty standard with HTML 5.
And pretty much everybody uses it on the web.
It's a way for us to have characters, whether they're Latin characters, you know ABC's or we have Korean
Japanese, Chinese characters, they're all represented by a number, and UTF 8 is a way of encoding these
numbers to make sure that we can read them.
It's actually quite a complex subject, so I recommend the Wikipedia page that talks about UTF 8.
For now just know that UTF 8 is a type of encoding that is pretty much standard across the web.
You might have also seen ASCII and ASCII was before UTF 8 existed.
ASCII was mainly for Latin based languages, so the ABCs; and UTF expanded that to include many more from
many more languages from different parts of the world.
Alright well a little bit of a tangent that we didn't need to go on, but as you can see over here we
have
'helllloooooo there' and we're able to read the file.
Alright.
Very cool.
Let me just add one other method to your repertoire.
Let's also do 'fs.readFileSync()' and 'fs.readFileSync()' works a little bit different than the previous
one and sync stands for synchronous.
So we'll see what happens.
I'm going to put in
'hello.txt' once again, and this time I'm just going to leave it like that.
And let's just say that this is going to equal the file and we'll just 'console.log(file)'.
Alright.
Let me save that and run this.
Alright.
We know once again that we got this because we need to do 'toString()'.
So let's do that.
I'm going to try 'toString()'
I'm going to save and run this again. Alright I get
'helllloooooo there!!!'
But just to distinguish them, let's just say that this is number one and this is number two.
I save this, run this again.
And whoa, what just happened.
We have one coming after the two.
Now if you've watched the asynchronous synchronous video in this course, then you might understand what's
happening here.
If not I'm going to do a quick synopsis.
But I do recommend you check out that video. Now read file is asynchronous, and that's why it has something
called the callback function.
It is saying: hey I'm going to read this file, and when I'm done with it, you just keep going on with your
business and keep reading.
Line three, line four, line five, line six, all the way to 11.
When I'm done, I'll let you know and I'll either give you an error or some data. 'readFileSync()' which
is synchronous says: I'm going to read this file.
Don't do anything, just wait here when I'm done
I'm going to assign it to 'file' and then you can keep going.
So as you can see here what happens is we read the file and because this is an asynchronous, it's going
to run this and come to line 10 and run the 'fs.readFileSync()' and the 'readFileSync()' because it's saying
hey wait until I'm done this, it is going to print out
'helllloooooo there!!!'
and then the asynchronous code is going to say: hey I finished reading the file,
here's the data and it prints out down over here.
Again I really really recommend you check out the asynchronous synchronous video.
But for now hopefully that makes sense.
Now looking at this, which one should you use? Well for a simple case like this,
we can use whichever we want. But you may see a problem with the second one especially if we're building
something like a server.
If we have a massive file that has something more than 'helllloooooo there!!!',
well if we're doing 'readFileSync()', what happens is we're going to halt or pause the execution of our
file and it's going to read the entire text.
And the program is just going to be waiting for that to finish; versus the read file which says: hey you
can keep going ahead.
I'll let you know when I'm done with this.
So when you're building a server and let's say an express server with routes.
Well in that case we want to use readFile() if we're reading any sort of a file or text file, whatever
it is.
Because that way we're not blocking the execution and the program can keep doing things.
Alright.
So one last time just to clarify things.
I'm going to label this as 'Async' and 'Sync'.
So once again if we run this we have 'Sync' and 'Async'.
'helllloooooo there!!!'
But very cool we're able to read a file and both instances which was kind of cool.
Let me show you a few more of these and then tell you exactly why this is so exciting, and how we can
use it to do some really exciting things.
So let's say the 'helllloooooo there!!!' text is kind of boring and we want to add to it.
Well we can do something like this.
'fs.appendFile()' we give it the file we want to append.
So in this case it's './hello.txt'.
And by the way if this doesn't exist, it'll actually just create the file for us.
And the second parameter will say what we want to add.
So let's say we also wanted to add maybe a space and then say "This is so cool", now there is an exclamation mark.
And finally third parameter takes in an error and this error will just say that if there is an error.
Let me make this smaller
so you can see - let's just say if there is an error, we'll just 'console.log'
'console.log(err)'
Alright.
Perfect.
So let's run this and see what happens.
I'm going to run the script and I get
'helllloooooo there!!!'
Because these ran first but if I go to 'hello.txt', look at that, we just wrote to a file. And now we have "This
is so cool."
So if I run this again and let me clear this just so we have more space
Look at that. I get
'helllloooooo there!!!'
'This is so cool'
'helllloooooo there!!!'
'This is so cool'
If we go back to the hello.txt file, Oh boy we've add in even more
'This is so cool'
text.
So that's pretty cool.
We just added a few more lines to our file.
Let me remove that just so it doesn't get too annoying.
Going to save it and come back.
All right so we learned the append.
Let me just comment this out so that it doesn't interfere with any of the other ones.
Let's do a 'write' this time - now create some space here so we can see
With the write, as you can imagine,
'fs.writeFile()' and we'll just say that this file will be called 'bye.txt'.
Very original naming by myself.
And will just say.
Sad to see you go.
And once again it'll take an error and this error will say 'if (err)' 'console.log(err)'
Alright, so let's save that, run the file or run the script.
And look at that, 'bye.txt' just got added.
If we click on it, we have "Sad to see you go".
We've just created a new text file.
Very very cool.
By the way, just so you know what happens, if let's say I misspell the read file and now it's 'hell.txt'
Well if I run this, I'll get an error and you can see that.
Because it errors out I get the error message, error with a lot of R's.
Alright.
A little bit of a segue there but just in case people are asking or you were thinking about that.
Alright.
Woo~ I'm getting tired so let's do one last one and then I'm going to tell you exactly why this is so
cool.
The final one we're going to do - again let's make some space here - is going to be the 'delete'.
So with that 'delete' we can do 'fs.unlink()'.
the name is a little bit off here but 'fs.unlink()' and we'll say the 'bye.txt' and notice that I was able to
just do 'bye.txt' but just to keep it consistent, I'll just say './bye.txt' with the './' before
it
and with this one.
Again if there's any errors
just 'console.log(err)'
Whoops I forgot to do the 'if'
perfect, now
Well let's see what happens here.
And just for fun, let's just add a 'console.log()' here after it has done deleting and we'll say 'inception' because
we're saying 'bye' to the './bye.text'. Mind blown.
Alright let's run this.
I'm going to say - and we'll comment out the write file here because you don't need to write it,
we already have the 'bye.text'.
Let me save.
Now if you look at the 'bye.text', let's see what happens here when I run the script
look at that, the 'bye.text' was just removed.
Inception just happened.
Awesome.
So that was very exciting for me hopefully it was for you as well.
But why is this exciting?
Well because now we're able to use programming in javascript outside of just web browsers and outside
of just building websites and servers.
What we can do now with this knowledge is we can make our lives easier.
You can think of cases maybe you want to read an excel file and there are things that you can use, using
read file and using NPM modules that you can read.
Let's say.
Columns in an excel and do some math.
Or maybe you have an excel sheet with all these emails and you want to send out an e-mail to let's say
100 people on this list but you also don't want to be caught spamming people by the google filter saying
that hey this account is spamming because they just sent 100 emails in two minutes. Well using something
like Node, you can create perhaps something that in every six hours, sends out five e-mails.
Another thing you can do if you had a robot for example something similar to a file system instead of
having reading from the file.
You can read from the robot, maybe through wireless maybe through wires and get an input of some data
that the robot has.
Maybe it detected a cat.
Well using that input, you can do something you can respond maybe send you an e-mail saying that the
robot just detected a cat and send that e-mail out or send an output.
And that's what we call input output, where you're getting an input from another source, another machine
and then you are also outputting something for consumption, that is really really exciting because it
opens up the world where you can create tools for yourself.
You can automate things.
You can literally do anything that your mind can think of.
And I know this is still early and you're still at the beginning of your developer career but this is
the first step into understanding how you can use programming to solve problems and make your life
more efficient, or maybe just a little bit more fun.
So in the next video we're going to do something fun.
We're going to use what we've just learned and we're going to help Santa out on a coding challenge.
I'll see you in that one.
Bye-Bye