WT 02

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

SIES College of Management Studies FYMCA (Revised), Sem I,

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.

const EventEmitter = require('events');

class EmployeeEmitter extends EventEmitter {}

const employeeEmitter = new EmployeeEmitter();

employeeEmitter.on('EmployeeInfo', (CCode, CName, CPhone, CCity) => {

console.log(`CCode: ${CCode}`);

console.log(`CName: ${CName}`);

console.log(`CPhone: ${CPhone}`);

console.log(`CCity: ${CCity}`);

});

employeeEmitter.emit('EmployeeInfo', 100, 'Jidnesh', '9568742369', 'Navi Mumbai');

2.Write an Event Emitter code in Node.js to emit various calculator-based


calculation events. (Make your necessary assumptions)

const EventEmitter = require('events');

class Calculator extends EventEmitter {

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

const calculator = new Calculator();

calculator.on('addition', result => {

console.log(`The result of the addition is: ${result}`);

});

calculator.on('subtraction', result => {

console.log(`The result of the subtraction is: ${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('multiplication', result => {

console.log(`The result of the multiplication is: ${result}`);

});

calculator.on('division', result => {

console.log(`The result of the division is: ${result}`);

});

calculator.on('divisionByZero', () => {

console.log('Cannot divide by zero');

});

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

var events = require('events');

var eventEmitter = new events.EventEmitter();

// listener #1

var listner1 = function listner1() {

console.log('listner1 executed.');

// listener #2

var listner2 = function listner2() {

console.log('listner2 executed.');

eventEmitter.addListener('connection', listner1);

eventEmitter.on('connection', listner2);

var eventListeners = require('events').EventEmitter.listenerCount

(eventEmitter,'connection');

console.log(eventListeners + " Listner(s) listening to connection event");

eventEmitter.emit('connection');

eventEmitter.removeListener('connection', listner1);

console.log("Removed the binding of listner1 function .");

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(eventListeners + " Listner(s) listening to connection event");

console.log("End.");

4. Write an event emitter code to design an event called as “calculateSalary”


which is used to calculate the salary of an employee by passing some
arguments like Basic Salary, HRA (20% of Basic), DA(100% of Basic), TA, and
deductions like Income Tax (30% of Basic) and Professional Tax of 200.

const EventEmitter = require('events');

class SalaryCalculator extends EventEmitter {

calculateSalary(basicSalary, hra, da, ta, incomeTax, professionalTax) {

const salary = basicSalary + hra + da + ta - incomeTax - professionalTax;

this.emit('calculateSalary', salary);

const calculator = new SalaryCalculator();

calculator.on('calculateSalary', salary => {

console.log(`The salary is: ${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

});

calculator.calculateSalary(1000000, 25000, 10000, 5000, 9000, 200);

5. Create a HTTP server to display your basic information Name, Age and
Address on the client.

const http = require('http');

const server = http.createServer((req, res) => {

res.writeHead(200, { 'Content-Type': 'text/html' });

res.write('<h1>Basic Information</h1>');

res.write('<p>Name: Jidnesh Madhavi</p>');

res.write('<p>Age: 21</p>');

res.write('<p>Address: A-303, Pawandhara, Sector-17, Kamothe, Navi Mumbai</p>');

res.end();

});

server.listen(8080, () => {

console.log('Server listening on port 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.

const http = require('http');

const server = http.createServer();

const moment = require("moment");

let s= new Date().toLocaleString();

let print = console.log("The time & date is", s);

server.on('request', (req, res) => {

console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);

res.setHeader('Content-Type','text/plain');

res.setHeader('Content-Type','text/plain');

res.writeHead(200);

res.write("The time & date is ");

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

const http = require('http');

const url = require('url');

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.

const server = http.createServer((req, res) => {

res.writeHead(200, { 'Content-Type': 'text/html' });

const query = url.parse(req.url, true).query;

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.

const http = require('http');

const url = require('url');

const server = http.createServer((req, res) => {

res.writeHead(200, { 'Content-Type': 'text/html' });

const query = url.parse(req.url, true).query;

const operator = query.operator;

const num1 = parseInt(query.num1);

const num2 = parseInt(query.num2);

let result = num1 + num2;

res.write(`<h1>Addition of ${num1} & ${num2}</h1>`);

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 http = require('http');

const fs = require('fs');

var path = require('path');

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$")){

var cssPath = path.join(__dirname, 'public',req.url);

var fileStream = fs.createReadStream(cssPath, "UTF-8");

res.writeHead(200, {'Content-type':'text/css'});

fileStream.pipe(res);

}else if(req.url.match("\.png$")){

var imgPath = path.join(__dirname, 'public',req.url);

var fileStream = fs.createReadStream(imgPath);

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

fs.writeFile('TxtFile1.txt', 'Hello World!', err => {

if (err) {

console.error(err);

} else {

console.log('Text written to file');

});

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

fs.readFile('TxtFile1.txt', 'utf8', (err, data) => {

if (err) {

console.error(err);

} else {

console.log(`Text read from file: ${data}`);

});

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 {

console.log('Text appended to file');

});

fs.readFile('TxtFile1.txt', 'utf8', (err, data) => {

if (err) {

console.error(err);

} else {

console.log(`Text read from file: ${data}`);

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

fs.readFile('TxtFile1.txt', 'utf8', function (err,data) {

if (err) throw err;

console.log("Data in File 1: " + data);

fs.writeFile('TxtFile2.txt',data, function (err) {

if (err) throw err;

else console.log("Data in File 2: " + data);

});

});

fs.readFile('TxtFile2.txt', 'utf8', function(err, data){

console.log("Data in File 2 before Change: " + data);

});

13. Write a Node.js code to delete any text file

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

fs.unlink('TxtFile1.txt', err => {

if (err) {

console.error(err);

} else {

console.log('File deleted');

});

Subject: MCAL14 Web Technologies Lab Academic Year First Half 2022_23
Batch:2022_24

You might also like