SlideShare a Scribd company logo
The Server-side JavaScript
Van-Duyet Le
me@duyetdev.com
Introduce about
What to expect ahead….
Introduction
Some (Confusing) Theory
5 Examples
A couple of weird diagrams
2 Pics showing unbelievable benchmarks
Some stuff from Internet
And Homer Simpson
Background
 V8 is an open source JavaScript engine developed by
Google. Its written in C++ and is used in Google Chrome
Browser.
 Node.js runs on V8.
 It was created by Ryan Dahl in 2009.
 Latest version is 4.0.0
 Is Open Source. It runs well on Linux systems, can also run
on Windows systems.
Introduction: Basic
In simple words Node.js is ‘server-side
JavaScript’.
In not-so-simple words Node.js is a high-performance
network applications framework, well
optimized for high concurrent environments.
It’s a command line tool.
In ‘Node.js’ , ‘.js’ doesn’t mean that its solely written
JavaScript. It is 40% JS and 60% C++.
From the official site:
‘Node's goal is to provide an easy way to build
scalable network programs’ - (from nodejs.org!)
Introduction: Advanced (& Confusing)
Node.js uses an event-driven, non-blocking I/O
model, which makes it lightweight. (from
nodejs.org!)
It makes use of event-loops via JavaScript’s
callback functionality to implement the non-
blocking I/O.
Programs for Node.js are written in JavaScript but
not in the same JavaScript we are use to.
Everything inside Node.js runs in a single-thread.
Example-1: Getting Started & Hello World
Install/build Node.js.
Open your favorite editor and start typing
JavaScript.
When you are done, open cmd/terminal and type
this:
‘node your_file.js’
Here is a simple example, which prints ‘hello world’
var sys = require(“sys”);
setTimeout(function(){
sys.puts(“world”);},3000);
sys.puts(“hello”);
//it prints ‘hello’ first and waits for 3 seconds and then
prints ‘world’
Some Theory: Event-loops
Event-loops are the core of event-driven programming,
almost all the UI programs use event-loops to track the
user event, for example: Clicks, Ajax Requests etc.
Client
Event loop
(main thread)
C++
Threadpool
(worker
threads)
Clients send HTTP requests
to Node.js server
An Event-loop is woken up by OS,
passes request and response objects
to the thread-pool
Long-running jobs run
on worker threads
Response is sent
back to main thread
via callback
Event loop returns
result to client
Some Theory: Non-Blocking I/O
Traditional I/O
var result = db.query(“select x from table_Y”);
doSomethingWith(result); //wait for result!
doSomethingWithOutResult(); //execution is blocked!
Non-traditional, Non-blocking I/O
db.query(“select x from table_Y”,function (result){
doSomethingWith(result); //wait for result!
});
doSomethingWithOutResult(); //executes without any delay!
What can you do with Node.js ?
 You can create an HTTP server and print ‘hello world’
on the browser in just 4 lines of JavaScript.
 You can create a TCP server similar to HTTP server, in
just 4 lines of JavaScript.
 You can create a DNS server.
 You can create a Static File Server.
 You can create a Web Chat Application.
 Node.js can also be used for creating online games,
collaboration tools or anything which sends updates to
the user in real-time.
Example -2 &3 (HTTP Server & TCP
Server)
 Following code creates an HTTP Server and prints ‘Hello World’ on
the browser:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn'); }).listen(5000,
"127.0.0.1");
 Here is an example of a simple TCP server which listens on port
6000 and echoes whatever you send it:
var net = require('net');
net.createServer(function (socket) {
socket.write("Echo serverrn");
socket.pipe(socket); }).listen(6000, "127.0.0.1");
Node.js Ecosystem
 Node.js heavily relies on modules, in previous examples
require keyword loaded the http & net modules.
 Creating a module is easy, just put your JavaScript code in a
separate js file and include it in your code by using keyword
require, like:
var modulex = require(‘./modulex’);
 Libraries in Node.js are called packages and they can be
