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?

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)

Mappeoppgave 2 2003
Mappeoppgave 2   2003Mappeoppgave 2   2003
Mappeoppgave 2 2003
Anniken
 
Project54 Research Areas
Project54 Research AreasProject54 Research Areas
Project54 Research Areas
Andrew Kun
 
P2 Tour
P2 TourP2 Tour
P2 Tour
scottkarp
 
ChiefStrategyOfficer_preso_rev0
ChiefStrategyOfficer_preso_rev0ChiefStrategyOfficer_preso_rev0
ChiefStrategyOfficer_preso_rev0
Jenni Lloyd
 
Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8
ssjaspb
 
FM update by Mundie Salm
FM update by Mundie SalmFM update by Mundie Salm
FM update by Mundie Salm
farmingmatters
 
Studying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research OpportunitiesStudying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research Opportunities
Andrew Kun
 
Budapest exchange program for UNH ECE students
Budapest exchange program for UNH ECE studentsBudapest exchange program for UNH ECE students
Budapest exchange program for UNH ECE students
Andrew Kun
 
Merkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko HitzaMerkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
My e-Portfolio
My e-PortfolioMy e-Portfolio
My e-Portfolio
marlene chuc
 
What You Didnt Know You Dont Know About Compliance Mar 29 07a
What You Didnt Know You Dont Know About Compliance Mar 29 07aWhat You Didnt Know You Dont Know About Compliance Mar 29 07a
What You Didnt Know You Dont Know About Compliance Mar 29 07a
Freelancer Training
 
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
 
Mappeoppgave 2 2003
Mappeoppgave 2   2003Mappeoppgave 2   2003
Mappeoppgave 2 2003
Anniken
 
Project54 Research Areas
Project54 Research AreasProject54 Research Areas
Project54 Research Areas
Andrew Kun
 
ChiefStrategyOfficer_preso_rev0
ChiefStrategyOfficer_preso_rev0ChiefStrategyOfficer_preso_rev0
ChiefStrategyOfficer_preso_rev0
Jenni Lloyd
 
Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8
ssjaspb
 
FM update by Mundie Salm
FM update by Mundie SalmFM update by Mundie Salm
FM update by Mundie Salm
farmingmatters
 
Studying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research OpportunitiesStudying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research Opportunities
Andrew Kun
 
Budapest exchange program for UNH ECE students
Budapest exchange program for UNH ECE studentsBudapest exchange program for UNH ECE students
Budapest exchange program for UNH ECE students
Andrew Kun
 
Merkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko HitzaMerkatu ikerketa-Irutxuloko Hitza
Merkatu ikerketa-Irutxuloko Hitza
Itxaso Ferreras
 
What You Didnt Know You Dont Know About Compliance Mar 29 07a
What You Didnt Know You Dont Know About Compliance Mar 29 07aWhat You Didnt Know You Dont Know About Compliance Mar 29 07a
What You Didnt Know You Dont Know About Compliance Mar 29 07a
Freelancer Training
 
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
 

Similar to Intro to node.js - Ran Mizrahi (28/8/14) (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
 

Recently uploaded (20)

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
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
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
 
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
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
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.
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
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
 
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
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
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
 
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
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
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.
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
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
 

Intro to node.js - Ran Mizrahi (28/8/14)

  • 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 ! !