The main objective of this module is to focus on the operations offered by Node.js through the File System.
What do you mean by File System in Node.JS?
To manage file operations like creating, accessing, erasing, and other similar tasks, Node.js provides a built-in module called FS (File System).
Node.js provides interfaces, mostly around common POSIX functions, to provide file I/O capabilities.
Depending on a user’s needs, all file system processes can be either asynchronous or synchronous.
It is possible to interact with your computer’s file system thanks to the Node.js file system module.
Use the require() function to incorporate the File System module:
var fs = require('fs');
Now that you have understood what is meant by File Systems, let us understand:
What is A File System Module used for?
The File System module is frequently used for the following purposes:
- File Reading
- File Creation
- File Updation
- File Removal
- File Renaming
Let’s discuss each of them in-depth!!
Using the fs.readFile() method and providing the file path, encoder, and a callback function which will be invoked with the data files (and any errors) is the easiest way to read a file in Node.js.
Files can be read in Node in three different methods.
The promise way, the callback way, and the synchronous way are the names of these approaches.
- The Promise Way:
We can leverage the asynchronous async and await features of current Javascript thanks to the promise method.
- The Callback Way:
The Callback method makes use of the fs.readFile Node API. We received the callback from this method once the entire file had been read into memory.
- The Synchronous Way:
The fs.readFileSync Node API is used to implement the synchronous approach.
Get 100% Hike!
Master Most in Demand Skills Now!
There are several ways to create new files in the File System module:
- fs.appendFile()
- fs.open()
- fs.writeFile()
fs.appendFile()
To asynchronously add the supplied data to a file, use the fs.appendFile() method. If the file doesn’t already exist, one is created. You can alter how the operation behaves by using the options parameter.
The syntax goes as follows:
fs.appendFile( path, data[, options], callback )
fs.open()
Multiple operations are performed on a file using the fs.open() function.
The fs class, a module that allows access to the physical file system, must first be loaded. A required procedure is employed for it.
For instance: var fs = require(‘fs’)
Syntax:
fs.open( filename, flags, mode, callback )
fs.writeFile()
The asynchronous writing of the provided data to a file is accomplished using the fs.writeFile() method. If the file already exists, it will usually be replaced. You can alter the method’s functionality by using the ‘options’ parameter.
The syntax for the same is as follows:
fs.writeFile( file, data, options, callback )
Updates to files can be made using the File System module’s methods:
- fs.writeFile()
- fs.appendFile()
fs.writeFile()
The selected file and its contents are replaced via the fs.writeFile() method:
var fs = require('fs');
fs.writeFile('File1.txt', 'My Content', function (err) {
if (err) throw err;
console.log('Replaced!');
});
fs.appendFile()
Append “Added Text.” to the end of the file “File1.txt”:
var fs = require('fs');
fs.appendFile('File2.txt', 'Content Added', function (err) {
if (err) throw err;
console.log('Updated!');
});
Use the fs.unlink() method to remove a file with the File System module.
The file given by the fs.unlink() function is removed:
var fs = require('fs');
fs.unlink('File3.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Use the fs.rename() method in the File System module to rename a file.
var fs = require('fs');
fs.rename('File3.txt', 'File4.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});
We will be studying NodeJS in more detail in the upcoming chapters, we hope you are finding this tutorial helpful!!