installed by typing
npm install “package_name”; //package should be
available in npm registry @ nmpjs.org
 NPM (Node Package Manager) comes bundled with Node.js
installation.
Example-4: Lets connect to a DB
(Mongoose)
Install mongoose using npm, elegant mongodb object
modeling for node.js
npm install mongoose
Code to retrieve all the documents from a collection:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Cat = mongoose.model('Cat', { name: String });
var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
When to use Node.js?
Node.js is good for creating streaming based real-
time services, web chat applications, static file
servers etc.
If you need high level concurrency and not worried
about CPU-cycles.
You can use the same language at both the places:
server-side and client-side.
Example-5: Twitter Streaming
Install nTwitter module using npm:
Npm install ntwitter
Code:
var twitter = require('ntwitter');
var twit = new twitter({
consumer_key: ‘c_key’,
consumer_secret: ‘c_secret’,
access_token_key: ‘token_key’,
access_token_secret: ‘token_secret’});
twit.stream('statuses/sample', function(stream) {
stream.on('data', function (data) {
console.log(data); });
});
Some Node.js benchmarks
Taken from:
http://code.google.com/p/node-js-vs-
apache-php-benchmark/wiki/Tests
A benchmark between Apache+PHP
and node.js, shows the response
time for 1000 concurrent connections
making 10,000 requests each, for 5
tests.
Taken from:
http://nodejs.org/jsconf2010.p
df
The benchmark shows the
response time in milli-secs
for 4 evented servers.
When to not use Node.js
When you are doing heavy and CPU intensive
calculations on server side, because event-loops
are CPU hungry.
Most of the packages are also unstable. Therefore
is not yet production ready.
Read further on disadvantages of Node.js on Quora:
http://www.quora.com/What-are-the-disadvantages-
of-using-Node-js
Appendix-1: Who is using Node.js in
production?
Yahoo! : iPad App Livestand uses Yahoo!
Manhattan framework which is based on Node.js.
LinkedIn : LinkedIn uses a combination of Node.js
and MongoDB for its mobile platform. iOS and
Android apps are based on it.
 eBay : Uses Node.js along with ql.io to help
application developers in improving eBay’s end user
experience.
Complete list can be found at:
https://github.com/joyent/node/wiki/Projects,-
Applications,-and-Companies-Using-Node
Appendix-2: Resource to get started
Watch this video at Youtube:
http://www.youtube.com/watch?v=jo_B4LTHi3I
Read the free O’reilly Book ‘Up and Running with Node.js’
@ http://ofps.oreilly.com/titles/9781449398583/
Visit www.nodejs.org for Info/News about Node.js
Watch Node.js tutorials @ http://nodetuts.com/
For Info on MongoDB:
http://www.mongodb.org/display/DOCS/Home
For anything else Google!
Appendix-3: Some Good Modules
Express – to make things simpler e.g. syntax, DB
connections.
Jade – HTML template system
Socket.IO – to create real-time apps
Nodemon – to monitor Node.js and push change
automatically
CoffeeScript – for easier JavaScript development
Introduce about Nodejs - duyetdev.com

More Related Content

PPT
RESTful API In Node Js using Express
PDF
Original slides from Ryan Dahl's NodeJs intro talk
KEY
Writing robust Node.js applications
PDF
NodeJS for Beginner
PDF
NodeJS: an Introduction
PDF
NodeJS
PPTX
Introduction to Node js
PPTX
Introduction Node.js
RESTful API In Node Js using Express
Original slides from Ryan Dahl's NodeJs intro talk
Writing robust Node.js applications
NodeJS for Beginner
NodeJS: an Introduction
NodeJS
Introduction to Node js
Introduction Node.js

What's hot (20)

