88
REAL WORLD LESSONS ON THE ANTI-PATTERNS OF NODE.JS @Ben_Hall [email protected] Blog.BenHall.me.uk

Node.js Anti Patterns

Embed Size (px)

DESCRIPTION

Presentation given at Oredev 2014 on the 6th November.

Citation preview

Page 1: Node.js Anti Patterns

REAL WORLD LESSONS ON THE ANTI-PATTERNS OF NODE.JS

@Ben_Hall

Ben@BenHall .me.uk

Blog.BenHall .me.uk

Page 2: Node.js Anti Patterns

MY BATTLE SCARS OF NODE.JS

@Ben_Hall

Ben@BenHall .me.uk

Blog.BenHall .me.uk

Page 3: Node.js Anti Patterns

MY LIFE, MY STORY… BY BEN HALL.

PUBLISHER TBC

Page 4: Node.js Anti Patterns

$ whoami

Ocelot Uproar

@Ben_Hall

Tea boy > Tester > Developer > Freelancer > Tea boy?

Page 5: Node.js Anti Patterns

Agenda

• Modules + NPM

• Project Structure

• Async

• Error Handling

• Testing

• Debugging

• Production

Page 6: Node.js Anti Patterns

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

Page 7: Node.js Anti Patterns

WHO CARES ABOUT CLEAN CODE IF THERE ARE NO SALES?

Page 8: Node.js Anti Patterns

Prototypes

Page 9: Node.js Anti Patterns
Page 10: Node.js Anti Patterns
Page 11: Node.js Anti Patterns

MODULES + NPM

Page 12: Node.js Anti Patterns

Package.json + NPMWonderful!

Look at NuGet, GoDep, Bundler

Page 13: Node.js Anti Patterns

AngularJs 1.2 => 1.3

1. "dependenc ies" : {

2. "angu lar" : "^1.2 .16”

3. }

��

Page 14: Node.js Anti Patterns

Angular 1.2 => 1.3

> angular.element(document)

