Node.js How JavaScript is Changing Server Programming Tom - - PowerPoint PPT Presentation

node js
SMART_READER_LITE
LIVE PREVIEW

Node.js How JavaScript is Changing Server Programming Tom - - PowerPoint PPT Presentation

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 started Server Side Javascript IS SO


slide-1
SLIDE 1

Node.js

How JavaScript is Changing Server Programming

Tom Hughes-Croucher @sh1mmer

slide-2
SLIDE 2

This is how we roll

  • 1. Server-Side JavaScript Overview
  • 2. Introduction to Node.js
  • 3. The Node.js ecosystem
  • 4. Getting started
slide-3
SLIDE 3

Server Side Javascript IS SO AWESOME

MY ENTIRE PRESENTATION IS IN

COMIC SANS

AND YOU WILL STILL LOVE ME AT THE END

slide-4
SLIDE 4

Why SSJS?

slide-5
SLIDE 5

JavaScript programmers

3 > 2 > 1

slide-6
SLIDE 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

slide-7
SLIDE 7

Laziness or “I’m sick

  • f writing stuff twice”

I could have said efficiency, but I think we all secretly long to lounge around in our y-fronts.

slide-8
SLIDE 8

Progressive Enhancement is free*

Remember WWCD (What Would Crockford Do)

*close enough

slide-9
SLIDE 9

TL;DR: SSJS is Awesome

Like a Unicorn riding a Narwhal

slide-10
SLIDE 10
slide-11
SLIDE 11

Why now?

slide-12
SLIDE 12
  • 1. Professionalism
slide-13
SLIDE 13
slide-14
SLIDE 14

“Yahoo!'s corporate motto is: Don't be eval().“

slide-15
SLIDE 15

“Doug Crockford's JavaScript is so strict, that uncaught exceptions would trigger global thermonuclear war“

slide-16
SLIDE 16
  • 2. JavaScript

Runtimes

slide-17
SLIDE 17

Runtimes

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

JavaScript Performance

V8 Spider Monkey

slide-19
SLIDE 19

Node.js

  • Server-side JavaScript process
  • Uses V8
  • Non-blocking
  • Event Driven
  • CommonJS module format
slide-20
SLIDE 20

Node.js

  • Server-side JavaScript process
  • Uses V8
  • Non-blocking
  • Event Driven
  • CommonJS module format

A W E S O M E !

slide-21
SLIDE 21

Node is ☜⎻⎻⎻⎻⎻☞ this fast

slide-22
SLIDE 22

concurrency=300, Smaller is Better

response size (bytes) response time (ms)

100 200 300 400 24 26 28 210 212 214 216 218 server nginx thin tornado node_buffer

slide-23
SLIDE 23
slide-24
SLIDE 24

Why non-blocking matters

slide-25
SLIDE 25

var result = db.query("select * from T"); // use result

slide-26
SLIDE 26

What are we waiting for?

slide-27
SLIDE 27

Event Loop vs. Threads

slide-28
SLIDE 28

concurrency × reqs/sec

http://blog.webfaction.com/a-little-holiday-present

slide-29
SLIDE 29

concurrency × memory

http://blog.webfaction.com/a-little-holiday-present

slide-30
SLIDE 30

Node Basics

slide-31
SLIDE 31

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');

slide-32
SLIDE 32

var http = require('http'); //include the http library

slide-33
SLIDE 33

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

slide-34
SLIDE 34

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }) //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

slide-35
SLIDE 35

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

slide-36
SLIDE 36

Node Ecosystem

slide-37
SLIDE 37
slide-38
SLIDE 38
slide-39
SLIDE 39
slide-40
SLIDE 40
slide-41
SLIDE 41
slide-42
SLIDE 42
slide-43
SLIDE 43
slide-44
SLIDE 44
slide-45
SLIDE 45
slide-46
SLIDE 46
slide-47
SLIDE 47

NPM

Node Package Manager

slide-48
SLIDE 48

npm install mustache

slide-49
SLIDE 49

NPM is written in JavaScript!

slide-50
SLIDE 50

// 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 }

slide-51
SLIDE 51

node-repl

Interactive JavaScript terminal

slide-52
SLIDE 52

Which libraries to use?

slide-53
SLIDE 53

Mustache.js

slide-54
SLIDE 54
slide-55
SLIDE 55

var view = { title: "Joe", calc: function() { return 2 + 4; } } var template = "{{title}} spends {{calc}}"; var html = Mustache.to_html(template, view);

slide-56
SLIDE 56

node-paperboy

slide-57
SLIDE 57
slide-58
SLIDE 58

http:// wargamez.mape.me/

slide-59
SLIDE 59
slide-60
SLIDE 60

DOM+YUI3

slide-61
SLIDE 61

Rendering HTML

http://yuiloader.davglass.com/calendar/

slide-62
SLIDE 62

Multi-core Node.js

Node+Web Workers

slide-63
SLIDE 63

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' });

  • nmessage = function(e) {

postMessage({ test : 'this is a test' }); };

  • nclose = function() {

sys.debug('Worker shuttting down.'); };

Master Worker

slide-64
SLIDE 64

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

slide-65
SLIDE 65

Today presentation was

Brought to you by the letters: J and S And the fonts: Comic Sans

monofur

Tom Hughes-Croucher @sh1mmer croucher@yahoo-inc.com Slides, etc --> http:// speakerrate.com/sh1mmer Pls rate me. kthxbai.