Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
Practical No:-7
Q1 Introduction to Node JS:
A]Create a Node js script file that displays hostname and platform
details of current system on the console..
Source Code:
var os = require('os'); console.log("Platform of OS:
"+os.platform()); console.log("Hostname of OS:
"+os.hostname());
Output :
k C:\Sudhir> node host.js Platform of OS:
win32
Hostname of OS: DESKTOP-6NPP4PK k
C:\SUdhir>
Q2 ]Create a user defined module named Math with four
functions Addition, Substraction, Multiplication,Division and
export them.Import math module from other Node js script file
and invoke all the four functions to perform operations on given
input.
Source Code:
Math Module
// Math.js
// Function to perform addition function
addition(a, b) { return a + b;
}
// Function to perform subtraction function
subtraction(a, b) { return a - b;
}
Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
// Function to perform multiplication function
multiplication(a, b) { return a * b;
}
// Function to perform division function
division(a, b) {
if (b === 0) { throw new Error("Division by zero is
not allowed");
} return a
/ b;
}
// Exporting functions
module.exports = { addition,
subtraction, multiplication,
division
};
New.js
// app.js
// Importing Math module const Math =
require('./Math');
// Invoking all the four functions to perform operations const a = 10; const b =
5; console.log("Addition:", Math.addition(a, b)); console.log("Subtraction:",
Math.subtraction(a, b)); console.log("Multiplication:", Math.multiplication(a,
b)); console.log("Division:", Math.division(a, b));
Output :
k C:\Sudhir> node New.js Addition: 15
Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
Subtraction: 5
Multiplication: 50
Division: 2
k C:\Sudhir>
Q.3]Create a Node JS Script that displays a message Welcome to
Node JS through loop,with delay in between the iteration. Using
setTimeOut{
Source Code:
for(var i=0; i<10; i++)
{
setTimeout(function()
{ console.log("Welcome to Node JS");},1000);
}
Output :
PS C:\Sudhir> node loop.js
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
Welcome to Node JS
k C:\Sudhir>
Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
Q.4]Create a user defined date module that can give you the
current date and time.
Source Code:
dmodule.js
// dmodule.js
// Function to get the current date and time function
getCurrentDateTime() { const
currentDate = new Date(); return
currentDate;
}
// Exporting the getCurrentDateTime function module.exports =
getCurrentDateTime; date.js
// date.js
// Importing the DateModule const getCurrentDateTime =
require('./dmodule');
// Getting the current date and time using the imported function const
currentDateAndTime = getCurrentDateTime();
// Displaying the current date and time console.log("Current Date and Time:",
currentDateAndTime);
Output :
k C:\Sudhir> node date.js
Current Date and Time: 2024-05-10T07:51:31.877Z k
C:\Sudhir>
Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
Q.5]Create a days Till custom module. It should be able to give
you the number of days till Christmas and the number of days.
number of days till your Birthday.
Source Code:
daysTill.js
// daysTill.js
function daysUntilChristmas() { const today = new Date();
const christmas = new Date(today.getFullYear(), 11, 25);
if (today.getMonth() === 11 && today.getDate()
> 25) {
christmas.setFullYear(christmas.getFullYear() + 1);
} const oneDay = 1000 * 60 *
60 * 24;
const daysLeft = Math.ceil((christmas - today) / oneDay); return daysLeft;
} function daysUntilMothersDay() {
const today = new Date(); const year
=
today.getFullYear(); // Find
the second Sunday in May
const mothersDay = new Date(year,
4, 1); while
(mothersDay.getDay() !== 0) {
mothersDay.setDate(mothersDay.getDate() + 1);
} mothersDay.setDate(mothersDay.getDate() + 7); const
oneDay = 1000 * 60 * 60 * 24; const daysLeft =
Math.ceil((mothersDay - today) / oneDay); return daysLeft;
} module.exports = { daysUntilChristmas,
daysUntilMothersDay
};
Custome.js
const daysTill = require('./daysTill');
Class:-MCA-II Name:- Shubham Balaso Deshmukh
Subject:- Advanced Web Technology Roll No: 150
const daysUntilChristmas = daysTill.daysUntilChristmas(); const
daysUntilMothersDay = daysTill.daysUntilMothersDay(); console.log(`Days until
Christmas: ${daysUntilChristmas}`); console.log(`Days until Mother's Day:
${daysUntilMothersDay}`);
Output :
k C:\Sudh> node custome.js Days
until Christmas: 229
Days until Mother's Day: 2