Ex No: 6 NON-BLOCKING CODE IN NODE.JS
Aim: To demonstrate node.js non-blocking code by reading a text file.
Procedure
Step1: Install Node Package Manager (npm) in the system.
Step2: Initialize Node.js using "npm init -y" in the terminal.
Step3: Create a file “readFile.js” to demonstrate non-blocking code in node js.
Step4: Create a text file “myPage.txt” which is read by the "readFile.js".
Step5: Run the code using "node readFile.js".
Source Code

readFile.js


const fs = require('fs');

console.log('Starting to read file...');

fs.readFile('myPage.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading file:', err);
        return;
    }
    console.log('File content:', data);
});
let sum = 0;
for(let i=1; i<=10; i++){
    sum = sum + i;
}
console.log('Sum: ', sum);

console.log('File read initiated.');
Result: Thus node.js non-blocking code to read a text file is implemented.