Sails.js – The MVC Framework for Node.js – Node.JS Base

Sails.js – The MVC Framework for Node.js – Node.JS Base

0 0


SxSW Hackathon Presentation

On Github dcbartlett / SxSW_Hackathon


The MVC Framework for Node.js

AHOY! Mateys!

Sail.js is a MVC framework built to run on node.js. You can have your project up and production ready in weeks instead of months.

Node.JS Base

Sails.JS is built on Node.JS.

Why do we use Node.JS?

  • It's asynchronous, which means better performance
  • Javascript is the one language to rule them all, whether we like it or not
  • It makes realtime apps way easier
  • It's IO/networking/disk libraries are modern.
  • Javascript uses closures, a powerful programming tool.

Sails.JS, Why you should use it?

We think you should use Sails.JS to build...well, eveything.

Sails.JS has a RESTful API built in.

Sails.JS creates a default codebase for your project.

Sails.JS allows for different databases.

Sails.JS has support for socket/websockets.


Sails.JS has a scaffolding system that generates a simple CRUD system for each model you create.

API is reachable both internally and via calls to a URI (<Operation>)


Blueprints are templates that are created for your project when you generate it.

sails new <project_name>

sails lift


Waterline is a brand new kind of storage and retrieval engine.

A uniform API for accessing different kinds of databases, protocols, and 3rd party APIs.

"Socket" to them!

Sails.js allows for the use of sockets and websockets by default.

Realtime IO apps for the Realtime World.

Diving Deeper

Models - Who wants to model for us?





Example: Model

module.exports = {
 adapter: 'sails-mysql',
 attributes: {

    // Simple attribute:
    name: 'STRING',
    email: 'STRING',
    phoneNumber: {
      type: 'STRING',
      defaultValue: '555-555-5555'

Example: View


<!DOCTYPE html>
		<title><%- title %></title>

		<!-- Viewport mobile tag for sensible mobile support -->
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<!-- JavaScript and stylesheets from your public folder are included here -->
		<%- assets.css %>
		<%- assets.js %>

		<%- body %>

		<!-- Templates from your view path are included here -->
		<%- assets.templateLibrary %>

Example: View


<section id="about">
		<%- partial('sections/about')%>
<section id="blog">
		<%- partial('sections/blog')%>

Example: Controller

var ChickenController = {

  // Peck the chicken specified by id (subtract 50 HP)
  peck: function (req,res) {
    Chicken.find(req.param('id')).done(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);

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

      // Persist the change
      Chicken.update(,chicken).done(function (err) {
        if (err) return res.send(err,500);

        // Report back with the new state of the chicken

module.exports = ChickenController;

Example: Policies


module.exports.policies = {

	UserController: {

		// All actions in the user controller need authenticaton.
		'*': 'authenticated',
		index: ['authenticated', 'level']

Example: Policies


* Allow any authenticated user.
module.exports = function (req,res,ok) {
	// User is allowed, proceed to controller
	if (req.session.authenticated) {
		return ok();

	// User is not allowed
	else {
		return res.send("You are not permitted to perform this action.",403);

Q & A