PDF
Nodejs vatsal shah
KEY
NodeJS
PPT
Node js presentation
PDF
Philly Tech Week Introduction to NodeJS
PDF
Nodejs in Production
PDF
Server Side Event Driven Programming
KEY
Introduction to node.js
PPTX
introduction to node.js
PPTX
Java script at backend nodejs
PDF
Building servers with Node.js
KEY
OSCON 2011 - Node.js Tutorial
PDF
Node Architecture and Getting Started with Express
PDF
Getting started with developing Nodejs
PDF
Node.js and How JavaScript is Changing Server Programming
PDF
All aboard the NodeJS Express
KEY
node.js: Javascript's in your backend
PDF
Comet with node.js and V8
KEY
A million connections and beyond - Node.js at scale
PPTX
Intro to Node.js (v1)
Nodejs vatsal shah
NodeJS
Node js presentation
Philly Tech Week Introduction to NodeJS
Nodejs in Production
Server Side Event Driven Programming
Introduction to node.js
introduction to node.js
Java script at backend nodejs
Building servers with Node.js
OSCON 2011 - Node.js Tutorial
Node Architecture and Getting Started with Express
Getting started with developing Nodejs
Node.js and How JavaScript is Changing Server Programming
All aboard the NodeJS Express
node.js: Javascript's in your backend
Comet with node.js and V8
A million connections and beyond - Node.js at scale
Intro to Node.js (v1)
Ad

Similar to Introduce about Nodejs - duyetdev.com (20)

PPTX
Introduction to Node.js
PPTX
PPTX
PPTX
PPT
Node js
PPTX
NodeJS
PDF
Day In A Life Of A Node.js Developer
PDF
Day in a life of a node.js developer
PPTX
Nodejs intro
PDF
soft-shake.ch - Hands on Node.js
PPTX
Real World Lessons on the Pain Points of Node.JS Application
PPTX
Kalp Corporate Node JS Perfect Guide
PDF
Tech io nodejs_20130531_v0.6
PDF
NodeJS : Communication and Round Robin Way
PDF
Basic Understanding and Implement of Node.js
PPT
Node js Modules and Event Emitters
DOCX
unit 2 of Full stack web development subject
PPT
Node js beginner
PPTX
Node.js: A Guided Tour
PDF
Node js introduction
Introduction to Node.js
Node js
NodeJS
Day In A Life Of A Node.js Developer
Day in a life of a node.js developer
Nodejs intro
soft-shake.ch - Hands on Node.js
Real World Lessons on the Pain Points of Node.JS Application
Kalp Corporate Node JS Perfect Guide
Tech io nodejs_20130531_v0.6
NodeJS : Communication and Round Robin Way
Basic Understanding and Implement of Node.js
Node js Modules and Event Emitters
unit 2 of Full stack web development subject
Node js beginner
Node.js: A Guided Tour
Node js introduction
Ad

More from Van-Duyet Le (20)

