come sail away with me (you guys): node.js mvc web api's using sails.js

21
Come Sail Away With Me *you guys* Node.js MVC Web API’s Using Sails.js

Upload: eric-nograles

Post on 16-Jul-2015

813 views

Category:

Technology


1 download

TRANSCRIPT

Come Sail Away With Me*you guys*

Node.js MVC Web API’s Using Sails.js

Eric Nograles

Software Developer (352 Inc.)

Email: [email protected]

Twitter: @grales

GitHub:

https://github.com/ericnograles

LinkedIn:

https://www.linkedin.com/in/ericn

ograles

My 2014A JavaScript Vision Quest

The Result…Proxy.gg (https://www.proxy.gg)

SailsOur Web API Weapon of Choice

Meet Sails!

• Full MVC framework on Node.js and Express

• Can be used in request/response web or Web

API capacity

• Integrated, curated libraries

• Wide community support

The Sails Way

• Stay true to Node.js/Express development

• Convention over configuration, but not to the

point of “dark magic”

• “Just enough” framework

A working WebAPI in under 1 minute

Don’t believe me, just watch.

A 3 Hour 40 Minute Tour

• Routing/Policies

• Waterline ORM

• WebSockets pub/sub using socket.io

• Separation of Concerns

• Unit Testing

Routing/Policies

• Conventions

• /controllers/ExampleController.someAction =

/example/someAction

• If a corresponding model is defined, RESTful routes enabled

• Controller Actions: find/findOne, create, update, destroy

• Policies defined in /config/policies.js (which uses /api/policies) are enforced

• Features

• Express-style routes

• Route conventions highly configurable

• Conventions

• Globally accessible in Sails

• Uses default connection defined in /config/models.js and

/config/connections.js

• Model connections and settings are overridable

• Features

• Schema enforcement

• Lifecycle callbacks (events after create, update, destroy)

Pub/Sub

• Conventions

• WebSockets globally accessible via sails.io.sockets

• All WebSocket settings defined by

/config/sockets.js

• Features

• Socket.io client readily available from startup

• Distributed pub/sub capability (via redis)

Great tools and allBut, how do we make this enterprise grade?

Separation of Concerns: A

Classical Approach

Separation of Concerns:

Sails.js

Unit Testing

• Mocha: a JavaScript unit testing framework

• Integrates seamlessly with WebStorm

• Proxyquire: mock require dependencies

• Grunt: Automated task runner for tests, integrates

with CI

Install grunt-mocha-test from NPM

Establish test.js under /tasks,

configure mochaTest to run

tests/**/*.spec.js.

Establish a /tests folder. Follow the structure

of the /api folder

Author some unit tests using *.spec.js

naming convention. Use proxyquire to

inject require modules where applicable.

Run grunt mochaTest

WebStorm Integration

Avast!

• It’s a full MVC solution on Node.js! Yay!

• Sails offers enough convention to increase your

development velocity…

• …but, also offers enough configuration to

handle project realities

• Enterprise patterns translate well to Sails

GitHub Goodies!

• https://github.com/ericnograles/DeepThought

• master: Close to “out-of-the-box” Sails

• repositoryPattern: master + repo pattern

• fullEnterprise: Full n-tiered architecture,

with a redis application cache

Thanks!

_.each(attendees, function(attendee) {

eric.highFive(attendee);

});