Node Js
Create a custom server using http module and explore the other modules of Node JS like OS, path, event.
File 1: os.js
1const os=require('os');
2console.log('Data retrieved by Using OS Module in Node Js\n');
3console.log('Hostname:'+os.hostname()+'\n');
4console.log('Architecture:'+os.arch()+'\n');
5console.log('OS Platform:'+os.platform()+'\n');
6console.log('OS Type'+os.type()+'\n');
7var freeMemory=os.freemem();
8console.log('Free RAM:'+Math.round(freeMemory/1024/1024/1024)+'GB\n');
9var totalMemory=os.totalmem();
10console.log('Total RAM:'+Math.round(totalMemory/1024/1024/1024)+'GB\n');
File 2: path.js
1const path = require('path');
2console.log('File Name: \n'+__filename+'\n\n');
3console.log('Directory Name: \n'+__dirname+'\n\n');
4
5console.log('Base Name of File Name: \n'+path.basename(__filename)+'\n\n');
6console.log('Base Name of Directory Name:\n'+path.basename(__dirname)+'\n\n');
7var data = path.parse(__filename);
8console.log('Parser Data: \n');
9console.log(data);
10console.log('\n\n');
11console.log('isAbsolute for __filename: '+path.isAbsolute(__filename)+'\n');
12console.log('isAbsolute for given relative file : '+path.isAbsolute('./os.js')+'\n');
13console.log(path.join("MGIT","ET","CSM and CSD"));
14console.log(path.join("/MGIT","ET","CSM and CSD"));
15console.log(path.join("/MGIT","//ET","CSM and CSD"));
16console.log(path.join("/MGIT","//ET","../CSM and CSD"));
17console.log(path.join(__dirname,"CSM and CSD"));
File 3: event.js
1var fs = require('fs');
2var rs = fs.createReadStream('./myfile.txt');
3rs.on('open', function () {
4console.log('The file is open');
5});
6var events = require('events');
7var eventEmitter = new events.EventEmitter();
8//Create an event handler:
9var myEventHandler = function () {
10console.log('Welcome to Node Js Lab...!');
11}
12//Assign the event handler to an event:
13eventEmitter.on('welcome', myEventHandler);
14//Fire the 'welcome' event:
15eventEmitter.emit('welcome');
for output:
install Nodejs on your system. create myfile.txt for execution on terminal: node filename.js