SlideShare a Scribd company logo
SAILS.JS
AN INTRODUCTION
| |Nic Jansma nicj.net @NicJ
SAILS.JS IS:
A MVC backend web framework for Node.js
Built on top of Express
Inspired by Ruby on Rails / Symfony / Zend
Convention over configuration
Ideal for chat, realtime dashboards and multiplayer games
SAILS.JS CORE FEATURES
100% JavaScript
Database agnostic (includes custom ORM)
Auto-generated REST APIs
Easy WebSocket support and integration with REST
Reusable security policies
Front-end agnostic
Flexible asset pipeline (builds)
GETTING STARTED
npminstall-gsails
sailsnew[projectpath]
cd[projectpath]
RUNNING YOUR NEW
WEB APP
sailslift
info:Startingapp...
info:
info:
info: Sails <|
info: v0.10.2 |
info: /|.
info: /||
info: ,' |' 
info: .-'.-==|/_--'
info: `--'-------'
info: __---___--___---___--___---___--___
info: ____---___--___---___--___---___--___-__
info:
info:Serverliftedin`example1`
info:Toseeyourapp,visithttp://localhost:1337
info:ToshutdownSails,press<CTRL>+Catanytime.
debug:--------------------------------------------------------
debug:::ThuAug07201406:43:55GMT-0400(EasternDaylightTime)
debug:Environment:development
debug:Port :1337
debug:--------------------------------------------------------
HTTP://LOCALHOST:1337
GENERATE MODEL AND
REST API
Let's create a new beermodel and REST API
sailsgenerateapibeer
This creates skeleton files:
apicontrollersBeerController.jsand
apimodelBeer.js
TRY IT OUT
http://localhost:1337/beer
[]
http://localhost:1337/beer/create?name=Centennial
IPA&brewery=Founders&have=10
{
"name":"AllDayIPA",
"brewery":"Founders",
"createdAt":"2014-08-07T13:11:10.536Z",
"updatedAt":"2014-08-07T13:38:21.517Z",
"id":1,
"have":10
},
TRY IT OUT
http://localhost:1337/beer
[
{
"name":"AllDayIPA",
"brewery":"Founders",
"createdAt":"2014-08-07T13:11:10.536Z",
"updatedAt":"2014-08-07T13:38:21.517Z",
"id":1,
"have":10
}
]
WHAT JUST HAPPENED
Sails created a Model (Beer.js) and Controller
(BeerController.js)
Sails blueprints automatically configure new routes for the
model (eg POST /beerand /beer/create).
Sails uses whatever storage layer you want for persistence
ANATOMY OF A SAILS.JS
APP
api/controller/- controllers
api/models/- models
api/policies/- authentication / authorization
api/responses/- res.xyz()handlers
api/services/- services
assets/- static assets
config/- app config
tasks/- grunt / cli tasks
views/- views
MODELS
api/models/*
Uses (best parts of Active Record,
Hibernate and Mongoose)
http://sailsjs.org/#!documentation/models
Waterline ORM
module.exports={
attributes:{
name:{
type:'string',
required:true
},
brewery:{
type:'string',
required:true
},
have:{
type:'integer',
defaultTo:1
}
}
};
CONTROLLERS
api/controllers/*
With Blueprints (config/blueprints.js), you
automatically get CRUD REST and "shortcut" routes
http://links.sailsjs.org/docs/controllers
BLUEPRINT ROUTES
By default, Sails creates three types of blueprint routes:
RESTful routes for /:model(HTTP GET, DELETE, etc)
Shortcut routes to easily test your model via HTTP GET
requests, such as /:model/delete/:id(should be
turned off in production)
Action routes for any additional actions in your
controllers
Authentication and access control are handled via policies
BLUEPRINT ROUTES
REST Shortcut
Query GET /api/:model
Fetch GET /api/:model/:id
Create POST /api/:model GET /api/:model/create
Update PUT /api/:model/:id GET /api/:model/update/:id
Delete DELETE /api/:model/:id GET /api/:model/destroy/:id
BLUEPRINT ACTIONS
By default, Sails creates actions on your controllers for ORM
functionality:
find
findOne
create
update
destroy
populate
add
remove
CUSTOM ACTIONS AND
ROUTES
//config/routes.js
module.exports.routes={
'/':{
view:'homepage'
},
'post/beer/:id/drink':'BeerController.drink'
}
//BeerController.js
module.exports={
drink:function(req,res){
if(!req.params.id){returnres.badRequest('IDnotsupplied');}
Beer.findOne({id:req.params.id},function(err,model){
if(err||!model){
returnres.badRequest('Beernotfound');
}
model.have--;
model.save(function(err){
returnres.ok(model);
});
});
}
};
VIEWS
api/views/*
Uses EJS by default but can switch to Jade, etc
http://sailsjs.org/#!documentation/views
//BeerController.js
module.exports={
show:function(req,res){
Beer.find({},function(err,beers){
res.view('show-beers',{title:'Beers',beers:beers});
});
}
};
//show-beers.ejs
<ul>
<li> </li>
</ul>
<%for(vari=0;i<beers.length;i++){%>
<%=beers[i].name%>
<%}%>
SOCKET.IO INTEGRATION
Sails automatically translates incoming socket.io messages
into Express requests
Also gives you Pub/Sub via res.broadcast()and
req.join()
varsocket=io.connect('http://localhost:1337');
socket.request('/beer',{},function(beers){console.log(neers);});
LINKS
Sails.js:
SailsCasts:
Presentation:
Code:
sailsjs.org
irlnathan.github.io/sailscasts/
slideshare.net/nicjansma
github.com/nicjansma/talks/
Thanks - Nic Jansma - -nicj.net @NicJ

More Related Content

PDF
NodeWay in my project & sails.js
PDF
Sails Framework Instroduction
PDF
Sails.js Model / ORM introduce
PPT
PDF
Intro to Sails.js
PDF
The Evolution of Airbnb's Frontend
PPTX
Spring Boot Update
PPT
Build Your Own CMS with Apache Sling
NodeWay in my project & sails.js
Sails Framework Instroduction
Sails.js Model / ORM introduce
Intro to Sails.js
The Evolution of Airbnb's Frontend
Spring Boot Update
Build Your Own CMS with Apache Sling

What's hot (20)

PDF
Microservices with Spring Boot
PDF
Node PDX: Intro to Sails.js
PDF
RESTful Web Applications with Apache Sling
PDF
Nuxt.js - Introduction
PPTX
PPTX
Content-centric architectures - case study : Apache Sling
PDF
RESTful web apps with Apache Sling - 2013 version
PPTX
Javascript Bundling and modularization
PDF
REST to JavaScript for Better Client-side Development
PPTX
DEV208 - ASP.NET MVC 5 新功能探索
PPTX
Build RESTful API Using Express JS
PDF
Angular2 ecosystem
PPTX
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
PDF
Building Isomorphic Apps (JSConf.Asia 2014)
PDF
(2018) Webpack Encore - Asset Management for the rest of us
PDF
Avoiding Common Pitfalls in Ember.js
PDF
Developing, Testing and Scaling with Apache Camel - UberConf 2015
PDF
Grunt.js and Yeoman, Continous Integration
PPTX
Introdcution to Adobe CQ
PDF
Introducing CQ 5.1
Microservices with Spring Boot
Node PDX: Intro to Sails.js
RESTful Web Applications with Apache Sling
Nuxt.js - Introduction
Content-centric architectures - case study : Apache Sling
RESTful web apps with Apache Sling - 2013 version
Javascript Bundling and modularization
REST to JavaScript for Better Client-side Development
DEV208 - ASP.NET MVC 5 新功能探索
Build RESTful API Using Express JS
Angular2 ecosystem
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
Building Isomorphic Apps (JSConf.Asia 2014)
(2018) Webpack Encore - Asset Management for the rest of us
Avoiding Common Pitfalls in Ember.js
Developing, Testing and Scaling with Apache Camel - UberConf 2015
Grunt.js and Yeoman, Continous Integration
Introdcution to Adobe CQ
Introducing CQ 5.1
Ad

Viewers also liked (20)

PPT
NijmegenJS - SailsJS intro march 10 2016
PPTX
Angular JS 2.0 & React with Kendo UI
PDF
SxSW 2015
PPT
Developing Rest services with SailsJs by Andrey Kolodnitskiy
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
PDF
Using Phing for Fun and Profit
PPTX
Using Modern Browser APIs to Improve the Performance of Your Web Applications
PDF
The Happy Path: Migration Strategies for Node.js
PDF
Measuring Continuity
PDF
How to Build a Great Web Application
PPTX
Appcelerator Titanium Intro
PDF
Appcelerator Titanium Intro (2014)
PDF
Forensic Tools for In-Depth Performance Investigations
PDF
Richard rodger technical debt - web summit 2013
PPTX
Html5 devconf nodejs_devops_shubhra
PDF
Measuring the Performance of Single Page Applications
PDF
Reveal.js
PDF
Javascript Module Patterns
PDF
From Push Technology to Real-Time Messaging and WebSockets
PDF
Measuring Real User Performance in the Browser
NijmegenJS - SailsJS intro march 10 2016
Angular JS 2.0 & React with Kendo UI
SxSW 2015
Developing Rest services with SailsJs by Andrey Kolodnitskiy
TypeScript - Silver Bullet for the Full-stack Developers
Using Phing for Fun and Profit
Using Modern Browser APIs to Improve the Performance of Your Web Applications
The Happy Path: Migration Strategies for Node.js
Measuring Continuity
How to Build a Great Web Application
Appcelerator Titanium Intro
Appcelerator Titanium Intro (2014)
Forensic Tools for In-Depth Performance Investigations
Richard rodger technical debt - web summit 2013
Html5 devconf nodejs_devops_shubhra
Measuring the Performance of Single Page Applications
Reveal.js
Javascript Module Patterns
From Push Technology to Real-Time Messaging and WebSockets
Measuring Real User Performance in the Browser
Ad

Similar to Sails.js Intro (20)

PDF
JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"
PPTX
Like Ruby on Rails for Node - the Sails js framework
PDF
Introduction to sails.js
PPTX
Come Sail Away With Me (you guys): Node.js MVC Web API's Using Sails.js
PDF
MVC way to introduce Sails.js - node.js framework
PDF
Rags to Open Source - AllThingsOpen, Raleigh, NC 2014
PDF
Intro to Sail.js
PDF
Sails.js: Rags to...Open Source
PDF
Sails.js - Overview
PDF
Creating applications with Grails, Angular JS and Spring Security
ODP
Dynamic Languages Web Frameworks Indicthreads 2009
PPT
JavaOne 2008 - TS-5764 - Grails in Depth
PPT
Fast web development using groovy on grails
PDF
About REST & Symfony
PPT
Grails Introduction - IJTC 2007
PPTX
Building a scalable API with Grails
PDF
Coders Workshop: API First Mobile Development Featuring Angular and Node
ODP
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
PPTX
Single-page applications and Grails
PDF
Client Side MVC with Backbone and Rails
JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"
Like Ruby on Rails for Node - the Sails js framework
Introduction to sails.js
Come Sail Away With Me (you guys): Node.js MVC Web API's Using Sails.js
MVC way to introduce Sails.js - node.js framework
Rags to Open Source - AllThingsOpen, Raleigh, NC 2014
Intro to Sail.js
Sails.js: Rags to...Open Source
Sails.js - Overview
Creating applications with Grails, Angular JS and Spring Security
Dynamic Languages Web Frameworks Indicthreads 2009
JavaOne 2008 - TS-5764 - Grails in Depth
Fast web development using groovy on grails
About REST & Symfony
Grails Introduction - IJTC 2007
Building a scalable API with Grails
Coders Workshop: API First Mobile Development Featuring Angular and Node
Grails 0.3-SNAPSHOT Presentation WJAX 2006 English
Single-page applications and Grails
Client Side MVC with Backbone and Rails

More from Nicholas Jansma (6)

PPTX
Modern Metrics (2022)
PPTX
Check Yourself Before You Wreck Yourself: Auditing and Improving the Performa...
PDF
When Third Parties Stop Being Polite... and Start Getting Real
PDF
Reliably Measuring Responsiveness
PDF
Make It Fast - Using Modern Browser Performance APIs to Monitor and Improve t...
PPTX
Debugging IE Performance Issues with xperf, ETW and NavigationTiming
Modern Metrics (2022)
Check Yourself Before You Wreck Yourself: Auditing and Improving the Performa...
When Third Parties Stop Being Polite... and Start Getting Real
Reliably Measuring Responsiveness
Make It Fast - Using Modern Browser Performance APIs to Monitor and Improve t...
Debugging IE Performance Issues with xperf, ETW and NavigationTiming

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Approach and Philosophy of On baking technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
20250228 LYD VKU AI Blended-Learning.pptx
sap open course for s4hana steps from ECC to s4
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Approach and Philosophy of On baking technology
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Dropbox Q2 2025 Financial Results & Investor Presentation

Sails.js Intro