Node.js
  How JavaScript is Changing Server Programming




Tom Hughes-Croucher                  @sh1mmer
This is how we roll

1. Server-Side JavaScript Overview
2. Introduction to Node.js
3. The Node.js ecosystem
4. Getting sta...
Server Side Javascript
  IS SO AWESOME
    MY ENTIRE PRESENTATION IS IN
          COMIC SANS
AND YOU WILL STILL LOVE ME AT...
Why SSJS?
JavaScript
programmers
    3>2>1
Massive Code base of YUI
 and other JS libraries
  I heard some people use this thing called jQuery,
         but I’m not ...
Laziness or “I’m sick
of writing stuff twice”
I could have said efficiency, but I think we all secretly long
             ...
Progressive
Enhancement is free*
Remember WWCD (What Would Crockford Do)




                             *close enough
TL;DR:
SSJS is Awesome
 Like a Unicorn riding a Narwhal
Why now?
1. Professionalism
“Yahoo!'s corporate
 motto is: Don't be
      eval().“
“Doug Crockford's JavaScript is
    so strict, that uncaught
 exceptions would trigger global
      thermonuclear war“
2. JavaScript
   Runtimes
Runtimes

• V8 (Google), C++
• Spider Monkey (Mozilla), C++
• Rhino (Mozilla), Java
• JavaScript Core (Apple), C++
V8




                    Spider
                    Monkey

JavaScript Performance
Node.js
• Server-side JavaScript process
• Uses V8
• Non-blocking
• Event Driven
• CommonJS module format
AW Node.js
•
  ESServer-side JavaScript process



      OM
•   Uses V8

•   Non-blocking

•
•
    Event Driven
    Common...
Node is
 ⎻⎻⎻⎻⎻
this fast
concurrency=300, Smaller is Better
                 400




                 300
response time (ms)




                  ...
Why non-blocking
   matters
var result =
db.query("select * from T");
// use result
What are we waiting
       for?
Event Loop vs.
  Threads
Apache vs NGINX
concurrency × reqs/sec




http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX
concurrency × memory




http://blog.webfaction.com/a-little-holiday-present
Node Basics
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
...
var http = require('http');

//include the http library
http.createServer(function (req, res) {

}).listen(8124, "127.0.0.1");

//create an http server
//when ‘stuff’ happens cal...
http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello Worldn');...
console.log('Server running at http://127.0.0.1:8124/');

//write Server is running at http://127.0.0.1:8124/
//to the con...
Node Ecosystem
NPM
Node Package Manager
npm install mustache
NPM is written in
  JavaScript!
