Владимир Мигуро "Дао node.js"

18
node.js dao node.js dao Vova Miguro Vova Miguro

Upload: miniq-epam-systems-inc

Post on 10-May-2015

1.691 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Владимир Мигуро "Дао Node.js"

node.js daonode.js daoVova MiguroVova Miguro

Page 2: Владимир Мигуро "Дао Node.js"

What’s it all about?What’s it all about?

Page 3: Владимир Мигуро "Дао Node.js"

Node.jsNode.js is a platform built on is a platform built on Chrome’s JavaScriptChrome’s JavaScript runtimeruntime for for easily building fast, scalable network easily building fast, scalable network applications.applications.

-nodejs.org

Page 4: Владимир Мигуро "Дао Node.js"

How?How?

Let’s keep slow operations Let’s keep slow operations from blocking other from blocking other operations.operations.

Page 5: Владимир Мигуро "Дао Node.js"

traditional I/Otraditional I/O

varvar data = file.read( data = file.read(‘image.png’‘image.png’);); //zzz...//zzz... doSomething(data);doSomething(data);

something not right here...something not right here...

Page 6: Владимир Мигуро "Дао Node.js"

async I/Oasync I/O

file.read(file.read(‘image.png’‘image.png’,,functionfunction(data){(data){ doSomething(image);doSomething(image); });}); doSomethingElse();doSomethingElse();

profit!profit!

Page 7: Владимир Мигуро "Дао Node.js"

callback()callback()

callback()callback()callback()callback()

Page 8: Владимир Мигуро "Дао Node.js"

node.js internalsnode.js internals

• v8v8 (yeah, it’s Chrome’s engine) (yeah, it’s Chrome’s engine)

• libevlibev (event loop) (event loop)

• libeiolibeio (asynchronous version of (asynchronous version of POSIX API)POSIX API)

Page 9: Владимир Мигуро "Дао Node.js"

it is only one thread!it is only one thread!

file.read(file.read(‘file.txt’‘file.txt’,,functionfunction(data){(data){//will never fire...//will never fire...

})}) whilewhile((truetrue){){

//blocking entire process//blocking entire process }}

Page 10: Владимир Мигуро "Дао Node.js"

http serverhttp server

varvar http = require( http = require('http''http');); http.createServer(http.createServer(functionfunction (req, res) { (req, res) { res.writeHead(res.writeHead(200200, {, {'Content-Type''Content-Type': : 'text/plain''text/plain'});}); res.end(res.end('Hello World\n''Hello World\n');); }).listen(}).listen(30003000););

Page 11: Владимир Мигуро "Дао Node.js"

watch filewatch file

varvar fs = require( fs = require('fs''fs');); fs.watchFile(fs.watchFile('system.log''system.log',,functionfunction () { () { console.log(console.log('log changed!''log changed!');); });});

Page 12: Владимир Мигуро "Дао Node.js"
Page 13: Владимир Мигуро "Дао Node.js"

node & mongonode & mongo

varvar mongodb = require( mongodb = require('mongodb''mongodb');); varvar server = server = newnew Server( Server('127.0.0.1''127.0.0.1', , 2701727017, {});, {}); newnew Db( Db('test''test', server, {}).open(, server, {}).open(functionfunction (error, client) { (error, client) { varvar collection = collection = newnew Collection(client, Collection(client, 'tmp''tmp');); //insert doc//insert doc collection.insert({a:collection.insert({a:1212,b:,b:'string''string'},{},function(){},{},function(){ //find doc//find doc collection.find({}, {limit:collection.find({}, {limit:1010}).toArray(}).toArray(functionfunction(err, docs) {(err, docs) { //do smth with docs//do smth with docs });}); });}); });});

Page 14: Владимир Мигуро "Дао Node.js"

node & web socketsnode & web sockets

//server//server

var io = require('socket.io').listen(80); io.sockets.on(io.sockets.on('connection''connection', , functionfunction (socket) { (socket) { socket.emit(socket.emit('news''news', {hello:, {hello:'world''world'});});

});}); //client//client

varvar socket = io.connect( socket = io.connect(''http://localhost'););

socket.on(socket.on('news''news', , functionfunction (data) { (data) {

console.log(data);console.log(data);

});});

Page 15: Владимир Мигуро "Дао Node.js"

let’s see...let’s see...

Page 16: Владимир Мигуро "Дао Node.js"

suited for...suited for...

• real timereal time

• streamingstreaming

• process monitoringprocess monitoring

• JSON API’sJSON API’s

Page 17: Владимир Мигуро "Дао Node.js"

do not use for this...do not use for this...

• CPU heavy appsCPU heavy apps

• data transformationdata transformation

Page 18: Владимир Мигуро "Дао Node.js"

??