Realtime and Scalable Web Applications

node.js file system: A detailed walkthrough with codes 2022

file management isometric vector

In this article, we’ll be taking a look at how you can work with files using the Node.js file system. We’ll start by discussing some of the basics of reading and writing files, after which we’ll take a look at some more specific use cases.

Synchronous vs Asynchronous in node.js

Asynchronous methods are much better to be used rather than synchronous methods, because the former never blocks the executing program and the latter do always block the program execution, which is not needed. Every asynchronous function of the fs module accepts the last parameter as the callback function and the first parameter as the error.

Approach taken synchronously – These functions are known as blocking functions because they wait for each operation to finish before executing the next one. As a result, the next command cannot be executed until and unless the query has finished executing in order to receive all of the results from previous commands.

Approach taken asynchronously: These are known as non-blocking functions because they never wait for an operation to finish; instead, they carry out all tasks at once. Each operation’s outcome will be handled as soon as it becomes available, meaning that each order will be carried out shortly after the one before it. While the preceding command processes the data in the background and loads the outcome when it is done,

Cases of Use

Choose synchronous processing over asynchronous processing if your processes don’t require doing much heavy lifting, such as querying enormous amounts of data from a database. So in this case

You can display a progress bar to the user asynchronously while working on more time-consuming tasks in the background. This situation is great for GUI-based applications.

With a file a list of following operations can be done –

  1. Read
  2. Write
  3. Open
  4. Close
  5. Append
  6. Delete

We have defined all the operations with both synchronous and asynchronous manner

Make a text file called readText.txt and fill it with the following information:

Inside the text file write this sample text – “This file is being read by both synchronously and asynchronously methods”

Read a file synchronously node.js

The syntax to read a file in sync mode is

fs.readFileSync( path, options )

Note -(fs is the name of the variable which holds the filesystem module, according to naming conventions we have written fs in the syntax but we can name a variable with any name we want in our examples we have named the variables as fsys, Read this post about naming conventions and programming basics.)

The arguments it takes are –

path: The text file’s relative path is used. A URL type path is also possible. Additionally, the file may be a file descriptor. Just put the filename in quotes if both files are in the same folder.

options: It is an optional parameter that contains an encoding, a flag, and data definition in the encoding. Its default value of null returns a raw buffer, and the flag includes information about file actions. ‘r’ is the default value for it.

This is the code to read the file synchronously –

const fsys = require(‘fs’);

const readStream = fsys.readFileSync(‘ readText.txt’);

console.log(“Synchronous read result is – ” + readStream.toString());

The output of this code as mentioned above is – “Synchronous read result is – This file is being read by both synchronously and asynchronously methods”. The toString method which was applied to variable readStream(readStream.toString()) allows the variable readStream to store the contents of the file in a String format or we can say in a sentence format. You can refer to this post if you want to know more about how the functions are applied to a variable(when we have written readStream.toString() we are applying the function toString() to the readStream variable).

So the file has been read successfully having all of its contents and all the contents have been stored in the variable named readStream along with display of it on the screen. We can access the contents of the file anytime we want through this variable.

Read a file asynchronously node.js

The syntax to read a file in async mode is

fs.readFile( filename, encoding, callback_function )

The fs.readFile function takes in 3 arguments

filename: It contains the name of the file to read or, if the file is stored elsewhere, the entire path is then given.

Encoding: It stores the file’s encoding. The predetermined value is “utf8.”

callback function: This callback function is used after a file has been read. There are two requirements:

err: If there is an error thrown while reading

data: The file’s content.

Now we will look at how to read a file asynchronously –

var fsys = require(“fs”);

fsys.readFile(‘readText.txt’, function (errorInRead, successInRead) {

if(errorInRead) {

console.log(errorInRead);

return;

}

console.log(” Asynchronous read result is – ” + successInRead.toString());

});

Here in order to read the file we have used callback mechanism. You can read every detail and the concept about callbacks in node.js in this post.

Here we have read that same file and this time via asynchronous callback. So here if there is any error caught while reading the file it will be shown in the console and the contents of error is in errorInRead variable so we have put an if statement to handle that error, and if there is success in reading the file its contents gets stored in successInRead variable.

