Top Banner
I Know it was MEAN but I Cut the Cord to LAMP Anyway
42

I Know It was MEAN but I Cut the Cord to LAMP Anyway

Aug 21, 2015

Download

Technology

brianhyder
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: I Know It was MEAN but I Cut the Cord to LAMP Anyway

I Know it was MEAN

but I Cut the Cord to LAMP Anyway

Page 2: I Know It was MEAN but I Cut the Cord to LAMP Anyway

The Next Milestone in Web Development

[email protected]

@GetPencilBlue

Page 3: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning Development Deployment

Actually do it Managing the flexibility What are my options?

Do I need a framework? The Basics The Upgrade

Dealing with SEO Simplifying Client Code Server Options

Finding the Right Talent Promise vs. Callback Choosing a Cloud

Page 4: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Follow along at:

pencilblue.org/presentation

Page 5: I Know It was MEAN but I Cut the Cord to LAMP Anyway

MongoDB

Page 6: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Failing to Plan

Pros ConsEasy Install Not good for inter object relationships

Flexible

Quick Development

Page 7: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Prototyping vs. Production

Page 8: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Flexible Base Service

+ save+ delete+ load

Widget Service

+ save+ delete+ load

Widget Service

+ save

Sprocket Service

+ save

{ object_type: “widget”, name: “”, description: “”}:

Page 9: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Too Flexible?

Document with Strings{ object_type: “event”, type: “startup”, location: { lat: “35.7806”, lon: “78.6389” }}

Document with Floats{ object_type: “event”, type: “startup”, location: { lat: 35.7806, lon: 78.6389 }}

● Valid JSON● Pases non-strict validation (JS)● Geospatial index fails on insert

Page 10: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Schema Documentation

Page 11: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Common Data Fields

{ “first_name”: “Charlie”, “last_name”: “Daniels”, ...

“doc_type”: “user”, “doc_version”: 1, “created”: “2014-09-11 15:01:07.602Z”, “last_modified”: “2014-09-11 15:01:07.602Z”, “created_by”: "5411b9334572668410000fd0”, “last_modified_by”: "5411b9334572668410000fd0”}

Page 12: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Geospatial Indexing

The $geoWithin operator does not return sorted results. As a result MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.

db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } );

