SlideShare a Scribd company logo
1 of 30
Download to read offline
Basic Understanding and
Implement of Node.js
Gary
2014/04/18
Outline
• Review of Node.js
• Event Loop
• Basic Thesis
• Callback
• Asynchronous Mechanism
• Module
• Express
• Conclusion
Review of Node.js
• A platform built on Chrome’s JavaScript runtime for easily building
fast, scalable network applications.
• Uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient
• Perfect for data-intensive real-time applications that run across
distributed devices
• Contains a built-in HTTP server library
• Making it possible to run a web server without the use of external
software, such as Apache or Lighttpd
• Allowing more control of how the web server works
Review of Node.js
• Feature of Node.js
• V8 JavaScript Engine
• Single-threaded
• Event-driven
• Non-blocking I/O model
Review of Node.js
Event Loop – Basic Thesis
• The first basic thesis of node.js is that I/O is expensive
• So the largest waste with current programming technologies comes
from waiting for I/O to complete.
Event Loop – Basic Thesis
• There are several ways in which one can deal with the performance
impact
• Synchronous
• you handle one request at a time, each in turn.
• fork a new process:
• you start a new process to handle each request.
• threads:
• start a new thread to handle each request.
Event Loop – Basic Thesis
• The second basis thesis is that thread-per-connection is memory-
expensive
• Apache is multithreaded. It spawns a thread per request (or process,
it depends on the conf).
• Node.js is not multithreaded, because threads and processes carry a
heavy memory cost.
Event Loop – Basic Thesis
Node.js App
Read file
Send file I/O request
Send HTTP request
Send Internet I/O request
SQL inquire
Send database I/O request
file I/O response Internet I/O reponse
database I/O response
Event Loop – Callback
Node.js App
Event Loop
Read file
Send file I/O request
Send HTTP request
Send Internet I/O request
SQL inquire
Send database I/O request
file I/O response
Internet I/O request
database I/O request
Event Loop – Callback
• An event loop is “an entity that handles and processes external
events and converts them into callback invocations”.
• At an I/O call, your code saves the callback and returns control to the
node.js runtime environment.
• The callback will be called later when the data actually is available.
Event Loop – Callback
• First-class functions
• E.g. we pass around functions as data, shuffle them around and execute them
when needed.
• Function composition
• Also known as having anonymous functions or closures that are executed
after something happens in the evented I/O.
function say(word) {
console.log(word);
}
function execute(someFunction, value) {
someFunction(value);
}
execute(say, "Hello");
function execute(someFunction, value) {
someFunction(value);
}
execute(function(word){ console.log(word) }, "Hello");
Event Loop – Callback
• Run the node.js app, it will immediately output “Server has started”
• When sending request to server, “Request received” is shown
• This is an event-driven
asynchronous server-side
JavaScript and its callback
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
Event Loop – Asynchronous Mechanism
• Node.js is event-driven design
• In web application, it is said to have the fastest real-time response
• The principle is to use libuv to implement event polling, constantly
check for events that need to be processed
• If it finds an event on standby, run and trigger the corresponding
handler.
Event Loop – Asynchronous Mechanism
• Node.js keeps a single thread for your code
• Doing a “sleep” will block the server
• So while that code is running, node.js will not respond to any other
requests from clients, since it only has one thread for executing your
code.
• Or if you would have some CPU -intensive code, say, for resizing
images, that would still block all other requests.
Event Loop – Asynchronous Mechanism
• The console will shown
“blah…” after the file
is read
file.open();
while(true) {
result = file.read();
if (!result)
break;
/* Do something... */
}
console.log('blah blah blah...');
Difficult task
Easy task
Difficult task
done on time
Easy task
delayed
Event Loop – Asynchronous Mechanism
• process.nextTick(callback)
• On the next loop around the event loop call this callback.
• This is not a simple alias to setTimeout(fn, 0), it's much more efficient. It
typically runs before any other I/O events fires
• Node.js implements a queue storing events
• All event triggers are driven by the engine of a polling event.
• process.nextTick () is that we can put a program into the event queue,
the next time the event polling is driven.
Event Loop – Asynchronous Mechanism
Difficult task
Easy task
Easy task done.
Strill delayed
difficult task
done
function readLoop() {
result = file.read();
if (!result)
return;
/* Do something... */
process.nextTick(readLoop);
}
file.open();
process.nextTick(readLoop);
console.log('blah blah blah...');
Event Loop – Asynchronous Mechanism
• child_process.fork
• Spawn Node processes
• Having all the methods in a normal ChildProcess instance, the returned object
has a communication channel built-in.
• Assume at least 30ms startup and 10mb memory for each new Node. That is,
you cannot create many thousands of them.
Event Loop – Asynchronous Mechanism
var n = child_process.fork('./child.js');
n.on('message', function(m) {
console.log('PARENT got message:', m);
});
n.send({ hello: 'world' });
process.on('message', function(m) {
console.log('CHILD got message:', m);
});
process.send({ foo: 'bar' });
Difficult task
Easy task 1
difficult task
done
Easy task 2
Easy task 1 done
Easy task 2 done
Event Loop – Asynchronous Mechanism
• Cluster
• A single instance of Node runs in a single thread.
• To take advantage of multi-core systems the user will sometimes want to
launch a cluster of Node processes to handle the load.
• The cluster module allows you to easily create child processes that all share
server ports.
Event Loop – Asynchronous Mechanism
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) { // Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' +
worker.process.pid + ' died');
});
} else { // Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello worldn");
}).listen(8000);
}
Difficult task
Easy task 1
Easy task 2
Easy task 1 done
v
Easy task 2 done
difficult task
done
Event Loop – Asynchronous Mechanism
Module
• NPM(Node Package Manager)
• The official package manager for Node.js.
• As of Node.js version 0.6.3, npm is bundled and installed automatically with
the environment.
• Runs through the command line and manages dependencies for an
application.
• Allows users to install Node.js applications that are available on the npm
registry.
• Written entirely in JavaScript, and runs on the Node.js platform.
Module
• We use require and exports to connect with module
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
Module can be download with npm
var server = require("./server");
server.start();
Module can be written by requirements
server.js index.js
Express
• Express is a minimal and flexible node.js web application framework
• Providing a robust set of features for building single and multi-page,
and hybrid web applications.
Express
• >npm info express version
• edit package.json
• >npm install
{
"name": "hello-world",
"description": "hello world test app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.0.0"
}
}
Express
• Node.js has more and more resources related to development,
• No need to write many features, you can directly use the module has
been developed via official NPM tool from the others on the network
• Developers can focus wholeheartedly on the current development of
applications.
• We always depend on a lot of third-party modules, and whenever we
change the develop environment, it is necessary manually installing
modules
• package.json file directly manage project dependencies and
modules which being used by project
Express
• Three outstanding features
• routing distribution
• request processing
• rendering the view
var app = express();
app.get("/hello/:who", function(req, res) {
res.end("Hello, " + req.params.who + ".");
});
// 啓動Express
var express = require("express");
var app = express();
// 設置view目錄
app.set("views", __dirname + "/views");
// 設置模板引擎
app.set("view engine", "jade");
Conclusion
• Web applications typically have bottleneck in I/O of network, and
Node.js solves them well.
• Node.js uses JavaScript, which web developers already familiar with.
• NPM let everyone easily put theirs modules on.
• The disadvantage is that Node.js is new, not stable enough, API
regularly updated.