The output will be “Asynchronous read result is – This file is being read by both synchronously and asynchronously methods”

Open a file with fs node.js :- 

To create, read, or write a file, use the fs.open() method. While the fs.open() function performs several actions on a file, the fs.readFile() and fs.writeFile() methods only allow reading and writing to a file, respectively.

To access the actual file system, we must first load the fs class, a module.

We do so with the following code –

const fsys = require(‘fs’);

Open a file synchronously in node.js – 

Then we use the fsys variable to open the contents of the file and apply

Syntax for opening a file synchronously –

fs.readFileSync( path, options )

Arguments of readFileSync

path: The text file’s relative path is used. A URL type path is also possible. Additionally, the file may be a file descriptor. Just put the filename in quotes if both files are in the same folder.

options: It is an optional parameter that contains an encoding, a flag, and data definition in the encoding. Its default value of null returns a raw buffer, and the flag includes information about file actions. ‘r’ is the default value for it.

First we see how to do in synchronous way –

const fsys = require(‘fs’);

const fileContents = fsys.openSync(‘readText.txt’, ‘r’);

console.log(fileContents);

Output – This file is being read by both synchronously and asynchronously methods.

Open a file asynchronously in node.js –

Syntax for opening a file  asynchronously –

fs.open(path, flags, mode, callback)

Arguments of readFile: 

path: It contains the name of the file to read or, if the file is stored in another location, the full path.

flags: Flags describe how the file will behave when it is opened. (r, r+, rs, rs+, w, wx, w+, wx+, a, axe, a+, ax+) are all valid values.

Sets the file’s mode, such as r-read, w-write, or r+-readwrite. It is set to readwrite by default.

err: Should a mistake be made.

data: The file’s content. After the open operation has been completed, it is called.

To open a file asynchronously in node.js –

const fsys = require(“fs”);

fsys.open(‘readText.txt’, ‘r+’, function(errorInRead, readSuccess) {

if(errorInRead) {

console.log(errorInRead);

return;

}

console.log(“File has been opened successfully” + readSuccess.toString());

});

OutPut – File has been opened successfully This file is being read by both synchronously and asynchronously methods.

Writing a file in node.js

Asynchronous write

If the file already exists, this procedure will replace it. The provided data is written asynchronously to a file using the fs.writeFile() function. Normally, if the file already exists, it would be replaced. The method’s functionality can be changed by using the ‘options’ parameter.

Arguments it takes —

path: The path of the file where it has to be written is indicated by a string, buffer, URL, or integer file description. It will behave similarly to the fs.write() method when a file descriptor is used.

data: The object that will be sent to the file is either a string, buffer, typed array, or data view.

It is possible to specify optional parameters that will alter the result using a string or object called options. There are three optional settings for it:

Encoding: This string value identifies the file’s encoding. ‘utf8’ is the default setting.

mode: The file mode is indicated by an integer value. 0o666 is the default value.

flag: This string value indicates the flag that was used to write to the file. ‘w’ is the default value.

callback: This is the procedure that will be invoked after the method is finished running.

If the operation fails, an error would be thrown, which is err.

Syntax of write file is –

fs.writeFile(path, data, options, callback)

var fsys = require(“fs”);

fsys.writeFile((‘readText.txt’, ‘This sentence is the contents of file being written’, function(errorInRead) {

if(errorInRead) {

console.log(errorInRead);

return;

}

console.log(“Data has been written to file successfully!”);

console.log(“To verify the same let us read the file asynchronously “);

fsys.readFile(‘readText.txt’, function (errorInRead, readSuccess) {

if(errorInRead) {

console.log(errorInRead);

return;

}

console.log(“Read has been successful and the file content is ” + readSuccess.toString());

});

});

Output Will be —

Data has been written to file successfully!

To verify the same let us read the file asynchronously

This sentence is the contents of file being written

To write file synchronously —

Syntax —

fs.writeFileSync( file, data, options )

Arguments —

file: The path of the file where it has to be written is indicated by a string, buffer, URL, or integer file description. It will behave similarly to the fs.write() method when a file descriptor is used.

data: The object that will be sent to the file is either a string, buffer, typed array, or data view.

Options: You can specify extra parameters that will alter the outcome using a string or object. There are three optional settings for it:

encoding: It is a string that describes the file’s encoding. ‘utf8’ is the default setting.

mode: The file mode is indicated by an integer. 0o666 is the default value.

flag: The flag used while writing to the file is specified by a string. ‘w’ is the default value.

Code —

const fsys = require(‘fs’);

const write = “This sentence is the contents of file being written synchronously”;

fsys.writeFileSync(‘writeFile.txt’, write);

const read = fsys.readFileSync(‘writeFile.txt’, “utf8”));