// kludge until this is normal.
if (!process.EventEmitter.prototype.on) {
  process.EventEmitter.prototype.on =
process.Ev...
node-repl
Interactive JavaScript terminal
Which libraries to
      use?
Mustache.js
var view = {
  title: "Joe",
  calc: function() {
    return 2 + 4;
  }
}

var template = "{{title}} spends
{{calc}}";

va...
node-paperboy
http://
wargamez.mape.me/
DOM+YUI3
Rendering HTML



http://yuiloader.davglass.com/calendar/
Multi-core Node.js
    Node+Web Workers
Master
 var sys = require('sys');
 var Worker = require('webworker').Worker;

 var w = new Worker('foo.js');

 w.onmessage...
Summary
•   SSJS is awesome because

    •   We are JavaScript programmers

    •   Reuse (libraries/code)

    •   Progre...
Today presentation was
 Brought to you by         And the fonts:
    the letters:            Comic Sans
      J and S     ...
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Upcoming SlideShare
Loading in...5
×

Node.js and How JavaScript is Changing Server Programming

13,746

Published on

Node.js is a highly concurrent JavaScript server written on top of the V8 JavaScript runtime. This is awesome for a number of reasons. Firstly Node.js has re-architected some of the core module of V8 to create a server implementation that is non-blocking (similar to other event driven frameworks like Ruby’s Event Machine or Python’s Twisted). Event driven architectures are a natural fit for JavaScript developers because it’s already how the browser works. By using an event driven framework Node is not only intuitive to use but also highly scalable. Tests have shown Node instances running tens of thousands of simultaneous users.

This session will explore the architectural basics of Node.js and how it’s different from blocking server implementations such as PHP, Rail or Java Servlets. We’ll explore some basic examples of creating a simple server, dealing with HTTP requests, etc.

The bigger question is once we have this awesome programming environment, what do we do with it? Node already has a really vibrant collection of modules which provide a range of functionality. Demystifying what’s available is pretty important to actually getting stuff done with Node. Since Node itself is very low level, lot’s of things people expect in web servers aren’t automatically there (for example, request routing). In order to help ease people into using Node this session will look at a range of the best modules for Node.js.

Published in: Technology
1 Comment
25 Likes
Statistics
Notes
No Downloads
Views
Total Views
13,746
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
370
Comments
1
Likes
25
Embeds 0
No embeds

No notes for slide

Node.js and How JavaScript is Changing Server Programming

  1. 1. Node.js How JavaScript is Changing Server Programming Tom Hughes-Croucher @sh1mmer
  2. 2. This is how we roll 1. Server-Side JavaScript Overview 2. Introduction to Node.js 3. The Node.js ecosystem 4. Getting started
  3. 3. Server Side Javascript IS SO AWESOME MY ENTIRE PRESENTATION IS IN COMIC SANS AND YOU WILL STILL LOVE ME AT THE END
  4. 4. Why SSJS?
  5. 5. JavaScript programmers 3>2>1
  6. 6. Massive Code base of YUI and other JS libraries I heard some people use this thing called jQuery, but I’m not convinced it’ll catch on
  7. 7. Laziness or “I’m sick of writing stuff twice” I could have said efficiency, but I think we all secretly long to lounge around in our y-fronts.
  8. 8. Progressive Enhancement is free* Remember WWCD (What Would Crockford Do) *close enough
  9. 9. TL;DR: SSJS is Awesome Like a Unicorn riding a Narwhal
  10. 10. Why now?
  11. 11. 1. Professionalism
  12. 12. “Yahoo!'s corporate motto is: Don't be eval().“
  13. 13. “Doug Crockford's JavaScript is so strict, that uncaught exceptions would trigger global thermonuclear war“
  14. 14. 2. JavaScript Runtimes
  15. 15. Runtimes • V8 (Google), C++ • Spider Monkey (Mozilla), C++ • Rhino (Mozilla), Java • JavaScript Core (Apple), C++
  16. 16. V8 Spider Monkey JavaScript Performance
  17. 17. Node.js • Server-side JavaScript process • Uses V8 • Non-blocking • Event Driven • CommonJS module format
  18. 18. AW Node.js • ESServer-side JavaScript process OM • Uses V8 • Non-blocking • • Event Driven CommonJS module format E!
  19. 19. Node is ⎻⎻⎻⎻⎻ this fast
  20. 20. concurrency=300, Smaller is Better 400 300 response time (ms) server nginx 200 thin tornado node_buffer 100 24 26 28 210 212 214 216 218 response size (bytes)
  21. 21. Why non-blocking matters
  22. 22. var result = db.query("select * from T"); // use result
  23. 23. What are we waiting for?
  24. 24. Event Loop vs. Threads
  25. 25. Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present
  26. 26. Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present
  27. 27. Node Basics
  28. 28. var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
  29. 29. var http = require('http'); //include the http library
  30. 30. http.createServer(function (req, res) { }).listen(8124, "127.0.0.1"); //create an http server //when ‘stuff’ happens call this anonymous function //listen on port 8124 of the IP 127.0.0.1
  31. 31. http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }) //when ‘stuff’ happens my function fires //I get a request object and a response object //I write to the response object header //HTTP status 200 and content-type ‘text/plain’ //close the response with the body: //Hello World
  32. 32. console.log('Server running at http://127.0.0.1:8124/'); //write Server is running at http://127.0.0.1:8124/ //to the console
  33. 33. Node Ecosystem
  34. 34. NPM Node Package Manager
  35. 35. npm install mustache
  36. 36. NPM is written in JavaScript!
  37. 37. // kludge until this is normal. if (!process.EventEmitter.prototype.on) {   process.EventEmitter.prototype.on = process.EventEmitter.prototype.addListener } var path = require("path") if (!process.execPath) {   process.execPath = path.join(process.installPrefix, "bin", "node") } var npm = exports   , set = require("./lib/utils/set")   , get = require("./lib/utils/get")   , ini = require("./lib/utils/ini")   , log = require("./lib/utils/log")   , fs = require("fs") npm.commands = {} npm.SHOULD_EXIT = true try {   var j = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"))+"")   npm.version = j.version } catch (ex) {   log(ex, "error reading version")   npm.version = ex }
  38. 38. node-repl Interactive JavaScript terminal
  39. 39. Which libraries to use?
  40. 40. Mustache.js
  41. 41. var view = { title: "Joe", calc: function() { return 2 + 4; } } var template = "{{title}} spends {{calc}}"; var html = Mustache.to_html(template, view);
  42. 42. node-paperboy
  43. 43. http:// wargamez.mape.me/
  44. 44. DOM+YUI3
  45. 45. Rendering HTML http://yuiloader.davglass.com/calendar/
  46. 46. Multi-core Node.js Node+Web Workers
  47. 47. Master var sys = require('sys'); var Worker = require('webworker').Worker; var w = new Worker('foo.js'); w.onmessage = function(e) { sys.debug('Received mesage: ' + sys.inspect(e)); w.terminate(); }; w.postMessage({ foo : 'bar' }); Worker onmessage = function(e) { postMessage({ test : 'this is a test' }); }; onclose = function() { sys.debug('Worker shuttting down.'); };
  48. 48. Summary • SSJS is awesome because • We are JavaScript programmers • Reuse (libraries/code) • Progressive Enhancement • Node.js + YUI3 rocks • YUI 3’s was easy to get running on Node.js • Server side DOM allows for a single code base
  49. 49. Today presentation was Brought to you by And the fonts: the letters: Comic Sans J and S monofur Tom Hughes-Croucher Slides, etc --> http:// @sh1mmer speakerrate.com/sh1mmer croucher@yahoo-inc.com Pls rate me. kthxbai.
  1. A particular slide catching your eye?

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

×