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

SxSW Hackathon Presentation

On Github dcbartlett / SxSW_Hackathon

Sails.js

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.

RESTful API

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 (http://yourproject.org/Model/<Operation>)

Blueprints

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

sails new <project_name>

sails lift

Waterline

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?

Views

Controllers

Policies

Examples

Example: Model

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

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

Example: View

layout.ejs

<!DOCTYPE html>
<html>
	<head>
		<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 %>
	</head>

	<body>
		<%- body %>

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

Example: View

index.ejs

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

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.id,chicken).done(function (err) {
        if (err) return res.send(err,500);

        // Report back with the new state of the chicken
        res.json(chicken);
      });
    });

  }
};
module.exports = ChickenController;

Example: Policies

config/policies.js

module.exports.policies = {

	UserController: {

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

Example: Policies

api/policies/authenticated.js

/**
* 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