PDF
[LvDuit//Lab] Crawling the web
PDF
CTDL&GT: Các loại danh sách liên kết
PDF
Bài tập tích phân suy rộng.
PDF
Tổng hợp 35 câu hỏi phần triết học kèm trả lời
PDF
Hướng dẫn giải bài tập chuỗi - Toán cao cấp
PDF
Giáo trình C căn bản.
DOC
58 công thức giải nhanh hóa học
DOC
Bài tập tổng hợp dao động điều hòa - Con lắc đơn - Con lắc lò xo - Tổng hợp
DOC
Bài tập điện xoay chiều
DOC
Phương pháp: 10 dạng bài tập dao động điều hòa
DOC
Trắc nghiệm điện xoay chiều
DOC
Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động...
DOC
67 Bài Tập về Phương trình mũ và Phương trình Logarit
DOC
Kĩ thuật giải các loại hệ phương trình
DOC
Reported Speech (NC)
PDF
3000 từ tiếng Anh thông dụng
DOC
Thứ sáu ngày 13 với toán đồng dư.
DOC
GEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch Mã
PDF
[Sinh 12] 140 câu tiến hóa
PDF
Toán DH (THPT Lê Lợi)
[LvDuit//Lab] Crawling the web
CTDL&GT: Các loại danh sách liên kết
Bài tập tích phân suy rộng.
Tổng hợp 35 câu hỏi phần triết học kèm trả lời
Hướng dẫn giải bài tập chuỗi - Toán cao cấp
Giáo trình C căn bản.
58 công thức giải nhanh hóa học
Bài tập tổng hợp dao động điều hòa - Con lắc đơn - Con lắc lò xo - Tổng hợp
Bài tập điện xoay chiều
Phương pháp: 10 dạng bài tập dao động điều hòa
Trắc nghiệm điện xoay chiều
Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động...
67 Bài Tập về Phương trình mũ và Phương trình Logarit
Kĩ thuật giải các loại hệ phương trình
Reported Speech (NC)
3000 từ tiếng Anh thông dụng
Thứ sáu ngày 13 với toán đồng dư.
GEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch Mã
[Sinh 12] 140 câu tiến hóa
Toán DH (THPT Lê Lợi)

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Approach and Philosophy of On baking technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
August Patch Tuesday
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Approach and Philosophy of On baking technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
cloud_computing_Infrastucture_as_cloud_p
Network Security Unit 5.pdf for BCA BBA.
Heart disease approach using modified random forest and particle swarm optimi...
Mushroom cultivation and it's methods.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
1. Introduction to Computer Programming.pptx
August Patch Tuesday
NewMind AI Weekly Chronicles - August'25-Week II
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Introduce about Nodejs - duyetdev.com

  • 2. What to expect ahead…. Introduction Some (Confusing) Theory 5 Examples A couple of weird diagrams 2 Pics showing unbelievable benchmarks Some stuff from Internet And Homer Simpson
  • 3. Background  V8 is an open source JavaScript engine developed by Google. Its written in C++ and is used in Google Chrome Browser.  Node.js runs on V8.  It was created by Ryan Dahl in 2009.  Latest version is 4.0.0  Is Open Source. It runs well on Linux systems, can also run on Windows systems.
  • 4. Introduction: Basic In simple words Node.js is ‘server-side JavaScript’. In not-so-simple words Node.js is a high-performance network applications framework, well optimized for high concurrent environments. It’s a command line tool. In ‘Node.js’ , ‘.js’ doesn’t mean that its solely written JavaScript. It is 40% JS and 60% C++. From the official site: ‘Node's goal is to provide an easy way to build scalable network programs’ - (from nodejs.org!)
  • 5. Introduction: Advanced (& Confusing) Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight. (from nodejs.org!) It makes use of event-loops via JavaScript’s callback functionality to implement the non- blocking I/O. Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to. Everything inside Node.js runs in a single-thread.
  • 6. Example-1: Getting Started & Hello World Install/build Node.js. Open your favorite editor and start typing JavaScript. When you are done, open cmd/terminal and type this: ‘node your_file.js’ Here is a simple example, which prints ‘hello world’ var sys = require(“sys”); setTimeout(function(){ sys.puts(“world”);},3000); sys.puts(“hello”); //it prints ‘hello’ first and waits for 3 seconds and then prints ‘world’
  • 7. Some Theory: Event-loops Event-loops are the core of event-driven programming, almost all the UI programs use event-loops to track the user event, for example: Clicks, Ajax Requests etc. Client Event loop (main thread) C++ Threadpool (worker threads) Clients send HTTP requests to Node.js server An Event-loop is woken up by OS, passes request and response objects to the thread-pool Long-running jobs run on worker threads Response is sent back to main thread via callback Event loop returns result to client
  • 8. Some Theory: Non-Blocking I/O Traditional I/O var result = db.query(“select x from table_Y”); doSomethingWith(result); //wait for result! doSomethingWithOutResult(); //execution is blocked! Non-traditional, Non-blocking I/O db.query(“select x from table_Y”,function (result){ doSomethingWith(result); //wait for result! }); doSomethingWithOutResult(); //executes without any delay!
  • 9. What can you do with Node.js ?  You can create an HTTP server and print ‘hello world’ on the browser in just 4 lines of JavaScript.  You can create a TCP server similar to HTTP server, in just 4 lines of JavaScript.  You can create a DNS server.  You can create a Static File Server.  You can create a Web Chat Application.  Node.js can also be used for creating online games, collaboration tools or anything which sends updates to the user in real-time.
  • 10. Example -2 &3 (HTTP Server & TCP Server)  Following code creates an HTTP Server and prints ‘Hello World’ on the browser: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(5000, "127.0.0.1");  Here is an example of a simple TCP server which listens on port 6000 and echoes whatever you send it: var net = require('net'); net.createServer(function (socket) { socket.write("Echo serverrn"); socket.pipe(socket); }).listen(6000, "127.0.0.1");
  • 11. Node.js Ecosystem  Node.js heavily relies on modules, in previous examples require keyword loaded the http & net modules.  Creating a module is easy, just put your JavaScript code in a separate js file and include it in your code by using keyword require, like: var modulex = require(‘./modulex’);  Libraries in Node.js are called packages and they can be installed by typing npm install “package_name”; //package should be available in npm registry @ nmpjs.org  NPM (Node Package Manager) comes bundled with Node.js installation.
  • 12. Example-4: Lets connect to a DB (Mongoose) Install mongoose using npm, elegant mongodb object modeling for node.js npm install mongoose Code to retrieve all the documents from a collection: var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var Cat = mongoose.model('Cat', { name: String }); var kitty = new Cat({ name: 'Zildjian' }); kitty.save(function (err) { if (err) // ... console.log('meow'); });
  • 13. When to use Node.js? Node.js is good for creating streaming based real- time services, web chat applications, static file servers etc. If you need high level concurrency and not worried about CPU-cycles. You can use the same language at both the places: server-side and client-side.
  • 14. Example-5: Twitter Streaming Install nTwitter module using npm: Npm install ntwitter Code: var twitter = require('ntwitter'); var twit = new twitter({ consumer_key: ‘c_key’, consumer_secret: ‘c_secret’, access_token_key: ‘token_key’, access_token_secret: ‘token_secret’}); twit.stream('statuses/sample', function(stream) { stream.on('data', function (data) { console.log(data); }); });
  • 15. Some Node.js benchmarks Taken from: http://code.google.com/p/node-js-vs- apache-php-benchmark/wiki/Tests A benchmark between Apache+PHP and node.js, shows the response time for 1000 concurrent connections making 10,000 requests each, for 5 tests. Taken from: http://nodejs.org/jsconf2010.p df The benchmark shows the response time in milli-secs for 4 evented servers.
  • 16. When to not use Node.js When you are doing heavy and CPU intensive calculations on server side, because event-loops are CPU hungry. Most of the packages are also unstable. Therefore is not yet production ready. Read further on disadvantages of Node.js on Quora: http://www.quora.com/What-are-the-disadvantages- of-using-Node-js
  • 17. Appendix-1: Who is using Node.js in production? Yahoo! : iPad App Livestand uses Yahoo! Manhattan framework which is based on Node.js. LinkedIn : LinkedIn uses a combination of Node.js and MongoDB for its mobile platform. iOS and Android apps are based on it.  eBay : Uses Node.js along with ql.io to help application developers in improving eBay’s end user experience. Complete list can be found at: https://github.com/joyent/node/wiki/Projects,- Applications,-and-Companies-Using-Node
  • 18. Appendix-2: Resource to get started Watch this video at Youtube: http://www.youtube.com/watch?v=jo_B4LTHi3I Read the free O’reilly Book ‘Up and Running with Node.js’ @ http://ofps.oreilly.com/titles/9781449398583/ Visit www.nodejs.org for Info/News about Node.js Watch Node.js tutorials @ http://nodetuts.com/ For Info on MongoDB: http://www.mongodb.org/display/DOCS/Home For anything else Google!
  • 19. Appendix-3: Some Good Modules Express – to make things simpler e.g. syntax, DB connections. Jade – HTML template system Socket.IO – to create real-time apps Nodemon – to monitor Node.js and push change automatically CoffeeScript – for easier JavaScript development