db.places.find( { "loc": { "$geoWithin": { "$center": [[50, 50, 5] } } });

==

Page 13: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - Expense at Scale

Deployment Cost Managed

AWS m1.large $0.20/hr NO

Mongo HQ $18/GB* YES

Mongo Lab $15/GB* YES

Linode Medium $20/mo NO

* Other base fees may apply

Page 14: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - MMS & Getting to Scale

Page 15: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Routing & Frameworks

Page 16: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Do I need a framework?

● IS IT OPEN SOURCE?● How often will I need to upgrade?● If the project goes stagnant does that affect me?● If the project gets bought by a company I don’t like does that affect me?● What kind of custom functionality do I need to inject?● Will packages be sufficient or will I need to fork so I can experiment?● Do I want to maintain something custom?● Can I do as well as or better than what is already out there?

Page 17: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Rube Goldberg of Software

Page 18: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - The Forum Conundrum

Page 19: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - The Basic Example

var express = require('express')var app = express()

app.get('/', function (req, res) { res.send('Hello World!')})

var server = app.listen(3000, function () {

var host = server.address().address var port = server.address().port console.log('Example app listening at http://%s:%s', host, port)})

Page 20: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - Upgrading

What happens when I try to upgrade?

Chance of Conflict

Time

Page 21: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Angular

Page 22: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Google did it!

Page 23: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Weighing In

Page 24: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Documentation

Pros ConsLarge Documentation Site API Documentation assumes

understanding of Angular

Support Groups Error handling can be cryptic

Guided Tutorials & Example Projects

Page 25: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Crawlers and Javascript

Pros Cons

Server Rendered Landing Pages

SEO becomes easier because it is the traditional model.

Even mediocre crawlers can navigate your site

Mix and match of SPA and traditional model

Tracking which pages need to be rendered server-side

Single Page App Consistent SEO implementation

Metadata data changes based on page context

Google is only search engine boasting javascript execution.

Page 26: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - A Simple Example

<ol> <li ng-repeat="user in users" ng-bind="user"></li></ol>

//service codeangular.module('myservices', []).service('simpleService', function(){ this.getUsers = function() { return ['John', 'James', 'Jake']; }});

//controller codeangular.module('myapp', ['myservices']).controller('SimpleController', function($scope, simpleService) { $scope.users = simpleService.getUsers();});

Page 27: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - A Growing Stack

Data Access

Service Layer and Business Logic

API Controller

Angular Service

Angular ControllerClient

Server

Page 28: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Separation of Concern => Testability

describe("Unit Testing Examples", function() {

beforeEach(angular.mock.module('App'));

it('should have a LoginCtrl controller', function() { expect(App.LoginCtrl).toBeDefined(); });

it('should have a working LoginService service', inject(['LoginService', function(LoginService) { expect(LoginService.isValidEmail).not.to.equal(null); }]) );});

Page 29: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - Serving a Single Page App?

Considerations:

● How much data do I need to persist?● Is my application solely a SPA?

Possible Options:

● Apache / PHP● Python● Rails● Node.js

Page 30: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Node.js

Page 31: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Resource Management

Now that I use the same language across the entire stack hiring just got simpler.

Page 32: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Resource Management

Page 33: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Planning - Adoption

Big names are jumping on board:

● Linkedin● Walmart Labs● PencilBlue

http://nodejs.org/industry/

Page 34: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Promise vs. Callback

//Promise examplefunction fs_readFile (file, encoding) { var deferred = Q.defer() fs.readFile(file, encoding, function (err, data) { if (err) { deferred.reject(err) // rejects the promise with `er` as the reason } else { deferred.resolve(data) // fulfills the promise with `data` as the value } }) return deferred.promise // the promise is returned}

fs_readFile('myfile.txt').then(console.log, console.error)Courtesy of StrongLoop

Page 35: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Promise vs. Callback

//Callback examplefunction fs_readFile (file, encoding, cb) {

fs.readFile(file, encoding, cb);}

fs_readFile('myfile.txt', function (err, data) { if (err) { console.error(err) // outputs error } else { console.log(data) // output result } });

Courtesy of StrongLoop

Page 36: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Node Inspector

Page 37: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Clustering

if (cluster.isMaster) System.onMasterRunning();else onChildRunning();

System.onMasterRunning = function() { //spawn workers var workerCnt = os.cpus().length; for (var i = 0; i < workerCnt; i++) { cluster.fork(); } cluster.on('disconnect', System.onWorkerDisconntect);

pb.log.info('System[%s]: %d workers spawned. Listening for disconnects.', System.getWorkerId(), workerCnt);};

Core 1 Core 2 Core 3 Core 4 Core 5 Core 6 Core 7

MW W W W W W W

Page 38: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Development - Error Handling

var http = require(‘http’); var domain = require(‘domain’);

http.createServer(function(req, res){

var d = domain.create(); d.add(req); d.add(res); d.on(‘error’, handleError);

d.run(function() { handleRequest(req, res); });

}).listen(8080);

Page 39: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - Shooting for the Cloud

Elastic Beanstalk OpenShift Nodejitsu Bluemix

Linode Heroku

Page 40: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Deployment - Starting Simple

LB

M

W

M

W

W

W

Use SSL Termination

Pay for the backup service

Write a procedure for creating these nodes

Page 41: I Know It was MEAN but I Cut the Cord to LAMP Anyway

Pros Cons

Extremely FlexibleRapid Development

Expensive at Scale

Fast “Good Enough” HTTP Server Upgrades cause code changes

A battle tank that can do anything Error messages are a bit obfuscated

Fast and efficient Hard to debug

To Recap...

Page 42: I Know It was MEAN but I Cut the Cord to LAMP Anyway

The choice is yours but...