KEMBAR78
Exploring Node.jS
Exploring Node.JS 
Dr. Jayaraj Poroor 
DependSoft Consulting 
http://dependsoft.com
About DependSoft
My ongoing R&D work: IntegralJ 
http://integralj.org (open source)
My recent products 
● Virtual Private Transport 
o Keyhole approach to secure remote service access. 
o http://shelloid.com 
o Node.js, Netty, Redis, MySQL, Google protobuf. 
o Basic engine open sourced. 
● Sensoid IoT platform 
o Distributed stream query platform. 
o Node.js, Redis, InfluxDB, ElasticSearch, MySQL.
Server-side technology requirements 
● Performance 
o Need optimized execution 
● Concurrency 
o Need to support many concurrent client requests 
● Library support 
o e.g., database interfacing. 
● Framework support 
o For rapid application development.
Node.JS 
● Performance 
o Google’s V8 VM with native JIT compilation 
● Concurrency 
o Asynchronous I/O (libuv) + clustering 
● Library support 
o 39K projects in Github 
● Framework support 
o e.g., Express.JS
Synchronous vs Asynchronous I/O 
● Synchronous I/O 
o Thread blocks till I/O request is complete. 
o e.g., $result = mysql_query(“....”); //PHP 
● Asynchronous I/O 
o Thread does not block 
o Uses callback mechanism to notify completion 
o e.g., conn.query(“...”, function(err, rows) 
{ } );
Node.JS Threading model 
1. Single computational thread 
o Non-blocking, interleaved request processing 
o Background worker threads for I/O processing 
1. Clustering to utilize multi-core machines 
2. No shared memory between cluster processes.
Node.JS : When to use 
● Great for 
o I/O-centric applications 
 e.g., DB queries, File I/O , network I/O, invocation of 
other web services. 
o Real-time communication 
 WebSockets, HTTP long polling 
● Not so great for 
o Compute-centric applications 
 e.g., High-end machine learning backend
Event Loop Illustrated 
http://www.geekgirl.io/understanding-the-event-loop-in-nodejs/
Anatomy of a “hello world” Node.js 
var http = require('http'); 
var server = 
http.createServer(function (req, res) { 
res.writeHead(200, 
{'Content-Type': 'text/html'}); 
res.end('<h1>Hello World</h1>'); 
}); 
server.listen(3000); 
No separate HTTP 
engine like Apache 
required. 
Command: 
node app.js
Serving a file 
Buffers the entire file in memory. 
●Inefficient for large files. 
●Client needs to wait till entire file is 
read. 
Code source: https://github.com/substack/stream-handbook
Node.js Streams 
● Data served in 
chunks. 
● Data event fires 
whenever a new 
chunk is ready. 
Code source: Node.js in Action
Streams can be piped! 
File stream (input) is piped to the 
response stream (output). 
The response will employ 
chunked Transfer-Encoding. 
Code source: https://github.com/substack/stream-handbook
Package management 
● npm (Node Package Manager) 
o Provides simple & powerful package management. 
o Reads module dependencies from package.json 
o Typical usage: npm install or npm update. 
o Can store all dependent modules locally or globally
A sample package.json 
{ 
"name": "SomeApp", 
"description": "SomeApp Web Application", 
"version": "0.0.1", 
"private": true, 
"dependencies": { 
"express": "3.6.0", 
"connect": "2.15.0", 
"mysql": "*", 
} 
}
Secure HTTP with Node.js 
Code source: Node.js in Action 
Key and certificate files 
provided. 
Use gpg, openssl etc. to 
generate key and CSR.
Connect framework: Modular web apps 
Source: Node.js in Action 
Install: 
npm install connect
Connect: usage 
Create a Connect ‘app’. 
●Will store all middleware. 
●Itself just a function. 
Create a middleware ‘stack’. 
Requests will execute 
middleware functions till 
‘next()’ is not called or end of 
stack is reached. 
Code source: https://www.npmjs.org/package/connect
Connect: usage (2) 
● Middleware can be 
mounted on specific URL 
endpoints. 
● Does basic routing. 
● Error handler 
middleware. 
OR 
Create Connect-enabled 
server. 
Code source: https://www.npmjs.org/package/connect
Example Connect middlewares
Serving static files 
Static middleware 
configured with the 
folder from which 
files are served.
Express: Lightweight web framework 
Routing 
Code Source: https://www.npmjs.org/package/express
Bootstrapping Express application 
express – e output
Rendering views with Express 
Code Source: Node.js in Action 
Express configured 
with the views 
folder. 
View engine set as 
ejs. 
Looks up index.ejs 
in the views folder.
View lookup 
Source: Node.js in Action 
Source: Node.js in Action
Passing data to views 
Data passed to the 
view. 
photos is an array.
An example view 
title 
variable 
accessed. 
photos array 
accessed here.
Database connectivity 
npm install mysql 
var mysql = require('mysql'); 
var connection = mysql.createConnection({ 
host : 'localhost', 
user : 'me', 
password : 'secret' 
}); 
connection.connect(); 
connection.query('...', function(err, rows, fields) { 
if (err) throw err; 
connection.end(); 
}); 
https://github.com/felixge/node-mysql 
npms 
available for 
virtually any 
SQL/NoSQL 
database! 
Query completes 
only when the 
callback is invoked!
Authentication 
● passport.js 
npm install passport 
o Authentication middleware for node.js. 
o 140+ authentication strategies. 
o Supports OpenID and OAuth 
o http://passportjs.org 
o Session data can be serialized into a store like Redis.
Read more 
● Node.js in Action 
● http://expressjs.com 
● http://www.nodebeginner.org/
Thank You 
Dr. Jayaraj Poroor 
Founder, DependSoft Consulting 
Peace of mind with dependable software. 
jayaraj@dependsoft.com 
http://dependsoft.com

