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

Real World Lessons On The Anti-Patterns of Node.JS

Jun 29, 2015

Download

Technology

Ben Hall

Talk delivered at London Node User Group on 22nd October 2014. Talk covers my personal pain points, issues I've encountered with Node and some suggested alternatives
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Real World Lessons On The Anti-Patterns of Node.JS

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

@Ben_Hall

Ben@BenHall .me.uk

Blog.BenHall .me.uk

Page 2: Real World Lessons On The Anti-Patterns of Node.JS

MY BATTLE SCARS OF NODE.JS

@Ben_Hall

Ben@BenHall .me.uk

Blog.BenHall .me.uk

Page 3: Real World Lessons On The Anti-Patterns of Node.JS

MY LIFE, MY STORY, MY ANTI-PATTERNS

BY BEN HALL.

PUBLISHER TBC

Page 4: Real World Lessons On The Anti-Patterns of Node.JS

$ whoami

Ocelot Uproar

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

Page 5: Real World Lessons On The Anti-Patterns of Node.JS

My main pain points & agenda

• Modules + NPM

• Project Structure

• Async

• Error Handling

Page 6: Real World Lessons On The Anti-Patterns of Node.JS

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: Real World Lessons On The Anti-Patterns of Node.JS
Page 8: Real World Lessons On The Anti-Patterns of Node.JS
Page 9: Real World Lessons On The Anti-Patterns of Node.JS

MODULES + NPM

Page 10: Real World Lessons On The Anti-Patterns of Node.JS

AngularJs 1.2 => 1.3

1. "dependenc ies" : {

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

3. }

��

Page 11: Real World Lessons On The Anti-Patterns of Node.JS

Angular 1.2 => 1.3

> angular.element(document)

[#document]

> angular.element(document)

TypeError: undefi ned is not a function

Page 12: Real World Lessons On The Anti-Patterns of Node.JS

Lock Down Dependencies

Randomly breaking bui lds and deployments wil l occur

otherwise

Page 13: Real World Lessons On The Anti-Patterns of Node.JS

$ npm shrinkwrap

Lock down dependencies to what’s running local ly

��

Page 14: Real World Lessons On The Anti-Patterns of Node.JS
Page 15: Real World Lessons On The Anti-Patterns of Node.JS

Hard code versions in package.json

1. "dependencies": {

2. "angular": “1.2.23”

3. }

��

Page 16: Real World Lessons On The Anti-Patterns of Node.JS

$ npm outdated

Page 17: Real World Lessons On The Anti-Patterns of Node.JS

$ npm install

Downloading the internet on each deployment can be

slow

��

Page 18: Real World Lessons On The Anti-Patterns of Node.JS

$ npm install --production

��

Page 19: Real World Lessons On The Anti-Patterns of Node.JS

“HELPERS” & PROJECT STRUCTURE

��

Page 20: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 21: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 22: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 23: Real World Lessons On The Anti-Patterns of Node.JS

Libs/<Context>/<Task>.js

Why not an NPM?

��

Page 24: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 25: Real World Lessons On The Anti-Patterns of Node.JS

ASYNC

Because J avaSc r ip t

Page 26: Real World Lessons On The Anti-Patterns of Node.JS

Promises

Promises… Promises… Never break your promises.

Personal ly , never make promises.

Page 27: Real World Lessons On The Anti-Patterns of Node.JS

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

��

Page 28: Real World Lessons On The Anti-Patterns of Node.JS

Not part of the Node

Makes integration more diffi cult. Makes swapping

code in / out more painful.

Page 29: Real World Lessons On The Anti-Patterns of Node.JS

Callbacks

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

Page 30: Real World Lessons On The Anti-Patterns of Node.JS
Page 31: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 32: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 33: Real World Lessons On The Anti-Patterns of Node.JS

Loops + Async Callbacks��

Page 34: Real World Lessons On The Anti-Patterns of Node.JS

Loops + Async Callbacks��

Page 35: Real World Lessons On The Anti-Patterns of Node.JS

Generators are coming!

See Node >=0.11.2

��

Page 36: Real World Lessons On The Anti-Patterns of Node.JS

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

��

Page 37: Real World Lessons On The Anti-Patterns of Node.JS

ERROR HANDLING

Page 38: Real World Lessons On The Anti-Patterns of Node.JS

Wasn’t great from the start

Sti l l not great

Page 39: Real World Lessons On The Anti-Patterns of Node.JS

Try {} Catch {}��

Page 40: Real World Lessons On The Anti-Patterns of Node.JS

Try {} Catch {}��

Page 41: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 42: Real World Lessons On The Anti-Patterns of Node.JS

Returning String as Error��

Page 43: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 44: Real World Lessons On The Anti-Patterns of Node.JS

Zones are coming!

See Node >=0.11.2

��

Page 45: Real World Lessons On The Anti-Patterns of Node.JS

��

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

Page 46: Real World Lessons On The Anti-Patterns of Node.JS

Generators + Error Handling

��

Page 47: Real World Lessons On The Anti-Patterns of Node.JS

��

Page 48: Real World Lessons On The Anti-Patterns of Node.JS

AND FINALLY

Page 49: Real World Lessons On The Anti-Patterns of Node.JS

Node is *Amazing*

Page 50: Real World Lessons On The Anti-Patterns of Node.JS

Let’s you get away with a lot without hurting too much

Modules + NPM

Pro jec t S t ructure

Async

Error Hand l ing

Page 51: Real World Lessons On The Anti-Patterns of Node.JS

@Ben_Hall

Ben@BenHall .me.uk

Blog.BenHall .me.uk