You are on page 1of 5

Topic 1: Introduction to Node JS: Assignment 1:

a) Create a Node JS Script file that displays hostname & platform details of
current system on the console.(Hint : Use OS module)

var os = require('os');
console.log("Platform of OS: " + os.platform());
console.log("Hostname of OS: " + os.hostname());

b) Create a Node JS script file that displays �Hello� text in red color and
�Welcome to Node JS� text in rainbow colors on the console. (Hint: Use Colors
module)

$ npm init
$ npm install colors

var colors = require('colors');

console.log('Hello'.red);

console.log('Welcome to Node JS'.rainbow);

c) Create a user defined module named �Math� with four functions Addition,
Subtraction, Multiplication, Division and export them.
Import Math module form other Node JS Script file and invoke all the four
functions to perform operations on given input.

$ npm install Math

Math.JS

//Addition of Two Numbers


exports.add = function(a,b)
{
return a+b;
};
//Subtraction of two Numbers
exports.subtract = function(a,b)
{
reutn a-b;
};
//Multiplication of two Numbers
exports.multiply = function(a,b)
{
reutn a*b;
};
//Division of two Numbers
exports.Divide = function(a,b)
{
reutn a/b;
};

var Math= require('Math');


var a=20,b=50;
console.log("Addition:- " +Math.add(a,b));
console.log("Subtraction:- " +Math.subtract(a,b));
console.log("Multiplication:- " +Math.multiply(a,b));
console.log("Division:- " +Math.Division(a,b));
d) Create a Node JS Script that displays a message � Welcome to Node JS� through
loop, with delay in between the iterations i. Using setTimeOut()

for(var i = 0; i < 10; i++){


setTimeout(function(){
console.log('Welcome to Node JS');
},1000);
}

a) Create a NodeJS based script file, that provides implementation for �pwd�
command from �Node� shell.

var sys = require('Sys')


var exec = require('Child-Process').exec;
var child;

child = exec("pwd:, function(error, stdout, stderr)


{
sys.print('stdout:-' +stdout);
sys.print(stderr:-' +stderr);
if(error!== Null)
{
console.log('exec error:' +error);
}
});

b) Create a NodeJS based script file,that reads the name of the directory from the
command line arguments and displays
the list of directory contents (using fs module)
var fs = require("fs");

if(process.argv.length <=2)
{
console.log("Usage: +_filename +"Path/to/directory");
process.exit(-1);
var path = process.argv[2];
fs.readdir(path,function(err,items)
{
console.log(items);
fot(var i=0; i<items.length; i++)
{
contole.log(items[i]);
}
});

c) Create a Node JS script that reads the file name from console and displays the
contents of the file

i. Synchronous mode

var fs = require('fs');
try
{
var data = fs.readFileSync('KKnode_file.txt','utf8');
console.log(data);
}
catch(e)
{
console.log('Error:',e.stack);
}

ii. Asynchronous mode

var fs = require('fs')

fs.readFile('KKnode_file.txt', 'utf8',function(error, data)


{
if(error)
{
console.log(error:-' +error);
throw error;
}
console.log(data);
});

d) Create a NodeJS based script file, that reads the names of the 2 files from the
user (Use process module; On stdin �data� event
by using call-back accept the input from the user) and reads the content of first
file by using Read Stream API and writes in into second
file by using Write Stream API. If second file is available it should append the
content. If not it should create a new file and add the content to it.

var fs = require('fs');
spath ='./file1.txt';
dpath ='./file2.txt';
try
{
if(fs.existsSync(dpath))
{
var data = fs.readFileSync('file1.txt','utf8');
console.log(data);
fs.appendFile('file2.txt', data, (err)=>
{
if(error)
console.log(err);
console.log('successfully written');
}
}
}
catch(error)
{
console.log(err);
}

Assignment 3 :

a) Create a user defined date module that can give you the current date and time.

var http = require('http');


var dt = require('./mydatemodule');
http.createserver(function(req, res)
{
res.writeHead(200, { 'Content_type': 'text/Html'});
res.Write("Currently the Data & Time are : '+dt.mydatetime());
res.end();
}).listen(8080);

b) Write a Node script file to display current Date & Time by using user defined
date module.

var datetime = new Data();


confole.log(datetime);

c) Write a Node script file to find out how many seconds are there in a year. How
many seconds are
there in a century and writes the result into a file.

var fs = require('fs');
file_path=' ./file1.txt'
try
{
if(fs.exitsSync(file_path))
{
var d = new Date("01/01/2019") - new Data("01/01/2018") //for One Year
console.log(d);
fs.writeFile("file1.txt",d,function(err)
{
if(err)
throw err;
console.log("Salved!");
});

var d = new Date("01/01/2019") - new Date("01/01/1918") // for Century


console.log(d);

fs.appendFile('file1.txt",d,function(err)
{
if(err)
throw err;
console.log('Saved!');
});

}
}

d) Create a daysTill custom module. It should be able to give you the number of
days till Christmas and the
number of days till mother�s day. number of days till your Birthday.

(Hint : Subtract both the dates to get difference in no.of milliseconds)

var Daytill = reuire(./DaysTillmodule');

var oneDay = 24*60*60*1000;


var CurrDate = new Date (2019,03,01);
var ChirstmasDate = new Date (2019,12,25);
var MothersdayDate = new Date (2019,05,12);
var BirthDayDate = new Date (2019,06,01);
var Diffdays_Christmas = Math.round(Math.abs((CurrDate.getTime() -
ChirstmasDate.getTime()) / (oneDay)))

console.log('Number of Days till Christmas' +Diffdays_Christmas);

var Diffdays_Mothersday = Math.round(Math.abs((CurrDate.getTime() -


MothersdayDate.getTime()) / (oneDay)))

console.log('Number of Days till Mother's Day' +Diffdays_Mothersday);

var Diffdays_BirthDayDate = Math.round(Math.abs((CurrDate.getTime() -


BirthDayDate.getTime()) / (oneDay)))

console.log('Number of Days till my Birthday Date' +Diffdays_BirthDayDate);

You might also like