[MUSIC]
>> Hi, folks. My name is Aaron Powell,
and I'm a Principal Cloud Advocate here at Microsoft.
In this video, we're going to do
some improvements to application that we had last
time and the way that it works with
the file system and file system paths.
Picking up from where we completed the last video,
we're going to fix some potential issues that we have.
Right now we're expecting the application
to be run at node./index.js,
so running for the same directory that the index.js file is.
But if we're not running node from the same folder,
we'd have an unexpected result because either we
did not find the stores folder where we think it would be
or we might find the wrong stores folder.
You can test this for yourself.
Let's jump over to our terminal,
and I'm going to navigate into the stores folder.
Then we'll run the index.js window but from dot-dot-slash,
so we'll tell it to look up and find that file.
Now if we try and run our application,
we'll see that there's an error message that is going to pop up,
and that's because it can't find stores
from the current working directory.
It's expecting it to be relative to where index.js is,
but we're not where index.js is,
so it's relative to the current working directory.
Thankfully, node gives us an easy way to work
through this with a variable called __dirname.
This is the directory of the file that is being executed,
not the current working directory.
We can append that onto the start of our findSalesFiles,
and then also append the slash so that we can execute this.
We clear our terminal and run it again.
There we go. Now we've got full files
[inaudible] completely from the root of our file system
and not worrying about relative paths.
But we do have another potential issue that we've introduced.
We're using slash in a certain direction
which is valid on some operating system,
but other operating systems might use a different direction slash
or different part separated altogether.
So we're going to need to make sure
that we normalize this to work across
all operating systems because node
supports multiple operating systems.
To do that, we're going to use the path module.
First off, let's import path into our application.
We'll do that by const path equals require path.
It will import that module from the Node.js runtime
and then this gives us a new method that
we will want to use here called join;
join will take the multiple paths that we've provided to it,
all the path segments more accurately,
and it will apply the correct operating system separator.
So I can remove the slash direction that I had in
there and that will get inserted by path.join.
No more hard-coded file path separators.
There's still a few more places
though within application that we're doing this,
that we might want to go and update.
So if we come down into findSalesFiles,
you'll see that there's a few locations around here
that we're just going to update with path.join
and that will then correct all the normalization that we had.
Now, if we run our application,
it'll still output exactly the way that we were expecting it to.
Lastly, we're expecting that
the file name we are looking for is called sales.json.
But what happens if one store accidentally calls it
maybe tail turtles instead?
We're not going to pick up that file
because we'll have explicitly looking for a filename.
Instead, we just really care that it is a JSON file,
and that's what's being
uploaded because that's what we're going to unpack.
Again, we can use path for this.
We're going to use the path.extname
to then get the extension of that path,
and then test it against .json,
which is the path extension there.
If it matches, we ignore the name side of it,
if we just match on the path,
then we get the app they were expecting.
Now it's coming in.
I should rename one of those files
just to make sure it is still picked up.
So we'll change this to salesTotal. Yep, that'll do.
Then we're going to execute again and now we'll
see that the third one there has been updated.
This is now the application,
a little bit more robust when it comes to
working across multiple operating systems.
Join me next time where we're going to have a look
at how we can take our application and
start creating files and folders on disk so that
we can ultimately begin writing out our report.
Bye for now.