Common Node
-
Upload
oleg-podsechin -
Category
Technology
-
view
115 -
download
4
description
Transcript of Common Node
Intro to Common Node
#camdug 27.08.2011
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
Akshell NarwhalJS
Wakanda RingoJS
CommonJS modules
// math.jsexports.add = function(a, b) { return a + b; }
// server.jsvar add = require('math').add;console.log(add(2, 2));
// client.js???
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"],}
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
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); } }}
Async vs. Sync
http.get(url1) + http.get(url2)
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
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
Thank you!
Comments?
@olegpodsechin