exploring node.js

32
Exploring Node.JS Dr. Jayaraj Poroor DependSoft Consulting http://dependsoft.com

Upload: deepu-s-nath

Post on 12-Jul-2015

1.006 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Exploring Node.jS

Exploring Node.JSDr. Jayaraj Poroor

DependSoft Consultinghttp://dependsoft.com

Page 2: Exploring Node.jS

About DependSoft

Page 3: Exploring Node.jS

My ongoing R&D work: IntegralJ

http://integralj.org (open source)

Page 4: Exploring Node.jS

● Virtual Private Transporto Keyhole approach to secure remote service access.o http://shelloid.como Node.js, Netty, Redis, MySQL, Google protobuf.o Basic engine open sourced.

● Sensoid IoT platformo Distributed stream query platform.o Node.js, Redis, InfluxDB, ElasticSearch, MySQL.

My recent products

Page 5: Exploring Node.jS

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.

Page 6: Exploring Node.jS

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

Page 7: Exploring Node.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/Oo Thread does not blocko Uses callback mechanism to notify completiono e.g., conn.query(“...”, function(err, rows)

{ } );

Page 8: Exploring Node.jS

Node.JS Threading model

1. Single computational thread o Non-blocking, interleaved request processingo Background worker threads for I/O processing

1. Clustering to utilize multi-core machines2. No shared memory between cluster processes.

Page 9: Exploring Node.jS

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

Page 10: Exploring Node.jS

Event Loop Illustrated

http://www.geekgirl.io/understanding-the-event-loop-in-nodejs/

Page 11: Exploring Node.jS

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

Page 12: Exploring Node.jS

Serving a file

Code source: https://github.com/substack/stream-handbook

Buffers the entire file in memory. ●Inefficient for large files.●Client needs to wait till entire file is read.

Page 13: Exploring Node.jS

Node.js Streams

● Data served in chunks.

● Data event fires whenever a new chunk is ready.

Code source: Node.js in Action

Page 14: Exploring Node.jS

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

Page 15: Exploring Node.jS

Package management● npm (Node Package Manager)

o Provides simple & powerful package management.o Reads module dependencies from package.jsono Typical usage: npm install or npm update.o Can store all dependent modules locally or globally

Page 16: Exploring Node.jS

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": "*", }}

Page 17: Exploring Node.jS

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.

Page 18: Exploring Node.jS

Connect framework: Modular web apps

Source

: Node.js in A

ction

Install:npm install connect

Page 19: Exploring Node.jS

Connect: usageCreate 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

Page 20: Exploring Node.jS

Connect: usage (2)● Middleware can be

mounted on specific URL endpoints.

● Does basic routing.

● Error handler middleware.

Create Connect-enabled server.

Code source: https://www.npmjs.org/package/connect

OR

Page 21: Exploring Node.jS

Example Connect middlewares

Page 22: Exploring Node.jS

Serving static filesStatic middlewareconfigured with the folder from which files are served.

Page 23: Exploring Node.jS

Express: Lightweight web framework

Code Source: https://www.npmjs.org/package/express

Routing

Page 24: Exploring Node.jS

Bootstrapping Express application

express – e output

Page 25: Exploring Node.jS

Rendering views with Express

Code S

ou rce: Nod e.js in A

c tion

Express configured with the views folder.

View engine set as ejs.

Looks up index.ejs in the views folder.

Page 26: Exploring Node.jS

View lookup

Source: N

ode.js in Action

Source: Node.js in Action

Page 27: Exploring Node.jS

Passing data to views

Data passed to the view.photos is an array.

Page 28: Exploring Node.jS

An example view

photos array accessed here.

title variable accessed.

Page 29: Exploring Node.jS

Database connectivitynpm 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!

Page 30: Exploring Node.jS

Authentication● passport.js

o Authentication middleware for node.js.o 140+ authentication strategies.o Supports OpenID and OAutho http://passportjs.orgo Session data can be serialized into a store like Redis.

npm install passport

Page 31: Exploring Node.jS

Read more

● Node.js in Action● http://expressjs.com● http://www.nodebeginner.org/

Page 32: Exploring Node.jS

Thank You

Dr. Jayaraj PoroorFounder, DependSoft Consulting

Peace of mind with dependable software.

[email protected]://dependsoft.com