console.log(“contents of the file being written is “ + read)

Output —

contents of the file being written is This sentence is the contents of file being written synchronously

Appending at a file node.js

Appending at the end of a File: The data is synchronously or asynchronously appended to the file filepath using the fs.appendFile() method: The file path is specified by a String.

Syntax —

Asnyc manner —

fs.appendFile(filepath, data, options, callback);

Sync manner —

fs.appendFileSync(filepath, data, options);

Arguments it takes —

Data: This field must be filled out and contains any data you append to the file.

options: The encoding, mode, and flag are specified by this optional argument.

Callback: This function must be called once all data has been added to the file.

Code —

var fsys = require(‘fs’);
var writeData = “ This data has been appended to the file named readText.txt”;

fsys.appendFile(‘readText.txt’, writeData, ‘utf8’, function(errorInAppend) {
if(errorInAppend) {

console.log(“An error has been received”);

return;

}

})

fsys.appendFileSync(‘readText.txt’, writeData, ‘utf8′);

const read = fsys.readFileSync(‘writeFile.txt’, “utf8”);

console.log(“contents of the file being appended is “ + read)

OutPut –

contents of the file being appended is This file is being read by both synchronously and asynchronously methods This data has been appended to the file named readText.txt This data has been appended to the file named readText.txt

Notice that the line “This data has been appended to the file named readText.txt” has appeared twice in output is because in the same piece of code we have both used append file with async and sync manner.

Closing a file in Node.js

We use fs.close() method for all file closing related operations.

By asynchronously closing the specified file descriptor and clearing the associated file, the fs.close() method is employed. As a result, the file descriptor may be applied to additional files. While a file descriptor is being used for another operation when fs.close() is called on it, unexpected behaviour may result.

fd: The file descriptor of the file that is to be closed is indicated by the integer fd.

callback: When a method is invoked, a function called as a callback is called.

err: If the method fails, err is the error that would be thrown.

Syntax —-

Async method —-

fsys.close(fd, callback)

Example

fsys.close(fd, function(errorInClose) {

if(errorInClose) {

console.log(errorInClose);

}

console.log(“File closed successfully.”);

} )

Sync method —

fsys.closeSync( fd )

Deleting a file in Node.js

To delete a file or symbolic link from the filesystem, use the fs.unlink() method. It is advised to use fs.rmdir() to remove directories since this function does not handle them.

Note — This may permanently delete the file from your filesystem.

Argument it takes —

path: The file or symbolic link that needs to be deleted is represented by the string, buffer, or URL known as the path.

callback: When a method is invoked, a function called as a callback is called.

err: If the method fails, an error would be thrown.

Syntax —

fs.unlink(path, callback)

fs.unlink(‘fileName.txt’, function(errorInDelete) {

if(errorInDelete) {
return console.error(errorInDelete);

}

console.log(“File deleted successfully!”); });

Synchronous way –

fs.unlinkSync(path)

Related posts
Realtime and Scalable Web Applications

Node.js Twitter API: 5 Steps to Twittersphere Mastery

Realtime and Scalable Web Applications

Using Variables In Node.js: Quick and brief - Logic Latest

Realtime and Scalable Web Applications

Node.js event loops and callbacks: A brief walkthrough

Realtime and Scalable Web Applications

Import module in node.js: how to import and export modules

Leave a Reply

Your email address will not be published. Required fields are marked *