SlideShare a Scribd company logo
What is Node JS?
 A JavaScript runtime environment running Google







Chrome’s V8 engine
Node.js is a platform for building fast, scalable network
applications (web servers).
Server - side JavaScript
Uses event-driven, asynchronous I/O to minimize overhead
and maximize scalability
—Great for data-intensive, real-time applications
V8 JavaScript Engine

 V8 is written in C++ and is used in Google Chrome
 Runs on Windows (XP or newer), Mac OS X (10.5 or newer), and

Linux systems that use IA-32, x64, or ARM – you can install your
server on many devices.
What can NodeJS be used for?
 Network servers (HTTP, messaging)
 API backends
 Real time applications

 Streaming data
 Web sites
 Bots
Typical architecture
Node Package Manager (NPM)
 NPM is a package management and distribution





system for Node
you can quickly find packages to serve specific
purposes
An npm package is a directory structure with a
package.json file describing the package
Total Packages: over 31 000
can be installed simply by typing the following:
npm install moduleName
Node Package Manager (NPM)
 Databases: mysql, postgresql, mongodb
 DOM: jsdom
 Web frameworks: express, connect

 DOM Manipulation: jQuery
 Utility libraries: underscore ,backbone, request
 Templating: mustache, jade

 Testing: vows, expresso
Non-blocking I/O
 Servers do nothing but I/O (Scripts waiting on I/O

requests degrades performance )
 To avoid blocking, Node makes use of the event driven
nature of JS by attaching callbacks to I/O requests
THE NODE WAY
fs.read('very-big-file', function(data) {
complexOperation(data); }
);
// you can do a lot when waiting for data
doOtherStuff();
Blocking vs Non-Blocking flow
Basic HTTP Server
CODE:

http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(“Start Hello");
res.end('Hello Worldn');
})

 when ‘stuff’ happens my function fires
 I get a request object and a response object
 I write to the response object header HTTP status 200

(Standard response for successful HTTP requests) and
content-type ‘text/plain’
 We make changes to the response object and then these are
transmitted when we call: esponse.end();
Basic HTTP Server
 This line requires the http module that ships with Node.js and

makes it accessible through the variable http.
 var http = require("http");

server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888,”127.0.0.1”);

 We pass a function into the server which it uses to handle all

incoming requests.
 When ‘stuff’ happens call this anonymous function
 Listen on port 8888 of the ip 127.0.0.1
http.ServerRequest Object
 When listening for request events, the callback gets an

http.ServerRequest object as the first argument. This
object has methods and properties:
 req.url: This property contains the requested URL as a
string. It does not contain the schema, hostname, or
port, but it contains everything after that.
 req.method: This contains the HTTP method used on
the request. It can be, for
example, GET, POST, DELETE, or HEAD.
 req.headers: This contains an object with a property for
every HTTP header on the request.
http.ServerResponse Object
 Writing headers: res.writeHead(status, headers)
 Changing or setting a header res.setHeader(name, value);
 Writing a Piece of the Response Body:

res.write('Hello');
 or use an existing buffer:

var buffer = new Buffer('Hello World');
res.write(buffer);

 Streaming a mp4
var fs = require('fs');
require('http').createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'video/mp4'});
var rs = fs.createReadStream('test.mp4');
rs.pipe(res);

}).listen(4000);
Basic node JS Module








var mysql
= require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'nod',
password : 'nodejs',
database : "test"
});






function connectDatabase() {
connection.connect();
console.log("db connected");
}











//insert data to database and call the function received
function insertDatabase( data , callback ) {



exports.startDatabase = connectDatabase;



exports.insertDatabase = insertDatabase;;

connection.query('INSERT INTO users (user) VALUES (?)',[data.clientName], function ( err, result ) {
if (err) throw err;
//calling callback function from server-chat.js
callback ( result.insertId );
});
}
Example
 Free Spark implementation:

http://10.182.39.2/
Cosmin Mereuta

More Related Content

What's hot

Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
fakedarren
 
Node js presentation
Node js presentationNode js presentation
Node js presentationmartincabrera
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleTom Croucher
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
orkaplan
 
Building your first Node app with Connect & Express
Building your first Node app with Connect & ExpressBuilding your first Node app with Connect & Express
Building your first Node app with Connect & Express
Christian Joudrey
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
Apaichon Punopas
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
cacois
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 
Node.js in production
Node.js in productionNode.js in production
Node.js in production
Felix Geisendörfer
 
Node.js - As a networking tool
Node.js - As a networking toolNode.js - As a networking tool
Node.js - As a networking tool
Felix Geisendörfer
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
Kamal Hussain
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
Erik van Appeldoorn
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
Arvind Devaraj
 
Nodejs intro
Nodejs introNodejs intro
Nodejs intro
Ndjido Ardo BAR
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
David Boyer
 

What's hot (20)

Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Building your first Node app with Connect & Express
Building your first Node app with Connect & ExpressBuilding your first Node app with Connect & Express
Building your first Node app with Connect & Express
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
NodeJS
NodeJSNodeJS
NodeJS
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
Node ppt
Node pptNode ppt
Node ppt
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Node.js in production
Node.js in productionNode.js in production
Node.js in production
 
Node.js - As a networking tool
Node.js - As a networking toolNode.js - As a networking tool
Node.js - As a networking tool
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
Node.js
Node.jsNode.js
Node.js
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Nodejs intro
Nodejs introNodejs intro
Nodejs intro
 
Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3Nodejs - A-quick-tour-v3
Nodejs - A-quick-tour-v3
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
 

Similar to Scalable network applications, event-driven - Node JS

nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
WalaSidhom1
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
Adrien Guéret
 
node js.pptx
node js.pptxnode js.pptx
An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.jsAyush Mishra
 
Node js getting started
Node js getting startedNode js getting started
Node js getting started
Pallavi Srivastava
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
Nir Noy
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
Bareen Shaikh
 
Intro to Node
Intro to NodeIntro to Node
Intro to Node
Aaron Stannard
 
Introduction to Node js for beginners + game project
Introduction to Node js for beginners + game projectIntroduction to Node js for beginners + game project
Introduction to Node js for beginners + game project
Laurence Svekis ✔
 
Node.js 1, 2, 3
Node.js 1, 2, 3Node.js 1, 2, 3
Node.js 1, 2, 3
Jian-Hong Pan
 
Ajax
AjaxAjax
Ajax
Yoga Raja
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
Parth Joshi
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
Fulvio Corno
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
Alona Mekhovova
 
5.node js
5.node js5.node js
5.node js
Geunhyung Kim
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
Haci Murat Yaman
 

Similar to Scalable network applications, event-driven - Node JS (20)

nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
node js.pptx
node js.pptxnode js.pptx
node js.pptx
 
An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node js getting started
Node js getting startedNode js getting started
Node js getting started
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
Intro to Node
Intro to NodeIntro to Node
Intro to Node
 
Introduction to Node js for beginners + game project
Introduction to Node js for beginners + game projectIntroduction to Node js for beginners + game project
Introduction to Node js for beginners + game project
 
Node.js 1, 2, 3
Node.js 1, 2, 3Node.js 1, 2, 3
Node.js 1, 2, 3
 
Ajax
AjaxAjax
Ajax
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
5.node js
5.node js5.node js
5.node js
 
08 ajax
08 ajax08 ajax
08 ajax
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 

Recently uploaded

Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 

Recently uploaded (20)

Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 

Scalable network applications, event-driven - Node JS

  • 1.
  • 2. What is Node JS?  A JavaScript runtime environment running Google     Chrome’s V8 engine Node.js is a platform for building fast, scalable network applications (web servers). Server - side JavaScript Uses event-driven, asynchronous I/O to minimize overhead and maximize scalability —Great for data-intensive, real-time applications
  • 3. V8 JavaScript Engine  V8 is written in C++ and is used in Google Chrome  Runs on Windows (XP or newer), Mac OS X (10.5 or newer), and Linux systems that use IA-32, x64, or ARM – you can install your server on many devices.
  • 4. What can NodeJS be used for?  Network servers (HTTP, messaging)  API backends  Real time applications  Streaming data  Web sites  Bots
  • 6. Node Package Manager (NPM)  NPM is a package management and distribution     system for Node you can quickly find packages to serve specific purposes An npm package is a directory structure with a package.json file describing the package Total Packages: over 31 000 can be installed simply by typing the following: npm install moduleName
  • 7. Node Package Manager (NPM)  Databases: mysql, postgresql, mongodb  DOM: jsdom  Web frameworks: express, connect  DOM Manipulation: jQuery  Utility libraries: underscore ,backbone, request  Templating: mustache, jade  Testing: vows, expresso
  • 8. Non-blocking I/O  Servers do nothing but I/O (Scripts waiting on I/O requests degrades performance )  To avoid blocking, Node makes use of the event driven nature of JS by attaching callbacks to I/O requests THE NODE WAY fs.read('very-big-file', function(data) { complexOperation(data); } ); // you can do a lot when waiting for data doOtherStuff();
  • 10. Basic HTTP Server CODE: http = require("http"); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(“Start Hello"); res.end('Hello Worldn'); })  when ‘stuff’ happens my function fires  I get a request object and a response object  I write to the response object header HTTP status 200 (Standard response for successful HTTP requests) and content-type ‘text/plain’  We make changes to the response object and then these are transmitted when we call: esponse.end();
  • 11. Basic HTTP Server  This line requires the http module that ships with Node.js and makes it accessible through the variable http.  var http = require("http"); server = http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888,”127.0.0.1”);  We pass a function into the server which it uses to handle all incoming requests.  When ‘stuff’ happens call this anonymous function  Listen on port 8888 of the ip 127.0.0.1
  • 12. http.ServerRequest Object  When listening for request events, the callback gets an http.ServerRequest object as the first argument. This object has methods and properties:  req.url: This property contains the requested URL as a string. It does not contain the schema, hostname, or port, but it contains everything after that.  req.method: This contains the HTTP method used on the request. It can be, for example, GET, POST, DELETE, or HEAD.  req.headers: This contains an object with a property for every HTTP header on the request.
  • 13. http.ServerResponse Object  Writing headers: res.writeHead(status, headers)  Changing or setting a header res.setHeader(name, value);  Writing a Piece of the Response Body: res.write('Hello');  or use an existing buffer: var buffer = new Buffer('Hello World'); res.write(buffer);  Streaming a mp4 var fs = require('fs'); require('http').createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'video/mp4'}); var rs = fs.createReadStream('test.mp4'); rs.pipe(res); }).listen(4000);
  • 14. Basic node JS Module        var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'nod', password : 'nodejs', database : "test" });     function connectDatabase() { connection.connect(); console.log("db connected"); }          //insert data to database and call the function received function insertDatabase( data , callback ) {  exports.startDatabase = connectDatabase;  exports.insertDatabase = insertDatabase;; connection.query('INSERT INTO users (user) VALUES (?)',[data.clientName], function ( err, result ) { if (err) throw err; //calling callback function from server-chat.js callback ( result.insertId ); }); }
  • 15. Example  Free Spark implementation: http://10.182.39.2/