Post on 10-May-2015
transcript
node.js daonode.js daoVova MiguroVova Miguro
What’s it all about?What’s it all about?
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
How?How?
Let’s keep slow operations Let’s keep slow operations from blocking other from blocking other operations.operations.
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...
async I/Oasync I/O
file.read(file.read(‘image.png’‘image.png’,,functionfunction(data){(data){ doSomething(image);doSomething(image); });}); doSomethingElse();doSomethingElse();
profit!profit!
callback()callback()
callback()callback()callback()callback()
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)
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 }}
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););
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!');); });});
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 });}); });}); });});
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);
});});
let’s see...let’s see...
suited for...suited for...
• real timereal time
• streamingstreaming
• process monitoringprocess monitoring
• JSON API’sJSON API’s
do not use for this...do not use for this...
• CPU heavy appsCPU heavy apps
• data transformationdata transformation
??