Introduce about Nodejs - duyetdev.com

Van-Duyet Le
Van-Duyet LeStudent at University of Information Technology
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
1 of 20

Recommended

RESTful API In Node Js using Express by
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express Jeetendra singh
359 views23 slides
Original slides from Ryan Dahl's NodeJs intro talk by
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
2.1K views69 slides
Writing robust Node.js applications by
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
15.6K views82 slides
NodeJS for Beginner by
NodeJS for BeginnerNodeJS for Beginner
NodeJS for BeginnerApaichon Punopas
15K views54 slides
NodeJS: an Introduction by
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an IntroductionRoberto Casadei
213 views32 slides
NodeJS by
NodeJSNodeJS
NodeJSLinkMe Srl
783 views93 slides

More Related Content

What's hot

Nodejs vatsal shah by
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shahVatsal N Shah
1.1K views26 slides
NodeJS by
NodeJSNodeJS
NodeJS.toster
1.9K views38 slides
Node js presentation by
Node js presentationNode js presentation
Node js presentationmartincabrera
9.2K views14 slides
Philly Tech Week Introduction to NodeJS by
Philly Tech Week Introduction to NodeJSPhilly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJSRoss Kukulinski
484 views55 slides
Nodejs in Production by
Nodejs in ProductionNodejs in Production
Nodejs in ProductionWilliam Bruno Moraes
879 views29 slides
Server Side Event Driven Programming by
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven ProgrammingKamal Hussain
11K views28 slides

What's hot(20)

NodeJS by .toster
NodeJSNodeJS
NodeJS
.toster1.9K views
Philly Tech Week Introduction to NodeJS by Ross Kukulinski
Philly Tech Week Introduction to NodeJSPhilly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJS
Ross Kukulinski484 views
Server Side Event Driven Programming by Kamal Hussain
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
Kamal Hussain11K views
Introduction to node.js by jacekbecela
Introduction to node.jsIntroduction to node.js
Introduction to node.js
jacekbecela9.4K views
introduction to node.js by orkaplan
introduction to node.jsintroduction to node.js
introduction to node.js
orkaplan3.7K views
Java script at backend nodejs by Amit Thakkar
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
Amit Thakkar1.8K views
Building servers with Node.js by ConFoo
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
ConFoo12.2K views
OSCON 2011 - Node.js Tutorial by Tom Croucher
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
Tom Croucher5.3K views
Node Architecture and Getting Started with Express by jguerrero999
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
jguerrero9991.9K views
Getting started with developing Nodejs by Phil Hawksworth
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing Nodejs
Phil Hawksworth2.6K views
Node.js and How JavaScript is Changing Server Programming by Tom Croucher
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher10.4K views
All aboard the NodeJS Express by David Boyer
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
David Boyer11.3K views
node.js: Javascript's in your backend by David Padbury
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury12.6K views
Comet with node.js and V8 by amix3k
Comet with node.js and V8Comet with node.js and V8
Comet with node.js and V8
amix3k16.4K views
A million connections and beyond - Node.js at scale by Tom Croucher
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher32.7K views
Intro to Node.js (v1) by Chris Cowan
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
Chris Cowan1.5K views

Viewers also liked

