WT 02
WT 02
WT 02
Roll No: 25
Assignment 2
1. Write an Event Emitter code in Node.js to create an event naming
EmployeeInfo which when emitted displays various information of Customer
like CCode, CName, CPhone and CCity by passing these data as arguments.
console.log(`CCode: ${CCode}`);
console.log(`CName: ${CName}`);
console.log(`CPhone: ${CPhone}`);
console.log(`CCity: ${CCity}`);
});
add(a, b) {
const result = a + b;
this.emit('addition', result);
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
subtract(a, b) {
const result = a - b;
this.emit('subtraction', result);
multiply(a, b) {
const result = a * b;
this.emit('multiplication', result);
divide(a, b) {
if (b === 0) {
this.emit('divisionByZero');
return;
const result = a / b;
this.emit('division', result);
});
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
});
});
calculator.on('divisionByZero', () => {
});
calculator.add(1, 2);
calculator.subtract(5, 3);
calculator.multiply(4, 5);
calculator.divide(10, 5);
calculator.divide(10, 0);
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
3. Write an Event Emitter code to demonstrate all the Event Emitter API
methods
// listener #1
console.log('listner1 executed.');
// listener #2
console.log('listner2 executed.');
eventEmitter.addListener('connection', listner1);
eventEmitter.on('connection', listner2);
(eventEmitter,'connection');
eventEmitter.emit('connection');
eventEmitter.removeListener('connection', listner1);
eventEmitter.emit('connection');
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log("End.");
this.emit('calculateSalary', salary);
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
});
5. Create a HTTP server to display your basic information Name, Age and
Address on the client.
res.write('<h1>Basic Information</h1>');
res.write('<p>Age: 21</p>');
res.end();
});
server.listen(8080, () => {
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
6. Create a module that will display the current date and time along with
aconcatenated message, oncethe client tries to access the HTTP server.
res.setHeader('Content-Type','text/plain');
res.setHeader('Content-Type','text/plain');
res.writeHead(200);
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
res.end(s);
});
server.listen(8080);
7. Write a Node.js code to accept a query string from the user on the server
through the URL, and write it back to the client.
res.write(`<h1>Query String</h1>`);
res.write(`<p>${JSON.stringify(query)}</p>`);
res.end();
});
server.listen(8080, () => {
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
8. Write a Node.js code to pass specific input variables through the URL to
theserver and the server should process then ad respond back.
res.write(`<p>${result}</p>`);
res.end();
});
server.listen(8080, () => {
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
9. Write a Node.js code to display your short Bio-Data (along with Photograph)
saved in an HTML file in response to the client’s access request to the server.
const fs = require('fs');
http.createServer(function(req,res){
if(req.url ==="/"){
fs.readFile("./public/index.html","UTF-8",function(err,html){
res.writeHead(200, {'Content-type':'text/html'});
res.end(html);
});
}else if(req.url.match("\.css$")){
res.writeHead(200, {'Content-type':'text/css'});
fileStream.pipe(res);
}else if(req.url.match("\.png$")){
res.writeHead(200, {'Content-type':'image/png'});
fileStream.pipe(res);
}).listen(8080);
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
10. Write a Node.js code which can create a file “TxtFile1.txt”. Insert some text
in thefile and read the same from the file to be displayed.
const fs = require('fs');
if (err) {
console.error(err);
} else {
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
if (err) {
console.error(err);
} else {
});
11. Write a Node.js code to open an existing file and append the contents of
the file. Post updating display the contents of the file.
const fs = require('fs');
fs.appendFile('TxtFile1.txt', '\nThis File has been Appended with new Text', err => {
if (err) {
console.error(err);
} else {
});
if (err) {
console.error(err);
} else {
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
});
12. Write a Node.js code to read data from a file and write it into some other
file. Display contents of both the files.
var fs = require("fs");
});
});
});
const fs = require('fs');
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25
if (err) {
console.error(err);
} else {
console.log('File deleted');
});
Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24