Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js & Me - DenverJS January 2013

Node.js & Me - DenverJS January 2013

Nodes.js: based on my experience where I think it fits well, ways I've used it and an over view of some ECMAScript5 and Harmony features you can use with node.js today.

Presented at DenverJS January 3rd 2013.

Mike Brevoort

January 03, 2013
Tweet

More Decks by Mike Brevoort

Other Decks in Programming

Transcript

  1. AGENDA Well, it's will be a bit of a hodge

    podge... My thoughts on Node.js Discuss how I've used it Demo some easy to overlook features
  2. ME

  3. ME New York → Delaware/Philadephia → 10th year in Colorado

    Married 5 Children Coding for 19 years JavaScript, Java, Groovy, C/C++, C#, PHP, Python, XQuery Work @ Pearson Co-organizer of DenverJS
  4. NODE.JS & ME Met in Spring 2010 Production apps since

    Winter 2011 Generally an app maker, not a module author
  5. GOOD I/O bound programs API aggregation, orchestration, proxying Filtering and

    data normalization Edge server for client apps Streaming I/O I/O centric systems programming
  6. BAD LESS GOOD Primarily procedural and serial programs CPU bound

    tasks Heavy "business logic" Large code bases that "can't" be modularized Very large teams
  7. NODE.JS EVALUATOR For this presentation Execute inline code snippets in

    a spawned node.js process Communicate via socket.io between process and browser
  8. THE CLIENT CODE <script src="/socket.io/socket.io.js"></script> <script> </script> var socket =

    io.connect(); socket.on('result', function (data) { humane.log(data, { timeout: 5000, clickToClose: true, addnCls: 'humane-bigbox' }) }); function runit(elemId) { socket.emit('run', ['-e', document.getElementById(elemId).textContent]) } <pre> <code contenteditable id="code_evaluator"> console.log(process.pid); </code> </pre> <a onclick="runit('code_evaluator')"> <img src="img/run-dmc.jpg"/> </a>
  9. SERVER CODE var spawn = require('child_process').spawn , express = require('express')

    , app = express() , server = require('http').createServer(app) , io = require('socket.io').listen(server); app.use(express.static(__dirname + '/../reveal.js')); server.listen(4444); io.sockets.on('connection', function (socket) { socket.on('run', function (data) { runit(data, function (output) { socket.emit('result', output); }) }); }); function runit(options, cb) { var run = spawn('node', options) , output = ""; var append = function (val) { output += val }; run.stdout.on('data', append); run.stderr.on('data', append); run.on('exit', function (code) { if (code !== 0) output += 'EXITED WITH CODE: ' + code; cb(output); }); }
  10. ECMASCRIPT 5 Getters and Setters var obj = { get

    name() { return this._name }, set name(val) { this._name = val.toUpperCase(); } } obj.name = "mike"; console.log(obj.name);
  11. ECMASCRIPT 5 Array.prototype Still using underscore.js for this stuff? indexOf(value);

    lastIndexOf(value) filter(callback) forEach(callback) every(callback) map(callback) some(callback) reduce(callback[, initialValue]) reduceRight(callback[, initialValue])
  12. ECMASCRIPT 5 Object.seal(obj) Prevents adding or deleting properties Or changing

    the descriptors of any property Property values can be changed however var o = { a: 1 }; Object.seal(o); console.log("o is sealed? ", Object.isSealed(o)); o.b = 2; // BOOM
  13. ECMASCRIPT 5 Object.freeze(obj) Same as seal but properties can't be

    changed either var o = { a: 1 }; Object.freeze(o); console.log("o is freezed? ", Object.isFrozen(o)); o.a = 2; // BOOM
  14. ECMASCRIPT 5 Object.create Creates a new object with the specified

    prototype object and properties var obj = Object.create({x: 10, y: 20}, { z: { value: 30, writable: true }, sum: { get: function() { return this.x + this.y + this.z; } } }); log(obj.x); // 10 log(obj.y); // 20 log(obj.z); // 30 log(obj.sum); // 60
  15. V8 HARMONY OPTIONS node --v8-options | grep -A 1 harmony

    --harmony_typeof (enable harmony semantics for typeof) type: bool default: false --harmony_scoping (enable harmony block scoping) type: bool default: false --harmony_modules (enable harmony modules (implies block scoping )) type: bool default: false --harmony_proxies (enable harmony proxies) type: bool default: false --harmony_collections (enable harmony collections (sets, maps, a nd weak maps)) type: bool default: false --harmony (enable all harmony features (except typeof)) type: bool default: false
  16. HARMONY COLLECTIONS Maps (and WeakMaps) Harmony Iterators not implemented yet

    :( var assert = require('assert') , map = new Map() , obj = new Object() , foo = { a: 1 } , bar = { a: 1 }; obj[foo] = 'foo'; assert.equal('foo', obj[foo]); obj[bar] = 'bar'; assert.equal('bar', obj[bar]); assert.equal('bar', obj[foo]); // ??? -> keys converted to strings map.set(foo, 'foo'); assert.equal('foo', map.get(foo)); map.set(bar, 'bar'); assert.equal('bar', map.get(bar)); assert.equal('foo', map.get(foo));
  17. HARMONY COLLECTIONS Sets Harmony Iterators not implemented yet :( var

    assert = require('assert') , set = new Set() , foo = { a: 1 } , bar = { a: 1 } , baz = { a: 1 }; set.add(foo); set.add(bar); assert(set.has(foo)); assert(set.has(bar)); assert(!set.has(baz)); set.delete(foo); assert(!set.has(foo)); assert(set.has(bar));
  18. BLOCK SCOPE const let node --harmony --use-strict var assert =

    require('assert'); const baz = 'baz'; assert.equal('baz', baz); baz = 'new baz'; // BOOM! // SyntaxError: Assignment to constant variable. var assert = require('assert'); { var foo = 'foo'; let bar = 'bar'; } assert.equal('foo', foo); assert.equal('bar', bar); // BOOM! // ReferenceError: bar is not defined