Practical No-9_AWT Rk
Practical No-9_AWT Rk
Practical No.: 09
i) Reading a file
create sample.txt file with some content
Output:
const fs = require('fs/promises')
const writeFunct = async () =>{
try{
Mulund College of Commerce(Autonomous)
Advance Web Technologies
Aniket Zinjade 238756
Output:
iii) Create a Node.js program that allows users to input text via the command line. Once
the user enters the text, the program should save the input into a file named text.txt.
Additionally, the program should emit a custom event indicating that the text is ready
for processing.
const fs = require('fs');
const readline = require('readline');
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function handleInput(input) {
eventEmitter.emit('textReady', input);
}
eventEmitter.on('textReady', (text) => {
console.log(`Custom event fired: Text is ready - ${text}`);
fs.writeFile('text.txt', text, (err) => {
if (err) throw err;
console.log('Text has been saved to text.txt');
rl.close();
});
});
rl.question('Enter some text: ', (text) => {
handleInput(text);
});
iv) Create a simple Node.js program that creates an HTTP server and handles requests to
it.
const http = require('http');
const hostname = '127.0.0.1';