NodeJS

Crash course 

01

PAY Namibia 2018

 

Intro

Meet and greet
Quick recap of Javascript
NodeJS, npm
Firebase

Meet and greet

 

Hi, my name is Dušan. I live in Berlin, but I'm originally from Serbia.
I do programming for fun and profit and like to teach people.

Javascript cheatsheeet

Variables and types

let

const

- define a variable with block scope

- define a constant variable, cannot be changed after declaration

Javascript is dynamically typed

- variables can be a value of any type

let x = 5; // here value of the x is of type number
x = "some text"; // here value of the x is of type string

const unchangeable = "this cannot be changed";

unchangeable = "some other text";
//Throws "TypeError: Assignment to constant variable."

Javascript cheatsheeet

Conditionals and Loops

Conditionals                                                    Loops

if/else, switch                                                for, while 

let x = 5;

if(x < 6){
    console.log("smaller");
} else {
    console.log("larger or equal");
}

switch(x){
    case 5: {
        console.log("equals");
        break;
    }
    default: {
        console.log("equals....not");
    }
}
const arr = [1, 3, 5, 7, 9];

for(let i=0; i < arr.length; i++){
    console.log(arr[i]);
}

let cnt=0;

while(cnt < arr.length){
    console.log(arr[cnt]);
    cnt++;
}

map, reduce, filter, forEach, etc...

There are other way to do stuff using:

Javascript cheatsheeet

Functions

First class citizens

- they are objects, can be assigned to a variable,     passed to another function...

function sum(a,b){
    return a + b;
}

const sum2 = sum;
sum2(5,10);

//as callback
function transform(x, callback){
    return callback(x+2);
}

//can be anonymous (arrow function)
transform(7, x => { return Math.sqrt(x)*10; });

NodeJS

- Javascript runtime for network/server application

- Written on top of V8 (Chrome's JS engine)

- Event-driven architecture & non-blocking I/O

NodeJS

Setup
https://nodejs.org/en/download/

Hello World

const  http = require('http');

http.createServer((req, res)=>{
  res.end('Hello World');
}).listen(3333);



// - Save file on FS as filename.js
// - In terminal navigate to the folder where 
//   the file is and type: node filename.js
// - Start your browser and type localhost:3333

Modules 

//main.js
const  http = require('http');
const circle = require('./circle.js');

http.createServer((req, res)=>{
  const area = circle.area(4);
  res.end(area.toString());
}).listen(3333);
//circle.js
const { PI } = Math;

exports.area = function (r) {
    return 2*r*PI;
} 

Task:    Add function for circumference and print it bellow area

3rd party modules

- Tool for managing packages

- Public repository of libraries 

3rd party modules

- Initialize project folder with npm init

- Search npmjs.org for desired dependency

- In project folder run npm i packageName --save

- Import in your files according to the documentation

const _ = require('lodash');

const numList = [1, 2, 3];
 
const reversed = _.reverse(array);

console.log(reversed);

3rd party modules

- Initialize project folder with npm init

- Search npmjs.org for desired dependency

- In project folder run npm i packageName --save

- Import in your files according to the documentation

const _ = require('lodash');

const numList = [1, 2, 3];
 
const reversed = _.reverse(array);

console.log(reversed);

Process variable

- Global variable providing information about current process

// process.env -> info about environment, e.g. os, arch type, pwd
// process.argv -> array of arguments passed to the nodejs program
//full list here: https://nodejs.org/api/process.html

//main.js
const a = process.argv[2];
const b = process.argv[3];

//cast to Number becuase input is string
console.log(Number(a)+Number(b));

//run in node main.js 3 5

//Outputs: 8

It's time for coding

Create module which expose functions for calculating area and circumference for ellipse (inputs as program params)

Using readline and crypto packages, prompt user to insert a string and then hash it.  Prompt user until specific keyword is entered (e.g. EXIT).

https://nodejs.org/api/readline.html

https://nodejs.org/api/crypto.html

Made with Slides.com