Exploring Node.jS

  • 1.
    Exploring Node.JS Dr.Jayaraj Poroor DependSoft Consulting http://dependsoft.com
  • 2.
  • 3.
    My ongoing R&Dwork: IntegralJ http://integralj.org (open source)
  • 4.
    My recent products ● Virtual Private Transport o Keyhole approach to secure remote service access. o http://shelloid.com o Node.js, Netty, Redis, MySQL, Google protobuf. o Basic engine open sourced. ● Sensoid IoT platform o Distributed stream query platform. o Node.js, Redis, InfluxDB, ElasticSearch, MySQL.
  • 5.
    Server-side technology requirements ● Performance o Need optimized execution ● Concurrency o Need to support many concurrent client requests ● Library support o e.g., database interfacing. ● Framework support o For rapid application development.
  • 6.
    Node.JS ● Performance o Google’s V8 VM with native JIT compilation ● Concurrency o Asynchronous I/O (libuv) + clustering ● Library support o 39K projects in Github ● Framework support o e.g., Express.JS
  • 7.
    Synchronous vs AsynchronousI/O ● Synchronous I/O o Thread blocks till I/O request is complete. o e.g., $result = mysql_query(“....”); //PHP ● Asynchronous I/O o Thread does not block o Uses callback mechanism to notify completion o e.g., conn.query(“...”, function(err, rows) { } );
  • 8.
    Node.JS Threading model 1. Single computational thread o Non-blocking, interleaved request processing o Background worker threads for I/O processing 1. Clustering to utilize multi-core machines 2. No shared memory between cluster processes.
  • 9.
    Node.JS : Whento use ● Great for o I/O-centric applications  e.g., DB queries, File I/O , network I/O, invocation of other web services. o Real-time communication  WebSockets, HTTP long polling ● Not so great for o Compute-centric applications  e.g., High-end machine learning backend
  • 10.
    Event Loop Illustrated http://www.geekgirl.io/understanding-the-event-loop-in-nodejs/
  • 11.
    Anatomy of a“hello world” Node.js var http = require('http'); var server = http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('<h1>Hello World</h1>'); }); server.listen(3000); No separate HTTP engine like Apache required. Command: node app.js
  • 12.
    Serving a file Buffers the entire file in memory. ●Inefficient for large files. ●Client needs to wait till entire file is read. Code source: https://github.com/substack/stream-handbook
  • 13.
    Node.js Streams ●Data served in chunks. ● Data event fires whenever a new chunk is ready. Code source: Node.js in Action
  • 14.
    Streams can bepiped! File stream (input) is piped to the response stream (output). The response will employ chunked Transfer-Encoding. Code source: https://github.com/substack/stream-handbook
  • 15.
    Package management ●npm (Node Package Manager) o Provides simple & powerful package management. o Reads module dependencies from package.json o Typical usage: npm install or npm update. o Can store all dependent modules locally or globally
  • 16.
    A sample package.json { "name": "SomeApp", "description": "SomeApp Web Application", "version": "0.0.1", "private": true, "dependencies": { "express": "3.6.0", "connect": "2.15.0", "mysql": "*", } }
  • 17.
    Secure HTTP withNode.js Code source: Node.js in Action Key and certificate files provided. Use gpg, openssl etc. to generate key and CSR.
  • 18.
    Connect framework: Modularweb apps Source: Node.js in Action Install: npm install connect
  • 19.
    Connect: usage Createa Connect ‘app’. ●Will store all middleware. ●Itself just a function. Create a middleware ‘stack’. Requests will execute middleware functions till ‘next()’ is not called or end of stack is reached. Code source: https://www.npmjs.org/package/connect
  • 20.
    Connect: usage (2) ● Middleware can be mounted on specific URL endpoints. ● Does basic routing. ● Error handler middleware. OR Create Connect-enabled server. Code source: https://www.npmjs.org/package/connect
  • 21.
  • 22.
    Serving static files Static middleware configured with the folder from which files are served.
  • 23.
    Express: Lightweight webframework Routing Code Source: https://www.npmjs.org/package/express
  • 24.
    Bootstrapping Express application express – e output
  • 25.
    Rendering views withExpress Code Source: Node.js in Action Express configured with the views folder. View engine set as ejs. Looks up index.ejs in the views folder.
  • 26.
    View lookup Source:Node.js in Action Source: Node.js in Action
  • 27.
    Passing data toviews Data passed to the view. photos is an array.
  • 28.
    An example view title variable accessed. photos array accessed here.
  • 29.
    Database connectivity npminstall mysql var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'me', password : 'secret' }); connection.connect(); connection.query('...', function(err, rows, fields) { if (err) throw err; connection.end(); }); https://github.com/felixge/node-mysql npms available for virtually any SQL/NoSQL database! Query completes only when the callback is invoked!
  • 30.
    Authentication ● passport.js npm install passport o Authentication middleware for node.js. o 140+ authentication strategies. o Supports OpenID and OAuth o http://passportjs.org o Session data can be serialized into a store like Redis.
  • 31.
    Read more ●Node.js in Action ● http://expressjs.com ● http://www.nodebeginner.org/
  • 32.
    Thank You Dr.Jayaraj Poroor Founder, DependSoft Consulting Peace of mind with dependable software. jayaraj@dependsoft.com http://dependsoft.com