LIKE RUBY ON RAILS FOR NODE:
THE SAILS JS
FRAMEWORK
By Stenio Ferreira
: @stenio123
: /stenio123
stenio@impacta.us
Stenio Ferreira - www.impacta.us - May 2015
WHAT IS SAILS JS?
BLUEPRINTS
… and lodash, bluebird, etc
Stenio Ferreira - www.impacta.us - May 2015
SHOULD YOU CARE?
Benefits
• Turnkey development environment
• Project standardization
• Flexibility
Stenio Ferreira - www.impacta.us - May 2015
SAILS JS IN THE NODE WORLD
abstraction
Koa Hapi RestifyExpress
Sails
Js templating engines
Generators (Yeoman)
Node js
Loopback Meteor
Ember Angular
Stenio Ferreira - www.impacta.us - May 2015
HOW SAIL CAN BE USED
or
API Server
API Server
+
Client
Stenio Ferreira - www.impacta.us - May 2015
API SERVER
JSON
Stenio Ferreira - www.impacta.us - May 2015
API SERVER
> npm install sails
> sails new myproject
> sails generate api user
> sails lift
http://localhost:1337
/user
/create?
name=Stenio
&surname=Ferreira
Stenio Ferreira - www.impacta.us - May 2015
API + CLIENT
JSON
Stenio Ferreira - www.impacta.us - May 2015
API + CLIENT
Javascript templating engine support:
ejs, jade, handlebars, mustache underscore, hogan, haml, haml-coffee,
dust atpl, eco, ect, jazz, jqtpl, JUST, liquor, QEJS, swig, templayed,
toffee, walrus, & whiskers
Stenio Ferreira - www.impacta.us - May 2015
DATABASE
ORM
(Object Relational Mapping)
… and others
adapters
Stenio Ferreira - www.impacta.us - May 2015
WEBSOCKET
Automatic starts listening on
> sails lift
register: Model.watch(req.socket.id);
publish: Model.publishCreate({});
subscribe: io.socket.get(“/model”);
listen: io.socket.on(“model”, function(event){});
Client (on the view)
Server (on the controller)
Stenio Ferreira - www.impacta.us - May 2015
ROUTES
No need to declare if following naming conventions. Three types:
RESTful
GET, POST, PUT, DELETE to /user
CRUD Shortcuts
/user/create?name=joe
/user/update:1?name=Joe
Action routes
methods declared in controller
BLUEPRINTS
Stenio Ferreira - www.impacta.us - May 2015
MISCELANEOUS
Policies
Runs after route, before controller
Grunt
Automate tasks
Stenio Ferreira - www.impacta.us - May 2015
ANGULAR/ EMBER
or
BUT!
Make sure added complexity really necessary.
Jquery + socket.io?
RivetsJS + Backbone?
JSON
Stenio Ferreira - www.impacta.us - May 2015
CHALLENGES WITH SAILS
• Modular? Waterline
• User management?
• Grunt magic?
• Deployment?
• Outdated info? (official docs
current!)
Stenio Ferreira - www.impacta.us - May 2015
FUTURE OF SAILS
(BUSINESS)
As of Dec 2014, received $120k Seed from Y
Combinator.
Stenio Ferreira - www.impacta.us - May 2015
BEFORE I GO…
Impacta
Small businesses have lots of internal processes.
More than a few of those are inefficient.
Do you believe you can help fix that?
For more info – www.impacta.us/jobs
Stenio Ferreira - www.impacta.us - May 2015
THANK YOU!
www.impacta.us/jobs
Stenio@impacta.us
Questions?
Stenio Ferreira - www.impacta.us - May 2015
APPENDIX
Stenio Ferreira - www.impacta.us - May 2015
USER MANAGEMENT
No Ruby on Rails Devise equivalent – Passport only works for
authentication, not authorization (and password reset). Potential
solutions:
Sails-auth – npm library to provide authorization
Waterlock – npm library to provide authorization
Sails starter app – github repository with code for password reset
Stormpath.com – SaaS for user management in Node apps
Stenio Ferreira - www.impacta.us - May 2015
FOLDER STRUCTURE
Folder structure
Api
• Controllers
• Models
• Policies
• Responses
• Services
Assets
• ..
• Styles
• Importer.less
• Js
• Dependencies
Config
• …
• Connections.js
• Routes.js
• Session.js
• Sockets.js
• Views.js
Tasks
• Pipeline.js
Views
• …
• Layout.ejs
CLIENT
CLIENT
SERVER
SERVER
+ VIEW
Stenio Ferreira - www.impacta.us - May 2015
WATERLINE
Model hooks -
- beforeValidate(), afterValidate(), beforeCreate(), afterCreate()
https://github.com/balderdashy/waterline-docs/blob/master/models.md#lifecycle-
callbacks
Associations – to populate,
Post.findOne(id).populate(‘user’)
.then(function(populatedPost) {
//work with result
}).catch(function(err) {
//error handling
});
https://github.com/balderdashy/waterline-docs/blob/master/associations.md
Stenio Ferreira - www.impacta.us - May 2015
EJS ON SAILS
Server and client inside same project
... previous steps,
manually create views/user/index.ejs,
add ‘index’ method to UserController,
> sails lift
Server + client
JSON
http://localhost:1337/user/
<%- body %>
<head>
<scripts>
<%= user.name %>
Stenio Ferreira - www.impacta.us - May 2015
1) To change template engine
config/views.js
2) To use view-specifc layout
on controller:
res.view({layout: ‘myLayout’});
3) Javascript loading order:
assets/js/dependencies (alphabetically)
assets/js (alphabetically)
4) To load view-specific javascript
use ejs-locals (already installed)
on layout.ejs: <%- blocks.localScripts %>
on view:
<% block('localScripts', '<script src=”/myScript.js”></script>’) %>
VIEW CONSIDERATIONS
Stenio Ferreira - www.impacta.us - May 2015
CONTROLLERS ON SAILS
req and res objects – same as Express
Controllers are usually written as
//UserController.js
module.exports = {
index: function(req, res) {
var user = req.session.User
res.json({name:user.name})
}
}
*newbie tip – watch out for asynchronous methods before
returning response!
Stenio Ferreira - www.impacta.us - May 2015
SOCKET IO
Check example project at
https://github.com/stenio123/sails-socket-example
Sails documentation reference:
http://sailsjs.org/#!/documentation/reference/websockets/resource
ful-pubsub
To perform actions before socket connects, or
once it disconnects:
Config/sockets.js
Stenio Ferreira - www.impacta.us - May 2015
CONFIG OPTIONS
Config/
Check the config folder for lots of interesting
options:
Config/session.js : can specify db to store session
Config/csrf.js: specify cross-site request forgery protection
settings
Config/policies.js: specify policies to be applied to certain routes
Config/env: specify environment variables (dev, prod)
Config/local.js: configurations of local machine
Config/blueprints.js: set pluralize to true if working with Ember
Stenio Ferreira - www.impacta.us - May 2015
EXTRA
Testing
Sails supports Mocha and istanbul
Reference:
http://sailsjs.org/#!/documentation/concepts/Testing
Sails Support:
Basic questions: Stackoverflow
Advanced Questions: Gitter (https://gitter.im/balderdashy/sails)

Like Ruby on Rails for Node - the Sails js framework

  • 1.
    LIKE RUBY ONRAILS FOR NODE: THE SAILS JS FRAMEWORK By Stenio Ferreira : @stenio123 : /stenio123 stenio@impacta.us
  • 2.
    Stenio Ferreira -www.impacta.us - May 2015 WHAT IS SAILS JS? BLUEPRINTS … and lodash, bluebird, etc
  • 3.
    Stenio Ferreira -www.impacta.us - May 2015 SHOULD YOU CARE? Benefits • Turnkey development environment • Project standardization • Flexibility
  • 4.
    Stenio Ferreira -www.impacta.us - May 2015 SAILS JS IN THE NODE WORLD abstraction Koa Hapi RestifyExpress Sails Js templating engines Generators (Yeoman) Node js Loopback Meteor Ember Angular
  • 5.
    Stenio Ferreira -www.impacta.us - May 2015 HOW SAIL CAN BE USED or API Server API Server + Client
  • 6.
    Stenio Ferreira -www.impacta.us - May 2015 API SERVER JSON
  • 7.
    Stenio Ferreira -www.impacta.us - May 2015 API SERVER > npm install sails > sails new myproject > sails generate api user > sails lift http://localhost:1337 /user /create? name=Stenio &surname=Ferreira
  • 8.
    Stenio Ferreira -www.impacta.us - May 2015 API + CLIENT JSON
  • 9.
    Stenio Ferreira -www.impacta.us - May 2015 API + CLIENT Javascript templating engine support: ejs, jade, handlebars, mustache underscore, hogan, haml, haml-coffee, dust atpl, eco, ect, jazz, jqtpl, JUST, liquor, QEJS, swig, templayed, toffee, walrus, & whiskers
  • 10.
    Stenio Ferreira -www.impacta.us - May 2015 DATABASE ORM (Object Relational Mapping) … and others adapters
  • 11.
    Stenio Ferreira -www.impacta.us - May 2015 WEBSOCKET Automatic starts listening on > sails lift register: Model.watch(req.socket.id); publish: Model.publishCreate({}); subscribe: io.socket.get(“/model”); listen: io.socket.on(“model”, function(event){}); Client (on the view) Server (on the controller)
  • 12.
    Stenio Ferreira -www.impacta.us - May 2015 ROUTES No need to declare if following naming conventions. Three types: RESTful GET, POST, PUT, DELETE to /user CRUD Shortcuts /user/create?name=joe /user/update:1?name=Joe Action routes methods declared in controller BLUEPRINTS
  • 13.
    Stenio Ferreira -www.impacta.us - May 2015 MISCELANEOUS Policies Runs after route, before controller Grunt Automate tasks
  • 14.
    Stenio Ferreira -www.impacta.us - May 2015 ANGULAR/ EMBER or BUT! Make sure added complexity really necessary. Jquery + socket.io? RivetsJS + Backbone? JSON
  • 15.
    Stenio Ferreira -www.impacta.us - May 2015 CHALLENGES WITH SAILS • Modular? Waterline • User management? • Grunt magic? • Deployment? • Outdated info? (official docs current!)
  • 16.
    Stenio Ferreira -www.impacta.us - May 2015 FUTURE OF SAILS (BUSINESS) As of Dec 2014, received $120k Seed from Y Combinator.
  • 17.
    Stenio Ferreira -www.impacta.us - May 2015 BEFORE I GO… Impacta Small businesses have lots of internal processes. More than a few of those are inefficient. Do you believe you can help fix that? For more info – www.impacta.us/jobs
  • 18.
    Stenio Ferreira -www.impacta.us - May 2015 THANK YOU! www.impacta.us/jobs Stenio@impacta.us Questions?
  • 19.
    Stenio Ferreira -www.impacta.us - May 2015 APPENDIX
  • 20.
    Stenio Ferreira -www.impacta.us - May 2015 USER MANAGEMENT No Ruby on Rails Devise equivalent – Passport only works for authentication, not authorization (and password reset). Potential solutions: Sails-auth – npm library to provide authorization Waterlock – npm library to provide authorization Sails starter app – github repository with code for password reset Stormpath.com – SaaS for user management in Node apps
  • 21.
    Stenio Ferreira -www.impacta.us - May 2015 FOLDER STRUCTURE Folder structure Api • Controllers • Models • Policies • Responses • Services Assets • .. • Styles • Importer.less • Js • Dependencies Config • … • Connections.js • Routes.js • Session.js • Sockets.js • Views.js Tasks • Pipeline.js Views • … • Layout.ejs CLIENT CLIENT SERVER SERVER + VIEW
  • 22.
    Stenio Ferreira -www.impacta.us - May 2015 WATERLINE Model hooks - - beforeValidate(), afterValidate(), beforeCreate(), afterCreate() https://github.com/balderdashy/waterline-docs/blob/master/models.md#lifecycle- callbacks Associations – to populate, Post.findOne(id).populate(‘user’) .then(function(populatedPost) { //work with result }).catch(function(err) { //error handling }); https://github.com/balderdashy/waterline-docs/blob/master/associations.md
  • 23.
    Stenio Ferreira -www.impacta.us - May 2015 EJS ON SAILS Server and client inside same project ... previous steps, manually create views/user/index.ejs, add ‘index’ method to UserController, > sails lift Server + client JSON http://localhost:1337/user/ <%- body %> <head> <scripts> <%= user.name %>
  • 24.
    Stenio Ferreira -www.impacta.us - May 2015 1) To change template engine config/views.js 2) To use view-specifc layout on controller: res.view({layout: ‘myLayout’}); 3) Javascript loading order: assets/js/dependencies (alphabetically) assets/js (alphabetically) 4) To load view-specific javascript use ejs-locals (already installed) on layout.ejs: <%- blocks.localScripts %> on view: <% block('localScripts', '<script src=”/myScript.js”></script>’) %> VIEW CONSIDERATIONS
  • 25.
    Stenio Ferreira -www.impacta.us - May 2015 CONTROLLERS ON SAILS req and res objects – same as Express Controllers are usually written as //UserController.js module.exports = { index: function(req, res) { var user = req.session.User res.json({name:user.name}) } } *newbie tip – watch out for asynchronous methods before returning response!
  • 26.
    Stenio Ferreira -www.impacta.us - May 2015 SOCKET IO Check example project at https://github.com/stenio123/sails-socket-example Sails documentation reference: http://sailsjs.org/#!/documentation/reference/websockets/resource ful-pubsub To perform actions before socket connects, or once it disconnects: Config/sockets.js
  • 27.
    Stenio Ferreira -www.impacta.us - May 2015 CONFIG OPTIONS Config/ Check the config folder for lots of interesting options: Config/session.js : can specify db to store session Config/csrf.js: specify cross-site request forgery protection settings Config/policies.js: specify policies to be applied to certain routes Config/env: specify environment variables (dev, prod) Config/local.js: configurations of local machine Config/blueprints.js: set pluralize to true if working with Ember
  • 28.
    Stenio Ferreira -www.impacta.us - May 2015 EXTRA Testing Sails supports Mocha and istanbul Reference: http://sailsjs.org/#!/documentation/concepts/Testing Sails Support: Basic questions: Stackoverflow Advanced Questions: Gitter (https://gitter.im/balderdashy/sails)