Node.js and its file service module

Julia McGeoghan
Sep 17, 2018 · 3 min read

I’ve decided to work more with node.js and have gotten really excited by a lot of the possibilities it presents. I’ve worked with it before, but I’m being a lot more thorough in my approach to it recently.

In this post I will be examining a specific feature of node.js, the file service module, (a.k.a the fs module). Said module is incredibly useful and convenient when working with the file system, and includes the ability to create, append, delete, and set permissions on files, among many, many things.

To get more familiar with how the fs module works, I wrote a simple command line application that writes to a local file called QUICKNOTES.txt.

Based off the arguments given, my application will either create and write to a file, or append to an existing one. Two fs functions are used to accomplish these tasks, fs.writeFile and fs.appendFile. In this post I will be focusing solely on fs.appendFile.

fs.appendFile(fileName, appendText, function (err) {
if (err) throw err;
console.log('"' + appendText + '" added to ' + fileName);

Above is the method I used to append notes to my QUICKNOTES.txt file. If the QUICKNOTE.txt file doesn’t exist in the current directory fs.appendFile will create a new one, otherwise the existing one will be appended to.

The appendFile function takes the following as arguments:

  • A filepath ( path <string> | <Buffer> | <URL> | <number> filename or file descriptor )
  • The file’s data (data <string> | <Buffer>)
  • An options object (options <Object> | <string>)
  • A callback function (callback <Function>)

As you can see, there are many different ways I could have inputted a filepath in this function, through a string, buffer, url object using the file: protocol. However, given that my note taking application is primarily aimed towards creating files in the current working directory, only a relative filepath needs to be used, and as such the filename variable is simple set as:

var fileName = 'QUICKNOTES.txt';

All fs operations have a synchronous and asynchronous form, with each of these having their own strengths and weaknesses. In the scenario of the application I made, why would I go with one option or the other?

Looking through documentation for node.js, it says that for busy processes, programmers are strongly encouraged to use asynchronous calls to prevent the halting of all other potential processes. However for my note taking application, it isn’t likely to be used on a regular basis; the intent is for notes to be occasionally made. So from there it seems it’d make sense to use a synchronous function, right?

One advantage of using an asynchronous function is that a callback function can be executed immediately after the file is written to, and as the documentation states the first parameter in the callback is always reserved for an exception. This makes error handling more clean and straightforward, for example observe how error handling is done asynchronously:

fs.appendFile('message.txt', 'data to append', (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');

Versus a synchronus example:

try {
fs.appendFileSync('message.txt', 'data to append');
console.log('The "data to append" was appended to file!');
} catch (err) {
/* Handle the error */

The asynchronous version looks a bit cleaner to me, so from that aspect it seems more appealing to work with as far as I’m concerned.

However, as this medium post suggests, asynchronous functions can run much slower than synchronous ones, and as such one should default to synchronous functions whenever possible.

My application is very small and only runs one function when executed, so a focus on speed isn’t as sorely needed. However, I plan on adding a lot mote code to my application. What will happen if I decide to output to many different files, for example?

For the time being I decided to use the asynchronous append function (fs.appendFile), since the application itself is still small and I find the asynchronous version easier to work with. However as the application itself grows, I will refactor the application to use synchronous functions to prevent any loss in performance.


Misc. reflections about my contributions to Open Source software

Julia McGeoghan

Written by

Open source enthusiast. Wants to make cool things with code.


Misc. reflections about my contributions to Open Source software