http://www.meetup.com/Pittsburgh-Node-js/
NICHOLAS MCCLAY
UX Developer

@nickmcclay
MEET YOUR NEIGHBOR
Framework Overview
SAILS HIGHLIGHTS
• Robust Node.js MVC web server framework
!

• Railsy features - scaffolding, DB agnostic ORM
!

• Automatically generated RESTful JSON API
!

• Out-of-the-box real-time socket.io integration
!

• Role based access policies and ACL
!

• JS/CSS asset bundling and minification (Grunt)
QUICK NOTE:
Convention > Configuration = Magic
WHAT IS A
SINGLE PAGE APPLICATION?
TRADITIONAL WEB APPLICATION
http://www.cutekitties.com/kitten/1

One Kitten Please!

HTTP Request
Response
HTML
JavaScript
CSS
TADA! WEB PAGE!
SINGLE PAGE APPLICATION
http://www.cutekitties.com#/kitten/1

One Kitten Please!

HTTP Request
Application

Response
HTML
JavaScript
CSS
/kitten/1?format=json

GET /kitten/1

A JAX Request
[{

"_id" : "abc124efg345",

"lives" : 9,

"curiousity" : "massive",

"cuteness" : 9999,

"eyes" : "blue",

"fur" : "yellow"

}]

Response
JSON
TADA! WEB APP!
Heading out to sea
SAILS VERSIONS
V0.9

Coming

V0.10
V0.10

Soon

• Current NPM default

• beta version (RC3)

• Reliable starting place

• Significant Refactors

• What I’ll be demoing

• API changes

in this presentation

• Associations
• Here be dragons…

https://github.com/balderdashy/sails-docs/blob/master/Migration-Guide.md
DOCUMENTATION SOURCES
Official Website Documentation

http://sailsjs.org/#!documentation

GitHub Documentation Repo
Coming

V0.10
Soon

https://github.com/balderdashy/sails-docs/tree/0.10
CREATE A NEW SAILS PROJECT
http://sailsjs.org/#!getStarted
0.10.0 NPM INSTALL WEIRDNESS

If you encounter this, try:
npm install -g
SAILS CLI BASICS
Turn on asset linking

Make a new app

sails new <appName>
Run App

sails lift

--linker

Set Env Config

Display all logs

--dev --prod --verbose

Display Sails.js Version

sails version
SAILS SCAFFOLDING
Generate Scaffolding

sails generate (type) <id>
model, controller, default is both
Pluggable Generators for Scaffolding
Coming

V0.10
Soon

sails generate (type) <id>
model, controller, api, blog,
user, whatever you want!
MODELS

(Waterline Collection)

api/models

var Person = {

schema : true,

attributes: {

firstName: 'string',

lastName: 'string',

password : {

type : 'string',

required : true,

notEmpty : true

},

age : {

type: 'integer',

max: 150,

required: true

},

birthDate: 'date',

phoneNumber: {

type: 'string',

defaultsTo: '111-222-3333'

},

emailAddress: {

type: 'email', // Email type will get validated by the ORM

required: true

}

}

};

model.exports = Person;
BEYOND ATTRIBUTES
// Define a custom instance method

fullName: function() {

return this.firstName + ' ' + this.lastName;

},







// Lifecycle Callbacks

beforeCreate: function(values, next) {

bcrypt.hash(values.password, 10, function(err, hash) {

if(err) return next(err);

values.password = hash;

next();

});

},

// Override toJSON instance method

// to remove phoneNumber value

toJSON: function() {

var obj = this.toObject();

delete obj.password;

return obj;

}
ORM - WATERLINE

https://github.com/balderdashy/waterline
• ActiveRecord, Hibernate, and
Mongoose
• emphasis on modularity,
testability, and consistency
across adapters
• Waterline Adapter -> DB Query
• Custom Adapter methods
Coming

ASSOCIATIONS

V0.10

Associate models across different data stores
Defining Association
var Blog = {

attributes : {

title : 'string',

body : 'string',

author : {

model : 'person'

}

}

};

Soon

Relationship Types
• One way
• One-to-One
• One-to-Many
• Many-to-Many

Blog.find({title:'Sails.js Presentation'}).populate('author').exec(console.log);
Full Sail Ahead!
RESOURCEFUL ROUTING
CRUD Routes

get /:controller/:id?	

post /:controller	

put /:controller/:id	

delete /:controller/:id

FREE
To disable set the ‘shortcuts’ flag to
false in config/controllers.js,

REST Routes

/:controller/find/:id?	

/:controller/create	

