File System Operation in node.js

Node.js is opensource, completely free and powerful JavaScript library. Node.js is a server-side platform built on chrome’s V8, so it is very fast in executing code. It is mainly using for web application development like single page and video streaming sites. Nodejs also has multiple libraries by default in it, so it will reduce development effort. One of the library is File System formally using as “fs”
Node.js file system:
Node.js has a module called fs to access physical file system. This module is responsible for doing all synchronous and asynchronous I/O operations. All methods in fs module has synchronous as well as asynchronous forms. Asynchronous methods will take the last parameter as a callback function and the first parameter of the callback function as error
Operations
Below are the basic I/O operations done by Nodejs file system
1. Reading File
2. Writing File
3. Open File
4. Delete File
Reading File: use fs.readFile() method is used to read a file asynchronously
fs.readFile(fileName [,options], callback)
filename will be the full path and name of the file as a string format
options are the fags supplied to readFile method which can be included as encoding and mode of the file operation. By default, it will be “r”
callback with two parameters they are error and data
below is an example for reading a file
var fs = require(‘fs’);
fs.readFile(‘read.txt’, function (err, data) {
if (err) throw err;
console.log(data);
});
From the above example, it reads the read.txt file and executes callback function asynchronously. Above example reads the file successfully or throws an error. Error parameter contains error information if there is an error while reading the file. Data parameter will have the actual data information.
Writing File: writeFile method will write the data to a file. if file already exist in the specified path it will overrides the existing data otherwise it creates new file and write content to it.
Consider an example for writing file
fs.writeFile(filename, data[, options], callback)var fs = require(‘fs’);
fs.writeFile(‘write.txt’, ‘Hello World this is my first file’, function (err) {
if (err)
console.log(err);
else
console.log(“writing content to file is completed”);
});
Opening a File:
Opening a file is an operation supported by the file system to either reading or writing mode, in most of the cases file operation can based on the mode of the file.
var fs = require(“fs”);
console.log(“Going to open file!”);
fs.open(‘input.txt’, ‘r+’, function(error, data) {
if (error) {
return console.error(error);
}
console.log(“File opened successfully!”);
});
Delete File:
Use fs.unlink() method to delete an existing file from the file system
Below is the example for deleting an existing file
var fs = require(‘fs’);
fs.unlink(‘test.txt’, function () {
console.log(‘write operation complete.’);
});
Share your thoughts by writing to me at Asirinaidu.Paidi@suneratech.comor use the comments section below.
For more details on Web application development and application support, visit http://www.suneratech.com/