Mayuri
Mayuri
Q. Introduction to Node.js
a) Create a node.js script file that displays Hostname & Platform details of current
system on the console[Hint: Use OS module]
Source Code:
Var os = require('os');
console.log("Platform of os:"+os.platform());
console.log("Hostname of os "+os.hostname());
OUTPUT:
b) Create a user defined mode Math with four functions Addition, Subtraction,
Multiplication,
Division & 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
if (b === 0) {
throw new Error("Division by zero is not allowed");
}
return a / b;
}
// Exporting
functions
module.exports = {
addition,
subtraction,
multiplicati
on,division
};
New.js
// app.js
C:\Users\DYPATU>no
de New.jsAddition: 15
Subtraction: 5
Multiplication: 50
Division: 2
d) Create a user defined data module that can give you the current date and time.
Source Code:
dmodule.js
// dmodule.js
// date.js
// Getting the current date and time using the imported functionconst
currentDateAndTime = getCurrentDateTime();
Output :
C:\Users\DYP-ATU>
Class:MCA ll Name: Mayuri Pandurang Patil
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
Output :
C:\Users\DYP-ATU>node custome.js
Days until Christmas: 229
Days until Mother's Day: 2