SlideShare a Scribd company logo
Introduction to 
{ node.js } 
Ran Mizrahi (@ranm8) 
Founder and CEO, CoCycles
About { Me } 
• Founder and CEO of CoCycles. 
• Former Open Source Dpt. Leader of CodeOasis. 
• Architected and lead the development of the Wix App Market. 
• Full-stack and hands-on software engineer.
What is { node.js } 
• Server-side JavaScript development platform. 
! 
• Built on top of Chrome’s JavaScript runtime engine V8. 
! 
• Aims for easy development of scalable, non-blocking I/O, real 
time and network applications. 
! 
• Written in C/C++ and JavaScript. 
! 
• CommonJS module system. 
! 
• node.js is single-threaded and uses event-loop. 
! 
!
What is { V8 } 
• V8 is Google Chrome's JavaScript runtime engine. 
! 
• Implements ECMAScript specification (5th edition). 
! 
• Standalone and can be embedded to any C++ application. 
! 
• Compiles JavaScript to native machine code before executing it 
instead of interpreting. 
! 
• Open sourced under the new BSD license. 
! 
!
We All Love { Burgers }
{ Thread per Connection } Burgers Restaurant
Something is { WRONG }, we 
must 
do it { BETTER }!!!
Event-Driven { Burgers Restaurant }
{ Apache } vs. { NGINX } vs Performance 
Requests per second:
{ Apache } vs. { NGINX } vs Performance 
Memory usage:
{ Apache } vs. { NGINX } vs Performance 
So, what is the big difference between Apache and Nginx? 
• Apache uses one thread per connection. 
• Hard to scale. 
• Resource expensive. 
! 
• NGINX is single-threaded and uses event-loop for handling 
requests. 
• Easy to scale. 
• Lower resources consumption.
{ Blocking } I/O 
What is the software doing while it queries to the DB?!? 
var response = db.query('select * form users'); 
// use the result 
In most cases, nothing (/: 
• Better software should handle I/O differently! It should 
multitask. 
! 
• Other tasks should be performed while waiting.. 
! 
! 
!
{ Non-Blocking } I/O 
Non-blocking code: 
db.query('select * form users', function(result){ 
This is how I/O should be handled in concurrency, when the 
DB will respond, the given function will be executed. 
! 
! 
// Use the result 
}); 
! 
console.log(logSomething);
So Why Everyone Aren’t Using { Non-Blocking I/O } 
This what we learn: 
puts('Enter you name:'); 
var name = gets(); 
puts('Hello ' + name); 
Considered too complicated (:/ 
puts('Enter you name here:'); 
gets(function(name) { 
puts('Hello ' + name); 
});
But Why { JavaScript } ? 
JavaScript is designed specifically to be used with an event-loop: 
! 
• Anonymous functions, closures. 
! 
• Executes one callback at a time. 
! 
• Tested over years with I/O through DOM event callbacks. 
! 
• Web developers already know JavaScript (decreases the 
learning curve dramatically).
The { node.js } Project 
• Provides purely event-driven, non-blocking infrastructure to 
write high concurrency applications. 
! 
• Uses JavaScript for easy development of asynchronies apps. 
! 
• Open source and extendable module system. 
https://github.com/popular/starred
Some { Examples }
Hello { JavaScript Israel } 
setTimeout(function() { 
console.log(‘JavaScript Israel!'); 
}, 2000); 
! 
console.log('Hello'); 
• The program outputs “Hello”, then waits two seconds and 
outputs “JavaScript Israel!”. 
! 
• While waiting for the timeout to complete, node.js will keep 
performing other tasks. 
• Node exits automatically when nothing is left to do.
Streaming { HTTP Server } 
var http = require('http'); 
! 
var server = http.createServer(function(request, response) { 
response.writeHead(200, { 'Content-Type': 'text/plain' }); 
! 
setTimeout(function() { 
response.end(‘JavaScript Israel!n'); 
}, 2000); 
! 
response.write('Hellon'); 
}); 
! 
server.listen(8000); 
Let’s benchmark that... and see the results.. 
20 secs, with single thread is the result of non-blocking structure.
DNS { Resolver } 
var dns = require('dns'); 
! 
console.log('resolving google.com...'); 
! 
dns.resolve('google.com', function(error, addresses) { 
if (error) throw error; 
console.log('found: ' + addresses.join(', ')); 
}); 
Resolves “google.com” and outputs the result.
Common { Frameworks and 
Tools }
NPM { Node Package Manager } 
npm usage: 
Installs package in the current local directory: 
$ npm install express 
Installs package globally 
$ npm install express -g
{ Express } 
Express is a minimal and flexible node.js web framework, 
providing robust set of features for building web applications. 
Taken from http://expressjs.com 
Some of express features: 
• Robust routing. 
! 
• HTTP helpers (caching, redirection, etc.) 
! 
• Focuses on high performance. 
! 
• Simplified API for working in request/response environment. 
• Large community support. 
! 
! 
!
{ Express } 
Web Server example: 
var express = require(‘express’), 
app = express(); 
! 
app.get(‘/api/me', function(request, response) { 
// Return JSON encoded response 
response.json(200,{ 
code: 200, 
message: 'OK', 
payload: null 
}); 
}); 
• Creates new express HTTP server with route for path GET 
“/api/me”. 
! 
• Returns JSON formatted response.
{ Socket.IO (1.0) } By Automattic 
Socket.IO aims to make real-time apps possible in every browser 
and mobile device, blurring the differences between transport 
mechanisms. 
Taken from http://socket.io 
Some of socket.io main features: 
• Runs on top of Engine.IO as cross-browser/device bi-directional 
communication layer (XHR-polling, long-polling, 
WebSockets, Flash, etc.). 
! 
• Binary Support. 
! 
• Disconnections detection through heartbeats. 
! 
• Reconnection support with buffering. 
! 
!
{ Socket.IO } Notifications Example 
Server: 
var io = require('socket.io')(server); 
! 
io.on('connection', function(socket) { 
var notification = { body: 'Hello Exelate!' }; 
socket.emit('notification', notification, function(response) { 
console.log(response); 
}); 
}); 
Client: 
var socket = io('http://localhost'); 
socket.on('notification', function(data, callback) { 
console.log(data.body); 
callback('Hello to you too!'); 
});
{ Mongoose } 
Mongoose aims to provide elegant MongoDB object modeling 
(ODM) for node.js. 
Some of mongoose main features: 
• Allows creating DB schemas. 
! 
• Virtuals, Getters and Setters. 
! 
• Easy validation of DB objects. 
! 
• Extend easily! 
! 
! 
!
{ Mongoose } 
Mongoose cat document example: 
var mongoose = require('mongoose'); 
mongoose.connect('localhost', ‘my-db'); 
! 
var CatSchema = mongoose.Schema({ 
name: { 
type: String, 
required: true, 
default: 'My cat' 
} 
}); 
! 
var Cat = mongoose.model('Cat', CatSchema); 
! 
var kitty = new Cat({ name: 'Kati' }); 
kitty.save(function(err) { 
if (err) throw err; 
console.log('Saved!') 
});
{ TDD/BDD } using Mocha and Expect.js 
Mocha 
Mocha is a feature-rich JavaScript test frameworks running on 
node and the browser, making asynchronies tests easy. 
Main features: 
• Supports both TDD and BDD styles. 
! 
• Both browser and node support. 
! 
• Proper exit status for CI support. 
! 
• Really easy async tests. 
! 
• node.js debugger support. 
! 
• Highly flexible, choose and join the pieces yourself (spy library, 
assertion library, etc.).
{ TDD/BDD } using Mocha and Chai 
Chai 
Chai is a BDD/TDD assertion library for node and the browser 
that can be easily paired with any JavaScript testing framework. 
Main features: 
• BDD style. 
! 
• Compatible with all test frameworks. 
! 
• Both node.js and browser compatible. 
! 
• Standalone assertion library. 
• Extend easily!
{ TDD/BDD } using Mocha and Chai 
“Normal” test: 
var expect = require(‘chai').expect; 
! 
describe('Array', function() { 
describe('#indexOf()', function() { 
it('Expect -1 when the value is not present', function() { 
var array = [1, 2, 3]; 
expect(array.indexOf(4)).to.be(-1); 
}); 
}); 
}); 
Run it.. 
$ mocha --reporter spec 
Array 
#indexOf() 
✓ Expect -1 when the value is not present 
! 
! 
1 test complete (5 ms)
{ TDD/BDD } using Mocha and Expect.js 
“Async” test: 
var expect = require(‘chai').expect; 
! 
function asyncCall(val ,callback) { 
var prefix = ' - '; 
! 
setTimeout(function() { 
var newString = val + prefix + 'OK'; 
! 
callback(newString); 
}, 500); 
} 
! 
describe('asyncCall', function() { 
it('Add suffix that prefixed with - to the given string', function(done) { 
var testVal = 'Foo'; 
! 
asyncCall(testVal, function(response) { 
expect(response).to.contain(testVal + ' - OK'); 
done(); 
}); 
}); 
}); 
Let’s run it...
{ Use Cases } 
FXP.co.il 
• Real-time notifications, forum threads and posts. 
! 
• 30,000 concurrency connections! 
! 
• We started with version 0.4 )-:.. 
! 
• Today, runs on one web server for serving all those concurrent 
connections.. 
! 
My Contributions… 
! 
• Requestify - Simplified HTTP 
• winston-newrelic - Winston transporter for New Relic 
! 
!
Thank you! 
Questions?
Ad

More Related Content

What's hot (20)

Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
Christian Heilmann
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
Juho Vepsäläinen
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
Saai Vignesh P
 
Lecture 5 javascript
Lecture 5 javascriptLecture 5 javascript
Lecture 5 javascript
Mujtaba Haider
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
Yakov Fain
 
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter PilgrimJavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
JavaCro'14 - Scala and Java EE 7 Development Experiences – Peter Pilgrim
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
cacois
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Workshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte IWorkshop 16: EmberJS Parte I
Workshop 16: EmberJS Parte I
Visual Engineering
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven TomacJavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Guillaume Laforge
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
Scott Becker
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
Jyothishmathi Institute of Technology and Science Karimnagar
 
JavaScript Good Practices
JavaScript Good PracticesJavaScript Good Practices
JavaScript Good Practices
Jussi Pohjolainen
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
Bhavya Siddappa
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
Juliana Lucena
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
Visual Engineering
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
Juho Vepsäläinen
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
Visual Engineering
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
Yakov Fain
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
cacois
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume LaforgeGroovy Ecosystem - JFokus 2011 - Guillaume Laforge
Groovy Ecosystem - JFokus 2011 - Guillaume Laforge
Guillaume Laforge
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
Scott Becker
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
Bhavya Siddappa
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
Juliana Lucena
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
Visual Engineering
 

Viewers also liked (20)

03 introductions
03   introductions03   introductions
03 introductions
Luciana Viter
 
Læringspotentialet i sociale medier
Læringspotentialet i sociale medierLæringspotentialet i sociale medier
Læringspotentialet i sociale medier
Inger-Marie Christensen
 
0861599 seconds left before the buzzer...
0861599 seconds left before the buzzer...0861599 seconds left before the buzzer...
0861599 seconds left before the buzzer...
guest19b25
 
Shift Happens - Ben Horowitz
Shift Happens - Ben HorowitzShift Happens - Ben Horowitz
Shift Happens - Ben Horowitz
Razin Mustafiz
 
Measuring conversation and community
Measuring conversation and communityMeasuring conversation and community
Measuring conversation and community
Jenni Lloyd
 
La travesia
La travesiaLa travesia
La travesia
Luis Montalvan
 
The Ants
The AntsThe Ants
The Ants
Lakshita Gaur
 
euskara kontsumitzeko motibazioak
euskara kontsumitzeko motibazioakeuskara kontsumitzeko motibazioak
euskara kontsumitzeko motibazioak
Itxaso Ferreras
 
John Mitchell Presentation Oct 08 Ppt 2007
John Mitchell Presentation Oct 08    Ppt 2007John Mitchell Presentation Oct 08    Ppt 2007
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
My Carnegie Mellon University Master\'s Thesis
My Carnegie Mellon University Master\'s ThesisMy Carnegie Mellon University Master\'s Thesis
My Carnegie Mellon University Master\'s Thesis
Jonas Rolo
 
Blogs De Utilidade
Blogs De UtilidadeBlogs De Utilidade
Blogs De Utilidade
garciabarbontic
 
08 describing someone
08   describing someone08   describing someone
08 describing someone
Luciana Viter
 
Human resource planning
Human resource planningHuman resource planning
Human resource planning
Suhag Mistry
 
Om at være MOOC'er -at finde og skabe sine egne læringsstier
Om at være MOOC'er -at finde og skabe sine egne læringsstierOm at være MOOC'er -at finde og skabe sine egne læringsstier
Om at være MOOC'er -at finde og skabe sine egne læringsstier
Inger-Marie Christensen
 
παρουσίαση 1 ιδεοκατασκευών έκδοση 1
παρουσίαση 1 ιδεοκατασκευών έκδοση 1παρουσίαση 1 ιδεοκατασκευών έκδοση 1
παρουσίαση 1 ιδεοκατασκευών έκδοση 1
Γιώργος Παπανικολάου
 
The Watson Twins
The Watson TwinsThe Watson Twins
The Watson Twins
globeu
 
0861599 seconds left before the buzzer...
0861599 seconds left before the buzzer...0861599 seconds left before the buzzer...
0861599 seconds left before the buzzer...
guest19b25
 
Shift Happens - Ben Horowitz
Shift Happens - Ben HorowitzShift Happens - Ben Horowitz
Shift Happens - Ben Horowitz
Razin Mustafiz
 
Measuring conversation and community
Measuring conversation and communityMeasuring conversation and community
Measuring conversation and community
Jenni Lloyd
 
euskara kontsumitzeko motibazioak
euskara kontsumitzeko motibazioakeuskara kontsumitzeko motibazioak
euskara kontsumitzeko motibazioak
Itxaso Ferreras
 
John Mitchell Presentation Oct 08 Ppt 2007
John Mitchell Presentation Oct 08    Ppt 2007John Mitchell Presentation Oct 08    Ppt 2007
John Mitchell Presentation Oct 08 Ppt 2007
Dan Foster
 
My Carnegie Mellon University Master\'s Thesis
My Carnegie Mellon University Master\'s ThesisMy Carnegie Mellon University Master\'s Thesis
My Carnegie Mellon University Master\'s Thesis
Jonas Rolo
 
08 describing someone
08   describing someone08   describing someone
08 describing someone
Luciana Viter
 
Human resource planning
Human resource planningHuman resource planning
Human resource planning
Suhag Mistry
 
Om at være MOOC'er -at finde og skabe sine egne læringsstier
Om at være MOOC'er -at finde og skabe sine egne læringsstierOm at være MOOC'er -at finde og skabe sine egne læringsstier
Om at være MOOC'er -at finde og skabe sine egne læringsstier
Inger-Marie Christensen
 
The Watson Twins
The Watson TwinsThe Watson Twins
The Watson Twins
globeu
 
Ad

Similar to Intro to node.js - Ran Mizrahi (27/8/2014) (20)

What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
Balajihope
 
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Evan Chan
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
orkaplan
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
Ricardo Silva
 
mearn-stackjdksjdsfjdkofkdokodkojdj.pptx
mearn-stackjdksjdsfjdkofkdokodkojdj.pptxmearn-stackjdksjdsfjdkofkdokodkojdj.pptx
mearn-stackjdksjdsfjdkofkdokodkojdj.pptx
aravym456
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
async_io
 
concept of server-side JavaScript / JS Framework: NODEJS
concept of server-side JavaScript / JS Framework: NODEJSconcept of server-side JavaScript / JS Framework: NODEJS
concept of server-side JavaScript / JS Framework: NODEJS
Kongu Engineering College, Perundurai, Erode
 
Introduction to node.js by jiban
Introduction to node.js by jibanIntroduction to node.js by jiban
Introduction to node.js by jiban
Jibanananda Sana
 
MEAN Stack
MEAN StackMEAN Stack
MEAN Stack
Krishnaprasad k
 
MEAN Stack
MEAN StackMEAN Stack
MEAN Stack
Krishnaprasad k
 
Meanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore ChandraMeanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore Chandra
Kishore Chandra
 
Spark Summit 2014: Spark Job Server Talk
Spark Summit 2014:  Spark Job Server TalkSpark Summit 2014:  Spark Job Server Talk
Spark Summit 2014: Spark Job Server Talk
Evan Chan
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
Rami Sayar
 
Node.js on Azure
Node.js on AzureNode.js on Azure
Node.js on Azure
Sasha Goldshtein
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
Stuart (Pid) Williams
 
Node azure
Node azureNode azure
Node azure
Emanuele DelBono
 
Scalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJSScalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJS
Andhy Koesnandar
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
FITC
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
Yoann Gotthilf
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
Balajihope
 
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Evan Chan
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
orkaplan
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
Ricardo Silva
 
mearn-stackjdksjdsfjdkofkdokodkojdj.pptx
mearn-stackjdksjdsfjdkofkdokodkojdj.pptxmearn-stackjdksjdsfjdkofkdokodkojdj.pptx
mearn-stackjdksjdsfjdkofkdokodkojdj.pptx
aravym456
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
async_io
 
Introduction to node.js by jiban
Introduction to node.js by jibanIntroduction to node.js by jiban
Introduction to node.js by jiban
Jibanananda Sana
 
Meanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore ChandraMeanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore Chandra
Kishore Chandra
 
Spark Summit 2014: Spark Job Server Talk
Spark Summit 2014:  Spark Job Server TalkSpark Summit 2014:  Spark Job Server Talk
Spark Summit 2014: Spark Job Server Talk
Evan Chan
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
Rami Sayar
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
Stuart (Pid) Williams
 
Scalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJSScalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJS
Andhy Koesnandar
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
FITC
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
Yoann Gotthilf
 
Ad

Recently uploaded (20)

Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 

Intro to node.js - Ran Mizrahi (27/8/2014)

  • 1. Introduction to { node.js } Ran Mizrahi (@ranm8) Founder and CEO, CoCycles
  • 2. About { Me } • Founder and CEO of CoCycles. • Former Open Source Dpt. Leader of CodeOasis. • Architected and lead the development of the Wix App Market. • Full-stack and hands-on software engineer.
  • 3. What is { node.js } • Server-side JavaScript development platform. ! • Built on top of Chrome’s JavaScript runtime engine V8. ! • Aims for easy development of scalable, non-blocking I/O, real time and network applications. ! • Written in C/C++ and JavaScript. ! • CommonJS module system. ! • node.js is single-threaded and uses event-loop. ! !
  • 4. What is { V8 } • V8 is Google Chrome's JavaScript runtime engine. ! • Implements ECMAScript specification (5th edition). ! • Standalone and can be embedded to any C++ application. ! • Compiles JavaScript to native machine code before executing it instead of interpreting. ! • Open sourced under the new BSD license. ! !
  • 5. We All Love { Burgers }
  • 6. { Thread per Connection } Burgers Restaurant
  • 7. Something is { WRONG }, we must do it { BETTER }!!!
  • 8. Event-Driven { Burgers Restaurant }
  • 9. { Apache } vs. { NGINX } vs Performance Requests per second:
  • 10. { Apache } vs. { NGINX } vs Performance Memory usage:
  • 11. { Apache } vs. { NGINX } vs Performance So, what is the big difference between Apache and Nginx? • Apache uses one thread per connection. • Hard to scale. • Resource expensive. ! • NGINX is single-threaded and uses event-loop for handling requests. • Easy to scale. • Lower resources consumption.
  • 12. { Blocking } I/O What is the software doing while it queries to the DB?!? var response = db.query('select * form users'); // use the result In most cases, nothing (/: • Better software should handle I/O differently! It should multitask. ! • Other tasks should be performed while waiting.. ! ! !
  • 13. { Non-Blocking } I/O Non-blocking code: db.query('select * form users', function(result){ This is how I/O should be handled in concurrency, when the DB will respond, the given function will be executed. ! ! // Use the result }); ! console.log(logSomething);
  • 14. So Why Everyone Aren’t Using { Non-Blocking I/O } This what we learn: puts('Enter you name:'); var name = gets(); puts('Hello ' + name); Considered too complicated (:/ puts('Enter you name here:'); gets(function(name) { puts('Hello ' + name); });
  • 15. But Why { JavaScript } ? JavaScript is designed specifically to be used with an event-loop: ! • Anonymous functions, closures. ! • Executes one callback at a time. ! • Tested over years with I/O through DOM event callbacks. ! • Web developers already know JavaScript (decreases the learning curve dramatically).
  • 16. The { node.js } Project • Provides purely event-driven, non-blocking infrastructure to write high concurrency applications. ! • Uses JavaScript for easy development of asynchronies apps. ! • Open source and extendable module system. https://github.com/popular/starred
  • 18. Hello { JavaScript Israel } setTimeout(function() { console.log(‘JavaScript Israel!'); }, 2000); ! console.log('Hello'); • The program outputs “Hello”, then waits two seconds and outputs “JavaScript Israel!”. ! • While waiting for the timeout to complete, node.js will keep performing other tasks. • Node exits automatically when nothing is left to do.
  • 19. Streaming { HTTP Server } var http = require('http'); ! var server = http.createServer(function(request, response) { response.writeHead(200, { 'Content-Type': 'text/plain' }); ! setTimeout(function() { response.end(‘JavaScript Israel!n'); }, 2000); ! response.write('Hellon'); }); ! server.listen(8000); Let’s benchmark that... and see the results.. 20 secs, with single thread is the result of non-blocking structure.
  • 20. DNS { Resolver } var dns = require('dns'); ! console.log('resolving google.com...'); ! dns.resolve('google.com', function(error, addresses) { if (error) throw error; console.log('found: ' + addresses.join(', ')); }); Resolves “google.com” and outputs the result.
  • 21. Common { Frameworks and Tools }
  • 22. NPM { Node Package Manager } npm usage: Installs package in the current local directory: $ npm install express Installs package globally $ npm install express -g
  • 23. { Express } Express is a minimal and flexible node.js web framework, providing robust set of features for building web applications. Taken from http://expressjs.com Some of express features: • Robust routing. ! • HTTP helpers (caching, redirection, etc.) ! • Focuses on high performance. ! • Simplified API for working in request/response environment. • Large community support. ! ! !
  • 24. { Express } Web Server example: var express = require(‘express’), app = express(); ! app.get(‘/api/me', function(request, response) { // Return JSON encoded response response.json(200,{ code: 200, message: 'OK', payload: null }); }); • Creates new express HTTP server with route for path GET “/api/me”. ! • Returns JSON formatted response.
  • 25. { Socket.IO (1.0) } By Automattic Socket.IO aims to make real-time apps possible in every browser and mobile device, blurring the differences between transport mechanisms. Taken from http://socket.io Some of socket.io main features: • Runs on top of Engine.IO as cross-browser/device bi-directional communication layer (XHR-polling, long-polling, WebSockets, Flash, etc.). ! • Binary Support. ! • Disconnections detection through heartbeats. ! • Reconnection support with buffering. ! !
  • 26. { Socket.IO } Notifications Example Server: var io = require('socket.io')(server); ! io.on('connection', function(socket) { var notification = { body: 'Hello Exelate!' }; socket.emit('notification', notification, function(response) { console.log(response); }); }); Client: var socket = io('http://localhost'); socket.on('notification', function(data, callback) { console.log(data.body); callback('Hello to you too!'); });
  • 27. { Mongoose } Mongoose aims to provide elegant MongoDB object modeling (ODM) for node.js. Some of mongoose main features: • Allows creating DB schemas. ! • Virtuals, Getters and Setters. ! • Easy validation of DB objects. ! • Extend easily! ! ! !
  • 28. { Mongoose } Mongoose cat document example: var mongoose = require('mongoose'); mongoose.connect('localhost', ‘my-db'); ! var CatSchema = mongoose.Schema({ name: { type: String, required: true, default: 'My cat' } }); ! var Cat = mongoose.model('Cat', CatSchema); ! var kitty = new Cat({ name: 'Kati' }); kitty.save(function(err) { if (err) throw err; console.log('Saved!') });
  • 29. { TDD/BDD } using Mocha and Expect.js Mocha Mocha is a feature-rich JavaScript test frameworks running on node and the browser, making asynchronies tests easy. Main features: • Supports both TDD and BDD styles. ! • Both browser and node support. ! • Proper exit status for CI support. ! • Really easy async tests. ! • node.js debugger support. ! • Highly flexible, choose and join the pieces yourself (spy library, assertion library, etc.).
  • 30. { TDD/BDD } using Mocha and Chai Chai Chai is a BDD/TDD assertion library for node and the browser that can be easily paired with any JavaScript testing framework. Main features: • BDD style. ! • Compatible with all test frameworks. ! • Both node.js and browser compatible. ! • Standalone assertion library. • Extend easily!
  • 31. { TDD/BDD } using Mocha and Chai “Normal” test: var expect = require(‘chai').expect; ! describe('Array', function() { describe('#indexOf()', function() { it('Expect -1 when the value is not present', function() { var array = [1, 2, 3]; expect(array.indexOf(4)).to.be(-1); }); }); }); Run it.. $ mocha --reporter spec Array #indexOf() ✓ Expect -1 when the value is not present ! ! 1 test complete (5 ms)
  • 32. { TDD/BDD } using Mocha and Expect.js “Async” test: var expect = require(‘chai').expect; ! function asyncCall(val ,callback) { var prefix = ' - '; ! setTimeout(function() { var newString = val + prefix + 'OK'; ! callback(newString); }, 500); } ! describe('asyncCall', function() { it('Add suffix that prefixed with - to the given string', function(done) { var testVal = 'Foo'; ! asyncCall(testVal, function(response) { expect(response).to.contain(testVal + ' - OK'); done(); }); }); }); Let’s run it...
  • 33. { Use Cases } FXP.co.il • Real-time notifications, forum threads and posts. ! • 30,000 concurrency connections! ! • We started with version 0.4 )-:.. ! • Today, runs on one web server for serving all those concurrent connections.. ! My Contributions… ! • Requestify - Simplified HTTP • winston-newrelic - Winston transporter for New Relic ! !