Exploring Node.js File System Module


Node.js file system implements File I/O using wrappers around standard POSIX functions, which allows you to work with the file system on your computer. Today, I will explore some of the methods within the fs module.


File I/O and POSIX explanation:

File I/O refers to input and output operations performed on files. These operations include creating, reading, updating, and deleting files (commonly referred to as CRUD operations).

POSIX is a family of standards that allows for conventional standards between operating systems, maintaining compatibility between these systems. It provides uniformity for the API and utilities for operating systems, mostly used by Unix and its variants.


File System module overview:

There are two different forms for each method in the fs module, synchronous and asynchronous. When using synchronous methods, the system waits for the synchronous method to execute before moving onto another task. On the other hand, when using the asynchronous methods, they are executed on another thread, allowing the program to execute other tasks and the main thread is not blocked by the execution of the method. It is recommended to use asynchronous methods because they don't block the program when they are executed. This is very important for the user experience and prevents the frustration of waiting for a certain task to finish before using other functionalities of the program. I will pick one of the features of the fs module and explore asynchronous and synchronous way to use the feature.

In order to start using the fs module, you need to use the require() method:

const fs = require('fs');

Exploring reading files with fs module:

Methods to use when reading files with fs:
readFile()
readFileSync()
fsPromises.readFile()


To test the readFile(), readFileSync(), and fsPromises.readFile() methods, I created a sample_text.txt file to hold some sample text:






Asynchronous version: readFile() method

Explanation:

The readFile() method is used to read contents of a file asynchronously. Which means that while the program is reading the contents of the file, other tasks that are written after the readFile() method continue to execute. In the example below, the program prints a statement and some new lines after the method is called, however in the output they show up before the contents of the file because execution of console.log() method happens faster than reading a file.

Test Code:
                                                                                                                                                                   
//Asynchronous
console.log("Before method");
fs.readFile('sample_text.txt', 'utf8', function(err, contents) {
console.log("***-- Content of File --***")
console.log(contents);
console.log("***---------------------***")
});
console.log("After method");


console.log("")
console.log("")
console.log("")
                                                                                                                                                                   

Output:
                                                                                                                                                                   











                                                                                                                                                                   

Synchronous version: readFileSync() method

Explanation:

The readFileSync() method is used to read contents of a file synchronously. Which means that while the program is reading the contents of the file, other tasks that are written after the readFileSync() method cannot be executed (the program is blocked until the synchronous method executes). In the example below, the program prints a couple statements after the synchronous method is called. As you can see from the output, the statements are printed in the order they were written, because console.log() waits until readFileSync() method finishes executing.

Test Code:
                                                                                                                                                                   
//Synchronous
console.log("Before method");
console.log("***-- Content of File --***")
console.log(fs.readFileSync('sample_text.txt', 'utf8'));
console.log("***---------------------***")
console.log("After method");
                                                                                                                                                                   

Output:
                                                                                                                                                                   








                                                                                                                                                                   

fsPromises version: fsPromises.readFile() method

Explanation:

fsPromises.readFile() is an alternative method of using the asynchronous readFile() method. The difference is that it returns a Promise object instead of the callback. It is available from require('fs').promises, and you can see how it can be used below.

Test Code:

const fsPromises = require('fs').promises;

fsPromises.readFile('sample_text.txt', 'utf8')
.then(function(f) {

console.log(f);
})
.catch(function (err) {

console.log(err);
});






Comments

Popular posts from this blog

First Enhancement in Pandas

Working with Incomplete MultiIndex keys in Pandas

Progress in Open Source