More Related Content

What's hot

Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureColin Mackay
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Binary Studio
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaNurul Ferdous
 
Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013Oscar Renalias
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRob O'Doherty
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialTom Croucher
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express Jeetendra singh
 
Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Ganesh Kondal
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejsAmit Thakkar
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJSHüseyin BABAL
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsNodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsGanesh Iyer
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDDSudar Muthu
 
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...Edureka!
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven ProgrammingKamal Hussain
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Christian Joudrey
 

What's hot (20)

Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azure
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
 
Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJS
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node js
Node jsNode js
Node js
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsNodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
 

Similar to Basic Understanding and Implement of Node.js

introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRichard Lee
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101Rami Sayar
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comVan-Duyet Le
 
Node.js 101 with Rami Sayar
Node.js 101 with Rami SayarNode.js 101 with Rami Sayar
Node.js 101 with Rami SayarFITC
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introductionPrasoon Kumar
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.jsChris Cowan
 
A complete guide to Node.js
A complete guide to Node.jsA complete guide to Node.js
A complete guide to Node.jsPrabin Silwal
 
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 WhenFITC
 
Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Pavel Chunyayev
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1Mohammad Qureshi
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Lucas Jellema
 

Similar to Basic Understanding and Implement of Node.js (20)

introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Node.js
Node.jsNode.js
Node.js
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
What is Node.js
What is Node.jsWhat is Node.js
What is Node.js
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
 
