0% found this document useful (0 votes)
4 views5 pages

Practical No-9_AWT Rk

The document outlines practical exercises in Node.js, focusing on file operations such as reading, writing, appending, renaming, and deleting files. It also includes creating a command-line program to save user input to a file and emitting a custom event, as well as building a simple HTTP server to handle requests. Sample code snippets are provided for each operation to demonstrate the functionality.

Uploaded by

yashhmehtaa1807
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Practical No-9_AWT Rk

The document outlines practical exercises in Node.js, focusing on file operations such as reading, writing, appending, renaming, and deleting files. It also includes creating a command-line program to save user input to a file and emitting a custom event, as well as building a simple HTTP server to handle requests. Sample code snippets are provided for each operation to demonstrate the functionality.

Uploaded by

yashhmehtaa1807
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aniket Zinjade 238756

Practical No.: 09

Aim: Perform following operations in Node.js


i) Reading a file
ii) Writing a file

i) Reading a file
create sample.txt file with some content

a. Synchronously reads the entire contents of a file.


Read.js:
const fs = require("fs")
const a = fs.readFileSync('./sample.txt', "utf-8")
console.log(a)
Output:ge

b. Asynchronously reads the entire contents of a file.


Async.js:
const fs = require("fs")
fs.readFile("./sample.txt", "utf-8",
(error, data)=>{
if(error){
throw new Error('Error reading file!')
}
console.log(data)
}
)

Mulund College of Commerce(Autonomous)


Advance Web Technologies
Aniket Zinjade 238756

Output:

c. Read a file content in Async function.


Async_fun.js:
const fs = require("fs").promises;
async function readFile(){
try{
const data = await fs.readFile("sample.txt", "utf-8");
console.log(data);
}
catch(err){
console.log(err);
}
}
readFile()
Output:

ii) Writing a file


a. Create a file example.txt

b. Add some content to it. Read the file.


c. Append data to the file
d. Rename the file
e. Read the file again after appending
f. Delete the file

const fs = require('fs/promises')
const writeFunct = async () =>{
try{
Mulund College of Commerce(Autonomous)
Advance Web Technologies
Aniket Zinjade 238756

const d= await fs.readFile('example.txt', 'utf-8');


console.log(d)
await fs.writeFile('example.txt', 'Writing in a file', "utf-8")
await fs.appendFile('example.txt', '\n data append via node.js', "utf-8")
await fs.rename('example.txt','NewWrite.txt')
const data = await fs.readFile('NewWrite.txt', "utf-8")
console.log(data)
}
catch(err){
throw err
}
}
writeFunct()

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();

Mulund College of Commerce(Autonomous)


Advance Web Technologies
Aniket Zinjade 238756

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';

Mulund College of Commerce(Autonomous)


Advance Web Technologies
Aniket Zinjade 238756

const port = 4000;


const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
if (req.url === '/hello') {
res.end('Hello World!\n');
} else if (req.url === '/about') {
res.end('This is the about page.\n');
} else {
res.end('Node.js server!\n');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Output:

Mulund College of Commerce(Autonomous)


Advance Web Technologies

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy