Experiment #2
In this experiment, we explore how Node.js handles synchronous and asynchronous file operations using the built-in fs module. We compare fs.readFileSync() and fs.readFile() to understand the difference in execution order and blocking behavior.
Title:
Develop a Node.js Program for Demonstrating Synchronous and Asynchronous Operations.
Aim:
The objective of the project is to create a basic Node.js program that compares between synchronous and asynchronous file operations with the help of the File System (fs) module.
Software Installations Required for Project Completion:
- Windows 10/11
- Node.js
- NPM
- Visual Studio Code
Source Code:
Step 1: Create a Project Folder
Create a folder named:
NodeDemo
Step 2: Create a File Named test.txt
Hello Node.js
Step 3: Create a File Named Source.js
const fs = require("fs");console.log(fs.readFileSync("test.txt", "utf8"));fs.readFile("test.txt", "utf8", (err, data) => console.log(data));console.log("Done");
Step 4: Run the Program
node Source.js
Output
Hello Node.jsDoneHello Node.js
Terminal Output
C:\NodeDemo> node Source.jsHello Node.jsDoneHello Node.js
Result:
Thus, the Node.js program was executed successfully and the difference between synchronous and asynchronous file operations was demonstrated.






tq sir
ReplyDelete