Node.js 101 with Rami Sayar
Node.js 101 with Rami SayarNode.js 101 with Rami Sayar
Node.js 101 with Rami Sayar
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Nodejs
NodejsNodejs
Nodejs
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Node js internal
Node js internalNode js internal
Node js internal
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
A complete guide to Node.js
A complete guide to Node.jsA complete guide to Node.js
A complete guide to Node.js
 
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
 
Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015Ansible benelux meetup - Amsterdam 27-5-2015
Ansible benelux meetup - Amsterdam 27-5-2015
 
Node.js Introduction
Node.js IntroductionNode.js Introduction
Node.js Introduction
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)
 
Real time web
Real time webReal time web
Real time web
 

More from Gary Yeh

Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGLGary Yeh
 
Run-time of Node.js : V8 JavaScript Engine
Run-time of Node.js: V8 JavaScript EngineRun-time of Node.js: V8 JavaScript Engine
Run-time of Node.js : V8 JavaScript EngineGary Yeh
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
jQuery Mobile and JavaScript
jQuery Mobile and JavaScriptjQuery Mobile and JavaScript
jQuery Mobile and JavaScriptGary Yeh
 
JQuery mobile
JQuery mobileJQuery mobile
JQuery mobileGary Yeh
 
Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database ConnectivityGary Yeh
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvasGary Yeh
 
Git Workflow
Git WorkflowGit Workflow
Git WorkflowGary Yeh
 

More from Gary Yeh (10)

Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
 
Run-time of Node.js : V8 JavaScript Engine
Run-time of Node.js: V8 JavaScript EngineRun-time of Node.js: V8 JavaScript Engine
Run-time of Node.js : V8 JavaScript Engine
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
JQuery UI
JQuery UIJQuery UI
JQuery UI
 
jQuery Mobile and JavaScript
jQuery Mobile and JavaScriptjQuery Mobile and JavaScript
jQuery Mobile and JavaScript
 
JQuery mobile
JQuery mobileJQuery mobile
JQuery mobile
 
Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database Connectivity
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
 
Git Workflow
Git WorkflowGit Workflow
Git Workflow
 

Recently uploaded

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 

Recently uploaded (20)

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 

