Posts

Showing posts from September, 2017

Underscore Module

// UNDERSCORE MODULE // import underscore var _ = require('underscore'); // An array of many numbers var myArray = [12,32,7,2,445,21,2,3,87,34,32,544,23,523]; // let's find out numbers greater than 20 // and store them in results array var results = _.filter(myArray, function(item){     return item > 20; }); // display result console.log(results); var mySecondArray = [1,2,3,4,5,6,7,8,9]; var mySecondResults = _.reject(mySecondArray, function(item){     return (item % 2 != 0); }); // print second array console.log(mySecondResults);

setTimeout

setTimeout(function(){     console.log('Hello!'); }, 2000);  

setIntervalGlobal

// setInterval wit global update var x = 2; setInterval(function(){     // print number of apples     console.log('I have ' + x + ' apples right now.');     // increment by 1     x = x + 1;     console.log('I added another apple.');     // show result     console.log('Now I have ' + x + ' apples.'); }, 1000);

setInterval

// setInterval setInterval(function(){     var x = 1;     console.log('I have ' + x + ' apples.');     x = x + 1; }, 1000);

FS Module

// FS MODULE var fs = require('fs'); // write a file using fs var myText = 'This is a sample statement'; fs.writeFileSync('anything.txt', myText); // read a file fs.readFile('./anything.txt', function(err, data){     if(err){         console.error('Error is ' + err);     } else {         console.log(data.toString());     } }); // delete a file fs.unlink('./anything.txt', function(err){     if(err){         console.error('Error message: ' + err);     } else {         console.log('The file was successfully deleted.');     } });

Closure

// CLOSURE function outerFunction(){         // outer variable     var x = 20;     function innerFunction(someArgument){         // inner variable         var y = 10;         console.log('Outer variable is ' + x + ' and argument is ' + someArgument);     }     outerFunction.innerFunction = innerFunction; } // call the outer function outerFunction.innerFunction(50);

Lesson 3 Chat

Today we'll do a little more on FS module and then move on to External Node Modules Are you both here??? Ok... lets begin... So where exactly to begin.. In the last session we saw how to read a file using FS, today we'll do how to: Read, Write, Delete those files ... Lets start.. console.log are used usually to show messages or information... console.error(); specially used for showing errors.. of course we can also use console.log for errors but that's not the standard way... These two lines are enough to write to a file. The first argument is the filename and the second one is the content of the file we want to write.. Let's run it... Note that right now, we have only one file in the "Lesson 3" folder... After writing, there'll be one more file with the name anything.txt NOTE: In Windows, the command is "node filename" In Linux, it is: "nodejs filename" Good question renu! the anything.txt file got auto-created.... Let's try to d...

Lesson 2 Chat

Today we'll do ...  well at least these topics: 1) Functions .......................... DONE 2) File Based Modules 3) Internal Modules 4) External Node Modules First of all, what is a module? A module is like a .... header file in C/C++, in Node.js. A module can have multiple functions, states, properties, definations and a lot more... Before we dive into modules, lets do some basics first (this will help us in understanding modules better)... ------------------------------- OBJECTS ---------------------------- An object in Node.js has some "key" and "value" pairs. It is very different from objects in C++/Java (where an object is basically an implementation of class....where we say class is a blueprint of an object). Here it is a bit different because the objects here don't follow all the OOPs properties... Lets do a quick example So, that is how you declare an object. We just declared an object in Node. It is  really simple... We c...

OS Module

// import node module var os = require ( 'os' ); var gigabyte = 1 / Math . pow ( 1024 , 3 ); // show memory information of the laptop console . log ( 'Total RAM / memory: ' + os. totalmem () * gigabyte + ' GBs' ); console . log ( 'Free RAM / memory: ' + os. freemem () * gigabyte + ' GBs' ); console . log ( 'Consumed precentage: ' + ((os. totalmem () - os. freemem ()) / os. totalmem ()) * 100 ); // show CPU information console . log ( 'This laptop has ' + os. cpus ().length + ' CPUs.' );

FS Module

// THE FILE SYSTEM MODULE OR THE FS MODULE // IMPORT FS NODE MODULE var fs = require ( 'fs' ); // Read data from a text file fs. readFile ( './anything.txt' , function ( err , data ){ if (err){ console . error (err); } else { console . log (data. toString ()); } });

Functions

// A function can be defined in 3 ways // First way of defining a function // Function Declaration var mySum = function ( x , y ){ var z = x + y; console . log ( 'Sum of ' + x + ' and ' + y + ' is ' + z); } // Second way of declaring function: This is the arrow function var mySum2 = ( a , b ) => { var c = a + b; console . log ( 'Sum is ' + c); } // Third way of declaring a function function mySum3 ( m , n ) { var o = m + n; console . log ( 'Sum of numbers is ' + o); } // // Function call 1 // mySum(120,240); // // Function call 2 // mySum2(234,16); // // Function call 3 // mySum3(100,200); // EXPORTING MODULES module . exports = { mySum: mySum, mySum2: mySum2, mySum3: mySum3 }

Object

// Declaring an object var myObject = { name: 'Gautam' , age: 21 , email: 'gautamkabiraj@live.com' , hometown: 'kolkata' , interest : function (){ console . log ( 'I love Node.js!' ); }, sumMe : function ( a , b ){ var c = a + b; console . log ( 'Sum is ' + c); } }; // accessing object values console . log ( 'My name is ' + myObject.name); console . log ( 'My age is ' + myObject.age); myObject. interest (); myObject. sumMe ( 12 , 8 );s

Variables

var name = 'Gautam' ; var age = 21 ; var gender = 'Male' ; var indian = true ; var favoriteNumber = 12.56 ; console . log ( 'My name is ' + name ); console . log ( 'My age is ' + age ); console . log ( 'My gender is ' + gender ); console . log ( 'Am I indian? : ' + indian ); console . log ( 'My favorite number is ' + favoriteNumber );

Arrays

// Arrays var myArray = []; // adding an element to the array myArray . push ( 1 ); // print the array: console . log ( myArray ); // add another element myArray . push ( 5 ); // print array console . log ( myArray ); // pop an element myArray . pop ( 1 );

Lesson 1 Chat

Shall we start then? Today we're going to start Node.js. It's a big topic...I mean really big. Every programming language has some limit (for header files / libraries etc)... there's no limit in Node. This is because it keeps adding new features EVERY SINGLE DAY... So, let's begin with exploring what it is, where it is used and why we are learning it? I've created  a blog at address: learningnode.blogspot.com Here, I'll post all the topics that we covered today. Soon I'll make this blog private which will accept only your Email IDs and can see the blog... Are you able to follow me? Renu???? I think connection issue... Nope, BootStrap isn't over. We'll continue that while using Node.js....in combined form. I'll explain the remaining doubts in different session... And...where is renu now? :/ don't know make a call to her Okay.. let me... Actually anything is good for future purpose if you're good at it..... ...