+ All Categories
Home > Technology > Like Ruby on Rails for Node - the Sails js framework

Like Ruby on Rails for Node - the Sails js framework

Date post: 10-Aug-2015
Category:
Upload: stenio-ferreira
View: 213 times
Download: 1 times
Share this document with a friend
Popular Tags:
28
LIKE RUBY ON RAILS FOR NODE: THE SAILS JS FRAMEWORK By Stenio Ferreira : @stenio123 : /stenio123 [email protected]
Transcript
Page 1: Like Ruby on Rails for Node - the Sails js framework

LIKE RUBY ON RAILS FOR NODE:

THE SAILS JS FRAMEWORK

By Stenio Ferreira : @stenio123 : /[email protected]

Page 2: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

WHAT IS SAILS JS?

BLUEPRINTS

… and lodash, bluebird, etc

Page 3: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

SHOULD YOU CARE?

Benefits

• Turnkey development environment

• Project standardization

• Flexibility

Page 4: Like Ruby on Rails for Node - the Sails js framework

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

Page 5: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

HOW SAIL CAN BE USED

orAPI ServerAPI Server

+ Client

Page 6: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

API SERVER

JSON

Server

Clients

Page 8: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

API + CLIENT

Server

Client JSON

Page 9: Like Ruby on Rails for Node - the Sails js framework

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

Page 10: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

DATABASE

ORM(Object Relational Mapping)

… and others

adapters

Page 11: Like Ruby on Rails for Node - the Sails js framework

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)

Page 12: Like Ruby on Rails for Node - the Sails js framework

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

Page 13: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

MISCELANEOUS

PoliciesRuns after route, before controller

GruntAutomate tasks

Page 14: Like Ruby on Rails for Node - the Sails js framework

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

Page 15: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

CHALLENGES WITH SAILS

• Modular? Waterline

• User management?

• Grunt magic?

• Deployment?

• Outdated info? (official docs current!)

Page 16: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

FUTURE OF SAILS (BUSINESS)

As of Dec 2014, received $120k Seed from Y Combinator.

Page 17: Like Ruby on Rails for Node - the Sails js framework

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

Page 18: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

THANK YOU!

www.impacta.us/jobs

[email protected]

Questions?

Page 19: Like Ruby on Rails for Node - the Sails js framework

Stenio Ferreira - www.impacta.us - May 2015

APPENDIX

Page 20: Like Ruby on Rails for Node - the Sails js framework

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

Page 21: Like Ruby on Rails for Node - the Sails js framework

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

Page 22: Like Ruby on Rails for Node - the Sails js framework

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

Page 23: Like Ruby on Rails for Node - the Sails js framework

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 %>

Page 24: Like Ruby on Rails for Node - the Sails js framework

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

Page 25: Like Ruby on Rails for Node - the Sails js framework

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!

Page 26: Like Ruby on Rails for Node - the Sails js framework

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

Page 27: Like Ruby on Rails for Node - the Sails js framework

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

Page 28: Like Ruby on Rails for Node - the Sails js framework

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)


Recommended