/:controller/update/:id	

/:controller/destroy/:id

FREE
!

To disable set the ‘rest’ flag to false in
config/controllers.js
CONTROLLERS
api/controllers

var ChickenController = {

// Peck the chicken specified by id (subtract 50 HP)

peck: function (req,res) {

Chicken.find(req.param('id')).exec(function (err, chicken) {

if (err) return res.send(err,500);

if (!chicken) return res.send("No other chicken with that id exists!", 404);

if (chicken.hp <= 0) return res.send("The other chicken is already dead!", 403);

chicken.hp -= 50; // Subtract 50 HP from the chicken



chicken.save(function (err) { // Persist the change

if (err) return res.send(err,500);

// Report back with the new state of the chicken

res.json(chicken);

});

});

}

};

module.exports = ChickenController;
SOCKET.IO

http://socket.io/

// all controller routes return valid responses

socket.get('/todo/count', function(results) { console.log(results) });




// create a new item

socket.post('/todo', {"title" : "this is from sockets"}, function(err,results)
{ console.log(err,results) });




// all valid ways to update with websockets

socket.put('/todo', {'id' : 1, 'title' : "updated1"}, function(results) { console.log(results) });

socket.put('/todo/1', {'title' : "updated2"}, function(results) { console.log(results) });

socket.get('/todo/update/1', {'title' : "updated3"}, function(results) { console.log(results) });




// all valid ways to delete with websockets

socket.delete('/todo', {'id' : 1}, function(results) { console.log(results) });

socket.delete('/todo/1', function(results) { console.log(results) });

socket.get('/todo/delete/21',function(results) { console.log(results)});




// listen for messages

socket.on('message', function(message) { console.log(message) });




// listen for messages from a specific controller

socket.on('todo',function(message) { console.log(message) });

Coming

V0.10
Soon
POLICIES
api/policies/isAuthenticated.js
module.exports = function(req, res, next) {

// User is allowed, proceed to the next policy, 

// or if this is the last policy, the controller

if (req.session.authorized == true) {

return next();

}




// User is not allowed

// (default res.forbidden() behavior can be overridden in `config/403.js`)

return res.forbidden('You are not permitted to perform this action.');

};

config/policies.js
module.exports.policies = {

// Default policy for all controllers and actions

// (`true` allows public access) 

'*': true,




// Policies for /Foo/* routes

FooController: {

"*" : true,

'update' : 'isAuthenticated',

'destroy' : ['isAuthenticated','isOwner']

}

};
ASSET MANAGEMENT
assets/linker/

Enabling Asset Linking

sails new <appName>

--linker

Place Assets in between special linker flags
GET STARTED BUILDING
https://github.com/cgmartin/sailsjs-angularjs-bootstrap-example

https://www.npmjs.org/package/generator-sails

http://irlnathan.github.io/sailscasts/
THANKS!

@nickmcclay

More Related Content

KEY
Node.js 0.8 features
PDF
soft-shake.ch - Hands on Node.js
KEY
Writing robust Node.js applications
PDF
Introduction to Nodejs
PDF
What is nodejs
KEY
Node.js - Best practices
PDF
Building servers with Node.js
PPTX
introduction to node.js
Node.js 0.8 features
soft-shake.ch - Hands on Node.js
Writing robust Node.js applications
Introduction to Nodejs
What is nodejs
Node.js - Best practices
Building servers with Node.js
introduction to node.js

What's hot (20)

PDF
Node.js - A Quick Tour
KEY
Building a real life application in node js
KEY
A million connections and beyond - Node.js at scale
PPTX
Intro to Node.js (v1)
PPTX
The State of JavaScript (2015)
PDF
Ember background basics
PPT
Building your first Node app with Connect & Express
PDF
Comet with node.js and V8
KEY
Introduction to node.js
PDF
Node.js and How JavaScript is Changing Server Programming
KEY
node.js: Javascript's in your backend
PPT
Node js presentation
PDF
Express node js
PPTX
Node.js Patterns for Discerning Developers
PDF
Future Decoded - Node.js per sviluppatori .NET
PDF
Node Architecture and Getting Started with Express
PDF
Node.js API 서버 성능 개선기
KEY
NodeJS
PPTX
Java script at backend nodejs
PDF
node.js Module Development
Node.js - A Quick Tour
Building a real life application in node js
A million connections and beyond - Node.js at scale
Intro to Node.js (v1)
The State of JavaScript (2015)
Ember background basics
Building your first Node app with Connect & Express
Comet with node.js and V8
Introduction to node.js
Node.js and How JavaScript is Changing Server Programming
node.js: Javascript's in your backend
Node js presentation
Express node js
Node.js Patterns for Discerning Developers
Future Decoded - Node.js per sviluppatori .NET
Node Architecture and Getting Started with Express
Node.js API 서버 성능 개선기
NodeJS
Java script at backend nodejs
node.js Module Development
Ad

