Common Node

13
Intro to Common Node #camdug 27.08.2011

description

 

Transcript of Common Node

Page 1: Common Node

Intro to Common Node

#camdug 27.08.2011

Page 2: Common Node

Why use JavaScript on the server?

Language of the browser, widely accessible Dynamic language Closures and asynchronous programming Diverse, active, growing community We are stuck with it for the foreseeable future

Page 3: Common Node
Page 4: Common Node

Akshell NarwhalJS

Wakanda RingoJS

Page 5: Common Node

CommonJS modules

// math.jsexports.add = function(a, b) { return a + b; }

// server.jsvar add = require('math').add;console.log(add(2, 2));

// client.js???

Page 6: Common Node

CommonJS packages

// package.json{ "name": "hello", "version": "0.1.0", "description": "Hello world package", "keywords": ["hello", "world"], "author": "John Smith <[email protected]>", "main": "./lib/hello.js", "dependencies": {"whatever" : "1"}, "engines" : ["v8", "node", "rhino"],}

Page 7: Common Node

Node.js Event Loop

Handle all I/O in one process No blocking calls, use callbacks instead Less memory used and higher throughput due

to fewer context switches Use JavaScript closures to capture state

Page 8: Common Node

Async vs. Sync

function add(callback) { http.get(url1, function(response1) { var part1 = response1.data; http.get(url2, function(response2) { var part2 = response2.data; callback(part1 + part2); } }}

Page 9: Common Node

Async vs. Sync

http.get(url1) + http.get(url2)

Page 10: Common Node

Interoperability

Pure JavaScript CommonJS modules can run in any environment Templating, parsing, formatting, encoding

Anything that does I/O must expose either a synchronous or an asynchronous API Dictates the style of interface exposed by higher

level packages

Page 11: Common Node

Common Node

Implements a number of synchronous CommonJS specifications on top of Node

– Binary, IO, Filesystem, JSGI and more Uses node-fibers

Co routine implementation, not a fork or a hack of Node

Google's Traceur to support latest language features

Bridges the gap between platforms, sync and async Google “common node”, watch on GitHub

Page 12: Common Node
Page 13: Common Node

Thank you!

Comments?

@olegpodsechin