Sunday, June 4, 2017

NodeJS Practical Usage 1

Install NodeJS command prompt
Type node -> JS engine -> EG: 1+1 -> yields 2
ctrl c twice to exit

Editors
Visual studio code - good debugger -> step over, step into
Atom

MODULES
Reusable block of code does not impact other code

First class functions -> Use like variables -> functions are objects

//function statement
function calculate(){
  var a=1;
  var b=2;
  var c=a+b;
  console.log(c);
}

calculate();

//functions are first class
function logCalc(calculate){
  calculate();
}

logCalc(calculate);

Expression -> Block of code that results in a value

//function expression and its first class
var calcAll = function() {
  console.log('Hi expression');
}

calcAll();

Function on the fly


NodeJS Basics

Open-source
Cross-platform
Server-side Javascript runtime environment
Operates in single thread
Non-blocking I/O calls
Uses libuv - for asynchronous I/O 

Command Line Interface - pass command and arguments
Windows powershell - for linux and mac users

V8 - Javascript Engine - Written in C++ - NodeJS code to machine language - built by google
NodeJS is written in C++ - embedded V8 in to it
Visual Studio Code - C++ editor
Ecmascript - Standard by which JavaScript is based on

Node Core : Browser (JavaScript) -> HTTP Request/Response -> Server (JavaScript)

Javascript in Server:
Resusable code
Dealing with files
Deal with database
Communicate over internet / process information

C++ Core and JavaScript Core

Open-source
Cross-platform
Server-side Javascript runtime environment
Operates in single thread
Non-blocking I/O calls -> Uses libuv - for asynchronous I/O 

Advantages of JavaScript Closure on functions -> Data Encapsulation -> State do not change

Module - reusable JS code
Require -> gets the code to other file
 -> folder with index.js will srever as a module and we can access the exported modules in the place where we declare require(folder) 
 ->read JSON directly in to java script object
-> module.export.attribute -> can also be inherited by the base file
-> a lot of object creation pattern can be used
-> variable = means a new variable VS module.variable points to variable reference
-> can be used to use core modules in NodeJS webiste
->ES6 Ecma Script 8 uses export module and import * as moduleVar from modulename

On the fly u can create/pass function as parameter 

module.exports -> functionNameVariable

NameValue Pair
                     Street: ''Gate Pkwy'
                     person['firstname']

Function Constructors -> FunctionName.variable will give the value
Function Prototype -> FunctionName.prototype.protypename - function(){} -> prototype will apply to all objects of the original functionName
Advantages -> get access to the prototype to all down the line objects (Inheritance)

Pass by Value -> primitive type and Pass by reference -> object

Immediately invoke variable and functions
"String".use String functions
(function(){}); just enclose inside ()
helps in Encapsulating data -> do not inherit any other variables from outside


Events -> responding to something happened -> emitter -> listener for event is a function -> functionname.prototype.on('greet', function1 (add as many function as u want)) and when we call the function with 'greet' all the functions will be executed.
NodeEvents -> var emitterVar = new Emitter() and u can add as many functions to the emitter as u want and then make that event happen to run all the functions that the event has.

V8 engine supports this,
Template Literal -> `Hello ${JSVariable}' (grave symbol)
'use strict'; -> shows error upfront if we develop production code

Callback -> function calls the event back after completing asynchronously -> they are used as JavaScript is synchronous with system
Libuv -> deals with OS events like open a file or download a file, maintains a queue

Node lets us do javascript synchronously

Deal with binary data in JS-> new Buffer('Hello', 'utf8'),
new ArrayBuffer(size), Int32Array(buffer)

Functions are like variables in JS

Reading File -> fs.readFileSync(__dirPath+ '/greet.txt', 'utf8')
fs.readFile(__dirPath+ '/greet.txt', 'utf8', callbackFunction(err, data){use data here})
Error First Call back -> helps in structuring functions for Asynchronous functions
Streams -> chunks of data -> fs,createReadStream(), fs.createWriteStream()
Pipe -> connecting streams -> stream1.pipe(stream2)
Method chaining -> method returns an object we will call another method

****Skipped Web Servers

NPM -> Node Package Manager