- [Instructor] Reading files is another feature
of our FS module.
It's going to allow us to read the contents of a file,
encoded into some sort of a text format.
What we'll do first is we want to continue
to import our File System module.
Then in order to check this out,
we want to take a look over here in our start folder.
And notice that we have this file called readme.md.
It's filled with some Lorem ipsum text.
We just need to read something,
so might as well read some nonsense there.
What we'll do here is we want to go ahead
and replace the code that we had
with a variable called ipsum.
Set that equal to readFileSync.
We're going to read this synchronously.
And I'm going to include the name of the file, readme.md,
as well as the text encoding format for the file,
which is in most cases for text files going to be UTF-8.
Then we'll go ahead and log this to our console.
We should be able to see the contents
from our text file being read out here.
There's also an asynchronous counterpart to this function
called read file.
So just like before,
we're going to pass in the name of the file.
We're going to pass in UTF-8,
but this time, as you might expect,
we'll pass in a callback function.
The two arguments that are sent here are err and ipsum.
This means that we can get rid of the variable
that we've created.
And this is going to log to our console,
all of that text.
And then finally here, let's say, "reading the file".
This is going to let our users know that the process has begun.
Now what we'll do here is we'll clear this out.
We'll run node list.
This will happen the same way.
So we're reading the file.
This time, we're reading it asynchronously
when the system has finished retrieving the file's contents.
The callback is invoked,
allowing our main thread to do something else
in the meantime.
So up here at the top,
we should be able to see "reading the file" happens first.
Once we have the text read,
we can go ahead and see it there.