Page 1

ndjidoardo.bar@hypercube-research.com


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 clientside app over years.

Google V8, chrome, Sep 2008

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


 Highly scalable web servers for web

applications.  Web services (RESTful API)  Real-Time appsas its supports Web-Sockets.  App with queued inputs.  Data streaming apps.


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


 

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 oneof its parameters. This later is call afterthe asynchronous section is executed. var fs = require("fs"); fs.readFile ("foo.txt", "utf8", function(error, data) { if (error) { throw error; } console.log(data); }); console.log("Reading file...");


 

Using async module Using process.nextTick() trick:

function add (x, y, callback) {

function add (x, y, callback) { process.nextTick(function() {

setTimeout(function() { callback(x + y); callback(x + y); },0); } add(2, 3, console.log); console.log("The sum is:" );

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


Non-blocking has always to do with I/O (file database,…) system,  The following code is asyn but 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:" );


ď ˝

Install NodeJS :

http://nodejs.org/download/


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!


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 npm npm npm npm npm npm

install async install async@1.0.x install –g async serach <MODULE_NAME> outdated update npm –g rm asyn

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


ExpressJS helps to fit web projects to MCV design pattern

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


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

What is Node JS ?  

In this tutorial, we will learn about Express.js, Install Express via the Node Package Manager and Create Sample Web server using express.js...

What is Node JS ?  

In this tutorial, we will learn about Express.js, Install Express via the Node Package Manager and Create Sample Web server using express.js...

Advertisement