- greelgorke @ twitter, github, #node.js irc + google group
- http://greelgorke.tumblr.com/about
- working @ wowbiz.de
- working with Node over a year or so
- my English sucks, patches welcome, issues too.
- if I'm talking strange or too quite, tell me.
- lot's of code incoming
- this talk contains hot coded examples: bugs included.
- I'm not representing Node.js Community.
- Some practices presented here are opinions, some not.
- This talk is about software design, architecture and modularization
- SRP, DRY, generic interfaces, less is elegant, but readable first
- callback spaghetti, callback hell?
- 10k concurrent connections, non-blocking I/O
- server-side-js w/o jQuery?
- small core, big community
- callback !== closure
- "With great closures comes great responsibility"
- Closures are key to many useful patterns and programming techniques.
- Closures misused are poison
- cyclomatic complexity
- small and simple webchat
- evolve it applying few principles and practices
- reqs: serve static files, find and present avatars from twitter or github
- all in one: 1_gh.js
- chat not even working
- too painful..
- Identify responsibilities, separate concerns
- one thing doing one task well
- some don't know: require() caches -> a module is a singleton
- one function modules aka substack modules
- one -higher order- function modules, inject what you want.
function aCallback(error, result){}
function anAsyncThing(input, moreInput, callbackAtLast){}
- pass the callback as deep as possible
- caveat: an ENOENT isn't 404
- file explosion: many different responsibilities
- Server: get the request, send the response
- rendering, fetch user image, serve static, etc
- callback, done better
- first: modules are not enough
- callback pattern is simple and straight forward
- streams, events are evolution of callbacks
- decouple modules by message passing
- define a contract and document it
- emit, 'error', 'data' etc.
- caveat: need knowledge about format of results
- -> reduce needed knowledge about format
- decouple modules by uniformed interface and message passing
- format is string (or Buffer), coupling is evented or piped
- sometimes we still need to know what's coming in.
- some core modules are not that stream friendly as they should be: http/s headers
- know Connect/Express?
- what about async?
- plugin pattern
- Let's do same
- server is feeding the chain of plugable modules (Chain of Responsibility)
- using streams where appropriate
- imgRepo.js
- so far we've seen: callbacks, modules, streams
- what about routers: a router is just a plugin, that maintains a subchain (or a tree of sub-chains)
- Questions so far?
- Streams as flow control: design your app as a pipeline
- If your app transform string inputs: do core Streams, they're good till awesome
- heard about Flow Design? For node you can find something like that too: event-stream
- This is NOT common in Node
- a different way of thinking about applications and libs
- feels native to me.
- Still: we have to define a context, here it's http