SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
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) {
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)
})
Writable Stream
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()
Transform Stream
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()
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 = 500
res.end("Internal Server Error")
})
!

}

req.pipe(bodyParser)
.pipe(shortner)
.pipe(stringify)
.pipe(res)
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

Weitere ähnliche Inhalte

Ähnlich wie A little more advanced node

Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...
Databricks
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...
Databricks
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecture
zefhemel
 
Writing Continuous Applications with Structured Streaming PySpark API
Writing Continuous Applications with Structured Streaming PySpark APIWriting Continuous Applications with Structured Streaming PySpark API
Writing Continuous Applications with Structured Streaming PySpark API
Databricks
 

Ähnlich wie A little more advanced node (20)

Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.js
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Writing RESTful web services using Node.js
Writing RESTful web services using Node.jsWriting RESTful web services using Node.js
Writing RESTful web services using Node.js
 
Elasticsearch And Apache Lucene For Apache Spark And MLlib
Elasticsearch And Apache Lucene For Apache Spark And MLlibElasticsearch And Apache Lucene For Apache Spark And MLlib
Elasticsearch And Apache Lucene For Apache Spark And MLlib
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...Easy, scalable, fault tolerant stream processing with structured streaming - ...
Easy, scalable, fault tolerant stream processing with structured streaming - ...
 
Java EE 7, what's in it for me?
Java EE 7, what's in it for me?Java EE 7, what's in it for me?
Java EE 7, what's in it for me?
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET Application
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecture
 
Serializing EMF models with Xtext
Serializing EMF models with XtextSerializing EMF models with Xtext
Serializing EMF models with Xtext
 
Writing Continuous Applications with Structured Streaming PySpark API
Writing Continuous Applications with Structured Streaming PySpark APIWriting Continuous Applications with Structured Streaming PySpark API
Writing Continuous Applications with Structured Streaming PySpark API
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 

Mehr von Philipp Fehre

Node.js and couchbase Full Stack JSON - Munich NoSQL
Node.js and couchbase   Full Stack JSON - Munich NoSQLNode.js and couchbase   Full Stack JSON - Munich NoSQL
Node.js and couchbase Full Stack JSON - Munich NoSQL
Philipp Fehre
 

Mehr von Philipp Fehre (19)

node.js and native code extensions by example
node.js and native code extensions by examplenode.js and native code extensions by example
node.js and native code extensions by example
 
Jruby a Pi and a database
Jruby a Pi and a databaseJruby a Pi and a database
Jruby a Pi and a database
 
Couchbase Mobile on Android
Couchbase Mobile on AndroidCouchbase Mobile on Android
Couchbase Mobile on Android
 
From 0 to syncing
From 0 to syncingFrom 0 to syncing
From 0 to syncing
 
Node.js and couchbase Full Stack JSON - Munich NoSQL
Node.js and couchbase   Full Stack JSON - Munich NoSQLNode.js and couchbase   Full Stack JSON - Munich NoSQL
Node.js and couchbase Full Stack JSON - Munich NoSQL
 
You got schema in my json
You got schema in my jsonYou got schema in my json
You got schema in my json
 
What is new in Riak 2.0
What is new in Riak 2.0What is new in Riak 2.0
What is new in Riak 2.0
 
Ember background basics
Ember background basicsEmber background basics
Ember background basics
 
Ember learn from Riak Control
Ember learn from Riak ControlEmber learn from Riak Control
Ember learn from Riak Control
 
Testing tdd jasmine
Testing tdd jasmineTesting tdd jasmine
Testing tdd jasmine
 
Testing tdd dom
Testing tdd domTesting tdd dom
Testing tdd dom
 
Something about node basics
Something about node basicsSomething about node basics
Something about node basics
 
Something about node in the realworld
Something about node in the realworldSomething about node in the realworld
Something about node in the realworld
 
Riak Intro at Munich Node.js
Riak Intro at Munich Node.jsRiak Intro at Munich Node.js
Riak Intro at Munich Node.js
 
PUT Knowledge BUCKET Brain KEY Riak
PUT Knowledge BUCKET Brain KEY RiakPUT Knowledge BUCKET Brain KEY Riak
PUT Knowledge BUCKET Brain KEY Riak
 
Campfire bot lightning talk
Campfire bot lightning talkCampfire bot lightning talk
Campfire bot lightning talk
 
Lighting fast rails with zeus
Lighting fast rails with zeusLighting fast rails with zeus
Lighting fast rails with zeus
 
JavaScript frontend testing from failure to good to great
JavaScript frontend testing from failure to good to greatJavaScript frontend testing from failure to good to great
JavaScript frontend testing from failure to good to great
 
Network with node
Network with nodeNetwork with node
Network with node
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

A little more advanced node

  • 3. Streams all the way down!
  • 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) })
  • 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()
  • 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. Why streams are great function httpHandler(req, res) { req.pipe(bodyParser) .pipe(shortner) .pipe(stringify) .pipe(res) }
  • 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. Ok just a quick thing build cat!
  • 12. Debugging… because not all code is great :(
  • 13. Seen that before? def my_method a require "pry" binding.pry end ! my_method "foo"
  • 14. And this? function myMethod(a) { debugger } ! myMethod("foo")
  • 15. using the node.js debugger