Ex No: 7 BLOCKING CODE IN NODE.JS
Aim: To demonstrate node.js 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 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...');

try {
    const data = fs.readFileSync('myPage.txt', 'utf8');
    console.log('File content:', data);
} catch (err) {
    console.error('Error reading file:', err);
}

// This section calculates the sum of numbers from 1 to 10
let sum = 0;
for(let i=1; i<=10; i++){
    sum = sum + i;
}
 
// Prints the sum
console.log('Sum: ', sum);
console.log('File read completed.');

Result: Thus node.js blocking code to read a text file is implemented.