0
Building Servers  with Node.js         Travis Swicegood       #nodetalk   Licensed under Creative Commons,        Attribut...
Hi, I’m Travisbuilding servers with node.js   some rights reserved
#nodetalkbuilding servers with node.js   some rights reserved
What’s this               talk about?building servers with node.js   some rights reserved
What is                 Node.js?building servers with node.js   some rights reserved
Serversbuilding servers with node.js   some rights reserved
GTDbuilding servers with node.js         some rights reserved
Is it Right           For Me?building servers with node.js   some rights reserved
What is                 Node.js?building servers with node.js   some rights reserved
Evented I/O                   Networking                     Toolkitbuilding servers with node.js   some rights reserved
What’s “evented?”building servers with node.js   some rights reserved
Some Codebuilding servers with node.js      some rights reserved
Standard Data Store     var db = new Database({host: "localhost"});     db.connect();     var result = db.find({name: "a-p...
Evented Data Store     var db = new Database({host: "localhost"});     db.connect(function(conn) {       conn.find({name: ...
Event Loopbuilding servers with node.js       some rights reserved
An Analogybuilding servers with node.js      some rights reserved
A rabbi, a priest,  and a cowboy walk      into a bar.building servers with node.js   some rights reserved
Take 1     Simple Bartenderbuilding servers with node.js   some rights reserved
Rabbibuilding servers with node.js           some rights reserved
Priestbuilding servers with node.js            some rights reserved
Cowboy…building servers with node.js     some rights reserved
… he starts                     to talkbuilding servers with node.js   some rights reserved
Bartender is                 now blockedbuilding servers with node.js   some rights reserved
Take 2  Evented Bartenderbuilding servers with node.js   some rights reserved
Rabbibuilding servers with node.js           some rights reserved
Priestbuilding servers with node.js            some rights reserved
Cowboy…building servers with node.js     some rights reserved
… he starts                     to talkbuilding servers with node.js   some rights reserved
Bartender starts        filling ordersbuilding servers with node.js   some rights reserved
Fixing Take 1building servers with node.js   some rights reserved
Add more                    bartendersbuilding servers with node.js   some rights reserved
Limit time        with each clientbuilding servers with node.js   some rights reserved
Respond to                      Eventsbuilding servers with node.js   some rights reserved
Who uses this?building servers with node.js   some rights reserved
Nginxbuilding servers with node.js           some rights reserved
Memcachedbuilding servers with node.js   some rights reserved
Most NoSQLbuilding servers with node.js   some rights reserved
Serversbuilding servers with node.js   some rights reserved
Basic Socket Serverbuilding servers with node.js   some rights reserved
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Echo Server     var net = require("net");     net.createServer(function(socket) {       socket.on("data", function(data) {...
Storing Statebuilding servers with node.js   some rights reserved
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Personalized Echo Server     net.createServer(function(socket) {       var name = false;       socket.on("data", function(...
Web Serversbuilding servers with node.js   some rights reserved
Hello Worldbuilding servers with node.js       some rights reserved
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Something’s                     Differentbuilding servers with node.js   some rights reserved
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Hello World     var http = require("http");     http.createServer(function(request, response) {       response.writeHead(2...
Any idea why?building servers with node.js   some rights reserved
Hello World     var http = require("http");     http.createServer(function(request, response) {       setTimeout(function(...
Hello World     var http = require("http");     http.createServer(function(request, response) {       db.find({"slug": "a-...
Grokking                    the Requestbuilding servers with node.js   some rights reserved
Output the Requestbuilding servers with node.js   some rights reserved
Output the Request     var http = require("http");     http.createServer(function(request, response) {       response.writ...
Output the Request     var http = require("http");     http.createServer(function(request, response) {       response.writ...
Output the Request     $ curl -s http://localhost:4321/     { socket:        { bufferSize: 0,          fd: 7,          typ...
Output the Request     $ curl -s http://localhost:4321/     … way down around line 148 …       url: /,       method: GET,b...
Output the Request     $ curl -s http://localhost:4321/say?msg=Hello | grep "url:"       url: /say?msg=Hello,building serv...
Parsing the URLbuilding servers with node.js   some rights reserved
Parsing the URL     var http = require("http");     http.createServer(function(request, response) {       response.writeHe...
Parsing the URL     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello,   ...
Parsing the URL     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello,   ...
Parsing the URL     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello,   ...
Parsing the URL     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello,   ...
Parsing the URL     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello,   ...
Parsing the Querybuilding servers with node.js   some rights reserved
Parsing the Query     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello, ...
Parsing the Query     var http = require("http");     http.createServer(function(request, response) {       response.write...
Parsing the Query     var http = require("http");     http.createServer(function(request, response) {       response.write...
Parsing the Query     $ curl -s http://localhost:4321/say?msg=Hello     { href: /say?msg=Hello,       search: ?msg=Hello, ...
Receiving Databuilding servers with node.js   some rights reserved
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Receiving Data     function(request, response) {       var raw = "";       request.on("data", function(chunk) {         ra...
Something                        is Wrongbuilding servers with node.js      some rights reserved
Can’t handle Errors     $ node json-reverse.js     Listening on 4321     undefined:0     ^     SyntaxError: Unexpected end...
Fault-Tolerancebuilding servers with node.js   some rights reserved
Quick Refactor     var handleResponse = function(response, code, message) {        response.writeHead(code, {"Content-Type...
Fault Tolerant Reverse       request.on("end", function() {         if (!raw) {           handleResponse(response, 400, "R...
Fault Tolerant Reverse       request.on("end", function() {         if (!raw) {           handleResponse(response, 400, "R...
Fault Tolerant Reverse       request.on("end", function() {         if (!raw) {           handleResponse(response, 400, "R...
Fault Tolerant Reverse          // earlier code          } catch(e) {            handleResponse(response, 400, "Invalid JS...
Fault Tolerant Reverse          // earlier code          } catch(e) {            handleResponse(response, 400, "Invalid JS...
GTDbuilding servers with node.js         some rights reserved
Libraries Helpbuilding servers with node.js   some rights reserved
Express                     visionmedia/expressbuilding servers with node.js          some rights reserved
Hello World     var app = express.createServer();     app.get(/, function(req, res){         res.send(Hello World);     })...
Socket.IO                         http://socket.io/building servers with node.js                some rights reserved
Push from the Server     var http = require(http),           io = require(socket.io);     var server = http.createServer(f...
Socket.IO     // socket.io     var socket = io.listen(server);     socket.on(connection, function(client){       // new cl...
Sending to Client     // socket.io     var socket = io.listen(server);     socket.on(connection, function(client) {       ...
CoffeeScript                 jashkenas/coffee-scriptbuilding servers with node.js         some rights reserved
Remember this code?       request.on("end", function() {         if (!raw) {           handleResponse(response, 400, "Requ...
In CoffeeScript       request.on "end", () ->         return handleResponse response, 400, "Requires more data" unless raw...
Vows                         cloudhead/vows                        http://vowjs.org/building servers with node.js         ...
No Samplesbuilding servers with node.js     some rights reserved
Is it Right           For Me?building servers with node.js   some rights reserved
It Dependsbuilding servers with node.js       some rights reserved
The Checklistbuilding servers with node.js   some rights reserved
Control                          the Stackbuilding servers with node.js         some rights reserved
Comfortable                    with Changebuilding servers with node.js   some rights reserved
Full Control Over              App Logicbuilding servers with node.js   some rights reserved
Lots of Small                   Independent                      Piecesbuilding servers with node.js   some rights reserved
Then Maybebuilding servers with node.js   some rights reserved
Where to?building servers with node.js     some rights reserved
nodejs.orgbuilding servers with node.js     some rights reserved
#Node.js                      (freenode)building servers with node.js   some rights reserved
Travis Swicegood                       travisswicegood.com                           @tswicegood                       tra...
Upcoming SlideShare
Loading in...5
×

Building servers with Node.js

11,772

Published on

Published in: Technology
0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
11,772
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
360
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide

Transcript of "Building servers with Node.js"

  1. 1. Building Servers with Node.js Travis Swicegood #nodetalk Licensed under Creative Commons, Attribution, Share-Alike
  2. 2. Hi, I’m Travisbuilding servers with node.js some rights reserved
  3. 3. #nodetalkbuilding servers with node.js some rights reserved
  4. 4. What’s this talk about?building servers with node.js some rights reserved
  5. 5. What is Node.js?building servers with node.js some rights reserved
  6. 6. Serversbuilding servers with node.js some rights reserved
  7. 7. GTDbuilding servers with node.js some rights reserved
  8. 8. Is it Right For Me?building servers with node.js some rights reserved
  9. 9. What is Node.js?building servers with node.js some rights reserved
  10. 10. Evented I/O Networking Toolkitbuilding servers with node.js some rights reserved
  11. 11. What’s “evented?”building servers with node.js some rights reserved
  12. 12. Some Codebuilding servers with node.js some rights reserved
  13. 13. Standard Data Store var db = new Database({host: "localhost"}); db.connect(); var result = db.find({name: "a-post"}); // do something with resultbuilding servers with node.js some rights reserved
  14. 14. Evented Data Store var db = new Database({host: "localhost"}); db.connect(function(conn) { conn.find({name: "a-post"}, function(result) { // do something with result }); });building servers with node.js some rights reserved
  15. 15. Event Loopbuilding servers with node.js some rights reserved
  16. 16. An Analogybuilding servers with node.js some rights reserved
  17. 17. A rabbi, a priest, and a cowboy walk into a bar.building servers with node.js some rights reserved
  18. 18. Take 1 Simple Bartenderbuilding servers with node.js some rights reserved
  19. 19. Rabbibuilding servers with node.js some rights reserved
  20. 20. Priestbuilding servers with node.js some rights reserved
  21. 21. Cowboy…building servers with node.js some rights reserved
  22. 22. … he starts to talkbuilding servers with node.js some rights reserved
  23. 23. Bartender is now blockedbuilding servers with node.js some rights reserved
  24. 24. Take 2 Evented Bartenderbuilding servers with node.js some rights reserved
  25. 25. Rabbibuilding servers with node.js some rights reserved
  26. 26. Priestbuilding servers with node.js some rights reserved
  27. 27. Cowboy…building servers with node.js some rights reserved
  28. 28. … he starts to talkbuilding servers with node.js some rights reserved
  29. 29. Bartender starts filling ordersbuilding servers with node.js some rights reserved
  30. 30. Fixing Take 1building servers with node.js some rights reserved
  31. 31. Add more bartendersbuilding servers with node.js some rights reserved
  32. 32. Limit time with each clientbuilding servers with node.js some rights reserved
  33. 33. Respond to Eventsbuilding servers with node.js some rights reserved
  34. 34. Who uses this?building servers with node.js some rights reserved
  35. 35. Nginxbuilding servers with node.js some rights reserved
  36. 36. Memcachedbuilding servers with node.js some rights reserved
  37. 37. Most NoSQLbuilding servers with node.js some rights reserved
  38. 38. Serversbuilding servers with node.js some rights reserved
  39. 39. Basic Socket Serverbuilding servers with node.js some rights reserved
  40. 40. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  41. 41. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  42. 42. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  43. 43. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  44. 44. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  45. 45. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  46. 46. Echo Server var net = require("net"); net.createServer(function(socket) { socket.on("data", function(data) { socket.write("You said: " + data); socket.end(); }); socket.write("Please say something:n"); }).listen(1234); console.log("Listening on 1234");building servers with node.js some rights reserved
  47. 47. Storing Statebuilding servers with node.js some rights reserved
  48. 48. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  49. 49. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  50. 50. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  51. 51. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  52. 52. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  53. 53. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  54. 54. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  55. 55. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  56. 56. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  57. 57. Personalized Echo Server net.createServer(function(socket) { var name = false; socket.on("data", function(data) { if (!name) { name = data.toString(utf8).trim(); socket.write("Thanks, " + name + "n"); socket.write("Please say something:n"); } else { socket.write(name + ", you said: " + data); socket.end(); } }); socket.write("Please tell me your name: "); }).listen(1234);building servers with node.js some rights reserved
  58. 58. Web Serversbuilding servers with node.js some rights reserved
  59. 59. Hello Worldbuilding servers with node.js some rights reserved
  60. 60. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  61. 61. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  62. 62. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  63. 63. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  64. 64. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  65. 65. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  66. 66. Something’s Differentbuilding servers with node.js some rights reserved
  67. 67. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  68. 68. Hello World var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  69. 69. Any idea why?building servers with node.js some rights reserved
  70. 70. Hello World var http = require("http"); http.createServer(function(request, response) { setTimeout(function() { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>Hello, World!</h1>"); response.end(); }, 1000); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  71. 71. Hello World var http = require("http"); http.createServer(function(request, response) { db.find({"slug": "a-blog-post"}, function(post) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<h1>" + post.title + "</h1>"); response.write(post.body); response.end(); }); }).listen(4321); console.log("Now listening on port 4321");building servers with node.js some rights reserved
  72. 72. Grokking the Requestbuilding servers with node.js some rights reserved
  73. 73. Output the Requestbuilding servers with node.js some rights reserved
  74. 74. Output the Request var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write(require("util").inspect(request)); response.end(); }).listen(4321); console.log("Listening on 4321");building servers with node.js some rights reserved
  75. 75. Output the Request var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write(require("util").inspect(request)); response.end(); }).listen(4321); console.log("Listening on 4321");building servers with node.js some rights reserved
  76. 76. Output the Request $ curl -s http://localhost:4321/ { socket: { bufferSize: 0, fd: 7, type: tcp4, allowHalfOpen: true, _readWatcher: { socket: [Circular], callback: [Function: onReadable] }, readable: true, _writeQueue: [], _writeQueueEncoding: [], _writeQueueFD: [], … and so on, and so on …building servers with node.js some rights reserved
  77. 77. Output the Request $ curl -s http://localhost:4321/ … way down around line 148 … url: /, method: GET,building servers with node.js some rights reserved
  78. 78. Output the Request $ curl -s http://localhost:4321/say?msg=Hello | grep "url:" url: /say?msg=Hello,building servers with node.js some rights reserved
  79. 79. Parsing the URLbuilding servers with node.js some rights reserved
  80. 80. Parsing the URL var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); var parsedUrl = require("url").parse(request.url); response.write(require("util").inspect(parsedUrl)); response.end(); }).listen(4321); console.log("Listening on 4321");building servers with node.js some rights reserved
  81. 81. Parsing the URL $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  82. 82. Parsing the URL $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  83. 83. Parsing the URL $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  84. 84. Parsing the URL $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  85. 85. Parsing the URL $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  86. 86. Parsing the Querybuilding servers with node.js some rights reserved
  87. 87. Parsing the Query $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: msg=Hello, pathname: /say }building servers with node.js some rights reserved
  88. 88. Parsing the Query var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); var parsedUrl = require("url").parse(request.url, true); response.write(require("util").inspect(parsedUrl)); response.end(); }).listen(4321); console.log("Listening on 4321");building servers with node.js some rights reserved
  89. 89. Parsing the Query var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); var parsedUrl = require("url").parse(request.url, true); response.write(require("util").inspect(parsedUrl)); response.end(); }).listen(4321); console.log("Listening on 4321");building servers with node.js some rights reserved
  90. 90. Parsing the Query $ curl -s http://localhost:4321/say?msg=Hello { href: /say?msg=Hello, search: ?msg=Hello, query: { msg: Hello }, pathname: /say }building servers with node.js some rights reserved
  91. 91. Receiving Databuilding servers with node.js some rights reserved
  92. 92. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  93. 93. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  94. 94. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  95. 95. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  96. 96. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  97. 97. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  98. 98. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  99. 99. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  100. 100. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  101. 101. Receiving Data function(request, response) { var raw = ""; request.on("data", function(chunk) { raw += chunk; }); request.on("end", function() { var data = JSON.parse(raw), obj = {message: data.message.split("").reverse().join("")}; response.writeHead(200, {"Content-Type": "application/json"}); response.write(JSON.stringify(obj)); response.end(); }); }building servers with node.js some rights reserved
  102. 102. Something is Wrongbuilding servers with node.js some rights reserved
  103. 103. Can’t handle Errors $ node json-reverse.js Listening on 4321 undefined:0 ^ SyntaxError: Unexpected end of input at Object.parse (native) at IncomingMessage.<anonymous> (/Book/code/servers/json- reverse.js:8:21) at IncomingMessage.emit (events.js:39:17) at HTTPParser.onMessageComplete (http.js:111:23) at Socket.ondata (http.js:945:22) at Socket._onReadable (net.js:654:27) at IOWatcher.onReadable [as callback] (net.js:156:10)building servers with node.js some rights reserved
  104. 104. Fault-Tolerancebuilding servers with node.js some rights reserved
  105. 105. Quick Refactor var handleResponse = function(response, code, message) { response.writeHead(code, {"Content-Type": "application/json"}); response.write(message); response.end(); };building servers with node.js some rights reserved
  106. 106. Fault Tolerant Reverse request.on("end", function() { if (!raw) { handleResponse(response, 400, "Requires more data"); return; } try { var data = JSON.parse(raw); } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message"); return; } var obj = {message: data.message.split("").reverse().join("") }; handleResponse(response, 200, JSON.stringify(obj)) });building servers with node.js some rights reserved
  107. 107. Fault Tolerant Reverse request.on("end", function() { if (!raw) { handleResponse(response, 400, "Requires more data"); return; } try { var data = JSON.parse(raw); } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message");building servers with node.js some rights reserved
  108. 108. Fault Tolerant Reverse request.on("end", function() { if (!raw) { handleResponse(response, 400, "Requires more data"); return; } try { var data = JSON.parse(raw); } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message");building servers with node.js some rights reserved
  109. 109. Fault Tolerant Reverse // earlier code } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message"); return; } var obj = {message: data.message.split("").reverse().join("") }; handleResponse(response, 200, JSON.stringify(obj)) });building servers with node.js some rights reserved
  110. 110. Fault Tolerant Reverse // earlier code } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message"); return; } var obj = {message: data.message.split("").reverse().join("") }; handleResponse(response, 200, JSON.stringify(obj)) });building servers with node.js some rights reserved
  111. 111. GTDbuilding servers with node.js some rights reserved
  112. 112. Libraries Helpbuilding servers with node.js some rights reserved
  113. 113. Express visionmedia/expressbuilding servers with node.js some rights reserved
  114. 114. Hello World var app = express.createServer(); app.get(/, function(req, res){ res.send(Hello World); }); app.listen(3000);building servers with node.js some rights reserved
  115. 115. Socket.IO http://socket.io/building servers with node.js some rights reserved
  116. 116. Push from the Server var http = require(http),       io = require(socket.io); var server = http.createServer(function(req, res){  // your normal server code  res.writeHead(200, {Content-Type: text/html});  res.end(<h1>Hello world</h1>); }); server.listen(80);   // socket.io var socket = io.listen(server); socket.on(connection, function(client){   // new client is here!   client.on(message, function(){ … })   client.on(disconnect, function(){ … }) });building servers with node.js some rights reserved
  117. 117. Socket.IO // socket.io var socket = io.listen(server); socket.on(connection, function(client){   // new client is here!   client.on(message, function(){ … })   client.on(disconnect, function(){ … }) });building servers with node.js some rights reserved
  118. 118. Sending to Client // socket.io var socket = io.listen(server); socket.on(connection, function(client) { // send a message back to the client client.send({msg: "Thanks for connecting…"}); // broadcast a message to everyone client.broadcast({msg: "Another one connected!"}); });building servers with node.js some rights reserved
  119. 119. CoffeeScript jashkenas/coffee-scriptbuilding servers with node.js some rights reserved
  120. 120. Remember this code? request.on("end", function() { if (!raw) { handleResponse(response, 400, "Requires more data"); return; } try { var data = JSON.parse(raw); } catch(e) { handleResponse(response, 400, "Invalid JSON"); return; } if (!data.message) { handleResponse(response, 400, "Requires a message"); return; } var obj = {message: data.message.split("").reverse().join("") }; handleResponse(response, 200, JSON.stringify(obj)) });building servers with node.js some rights reserved
  121. 121. In CoffeeScript request.on "end", () -> return handleResponse response, 400, "Requires more data" unless raw try data = JSON.parse raw catch e return handleResponse response, 400, "Requires more data" return handleResponse response, 400, "Requires a message" unless data.message obj = message: data.message.split("").reverse().join("") handleResponse response, 200, JSON.stringify objbuilding servers with node.js some rights reserved
  122. 122. Vows cloudhead/vows http://vowjs.org/building servers with node.js some rights reserved
  123. 123. No Samplesbuilding servers with node.js some rights reserved
  124. 124. Is it Right For Me?building servers with node.js some rights reserved
  125. 125. It Dependsbuilding servers with node.js some rights reserved
  126. 126. The Checklistbuilding servers with node.js some rights reserved
  127. 127. Control the Stackbuilding servers with node.js some rights reserved
  128. 128. Comfortable with Changebuilding servers with node.js some rights reserved
  129. 129. Full Control Over App Logicbuilding servers with node.js some rights reserved
  130. 130. Lots of Small Independent Piecesbuilding servers with node.js some rights reserved
  131. 131. Then Maybebuilding servers with node.js some rights reserved
  132. 132. Where to?building servers with node.js some rights reserved
  133. 133. nodejs.orgbuilding servers with node.js some rights reserved
  134. 134. #Node.js (freenode)building servers with node.js some rights reserved
  135. 135. Travis Swicegood travisswicegood.com @tswicegood travis@domain51.com http://joind.in/2810building servers with node.js some rights reserved
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×