[LvDuit//Lab] Crawling the web by
[LvDuit//Lab] Crawling the web[LvDuit//Lab] Crawling the web
[LvDuit//Lab] Crawling the webVan-Duyet Le
931 views25 slides
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM by
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMJonas Bonér
11.9K views93 slides
Nodejs by
NodejsNodejs
NodejsToan Nguyen
2.1K views17 slides
Asynchronous I/O in NodeJS - new standard or challenges? by
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Dinh Pham
2.4K views26 slides
Hướng dẫn giải bài tập chuỗi - Toán cao cấp by
Hướng dẫn giải bài tập chuỗi - Toán cao cấpHướng dẫn giải bài tập chuỗi - Toán cao cấp
Hướng dẫn giải bài tập chuỗi - Toán cao cấpVan-Duyet Le
450.2K views14 slides
58 công thức giải nhanh hóa học by
58 công thức giải nhanh hóa học58 công thức giải nhanh hóa học
58 công thức giải nhanh hóa họcVan-Duyet Le
297K views7 slides

Viewers also liked(6)

[LvDuit//Lab] Crawling the web by Van-Duyet Le
[LvDuit//Lab] Crawling the web[LvDuit//Lab] Crawling the web
[LvDuit//Lab] Crawling the web
Van-Duyet Le931 views
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM by Jonas Bonér
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
Jonas Bonér11.9K views
Asynchronous I/O in NodeJS - new standard or challenges? by Dinh Pham
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham2.4K views
Hướng dẫn giải bài tập chuỗi - Toán cao cấp by Van-Duyet Le
Hướng dẫn giải bài tập chuỗi - Toán cao cấpHướng dẫn giải bài tập chuỗi - Toán cao cấp
Hướng dẫn giải bài tập chuỗi - Toán cao cấp
Van-Duyet Le450.2K views
58 công thức giải nhanh hóa học by Van-Duyet Le
58 công thức giải nhanh hóa học58 công thức giải nhanh hóa học
58 công thức giải nhanh hóa học
Van-Duyet Le297K views

Similar to Introduce about Nodejs - duyetdev.com

Introduction to Node.js by
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
71.2K views21 slides
Node by
NodeNode
NodeAnkit Chawla
63 views13 slides
Proposal by
ProposalProposal
ProposalConstantine Priemski
2.2K views20 slides
Nodejs by
NodejsNodejs
NodejsVinod Kumar Marupu
443 views21 slides
Node js by
Node jsNode js
Node jsChirag Parmar
2.9K views28 slides
NodeJS by
NodeJSNodeJS
NodeJSAlok Guha
1.6K views21 slides

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

Introduction to Node.js by Vikash Singh
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh71.2K views
NodeJS by Alok Guha
NodeJSNodeJS
NodeJS
Alok Guha1.6K views
Day in a life of a node.js developer by Edureka!
Day in a life of a node.js developerDay in a life of a node.js developer
Day in a life of a node.js developer
Edureka!2.5K views
Day In A Life Of A Node.js Developer by Edureka!
Day In A Life Of A Node.js DeveloperDay In A Life Of A Node.js Developer
Day In A Life Of A Node.js Developer
Edureka!1.2K views
soft-shake.ch - Hands on Node.js by soft-shake.ch
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
soft-shake.ch2.3K views
Real World Lessons on the Pain Points of Node.JS Application by Ben Hall
Real World Lessons on the Pain Points of Node.JS ApplicationReal World Lessons on the Pain Points of Node.JS Application
Real World Lessons on the Pain Points of Node.JS Application
Ben Hall688 views
Kalp Corporate Node JS Perfect Guide by Kalp Corporate
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
Kalp Corporate324 views
Tech io nodejs_20130531_v0.6 by Ganesh Kondal
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6
Ganesh Kondal1.4K views
NodeJS : Communication and Round Robin Way by Edureka!
NodeJS : Communication and Round Robin WayNodeJS : Communication and Round Robin Way
NodeJS : Communication and Round Robin Way
Edureka!2.9K views
Basic Understanding and Implement of Node.js by Gary Yeh
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
Gary Yeh886 views
Node.js: A Guided Tour by cacois
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois3.1K views
Node js introduction by Alex Su
Node js introductionNode js introduction
Node js introduction
Alex Su2.7K views

More from Van-Duyet Le

CTDL&GT: Các loại danh sách liên kết by
CTDL&GT: Các loại danh sách liên kếtCTDL&GT: Các loại danh sách liên kết
CTDL&GT: Các loại danh sách liên kếtVan-Duyet Le
714 views44 slides
Bài tập tích phân suy rộng. by
Bài tập tích phân suy rộng.Bài tập tích phân suy rộng.
Bài tập tích phân suy rộng.Van-Duyet Le
28.3K views1 slide
Tổng hợp 35 câu hỏi phần triết học kèm trả lời by
Tổng hợp 35 câu hỏi phần triết học kèm trả lờiTổng hợp 35 câu hỏi phần triết học kèm trả lời
Tổng hợp 35 câu hỏi phần triết học kèm trả lờiVan-Duyet Le
88.4K views47 slides
Giáo trình C căn bản. by
Giáo trình C căn bản.Giáo trình C căn bản.
Giáo trình C căn bản.Van-Duyet Le
3.4K views135 slides
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 by
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ợpBà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 tổng hợp dao động điều hòa - Con lắc đơn - Con lắc lò xo - Tổng hợpVan-Duyet Le
16.5K views19 slides
Bài tập điện xoay chiều by
Bài tập điện xoay chiềuBài tập điện xoay chiều
Bài tập điện xoay chiềuVan-Duyet Le
8.5K views8 slides

More from Van-Duyet Le(20)

CTDL&GT: Các loại danh sách liên kết by Van-Duyet Le
CTDL&GT: Các loại danh sách liên kếtCTDL&GT: Các loại danh sách liên kết
CTDL&GT: Các loại danh sách liên kết
Van-Duyet Le714 views
Bài tập tích phân suy rộng. by Van-Duyet Le
Bài tập tích phân suy rộng.Bài tập tích phân suy rộng.
Bài tập tích phân suy rộng.
Van-Duyet Le28.3K views
Tổng hợp 35 câu hỏi phần triết học kèm trả lời by Van-Duyet Le
Tổng hợp 35 câu hỏi phần triết học kèm trả lờiTổng hợp 35 câu hỏi phần triết học kèm trả lời
Tổng hợp 35 câu hỏi phần triết học kèm trả lời
Van-Duyet Le88.4K views
Giáo trình C căn bản. by Van-Duyet Le
Giáo trình C căn bản.Giáo trình C căn bản.
Giáo trình C căn bản.
Van-Duyet Le3.4K views
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 by Van-Duyet Le
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ợpBà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 tổng hợp dao động điều hòa - Con lắc đơn - Con lắc lò xo - Tổng hợp
Van-Duyet Le16.5K views
Bài tập điện xoay chiều by Van-Duyet Le
Bài tập điện xoay chiềuBài tập điện xoay chiều
Bài tập điện xoay chiều
Van-Duyet Le8.5K views
Phương pháp: 10 dạng bài tập dao động điều hòa by Van-Duyet Le
Phương pháp: 10 dạng bài tập dao động điều hòaPhương pháp: 10 dạng bài tập dao động điều hòa
Phương pháp: 10 dạng bài tập dao động điều hòa
Van-Duyet Le49.8K views
Trắc nghiệm điện xoay chiều by Van-Duyet Le
Trắc nghiệm điện xoay chiềuTrắc nghiệm điện xoay chiều
Trắc nghiệm điện xoay chiều
Van-Duyet Le1.5K views
Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động... by Van-Duyet Le
Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động...Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động...
Con lắc đơn - Con lắc lò xo - Tổng hợp dao động - Dao động tắt dần - Dao động...
Van-Duyet Le83K views
67 Bài Tập về Phương trình mũ và Phương trình Logarit by Van-Duyet Le
67 Bài Tập về Phương trình mũ và Phương trình Logarit67 Bài Tập về Phương trình mũ và Phương trình Logarit
67 Bài Tập về Phương trình mũ và Phương trình Logarit
Van-Duyet Le76.8K views
Kĩ thuật giải các loại hệ phương trình by Van-Duyet Le
Kĩ thuật giải các loại hệ phương trìnhKĩ thuật giải các loại hệ phương trình
Kĩ thuật giải các loại hệ phương trình
Van-Duyet Le3.4K views
Reported Speech (NC) by Van-Duyet Le
Reported Speech (NC)Reported Speech (NC)
Reported Speech (NC)
Van-Duyet Le502 views
3000 từ tiếng Anh thông dụng by Van-Duyet Le
3000 từ tiếng Anh thông dụng3000 từ tiếng Anh thông dụng
3000 từ tiếng Anh thông dụng
Van-Duyet Le1.5K views
Thứ sáu ngày 13 với toán đồng dư. by Van-Duyet Le
Thứ sáu ngày 13 với toán đồng dư.Thứ sáu ngày 13 với toán đồng dư.
Thứ sáu ngày 13 với toán đồng dư.
Van-Duyet Le354 views
GEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch Mã by Van-Duyet Le
GEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch MãGEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch Mã
GEN - ADN - Nhân Đôi ADN - Phiên Mã - Dịch Mã
Van-Duyet Le81.6K views
[Sinh 12] 140 câu tiến hóa by Van-Duyet Le
[Sinh 12] 140 câu tiến hóa[Sinh 12] 140 câu tiến hóa
[Sinh 12] 140 câu tiến hóa
Van-Duyet Le10.7K views
Toán DH (THPT Lê Lợi) by Van-Duyet Le
Toán DH (THPT Lê Lợi)Toán DH (THPT Lê Lợi)
Toán DH (THPT Lê Lợi)
Van-Duyet Le2.4K views
Tóan Trần Hưng Đạo DH by Van-Duyet Le
Tóan Trần Hưng Đạo DHTóan Trần Hưng Đạo DH
Tóan Trần Hưng Đạo DH
Van-Duyet Le692 views
4. thi thu lan 2 125 2012 hq by Van-Duyet Le
4. thi thu lan 2  125 2012 hq4. thi thu lan 2  125 2012 hq
4. thi thu lan 2 125 2012 hq
Van-Duyet Le673 views
191 câu trắc nghiệm chương 1, 2 - Sinh học 12 by Van-Duyet Le
191 câu trắc nghiệm chương 1, 2 - Sinh học 12191 câu trắc nghiệm chương 1, 2 - Sinh học 12
191 câu trắc nghiệm chương 1, 2 - Sinh học 12
Van-Duyet Le94.6K views

Recently uploaded

Business Analyst Series 2023 - Week 3 Session 5 by
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5DianaGray10
345 views20 slides
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveNetwork Automation Forum
43 views35 slides
Future of Indian ConsumerTech by
Future of Indian ConsumerTechFuture of Indian ConsumerTech
Future of Indian ConsumerTechKapil Khandelwal (KK)
24 views68 slides
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
38 views43 slides
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdfDr. Jimmy Schwarzkopf
24 views29 slides
Democratising digital commerce in India-Report by
Democratising digital commerce in India-ReportDemocratising digital commerce in India-Report
Democratising digital commerce in India-ReportKapil Khandelwal (KK)
20 views161 slides

Recently uploaded(20)

Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10345 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman38 views
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
Unit 1_Lecture 2_Physical Design of IoT.pdf by StephenTec
Unit 1_Lecture 2_Physical Design of IoT.pdfUnit 1_Lecture 2_Physical Design of IoT.pdf
Unit 1_Lecture 2_Physical Design of IoT.pdf
StephenTec15 views
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab23 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson126 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views

Introduce about Nodejs - duyetdev.com

  • 1. The Server-side JavaScript Van-Duyet Le me@duyetdev.com Introduce about
  • 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