On Github dcbartlett / SxSW_Hackathon
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.
Sails.JS is built on Node.JS.
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 (http://yourproject.org/Model/<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.
Sails.js allows for the use of sockets and websockets by default.
Realtime IO apps for the Realtime World.
Models - Who wants to model for us?
Views
Controllers
Policies
module.exports = { adapter: 'sails-mysql', attributes: { // Simple attribute: name: 'STRING', email: 'STRING', phoneNumber: { type: 'STRING', defaultValue: '555-555-5555' } } };
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>
index.ejs
<section id="about"> <span> <%- partial('sections/about')%> </span> </section> <section id="blog"> <span> <%- partial('sections/blog')%> </span> </section>
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;
config/policies.js
module.exports.policies = { UserController: { // All actions in the user controller need authenticaton. '*': 'authenticated', index: ['authenticated', 'level'] } };
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); } };