Node.js
-
Upload
hotrannam -
Category
Technology
-
view
2.379 -
download
0
Transcript of Node.js
Multi-Threaded server
Long running operations cause threads to block.Subsequent requests are in queue.
Multi-Threaded server
Disadvantages
• Thread context switching is slow• Threads take up too much memory• Thread-per-connection does not scale
Blocking I/O
• Synchronous I/O• Wait for I/O operations to complete• Idle time
var post = db.query(“select * from posts where id = 1”);
// waiting, waiting, waiting
showPost(post);
// do something unrelated to postdoAnotherThing();
Non-Blocking I/O
• Asynchronous I/O• Continue processing• Improve throughput and latency
var post = db.query(“select * from posts where id = 1”, callback);
doAnotherThing();
callback = function(post){ showPost(post); }
Single-Threaded server
Endless event-loop fires off long running operations andthen moves to next request
Multi-Threaded vs. Single-Threaded
http://blog.webfaction.com/a-little-holiday-present
Multi-Threaded vs. Single-Threaded
http://blog.webfaction.com/a-little-holiday-present
What is Node
• Created by Ryan Dahl• Google’s V8 JavaScript engine• Event loop• Non-blocking I/O• Event binding
A simple web server
var http = require("http");
http.createServer(function(req, res){ res.writeHead(200, {"Content-Type": "text/plain"}); return res.end("Hello Barcamp Saigon\n");}).listen(8080);
console.log("Server running at port 8080");
Callback
var http = require("http");
var callback = function(req, res){ res.writeHead(200, {"Content-Type": "text/plain"}); return res.end("Hello Barcamp Saigon\n");}
http.createServer(callback).listen(8080);
console.log("Server running at port 8080");
Event binding
var fs = require(“fs");
var readStream = fs.createReadStream(“/etc/sources”);
readStream.addListener(“data”, function(data){ console.log(data);});
readStream.addListener(“end”, function(data){ console.log(“file ended”);});
Parallel vs. Serial
var fs = require("fs");function writeBarcamp(){ return fs.writeFile('./barcamp.txt', 'Hi Barcamp!', function writeFile(err){ fs.readFile('./barcamp.txt', 'UTF-8', readFile); });}function writeNode(){ return fs.writeFile('./node.txt', 'Hi Node!', function writeFile(err){ fs.readFile('./node.txt', 'UTF-8', readFile); });}function readFile(err){}
writeBarcamp();writeNode();