Basic Understanding and Implement of Node.js

  • 1. Basic Understanding and Implement of Node.js Gary 2014/04/18
  • 2. Outline • Review of Node.js • Event Loop • Basic Thesis • Callback • Asynchronous Mechanism • Module • Express • Conclusion
  • 3. Review of Node.js • A platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. • Uses an event-driven, non-blocking I/O model that makes it lightweight and efficient • Perfect for data-intensive real-time applications that run across distributed devices • Contains a built-in HTTP server library • Making it possible to run a web server without the use of external software, such as Apache or Lighttpd • Allowing more control of how the web server works
  • 4. Review of Node.js • Feature of Node.js • V8 JavaScript Engine • Single-threaded • Event-driven • Non-blocking I/O model
  • 6. Event Loop – Basic Thesis • The first basic thesis of node.js is that I/O is expensive • So the largest waste with current programming technologies comes from waiting for I/O to complete.
  • 7. Event Loop – Basic Thesis • There are several ways in which one can deal with the performance impact • Synchronous • you handle one request at a time, each in turn. • fork a new process: • you start a new process to handle each request. • threads: • start a new thread to handle each request.
  • 8. Event Loop – Basic Thesis • The second basis thesis is that thread-per-connection is memory- expensive • Apache is multithreaded. It spawns a thread per request (or process, it depends on the conf). • Node.js is not multithreaded, because threads and processes carry a heavy memory cost.
  • 9. Event Loop – Basic Thesis Node.js App Read file Send file I/O request Send HTTP request Send Internet I/O request SQL inquire Send database I/O request file I/O response Internet I/O reponse database I/O response
  • 10. Event Loop – Callback Node.js App Event Loop Read file Send file I/O request Send HTTP request Send Internet I/O request SQL inquire Send database I/O request file I/O response Internet I/O request database I/O request
  • 11. Event Loop – Callback • An event loop is “an entity that handles and processes external events and converts them into callback invocations”. • At an I/O call, your code saves the callback and returns control to the node.js runtime environment. • The callback will be called later when the data actually is available.
  • 12. Event Loop – Callback • First-class functions • E.g. we pass around functions as data, shuffle them around and execute them when needed. • Function composition • Also known as having anonymous functions or closures that are executed after something happens in the evented I/O. function say(word) { console.log(word); } function execute(someFunction, value) { someFunction(value); } execute(say, "Hello"); function execute(someFunction, value) { someFunction(value); } execute(function(word){ console.log(word) }, "Hello");
  • 13. Event Loop – Callback • Run the node.js app, it will immediately output “Server has started” • When sending request to server, “Request received” is shown • This is an event-driven asynchronous server-side JavaScript and its callback var http = require("http"); function onRequest(request, response) { console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started.");
  • 14. Event Loop – Asynchronous Mechanism • Node.js is event-driven design • In web application, it is said to have the fastest real-time response • The principle is to use libuv to implement event polling, constantly check for events that need to be processed • If it finds an event on standby, run and trigger the corresponding handler.
  • 15. Event Loop – Asynchronous Mechanism • Node.js keeps a single thread for your code • Doing a “sleep” will block the server • So while that code is running, node.js will not respond to any other requests from clients, since it only has one thread for executing your code. • Or if you would have some CPU -intensive code, say, for resizing images, that would still block all other requests.
  • 16. Event Loop – Asynchronous Mechanism • The console will shown “blah…” after the file is read file.open(); while(true) { result = file.read(); if (!result) break; /* Do something... */ } console.log('blah blah blah...'); Difficult task Easy task Difficult task done on time Easy task delayed
  • 17. Event Loop – Asynchronous Mechanism • process.nextTick(callback) • On the next loop around the event loop call this callback. • This is not a simple alias to setTimeout(fn, 0), it's much more efficient. It typically runs before any other I/O events fires • Node.js implements a queue storing events • All event triggers are driven by the engine of a polling event. • process.nextTick () is that we can put a program into the event queue, the next time the event polling is driven.
  • 18. Event Loop – Asynchronous Mechanism Difficult task Easy task Easy task done. Strill delayed difficult task done function readLoop() { result = file.read(); if (!result) return; /* Do something... */ process.nextTick(readLoop); } file.open(); process.nextTick(readLoop); console.log('blah blah blah...');
  • 19. Event Loop – Asynchronous Mechanism • child_process.fork • Spawn Node processes • Having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. • Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them.
  • 20. Event Loop – Asynchronous Mechanism var n = child_process.fork('./child.js'); n.on('message', function(m) { console.log('PARENT got message:', m); }); n.send({ hello: 'world' }); process.on('message', function(m) { console.log('CHILD got message:', m); }); process.send({ foo: 'bar' }); Difficult task Easy task 1 difficult task done Easy task 2 Easy task 1 done Easy task 2 done
  • 21. Event Loop – Asynchronous Mechanism • Cluster • A single instance of Node runs in a single thread. • To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load. • The cluster module allows you to easily create child processes that all share server ports.
  • 22. Event Loop – Asynchronous Mechanism var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end("hello worldn"); }).listen(8000); } Difficult task Easy task 1 Easy task 2 Easy task 1 done v Easy task 2 done difficult task done
  • 23. Event Loop – Asynchronous Mechanism
  • 24. Module • NPM(Node Package Manager) • The official package manager for Node.js. • As of Node.js version 0.6.3, npm is bundled and installed automatically with the environment. • Runs through the command line and manages dependencies for an application. • Allows users to install Node.js applications that are available on the npm registry. • Written entirely in JavaScript, and runs on the Node.js platform.
  • 25. Module • We use require and exports to connect with module var http = require("http"); function start() { function onRequest(request, response) { console.log("Request received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } exports.start = start; Module can be download with npm var server = require("./server"); server.start(); Module can be written by requirements server.js index.js
  • 26. Express • Express is a minimal and flexible node.js web application framework • Providing a robust set of features for building single and multi-page, and hybrid web applications.
  • 27. Express • >npm info express version • edit package.json • >npm install { "name": "hello-world", "description": "hello world test app", "version": "0.0.1", "private": true, "dependencies": { "express": "4.0.0" } }
  • 28. Express • Node.js has more and more resources related to development, • No need to write many features, you can directly use the module has been developed via official NPM tool from the others on the network • Developers can focus wholeheartedly on the current development of applications. • We always depend on a lot of third-party modules, and whenever we change the develop environment, it is necessary manually installing modules • package.json file directly manage project dependencies and modules which being used by project
  • 29. Express • Three outstanding features • routing distribution • request processing • rendering the view var app = express(); app.get("/hello/:who", function(req, res) { res.end("Hello, " + req.params.who + "."); }); // 啓動Express var express = require("express"); var app = express(); // 設置view目錄 app.set("views", __dirname + "/views"); // 設置模板引擎 app.set("view engine", "jade");
  • 30. Conclusion • Web applications typically have bottleneck in I/O of network, and Node.js solves them well. • Node.js uses JavaScript, which web developers already familiar with. • NPM let everyone easily put theirs modules on. • The disadvantage is that Node.js is new, not stable enough, API regularly updated.