like ruby on rails for node - the sails js framework
Post on 10-Aug-2015
212 Views
Preview:
TRANSCRIPT
LIKE RUBY ON RAILS FOR NODE:
THE SAILS JS FRAMEWORK
By Stenio Ferreira : @stenio123 : /stenio123stenio@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 WORLDab
stra
ctio
n
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
orAPI ServerAPI Server
+ Client
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
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
WEBSOCKETAutomatic 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
PoliciesRuns after route, before controller
GruntAutomate tasks
Stenio Ferreira - www.impacta.us - May 2015
ANGULAR/ EMBER
or
BUT!
Make sure added complexity really necessary. Jquery + socket.io?
RivetsJS + Backbone?
Sails asAPI Server
Clients
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
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 STRUCTUREFolder structure
Api• Controllers• Models• Policies• Responses• ServicesAssets• ..• Styles
• Importer.less• Js
• Dependencies
Config• …• Connections.js• Routes.js• Session.js• Sockets.js• Views.jsTasks• Pipeline.jsViews• …• Layout.ejs
CLIENTCLIENT
SERVERSERVER + 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
Server + client
JSON
http://localhost:1337/user/
<%- body %>
<head>
<scripts>
VIEWS/LAYOUT.EJS VIEWS/USER/INDEX.EJS
<%= user.name %>
Stenio Ferreira - www.impacta.us - May 2015
1) To change template engineconfig/views.js
2) To use view-specifc layouton controller:res.view({layout: ‘myLayout’});
3) Javascript loading order:assets/js/dependencies (alphabetically)assets/js (alphabetically)
4) To load view-specific javascriptuse 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.jsmodule.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/resourceful-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
TestingSails supports Mocha and istanbul
Reference:
http://sailsjs.org/#!/documentation/concepts/Testing
Sails Support:Basic questions: Stackoverflow
Advanced Questions: Gitter (https://gitter.im/balderdashy/sails)
top related