nodejs intro

13
Introduction to [email protected]

Upload: ndjido-ardo-bar

Post on 09-May-2015

486 views

Category:

Software


1 download

DESCRIPTION

A introduction to NODEJS

TRANSCRIPT

Page 1: Nodejs intro

Introduction to

[email protected]

Page 2: Nodejs intro

JavaScript created in 1995 by Brendan Eich (@Netscape labs) .

JS become a ECMA (European Computer Manufacturers Association) standard by 1997. It remains a standard in client-side app over years.

Google V8, chrome, Sep 2008

Node created in 2009 by Ryan Dahl using V8 power (c++ & javascript) : With Node JS start being as a server-side language.

History

Page 3: Nodejs intro

Highly scalable web servers for web applications.

Web services (RESTful API)Real-Time apps as its supports Web-Sockets.

App with queued inputs.Data streaming apps.

What is it good for?

Page 4: Nodejs intro

Who is using NodeJS ?

Page 5: Nodejs intro

Single-threaded execution with the use of callback functions => not resource-intensive◦ suitable for high latency I/O operations e.g: database access◦ Scalable as it combines Server and application logic in one

single place

Node execution modele

Page 6: Nodejs intro

Node is event-driven, it handles all requests asynchronously from on single thread.

A event loop is used to schedule tasks in the event-driven programming.

Node programming style is christened Continous-Passing Style (CPS): Every asynchronous function has at least a Callback function as one of its parameters. This later is call after the asynchronous section is executed.

Node programming modele

var fs = require("fs"); fs.readFile ("foo.txt", "utf8", function(error, data) {

if (error) { throw error; }

console.log(data);}); console.log("Reading file...");

Page 7: Nodejs intro

Using async module Using process.nextTick() trick:

How to write an asyn function?

function add (x, y, callback) {

setTimeout(function() {

callback(x + y);

},0);}add(2, 3, console.log);console.log("The sum is:" );

function add (x, y, callback) {

process.nextTick(function() {

callback(x + y);

});}add(2, 3, console.log);console.log("The sum is:" );

Page 8: Nodejs intro

Non-blocking has always to do with I/O (file system, database,…)

The following code is asyn but not Non-blocking:

Asyn is not Non-blocking

function add (x, y, callback) {

process.nextTick(function() {

while (1) {

callback(x + y);

}});

}add(2, 3, console.log);console.log("The sum is:" );

Page 9: Nodejs intro

Install NodeJS :

Demo: Install NodeJS!

http://nodejs.org/download/

Page 10: Nodejs intro

Demo: Hello word! Create a app.js file with the following code:

Start your server by issuing the following cmd:◦ > node app.js

Nb: NodeJS has got a REPL!

Page 11: Nodejs intro

NodeJS extensions are known as modules

NodeJS is being backed by an active community which provides an overwhelming number of modules

NPM command is used to install and manage new modules:◦ Ex:

npm install async npm install [email protected] npm install –g async npm serach <MODULE_NAME> npm outdated npm update npm –g npm rm asyn

«  require(<MODULE_NAME>)» is used to load a given module

Node Package Manager

Page 12: Nodejs intro

ExpressJS helps to fit web projects to MCV design pattern

ExpressJS a NodeJS web framework

To install ExpressJS:◦ > npm install –g express

Express has got a scafolding capability that help you create a sustainable project structure, easy to extend and maintain. Express-generator is the module dedicated to that job.◦ > npm install –g express-generator

Page 13: Nodejs intro

Find a ExpressJS app example on my GITHUB: ◦ https://github.com/ndjido/NodeJS_intro

Thanks for your attention!