Viewers also liked (11)

PDF
Node.js 淺談res tful
PDF
Node.js 台灣,社群經驗分享 201312
PDF
Docker 導入:障礙與對策
PDF
MakerBoard: MT7688 Emulator
PDF
我編譯故我在:誰說 Node.js 程式不能編成 binary
PDF
Brig:Node.js + QML 華麗大冒險
PDF
初心者 Git 上手攻略
PDF
Node way
PDF
從線上售票看作業系統設計議題
PDF
給學生的:seo這條路
PDF
PWA and Chatbot - with e-Commerce experience sharing
Node.js 淺談res tful
Node.js 台灣,社群經驗分享 201312
Docker 導入:障礙與對策
MakerBoard: MT7688 Emulator
我編譯故我在:誰說 Node.js 程式不能編成 binary
Brig:Node.js + QML 華麗大冒險
初心者 Git 上手攻略
Node way
從線上售票看作業系統設計議題
給學生的:seo這條路
PWA and Chatbot - with e-Commerce experience sharing
Ad

Similar to Intro to Sail.js (20)

PPT
PPT
Developing Rest services with SailsJs by Andrey Kolodnitskiy
PDF
JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"
PDF
Node PDX: Intro to Sails.js
PDF
Intro to Sails.js
PPTX
Come Sail Away With Me (you guys): Node.js MVC Web API's Using Sails.js
KEY
Socket.io
PDF
Node.js Course 2 of 2 - Advanced techniques
PPT
Going crazy with Node.JS and CakePHP
PDF
Nodejs + Rails
PDF
Real-Time with Flowdock
PPTX
Building and Scaling Node.js Applications
PPT
Server side JavaScript: going all the way
PDF
Realtime MVC with Sails.js
PPTX
Like Ruby on Rails for Node - the Sails js framework
KEY
Practical Use of MongoDB for Node.js
PDF
Realtime web apps rails
PDF
Node.js to the rescue
PDF
Nodejs - A quick tour (v6)
PDF
Sails.js Intro
Developing Rest services with SailsJs by Andrey Kolodnitskiy
JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"
Node PDX: Intro to Sails.js
Intro to Sails.js
Come Sail Away With Me (you guys): Node.js MVC Web API's Using Sails.js
Socket.io
Node.js Course 2 of 2 - Advanced techniques
Going crazy with Node.JS and CakePHP
Nodejs + Rails
Real-Time with Flowdock
Building and Scaling Node.js Applications
Server side JavaScript: going all the way
Realtime MVC with Sails.js
Like Ruby on Rails for Node - the Sails js framework
Practical Use of MongoDB for Node.js
Realtime web apps rails
Node.js to the rescue
Nodejs - A quick tour (v6)
Sails.js Intro

More from Nicholas McClay (6)

PDF
Node.js and Parse
PPT
Get MEAN! Node.js and the MEAN stack
PPT
Node.js Cloud deployment
PPT
Coffee script throwdown
KEY
Node.js and NoSQL
PPTX
Node.js debugging
Node.js and Parse
Get MEAN! Node.js and the MEAN stack
Node.js Cloud deployment
Coffee script throwdown
Node.js and NoSQL
Node.js debugging

Recently uploaded (20)

PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
STKI Israel Market Study 2025 version august
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Five Habits of High-Impact Board Members
PDF
Getting Started with Data Integration: FME Form 101
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
DOCX
search engine optimization ppt fir known well about this
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Tartificialntelligence_presentation.pptx
observCloud-Native Containerability and monitoring.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Hindi spoken digit analysis for native and non-native speakers
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Getting started with AI Agents and Multi-Agent Systems
Final SEM Unit 1 for mit wpu at pune .pptx
STKI Israel Market Study 2025 version august
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Web Crawler for Trend Tracking Gen Z Insights.pptx
The various Industrial Revolutions .pptx
DP Operators-handbook-extract for the Mautical Institute
Group 1 Presentation -Planning and Decision Making .pptx
Five Habits of High-Impact Board Members
Getting Started with Data Integration: FME Form 101
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
search engine optimization ppt fir known well about this
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Tartificialntelligence_presentation.pptx

Intro to Sail.js