Open In App

What is the mechanism of reading the content of a file in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the mechanism of reading a file in NodeJs.

NodeJS is one of the most popular JavaScript frameworks that allows JavaScript to be used as a server-side language and to manage servers and their networks with the help of JavaScript and provide a pre-built module for working with files. Firstly we will understand how NodeJs works with files and after we will discuss how to read the data of a file and its mechanism. If the thought of as a server, then working with files is very common, we do most of the work on the server in files. It is very easy to work with files and also very convenient. If you look about a web service that runs a website, then it must have done it with files somewhere. We can work with files very easily in NodeJs NodeJs provides us many ways to work with files NodeJs also has some built-in modules with the help of which we can work with files. NodeJs treats a file as a document with built-in functions to store and read the necessary information that we can use to work with our files. It is very common to work with files for development purposes as it makes our work easier. In daily computer use, the user may wish to read and write files and data in various directories such as saving and downloading files or accessing data used in other applications. Similarly, server programs or command-line interface “CLI” tools may need to be written to a file to save the downloaded data, or data-intensive applications may also need to be exported to “JSON” or other formats. Might be possible. These programs will need to communicate with the file system of the operating system they are running on so that the NodeJs in the server can manage it.

If you want to more about file systems in NodeJs please refer to this article.

Mechanism of reading a file in NodeJS: In Node.js, you can work with files with the help of a built-in “fs” module and it’s very simple. and this module includes all functionality that will help you to work with files, And it can operate with both types of files on a local machine or server. And it gives you two types of facilities, which have the ability to work synchronously and asynchronously, you can use it in any NodeJs file and for this, there is no need for any external package.

Approach: Have to work with the file system, so the ‘fs’ module will be needed first, so require the ‘fs’ module in our codebase. The read method of the ‘fs’ module return buffer data, which will require a variable to hold. The buffer will store the data on a variable. Convert the buffer data to the normal string with the help of the toString() method. Finally log the data using the console.log() method.

Below is the step by step implementation of the above approach:

Step 1: First, let’s require a file system module into our program for reading the data from a file.

const fs = require('fs');

After requiring the ‘fs’ module moves to the second step.

Step 2: In this step create a variable to contain the data of a file.

let dataBufferContainer= '';

Step 3: After making the variable, store the file buffer data to the variable in the use of the readFileSync or readFile Method.

dataBufferContainer = fs.readFileSync('file.js');

Step 4: Convert buffer value to the normal string value with the use of the toString() method.

let data = dataBufferContainer.toString();

Step 5: Log the data using the console.log() method.

console.log(data);

Finally, you have read the data from the file.

Below is the implementation of the above approach:

index.js




// Firstly require a file-system module.
const fs = require('fs');
  
// data container.
let dataBufferContainer = '';
  
// read the use of fs.readFileSync method.
dataBufferContainer = fs.readFileSync('file.js');
  
// convert buffer data to string.
let data = dataBufferContainer.toString();
  
console.log(data);


file.js




GFG => Knowledge


Step to run the application:

node index.js

Output:



Last Updated : 04 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads