Upload
ben-hall
View
402
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Presentation given at Oredev 2014 on the 6th November.
Citation preview
REAL WORLD LESSONS ON THE ANTI-PATTERNS OF NODE.JS
@Ben_Hall
Ben@BenHall .me.uk
Blog.BenHall .me.uk
MY BATTLE SCARS OF NODE.JS
@Ben_Hall
Ben@BenHall .me.uk
Blog.BenHall .me.uk
MY LIFE, MY STORY… BY BEN HALL.
PUBLISHER TBC
$ whoami
Ocelot Uproar
@Ben_Hall
Tea boy > Tester > Developer > Freelancer > Tea boy?
Agenda
• Modules + NPM
• Project Structure
• Async
• Error Handling
• Testing
• Debugging
• Production
Disclaimer
Examples are not a refl ection on the developer/team/company. Based on
problems I ’ve caused myself or struggled with. JavaScript is very
subjective…blah blah blah… If you’re doing it r ight, then *amazing*! I ’m
not.
Love, Ben x
WHO CARES ABOUT CLEAN CODE IF THERE ARE NO SALES?
Prototypes
MODULES + NPM
Package.json + NPMWonderful!
Look at NuGet, GoDep, Bundler
AngularJs 1.2 => 1.3
1. "dependenc ies" : {
2. "angu lar" : "^1.2 .16”
3. }
��
Angular 1.2 => 1.3
> angular.element(document)
[#document]
> angular.element(document)
TypeError: undefi ned is not a function
Lock Down Dependencies
Randomly breaking bui lds and deployments wil l occur
otherwise
$ npm shrinkwrap
Lock down dependencies to what’s running local ly
��
Hard code versions in package.json
1. "dependencies": {
2. "angular": “1.2.23”
3. }
��
$ npm outdated
$ npm install
Downloading the internet on each deployment can be
slow
��
$ npm install --production
��
$ npm install -g
Genera l ly promoted by test f rameworks . Genera l ly ends up
not be ing in package. j son. Genera l ly broke my deployment .
��
“HELPERS” & PROJECT STRUCTURE
��
��
��
��
Libs/<Context>/<Task>.jsLibs/auth/users/create.js
Why not an NPM?
��
��
ASYNC
Because J avaSc r ip t
Promises
Promises… Promises… Never break your promises .
Personal ly , I never make promises .
http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/
��
Not part of the Node
Makes integration more diffi cult. Makes swapping
code in / out more painful.
Callbacks
So good it ’s got it ’s own website cal lbackhel l .com
“The goal i sn ’ t about removing levels o f indentat ion but rather wr i t ing modular
code that i s easy to reason about”
Strongloop Blog
http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/
��
��
Loops + Async Callbacks��
Loops + Async Callbacks��
“You can't get into callback hell if you don't go there.”
Isaac Schlueter
Generators are coming!
See Node >=0.11.2
��
http://blog.alexmaccaw.com/how-yield-will-transform-node
��
http://blog.alexmaccaw.com/how-yield-will-transform-node
��
ERROR HANDLING
Wasn’t great from the start
Sti l l not great
Try {} Catch {}��
Try {} Catch {}��
��
Domains haven’t really worked
��
Event Driven Errors��
Event Driven Errors��
Returning String as Error��
��
��
��
Async Flow Control
Zones are coming!
See Node >=0.11.2
��
��
https://raw.githubusercontent.com/strongloop/zone/master/showcase/curl/curl-zone.js
Generators + Error Handling
��
��
TESTING
Callback Hell v2
I t ’s even worse the second time around…
��
��
DEBUGGING
Yay! Console.log
��
Writing onto a fi le onto a production box is painful to debug
��
��
��
http://bigsnarf.files.wordpress.com/2014/02/screen-shot-2014-02-27-at-1-42-54-pm.png
node-bunyan��
��
https://papertrailapp.com/
@Stack72’s talk or speak with @gblock
Fr iday @ 14.20
BUILDING A MONITORING INFRASTRUCTURE WITH PUPPET
DEPLOYMENT
https://groups.google.com/forum/#!topic/nodejs/NQkz3-BEwYw
Binding to Port 80��
https://groups.google.com/forum/#!topic/nodejs/NQkz3-BEwYw
Binding to Port 80Answer: sudo node app.js
��
Nginx handles SSL Termination, Load Balancing, Cache Headers
Let Node be Node
http://blog.benhall.me.uk/2011/12/using-nginx-to-server-static-files-instead-of-node-js/
��
http://blog.benhall.me.uk/2011/12/using-nginx-to-server-static-files-instead-of-node-js/
��
Docker
Digital Ocean Droplet
Nginx bound to port 80
Node.js bound to 3000
ElasticSearch Container
Node.js bound to 3000
HTTP Request ��
AND FINALLY
Node is *Amazing*
Let’s you get away with a lot without hurting too much
Async and Errors are the biggest issue. Everything
else is easier once they’re solved.
@Ben_Hal l
Ben@BenHal l .me.uk
Jo inScrapbook.com