Sails.js makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.
To install the latest stable release with the command-line tool:
sudo npm -g install sails
Create a new app
# Create the app
sails new testProject
Lift Sails
# cd into the new folder
cd testProject
# Fire up the server
sails lift
The default port for Sails is 1337, so at this point, if you visit http://localhost:1337/, you'll see the default home page.
Now, let's get Sails to do cool stuff.
Sails.js API blueprints are nothing like scaffolds. Generated forms just don't make sense for modern web apps (no one uses them!) Instead, Sails allows you to generate a powerful RESTful JSON API using the command line tool. This is exactly what you need for AJAX web pages, realtime apps, SPAs, Backbone apps, Angular apps, Cordova/PhoneGap apps, native mobile apps, refrigerators, lamps, etc.
Without writing any code, Sails supports:
- filtering (
where
) - search (
or
,and
,in
,startsWith
,endsWith
,contains
,greaterThan
,lessThan
,not
) - sorting (
sort
) - pagination (
limit
,skip
,sort
) - JSONP
- CORS
- csrf protection
Best of all, all of these things work with both HTTP and WebSockets, and work across any of the supported database adapters, including PostgreSQL, MongoDB, and MySQL. Authentication and access control are implemented using policies. More on all that stuff here:
We'll need an empty model and controller:
sails generate user
If you check out your app, you'll notice that this created a file at /api/models/User.js and /api/controllers/UserController.js.
Now, if you send a POST request to http://localhost:1337/user
or visit http://localhost:1337/user/create
, you'll see:
{
"createdAt": "2013-01-10T01:33:19.105Z",
"updatedAt": "2013-01-10T01:33:19.105Z",
"id": 1
}
That's it! You just created a model in the database! You can also find
, update
, and destroy
users:
# List of all users
http://localhost:1337/user
# Find the user with id 1
http://localhost:1337/user/1
# Create a new user
http://localhost:1337/user/create?name=Fisslewick
(or send an HTTP POST to http://localhost:1337/user)
# Update the name of the user with id 1
http://localhost:1337/user/update/1?name=Gordo
(or send an HTTP PUT to http://localhost:1337/user/1)
# Destroy the user with id 1
http://localhost:1337/user/destroy/1
(or send an HTTP DELETE to http://localhost:1337/user/1)
This built-in API bundles optional support for JSONP-- and in general, Sails has built-in support for CORS, and CSRF protection. See your project's
config/cors.js
,config/csrf.js
, andconfig/controllers.js
files for more options.
These automatically generated URL routes are called "blueprints". Blueprints may be disabled, pluralized, or prefixed globally or on a per-controller basis.
But what if you need more customized logic?
Say, your UserController.create
needs to also send a confirmation email-- no problem.
Just write a custom create
method in your UserController
and it will be available using the same blueprint routes (e.g. POST /user
)
Custom controllers are just Express middleware, the de facto standard for writing server code in Node.js.
e.g.
// api/controllers/UserController
module.exports = {
/**
* @param {String} email
* @param {String} name
*/
create: function (req, res) {
User.create({
name: req.param('name'),
email: req.param('email')
})
.exec(function userCreated(err, newUser) {
// Bail out if there's an error!
// (this will use the app-global logic in config/500.js)
if (err) return res.serverError(err);
// Send some email
require('my-favorite-email-module').send({
html: 'Well that\'s neat.',
to: newUser.email
});
sails.log('New user created successfully!');
sails.log.verbose('Confirmation email sent to', newUser.email);
// Send JSON response
return res.json(newUser);
})
}
};
Worth noting is that the custom controller above still supports WebSockets out of the box, since Sails will actually simulate
req
andres
objects when it receives properly-formatted messages from Socket.io. Check outassets/js/app.js
in your project for an example of how to use Socket.io to talk to your Sails backend.
You can also define custom routes, controllers, and controller methods (aka "actions").
sails generate controller hello index
This will generate a file called HelloController.js
in your app's api/controllers
directory with one action, index()
.
Now let's edit that action to send back the string 'Hello World!'
.
// api/controllers/HelloController.js
module.exports = {
index: function(req, res) {
// Here, you can do all the Express/Connect things!
res.send('Hello World!');
}
};
Let's say we want the application to display this hello response specifically when a request comes in for http://localhost:1337/hi
.
Go into the /config/routes.js file and add a route like this:
// config/routes.js
module.exports = {
'/hi': 'HelloController.index'
};
Finally, restart the server by going to your node terminal and pressing control c. Then enter the following.
sails lift
Now when you visit http://localhost:1337/hi, or send a Sails-formatted Socket.io message to /hi
:
// Try this from the Chrome/Firebug javascript console on your app's home page:
socket.get('/hi', function (response) { console.log(response); });
You'll see:
Hello World!
https://github.com/balderdashy/sails/wiki
Short screencasts that take you through the basics of building traditional websites, single-page/mobile apps, and APIs using Sails. Perfect for both novice and tenured developers, but does assume some background on MVC: SailsCasts
If you have questions, ideas, or run into a problem, post it to our google group-- someone there might be able to help you. Sails.js Google Group
We're #sailsjs on freenode
Sails is tested with node versions 0.8.22 and 0.10.x, and built on the rock-solid foundations of Express and Socket.io.
The Sails framework was developed by Mike McNeil (@mikermcneil) and is maintained by Balderdash (@balderdashy), a realtime web & mobile studio I started with Heather White (@hdesignsit) in Austin, TX a few years ago. We design/build scalable Node.js apps for startups and enterprise customers.
After building a few realtime JavaScript apps and taking them into production, we realized that the Node.js development landscape was very much still the Wild West. Over time, after trying lots of different methodologies, we decided to crystallize all of our best practices into this framework. I hope it saves you some time :)
Sails is built around so many great open-source technologies that it would never have crossed our minds to keep it proprietary. We owe huge gratitude and props to TJ Holowaychuk (@visionmedia) and Guillermo Rauch (@guille) for the work they did, as well as the stewards of all the other open-source modules we use. Sails could never have been developed without your tremendous contributions to the node community.
Copyright © 2012-2013 Mike McNeil
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.