Learn nodejs, Writing and appending files

Another feature of the file system module is the ability to create new files, to write text or binary content to those files, or to append text or binary content to an existing file. Let’s go ahead and navigate to our exercise files. In the Start folder, you will notice that there is a create.js, which is an empty JavaScript file.

Let’s go ahead and open that up. So with this empty JavaScript file, we are just going to create a new markdown file. So the first thing I need to do is include the file system module. And the next thing that I need to do is include some content that I want to add to this file.

So I’m going to create a variable for some markdown content, and we’re going to add some markdown content to this file. I’m using a string template, so we’re using these back ticks, and one of the things that’s nice about string templates is they honor whitespace, so I can just… ‘Sample Markdown Title’, we will underline that with equal signs for heading one.

‘Sample Subtitle’, we can go ahead and underline that with dashes, for our heading two. And if we wanted bullet points, we could use the star character. So we have ‘point, point, point’.

So I’ve just created some sample markdown text in a template string. What I want to do is actually write this markdown text to a file. So I’m going to use the fs.writefile function. And just like every other file system function, this can be invoked synchronously or asynchronously. So I’m using the asynchronous version. And the first thing that we’re gonna add is the name of the file that we want to write. This is going to be ‘sample.md’. The next argument represents the content that we want to write to that file. My markdown content that I created on line three, and I’m also going to trim it.

We have some leading and trailing space there, I’ll get that out of there with the trim function. So the third argument is the callback function. This function will fire once we have created the file, or if there was an error creating the file, this function will also fire, and pass that error as an argument. So once we create this file, I’m just going to go ahead and console.log, “file Created”. There we go, so we can save this, and go back out to our terminal. And we can run this by typing “node create.” And when I do so, it says ‘File Created’.

So we can check to see if our file is created by navigating to our finder, or our explorer, and we can see that we have a ‘sample.md’ file here. If I open up this file, we can see that it contains the markdown content from that string template that I used when creating this markdown. So sweet, that is the way we go about creating files. Let’s go ahead and go back to the terminal. One of the things that we’ve included here when we do a directory listing, is the promptPerson JavaScript app that we developed in chapter four, lesson two. I can go ahead and run that app, so we can see what it does.

So when we were learning to use readline, we created this questionnaire, it asks for the name of the real person, and then some things the person might say. And when we type ‘exit’, the application quits, and it echoes back the things that that person might have said. So what we are going to do, is we are going to iterate on this application, so that we actually save a file based off of the name of the real person, and that we append to that markdown document, the things that a real person might say. So let’s go back to our exercise files. Under the Start folder, you can go ahead and open up ‘promptPerson.js’.

This is the exact same file that we created in chapter four, lesson two. The difference is, is it has a couple of TODOs in here. So down here at line fifteen, there is a TODO for creating the new markdown document, once we have the name of a real person. And then every time our user adds a saying, down here on line twenty-seven, we need to append that saying to a markdown file. So the first thing I’m going to do is go to the top of this document, and include my file system object. There we go. And now that I have the file system, I can come down here to where we’re supposed to create the markdown file, and use it.

So instead of this TODO message, I’m going to do ‘fs.writeFile’, and this time I’m going to use ‘writeFileSync’. In this case, this application only has one user. It runs in the terminal, so we’re not going to have a blocking request that will cause delays for other users. In addition to that, I want to make sure that the file is written before I prompt the user for anymore questions. Because we need to have the file, before we start appending to the file. So I’m going to use the synchronous version of the writeFile function for this. And the first thing that it’s going to take in is the name of the file.

I’m going to use the real person’s name to help me name the file, but I’m also going to add a .markdown extension. So we’ll use the real person’s name with a markdown extension, to create a new markdown file. The second thing that we need to add is the contents to the file. So in this case, I’m going to do this right here, by adding a template string as my second argument. And the file contents are going to start off with real person’s name, followed by a new line, underlined by equal signs, and then we will add two more new lines to that file.

So because I am doing this asynchronously, no need to add a callback function. When JavaScript processes this right file sync function, and gets to line seventeen, we will either have created a file, or we will have an error in doing so, and that error will automatically be thrown. So at this point, we have created a new file, let’s come down here and append sayings to that file. So one of the things we can do is erase this comment, and use the fs.appendFile function. So using the fs.appendFile, we can do this one asynchronously, in the case of this application, it doesn’t really matter much.

But when we append the file, it wants to know what file we should append. We’re going to append the realPerson.name markdown file, so we’ll go ahead and append this markdown extension on there. That’s the file that we want to append, and what do we want to append to this file? We want to add the saying. But I’m also going to use a template string here, because we want to add the saying as a markdown bullet point. So I will go ahead and add the saying. And we’ll trim that as well. And I will also add a \n for a new line. So now we should be appending each saying to this file.

Now I could throw in a third argument for a callback function, but there is no need to do that, because the only reason that I would do that, in this case, is to handle any errors with appending this file, and we are not going to do so on this iteration. So I’m just going to go ahead and save this, and let’s go back out to the terminal. And see if our new app is working. Went ahead and cleared any existing terminal information, so we can run our new promptPerson, simply by typing ‘node promptPerson’. And when we do so, it will ask us for the name of a real person.

Sticking with our presidential theme, I will use Thomas Jefferson. And what are some things that Thomas Jefferson might say? “Honesty is the first chapter in the book of wisdom.” What’s something else he might say? “Never spend money before you have earned it.” Okay, and then we’ll just go ahead and type ‘exit’. So we have the same application, it’s echoing back to us the things that Thomas Jefferson might say. But it also should have created a file.

So let’s go and look in our finder, and under our Start folder, you’ll notice that we have a Thomas Jefferson markdown file, named after the question answer for ‘What is the name of a real person?’ And if we open up that markdown file, we can see that each saying has been appended to this file. So if we continue to add sayings to this application, it will continue to append them as bullets in this markdown file. So the .fs module will give us the ability to append content to existing files, or create new files and add content to our newly-created file as well.

Leave a Reply

Notify of