[#document]

> angular.element(document)

TypeError: undefi ned is not a function

Page 15: Node.js Anti Patterns

Lock Down Dependencies

Randomly breaking bui lds and deployments wil l occur

otherwise

Page 16: Node.js Anti Patterns

$ npm shrinkwrap

Lock down dependencies to what’s running local ly

��

Page 17: Node.js Anti Patterns
Page 18: Node.js Anti Patterns

Hard code versions in package.json

1. "dependencies": {

2. "angular": “1.2.23”

3. }

��

Page 19: Node.js Anti Patterns

$ npm outdated

Page 20: Node.js Anti Patterns

$ npm install

Downloading the internet on each deployment can be

slow

��

Page 21: Node.js Anti Patterns

$ npm install --production

��

Page 22: Node.js Anti Patterns

$ 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 .

��

Page 23: Node.js Anti Patterns

“HELPERS” & PROJECT STRUCTURE

��

Page 24: Node.js Anti Patterns

��

Page 25: Node.js Anti Patterns

��

Page 26: Node.js Anti Patterns

��

Page 27: Node.js Anti Patterns

Libs/<Context>/<Task>.jsLibs/auth/users/create.js

Why not an NPM?

��

Page 28: Node.js Anti Patterns

��

Page 29: Node.js Anti Patterns

ASYNC

Because J avaSc r ip t

Page 30: Node.js Anti Patterns

Promises

Promises… Promises… Never break your promises .

Personal ly , I never make promises .

Page 31: Node.js Anti Patterns

http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/

��

Page 32: Node.js Anti Patterns

Not part of the Node

Makes integration more diffi cult. Makes swapping

code in / out more painful.

Page 33: Node.js Anti Patterns

Callbacks

So good it ’s got it ’s own website cal lbackhel l .com

Page 34: Node.js Anti Patterns

“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/

Page 35: Node.js Anti Patterns
Page 36: Node.js Anti Patterns
Page 37: Node.js Anti Patterns
Page 38: Node.js Anti Patterns
Page 39: Node.js Anti Patterns
Page 40: Node.js Anti Patterns

��

Page 41: Node.js Anti Patterns

��

Page 42: Node.js Anti Patterns

Loops + Async Callbacks��

Page 43: Node.js Anti Patterns

Loops + Async Callbacks��

Page 44: Node.js Anti Patterns

“You can't get into callback hell if you don't go there.”

Isaac Schlueter

Page 45: Node.js Anti Patterns

Generators are coming!

See Node >=0.11.2

��

Page 46: Node.js Anti Patterns

http://blog.alexmaccaw.com/how-yield-will-transform-node

��

Page 47: Node.js Anti Patterns

http://blog.alexmaccaw.com/how-yield-will-transform-node

��

Page 48: Node.js Anti Patterns

ERROR HANDLING

Page 49: Node.js Anti Patterns

Wasn’t great from the start

Sti l l not great

Page 50: Node.js Anti Patterns

Try {} Catch {}��

Page 51: Node.js Anti Patterns

Try {} Catch {}��

Page 52: Node.js Anti Patterns

��

Page 53: Node.js Anti Patterns

Domains haven’t really worked

��

Page 54: Node.js Anti Patterns

Event Driven Errors��

Page 55: Node.js Anti Patterns

Event Driven Errors��

Page 56: Node.js Anti Patterns

Returning String as Error��

Page 57: Node.js Anti Patterns

��

Page 58: Node.js Anti Patterns

��

Page 59: Node.js Anti Patterns

��

Async Flow Control

Page 60: Node.js Anti Patterns

Zones are coming!

See Node >=0.11.2

��

Page 61: Node.js Anti Patterns

��

https://raw.githubusercontent.com/strongloop/zone/master/showcase/curl/curl-zone.js

Page 62: Node.js Anti Patterns

Generators + Error Handling

��

Page 63: Node.js Anti Patterns

��

Page 64: Node.js Anti Patterns

TESTING

Page 65: Node.js Anti Patterns

Callback Hell v2

I t ’s even worse the second time around…

Page 66: Node.js Anti Patterns

��

Page 67: Node.js Anti Patterns

��

Page 68: Node.js Anti Patterns

DEBUGGING

Page 69: Node.js Anti Patterns

Yay! Console.log

��

Page 70: Node.js Anti Patterns

Writing onto a fi le onto a production box is painful to debug

��

Page 71: Node.js Anti Patterns

��

Page 72: Node.js Anti Patterns

��

http://bigsnarf.files.wordpress.com/2014/02/screen-shot-2014-02-27-at-1-42-54-pm.png

Page 73: Node.js Anti Patterns

node-bunyan��

Page 74: Node.js Anti Patterns

��

https://papertrailapp.com/

Page 75: Node.js Anti Patterns

@Stack72’s talk or speak with @gblock

Fr iday @ 14.20

BUILDING A MONITORING INFRASTRUCTURE WITH PUPPET

Page 76: Node.js Anti Patterns

DEPLOYMENT

Page 77: Node.js Anti Patterns

https://groups.google.com/forum/#!topic/nodejs/NQkz3-BEwYw

Binding to Port 80��

Page 78: Node.js Anti Patterns

https://groups.google.com/forum/#!topic/nodejs/NQkz3-BEwYw

Binding to Port 80Answer: sudo node app.js

��

Page 79: Node.js Anti Patterns

Nginx handles SSL Termination, Load Balancing, Cache Headers

Let Node be Node

Page 80: Node.js Anti Patterns

http://blog.benhall.me.uk/2011/12/using-nginx-to-server-static-files-instead-of-node-js/

��

Page 81: Node.js Anti Patterns

http://blog.benhall.me.uk/2011/12/using-nginx-to-server-static-files-instead-of-node-js/

��

Page 82: Node.js Anti Patterns
Page 83: Node.js Anti Patterns

Docker

Page 84: Node.js Anti Patterns

Digital Ocean Droplet

Nginx bound to port 80

Node.js bound to 3000

ElasticSearch Container

Node.js bound to 3000

HTTP Request ��

Page 85: Node.js Anti Patterns

AND FINALLY

Page 86: Node.js Anti Patterns

Node is *Amazing*

Page 87: Node.js Anti Patterns

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.

Page 88: Node.js Anti Patterns

@Ben_Hal l

Ben@BenHal l .me.uk

Jo inScrapbook.com