You are on page 1of 24

NodeJS

Copyright 2017 © Fortech. All rights reserved.


Before….

• Scope
– Vizibilitatea unei functii sau variabile in diferite parti ale codului in timpul executiei
– Local and global scope
var name = 'Todd';
var scope1 = function () {
// name is available here
var scope2 = function () {
// name is available here too
var scope3 = function () {
// name is also available here!
};
};
};

Copyright 2017 © Fortech. All rights reserved.


Before….

• Callback
– “A reference to executable code, or a piece of executable code, that is passed
as an argument to other code.”
//exemplu de cod
$('#element').fadeIn('slow', function() {
// callback function
});
//alt exemplu
var callback = function () { console.log(‘123’) };
setTimeout ( callback, 1000 );

Copyright 2017 © Fortech. All rights reserved.


Before….

• Other important concepts

– Closures

– Hoisting

– Prototype

Copyright 2017 © Fortech. All rights reserved.


Before….

Exercitiu:

Scrieti o functie care sa primeasca ca si parametri un text si un numar

Exemplu : functie ( “test to output”, 5) aceasta trebuie sa tipareasca ( console.log ) la fiecare


5 secunde textul
“Text To Output at 5 seconds”
“Text To Output at 10 seconds”
“Text To Output at 15 seconds”

Bonus: Dupa 13 secunde, de la lansarea in executie, indifferent de intervalul de timp, functia


sa nu mai tipareasca acel text.

Copyright 2017 © Fortech. All rights reserved.


Background

• Nodejs foloseste V8
• V8 Javascript engine, este creat si optimizat de Google
• A fost creat the Ryan Dahl in 2009
• Open Source

Copyright 2017 © Fortech. All rights reserved.


Introducere

• Server side Javascript


• Command tool
• Single-thread

“Node.js is a platform built on Chrome’s JavaScript runtime for easily building


fast, scalable network applications. Node.js uses an event-driven,non-blocking
I/O model that makes it lightweight and efficient, perfect for data-intensive
real-time applications that run across distributed devices”

Copyright 2017 © Fortech. All rights reserved.


Non-Blocking I/O

• Traditional I/O

result = db.query("select count(*) from MY_SUPER_BIG_TABLE")


doSomethingWithTheResult(result);//wait until the result
doSomethingWithoutRsult();//execution block

• Non-blocking I/O
db.query("select count(*) from MY_SUPER_BIG_TABLE", function (result) {
doSomethingWithTheResult(result);//wait until the result
});
doSomethingWithoutRsult();//execute without delay

Copyright 2017 © Fortech. All rights reserved.


Instalare si …..

• Instalare
– Download from : https://nodejs.org

• Run command: Node


• Hello World
– Create a helloworld.js
• console.log("Hello World");
– Run: node helloworld.js

Copyright 2017 © Fortech. All rights reserved.


Ce putem face cu NodeJs

• Server HTTP
• Server TCP
• Static file server
• Web Chat Application

Copyright 2017 © Fortech. All rights reserved.


Exemplu: Building a simple HTTP Server
One of the hardest things in NodeJS

Copyright 2017 © Fortech. All rights reserved.


Exemplu: Building a simple HTTP Server
Static File Read

Copyright 2017 © Fortech. All rights reserved.


Modules NodeJS

• Are un system simplu. Fiecare modul este un fisier.


• Creare de modul custom
– creare fisier
– Module.export = {
• myFunction: function (param ) {//doSomething}
– }
• Folosire: mymodule.myFunction (param )

Copyright 2017 © Fortech. All rights reserved.


• Node Package Manager
• https://www.npmjs.com/
• Module Populare

Express Underscore
Restify Mongoose
Jade Moment
Socket.io Grunt
Redis
Nodemon

Copyright 2017 © Fortech. All rights reserved.


• Commands
– npm init
– npm install
– npm run

Copyright 2017 © Fortech. All rights reserved.


Express Framework

• nodeJS Web Framework


• Inspirat din SINATRA ( Ruby )
• Are functionalitati pe care le poti gasi in orice web server
• https://expressjs.com/

Copyright 2017 © Fortech. All rights reserved.


Express Install

– npm install express --save

Copyright 2017 © Fortech. All rights reserved.


Express and

Copyright 2017 © Fortech. All rights reserved.


Express Routing

• - Routing – cum o aplicatie treebuie sa raspunda unei cereri, care e formata din path( url) si
din tipul cerere ( GET, POST )
– Exemplu get
app.get('/', function (req, res) {
res.send('Hello World!')
});
– Exemplu delete
app.delete('/', function (req, res) {
res.send('Hello World!')
});

Copyright 2017 © Fortech. All rights reserved.


Express Routing - Advance

app.route('/book')
.get(function (req, res) {
res.send('Get a random book')
})
.post(function (req, res) {
res.send('Add a book')
})
.put(function (req, res) {
res.send('Update the book')
})

Copyright 2017 © Fortech. All rights reserved.


Middlewares

• Functii care sunt executate de Express Routing


• Exemplu

app.get('/', function (req, res, next) {


if ( !UserAuthentificate() ) {
res.send('Hello Bad User ');
} else {
next();
}
}, function (req, res) {
res.send('Hello Good User');
});

Copyright 2017 © Fortech. All rights reserved.


Middlewares

• Functii care sunt executate de Express Routing


• Exemplu

app.get('/', function (req, res, next) {


if ( !UserAuthentificate() ) {
res.send('Hello Bad User ');
} else {
next();
}
}, function (req, res) {
res.send('Hello Good User');
});

Copyright 2017 © Fortech. All rights reserved.


………

Copyright 2017 © Fortech. All rights reserved.


Mini Aplicatie

• API Server – POST MANAGEMENT ( id, titlu, body )


– Sa aveti un middleware care salveaza in loguri informatii despre accesare API
– pe ruta ‘/’ server fisierul index.html
– pe ruta ‘api’ facem operatii crud ( raspuns JSON )
• api/
• posts - GET - return all posts
• posts/:id - GET - return one item
• posts - POST - insert one item
• posts/:id - PUT - update one item
• delete/:id - DELETE - delete one item
– Vom folosi modulele
• Express
• Nodemon

Copyright 2017 © Fortech. All rights reserved.

You might also like