Experiment · Node.js Lab
File Management and Streaming using Node.js
Aim
Implement file management and streaming using Node.js.
Explanation
-
fsis used to work with files in Node.js. -
writeFileSync()createsdata.txtand writes text into it. -
console.log()displays "File created". -
createReadStream()reads the file using streaming. -
The
dataevent reads the file data in chunks. -
The
endevent shows that reading is completed.
Source Code
src/App.js
const fs = require("fs");
fs.writeFileSync("data.txt", "Hello Node.js!");
console.log("File created.");
const stream = fs.createReadStream("data.txt", "utf8");
stream.on("data", chunk => console.log("Data:", chunk));
stream.on("end", () => console.log("Reading completed."));
01
Execute
Terminal
node app.js
Output
File created.
Data: Hello Students!
We are learning Node.js.
Reading completed.
Data: Hello Students!
We are learning Node.js.
Reading completed.
Result: The Node.js program has accomplished the task of file creation and using a stream to read its contents. Hence, file management and streaming processes have been carried out successfully.






0 Ravula Kartheek:
Post a Comment