- [Lecturer] You can also use the FS module to append files,
let's go into our start folder
and make a brand new file called append.js.
Now, one of the things that I want to point out is,
if you look in your assets folder
you'll notice a colors.json document.
If I look at this document, it's just a JSON object
that has some values, including a description
and a color list.
So we don't need to use the File System
to read JSON documents, I can go directly into my append.js
and import this data simply by requiring it.
I'll call it colorData,
and then I'm going to go ahead and require ./assets
/colors.json
and then, within this colorData file
I can actually use this information,
so I can say colorData
.colorList
and the colorData.colorList should give us this array,
this list of colors,
so what I'm going to do is loop through the array using forEach
and then we send a callback function to the forEach function
and the c is going to refer to each of these colors
so this forEach is actually going to loop through each color
and it's going to set c to a different color object
every time we iterate through the loop.
So from there I'm just going to go ahead
and append these colors to our notes file.
So let's go ahead and go up to the first line
and include the File System by requiring it.
So now within this forEach callback
what we're going to go ahead and do
is call fs.appendFile, the file that we want to append
is going to be in the storage-files directory
and we are also going to call that file colors.md
So that's the name of the file that we want to append,
notice that that file is not there at present,
so the fs.appendFile will create the file
if it doesn't exists and append to already existing file.
So what we're going to append is the color,
I'm going to actually use backticks
and I'm going to select c.name
and if I look at the JSON data
c.name should give me each color name, red, green or blue.
And I'm going to also use the c.hex
on the other side of a semicolon
and then I'm just going to follow that with a \n
so that we get a new line.
Great, so I'm going to go ahead and collapse the files
out of the code so I can add one more argument
to the append file function, and this is the callback
that will be called once the append is complete,
if there're any errors it will just pass those errors
to this callback.
And I will check to see if we have an error
and if we do I will throw it.
All right, so now we loaded some data
from a JSON file using the require statement
and then we looped through each of the colors
in that data and we should append a file
found in the storage-files directory called colors.md.
Let me open up my files again and open up my terminal.
And let's go ahead and run this, node append
and when I do so looks like nothing happens
but if we actually go into the storage-files directory
we will notice that there is now a colors.md document
and when I open up it says undefined, undefined, undefined
so I've obviously made a mistake somewhere,
let me go back to my append.js and the problem is,
is if I look at the colors.json
it's actually c.color
not c.name
so I accidentally used the wrong variable,
so we're not looking for c.name, we're looking for c.color
We go ahead and save that and we can call node append again.
And we can take a look at the colors.md,
notice that this time the correct color
was added to the document.
So that's what append does,
I can actually drop this file
by deleting the colors.md document
and I can run append several times,
every time I run append within the terminal
we have been reading the colors
and appending them to the file,
so if I look at colors.md you can see
that we have lots of lines
of the same red, green and blue colors.
So the File System gives us a way
to append files to the appendFile function.