NodeJs – File System

What Is File System ?

In terms of computing , Filesystem means the way of files is stored and retrieved in a manner from Disk partition.

File Server Using NodeJs :

Using “fs” Module in NodeJs allows you to access the physical file stream.This Module responsible for all the asynchronous and synchronous I/O operation.

var fs = require('fs');
//where fs inside the require is an module that 
    //allows you to access the file system

Common use of file System are ,

  • Read files
  • Create files
  • Update files
  • Delete files
  • Rename files

Read Files

Lets see how to read a file using fs.readFile() Method . Assume we have a text file contains a paragraph “Welcome to Techies Book ,Best tutorial site to learn nodeJs for beginners” in a filename Welcome.txt .
(located in the same folder as Node.js)

Syntax: fs.readFile(fileName [,options], callback)

Parameter Description:

  • filename: Full path and name of the file as a string.
  • options: It can be an object or string which can include a encoding and flag.The default encodeing is “utf8” and flag is “r” .
  • callback : a function with two parameters err and fd (filedata).once the operation is completed the callback will be called.

What is callback ?

Callback is a function called at a completion of given task if any error occurs during processing a request it simply throws the error and ready to handle next request.

Example Program For Reading File Using callback from server.js :

var fs = require('fs');
fs.readFile('Welcome.txt', function (err, data) {
     if (err) throw err;
     console.log(data);
});

The above requires the “fs” Module assigned in a variable fs.Using fs.readFile() method we start reading a file name Welcome.txt . if any error occurs during the read file the err parameter passes the error information in it .otherwise the data will be passed in a data parameter .so that you can the console.log() to display the data .

Paste the below paragraph in Welcome.txt file

Welcome to Techies Book ,Best tutorial site to learn nodeJs for beginners

Result :

D:\node project > node server.js
Welcome to Techies Book ,Best tutorial site to learn nodeJs for beginners

Writting Files

Lets see how to write a file using fs.writeFile() Method .If file contain any data already simply it overwrites everything with a new data otherwise it creates a new file and writes everything . (located in the same folder as Node.js)

Syntax: fs.writeFile(fileName [,options], callback)

Parameter Description:

  • filename: Full path and name of the file as a string.
  • options: It can be an object or string which can include a encoding and flag.The default encodeing is “utf8” and flag is “r” .
  • callback : a function with two parameters err and fd (filedata).once the operation is completed the callback will be called.

Example Program For Creating & Writing  File in server.js file :

var fs = require('fs');
fs.writeFile('Welcome.txt', 'Hello World!', function (err) { 
                        if (err)
        console.log(err);
                        else
        console.log('Write operation complete.');
});

The above requires the “fs” Module assigned in a variable fs.Using fs.writeFile () method we start Writing  a data in file name Welcome.txt . if any error occurs during the write file the err parameter passes the error information in it. That will be consoled .

Delete Files

Lets see how to delete file using fs.unlink() Method .it deletes the specified file using a file name. Ex: delete a file Welcome.txt

var fs = require('fs');

fs.unlink('Welcome.txt', function (err) {
  if (err) throw err;
  console.log('File deleted!');
});

I hope you have understand the basic file system operation using NodeJs.

Next Chapter >> NPM

Leave a Reply

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