More about node.js
Building maintainable node.js software
Streams all the way down!
Readable Stream
var Readable = require("stream").Readable
var util = require("util")
!

function ICanRead(options) {
Reada...
Writable Stream
var Writable = require("stream").Writable
var util = require("util")
!

function IConcatThings(options) {
Writable.call(th...
Transform Stream
var Transform = require("stream").Transform
var util = require("util")
!

function ITransformThings(options) {
Transform.c...
Why streams are great

function httpHandler(req, res) {
req.pipe(bodyParser)
.pipe(shortner)
.pipe(stringify)
.pipe(res)
}
Explicit error handling with streams

function httpHandler(req, res) {
shortner.on("error", function(err) {
res.statusCode...
Ok just a quick thing build cat!
Debugging… because not all code is great :(
Seen that before?

def my_method a
require "pry"
binding.pry
end
!

my_method "foo"
And this?

function myMethod(a) {
debugger
}
!

myMethod("foo")
using the node.js debugger
Upcoming SlideShare
Loading in …5
×

A little more advanced node

891 views

Published on

http://github.com/sideshowcoder/javascript-workshop

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
891
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

A little more advanced node

  1. 1. More about node.js
  2. 2. Building maintainable node.js software
  3. 3. Streams all the way down!
  4. 4. Readable Stream var Readable = require("stream").Readable var util = require("util") ! function ICanRead(options) { Readable.call(this, options) this.data = ["I", "can", "read"] } util.inherits(ICanRead, Readable) ! ICanRead.prototype._read = function(size) { var chunk = this.data.shift() this.push(chunk) } ! var rs = new ICanRead({}) ! rs.once("readable", function() { rs.pipe(process.stdout) })
  5. 5. Writable Stream
  6. 6. var Writable = require("stream").Writable var util = require("util") ! function IConcatThings(options) { Writable.call(this, options) this.data = [] } util.inherits(IConcatThings, Writable) ! IConcatThings.prototype._write = function(chunk, enc, done) { this.data.push(chunk) done() } ! var cs = new IConcatThings({ objectMode: true }) cs.on("finish", function() { console.log(cs.data) }) cs.write(1) cs.write(2) cs.write(3) cs.end()
  7. 7. Transform Stream
  8. 8. var Transform = require("stream").Transform var util = require("util") ! function ITransformThings(options) { Transform.call(this, options) this._writableState.objectMode = true this._readableState.objectMode = false } util.inherits(ITransformThings, Transform) ! ITransformThings.prototype._transform = function(chunk, enc, done) { this.push(chunk.toString()) done() } ! var ts = new ITransformThings() ts.pipe(process.stdout) ts.write(function(foo) { return foo }) ts.write("bar") ts.write([1,2,3]) ts.end()
  9. 9. Why streams are great function httpHandler(req, res) { req.pipe(bodyParser) .pipe(shortner) .pipe(stringify) .pipe(res) }
  10. 10. Explicit error handling with streams function httpHandler(req, res) { shortner.on("error", function(err) { res.statusCode = 500 res.end("Internal Server Error") }) ! } req.pipe(bodyParser) .pipe(shortner) .pipe(stringify) .pipe(res)
  11. 11. Ok just a quick thing build cat!
  12. 12. Debugging… because not all code is great :(
  13. 13. Seen that before? def my_method a require "pry" binding.pry end ! my_method "foo"
  14. 14. And this? function myMethod(a) { debugger } ! myMethod("foo")
  15. 15. using the node.js debugger

×