NodeJS Assignments
NodeJS Assignments
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
console.log('Hello'.red);
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.
Math.JS
a) Create a NodeJS based script file, that provides implementation for �pwd�
command from �Node� shell.
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);
}
var fs = require('fs')
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.
b) Write a Node script file to display current Date & Time by using user defined
date module.
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!");
});
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.