Top Banner
Introducing to node.js 2011.01.06 HTML5 AG Outsider
46

Introducing to node.js

Jul 02, 2015

Download

Documents

JeongHoon Byun

Presentation for HTML5 AG in Korea
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introducing to node.js

Introducingto node.js

2011.01.06HTML5 AG

Outsider

Page 2: Introducing to node.js

node.js is Server-side JavaScript

Page 3: Introducing to node.js

"Node looks like yet another take on the idea of server-side

JavaScript, but it's a lot more interesting

than that."

simon willison

Page 4: Introducing to node.js

CommonJS

started by Kevin DangoorJanuary 2009first named ServerJS renamed in August 2009http://www.commonjs.org/

Page 5: Introducing to node.js

JavaScript: not just for browsers any more!

Page 6: Introducing to node.js

"What I’m describing here is not a technical problem. It’s a matter of

people getting together and making a decision to step forward and start

building up something bigger and cooler together."

Kevin Dangoor

Page 7: Introducing to node.js

Specifications

ModulesSystemFilesystemUnit Testing....

Page 8: Introducing to node.js

Implementations

CouchDBRingoJS SproutCore node.js

Page 9: Introducing to node.js

node.js

Page 10: Introducing to node.js

history

creator : Ryan Dahl09.02.09 - V8 에 기반한 프로젝트

아이디어에 대한 글  을 올림09.02.15 - Github 에 프로젝트 시작09.11.08 - JSConf.eu 2009 발표(v0.1.16)

현재 - v0.2.6, 0.3.3

Page 11: Introducing to node.js

Motivation

Page 12: Introducing to node.js

Event Loop& non-blocking I/O

Page 13: Introducing to node.js

I/O  는 다르게 수행되어야 한

다 .

Page 14: Introducing to node.js

var result = db.query('select * from A')// use result

Page 15: Introducing to node.js

Why waiting?????

Page 16: Introducing to node.js

I/O latency

L1 : 3 cyclesL2 : 14 cyclesRAM : 250 cycles-------------------------------------------DISK : 41,000,000 cyclesNETWORK : 240,000,000 cycles

non-blocking

blocking

Page 17: Introducing to node.js

Apache vs NginX

Page 18: Introducing to node.js

Apache vs NginX

Page 19: Introducing to node.js

Single Thread Event loop

is better.

Page 20: Introducing to node.js

db.query('select * from A', function(result) { // use result});

Page 21: Introducing to node.js

왜 모두 event loop 를

사용하지 않는가 ???

Page 22: Introducing to node.js

Cultural & Infrastructual

Page 23: Introducing to node.js

Cultural

우리가 I/O  를 그렇게 배웠다

Page 24: Introducing to node.js

Cultural

puts('Enter your name : ');var neme = gets();puts('your name : ' + name);

Page 25: Introducing to node.js

Cultural

puts('Enter your name : ');gets(function (name) { puts('your name : ' + name);});

너무 복잡해서 사용안함 .

Page 26: Introducing to node.js

Infrastructual

single thread event loop 는non-blocking I/O 가 필요하다 .

Page 27: Introducing to node.js

Infrastructual

현재는 많은 인프라가 갖추어져 있다 .: Twisted, eventmachine

하지만 다른 라이브러리랑 섞어서 사용하 기가 어렵다 .

사용자가 event loop 와 non-blocking I/O 에 대한 전문 지식이 필요하다

Page 28: Introducing to node.js

Enter JavaScript

Page 29: Introducing to node.js

JavaScript

event loop 를 사용하도록 디자 인 되었다

익명함수와 클로져 한번의 하나의 콜백

DOM 이벤트 콜백을 통한 I/O 성능을 위한 확장경쟁이 붙었다 (Google,

Mozilla, Apple, Opera, MS)

Page 30: Introducing to node.js

node.jsa set of bindings to V8 JavaScript for scripting network programs

Page 31: Introducing to node.js

Features

evented Server-side JavaScriptbuilt on Google V8CommonJS module systemC 기반 , C AddonGreen Thread : one thread, one stackasyncounous, non-blocking 인터페이스

만 노출한다 펑션은 직접 I/O 에 접근하지 않는다 .

Page 32: Introducing to node.js

AdvantagesHigh-performance, FastRealtime App 에 좋다 .

다른 기술과 섞어 쓸 수 있다 .JavaScript 는 실제로 유니버셜랭귀지이다easy to Start

Page 33: Introducing to node.js

100 concurrent clients1 megabyte response

node 822 req/secnginx 708thin 85mongrel 4(bigger is better)

Page 34: Introducing to node.js

DisadvantagesMemory IssuesScale problems to Multiple CPUs or Data CenterToo Young

Page 35: Introducing to node.js

"Node.js Is Too Cool To Ignore"

Dustin McQuay

Page 36: Introducing to node.js

Usagesfrom ryannode.js v0.2.5

Page 37: Introducing to node.js

// helloworld.jsvar sys = require('sys')

setTimeout( function() { sys.puts('world');}, 2000);

sys.puts('hello');

Page 38: Introducing to node.js

// forever_helloworld.jsvar puts = require('sys').puts;

setInterval(function() { puts('hello');}, 500);process.on('SIGINT', function() { puts('good-bye'); process.exit(0);});

Page 39: Introducing to node.js

// tcp.jsvar tcp = require('net');

var server = tcp.createServer();server.on('connection', function(e) { e.write('hello!\n'); e.end();});

server.listen(8000);

Page 40: Introducing to node.js

// fileio.jsvar stat = require('fs').stat, puts = require('sys').puts;

stat('/etc/passwd', function(err, data) { if (err) throw err; puts('modified : ' + data.mtime);});

Page 41: Introducing to node.js

// simplehttp.jsvar http = require('http');

http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World\n');}).listen(8000);

Page 42: Introducing to node.js

// streamhttp.jsvar http = require('http');

http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write('Hello \n'); setTimeout(function() { res.write('world!\n'); res.end(); }, 2000);}).listen(8000);

Page 43: Introducing to node.js

// watchfile.jsvar fs = require('fs'), puts = require('sys').puts;

fs.watchFile('./test.txt', function() { puts(' 파일을 바꾸셨군요 .'); process.exit(0);});

Page 44: Introducing to node.js

node.js is ready?

Page 45: Introducing to node.js

"nodejs is awesome because it makes me

feel smart." Tobie Lagel

Page 46: Introducing to node.js

Questions...?

Blog : http://blog.outsider.ne.krTwitter : @outsider__email :[email protected]