- [Instructor] Node.js also ships with a module
that allows us to interact with the file system.
The fs module can be used to list files in directories,
create new files in directories, stream files,
watch files, modify file permissions,
just about anything you would need
to work with files and directories.
So first let's get started
by listing the contents of a directory.
You will notice within the start folder
of the exercise files under chapter five,
chapter five lesson one, there's a asset's directory.
And we have a couple of files within this directory.
So let's say we wanted to read the contents
of this directory.
We would start by creating a new Node module called list.js.
And notice that I've put the list Node module
on the same level as the asset's directory.
Within the list.js file I'm going to go ahead
and load the fs module by using the require function
and we're going to require the fs module.
Now I can read the contents of a directory
into a variable just by invoking fs.readdirSync
and then we should supply the path
to the directory that we want to read.
If we wanted to read the contents of this directory
we could just use ./ but since we want to read the contents
of the asset's directory we're going to use ./assets.
Great, I'll go ahead and console.log the files
and we'll take a look at this in the terminal.
So I'm going to go ahead and open up the terminal
and I will node list.
So these are just the three file names of the three files
that we have found within the asset's directory.
Notice the function that we used to read the files,
readdirSync, this function is executing synchronously.
That means that line three is blocking
which means that JavaScript is going to stop everything
it's doing until the files within that directory are read
before processing any more lines of code.
We can exemplify this with a couple of console logs.
So I'm going to go ahead and console.log started reading files
and I'll go ahead and console.log a complete.
So now, if I come down to the terminal
and type node list to run my application,
you can see within the console
the first log reads started reading files
and then the files are being read
and then we see the log that reads complete
before we dump the file array to the terminal.
So there's another way that we can read files
and that is to make line for non-blocking
by using an asynchronous function, readdir.
So readdir will read the files within the directory
but it doesn't set a variable.
Once the files within the directory are read
they will be passed to a callback function
that we supply as the second argument.
The first variable of this callback function
is any errors that have been thrown.
And then the second variable of this callback function
are the files themselves.
So I'll use a little arrow function here for this.
Now, when the files are read,
this callback function will be invoked.
Because this operates asynchronously,
lines eight and 10 will actually be executed
before the files are read.
So if we wanted to print these details to the user
when the files are read we'd actually have to
take these lines and place them
within the callback function.
The last thing that we need to do are handle any errors
that might have occurred asynchronously.
So we can do this simply by checking the error variable.
If it exists, we'll just go ahead and throw the error
and that will stop the processing of our Node.js module.
Great, so let's come down here to the terminal.
I'll move it down so we can see the code.
Clear the screen and now run our list.
So now the output is the same
except we are doing this asynchronously.
We have started reading the files
and then once the files are read we print a complete message
and dump that file array to the console
within the callback function.
To further exemplify this, I'm just going to go ahead
and take this console.log and place it down here on line 11.
When we run this you're going to notice the same outputs.
So if I node lists, we still see started reading the files
and then complete in the file array
after the files are read.
But notice that we make the call to read directory
before we print to the console started reading files.
That's because the callback function
is going to operate asynchronously
and it will be invoked once we have successfully completed
the reading of the directory.