Sails.js First Glance

Posted by n3integration on May 15, 2015

Over the years, my experience with JavaScript has been fairly broad: test automation, data transformations, and web development. I’ve more recently found myself exploring JavaScript for server-side development running on Node.js. While being faced with a tight deadline for developing a web application on limited personal time, a quick search for web frameworks produced Sails.js. Sails is a Model View Controller, or MVC framework built on Express that promises to be “The web framework of your dreams”. With such a bold statement, I decided to give it a further look.The latest available version at the time of this post is 0.11.0.

My requirements for a web framework were fairly straightforward:

  • Minimal learning curve
  • Must be actively maintained
  • Project skeleton must be provided: build script, directory structure, etc
  • Simple, yet flexible model persistence

Coming from the Node.js ecosystem, the installation of Sails.js is performed through the Node Package Manager (NPM):

$ npm -g install sails

Once installed, I created a new project:

$ sails new Registry
info: Created a new Sails app `Registry`!

This command setup the basic directory structure, project meta data, configuration files, build file, and server. Without any additional changes, let’s fire it up!

$ sails lift
info: Starting app...
debug: --------------------------------------------------------
debug: :: Thu May 14 2015 21:44:07 GMT-0400 (EDT)
debug: Environment : development
debug: Port : 1337
debug: --------------------------------------------------------

After opening my browser and pointing it to http://localhost:1337, a default homepage is displayed. A running web application with very little effort and no coding on my part.


As a part of the application, I am going to have to keep track of user information. Let’s get started by setting up a Model and Controller for our users. The following command creates the required files in the appropriate directories: api/models/Contact.js and api/controllers/ContactController.js.

$ sails generate api Contact
info: Created a new api!

Sails uses Waterline for Object-Relational Mapping (ORM). This makes it trivial to wire the data model to a relational database (MySQL, PostgreSQL) or document store (MongoDB) for persistence. Sails.js also provides the ability to mix-and-match data stores per model, if needed. The default behavior does not require a datastore. Instead, all data is read from and written to local disk, which is great for rapid application prototyping!

For the user model, I needed to add a few fields and their data type to the Contact.js file:

module.exports = {
 attributes: {
  id: {
    type: 'integer',
    primaryKey: true,
    autoIncrement: true
  firstName: {
    type: 'string'
  lastName: {
    type: 'string',
    index: true
  phoneNumber: {
    type: 'string',
    index: true
  email: {
    type: 'string',
    index: true

In order to test out the new controller, I first needed to restart the application and then issued a curl request, which returned an empty JSON array. Let’s go ahead and add a new contact, again using curl:

$ sails lift
info: Starting app...
$ curl http://localhost:1337/contact
$ curl http://localhost:1337/contact -XPOST \
  -H'Content-Type: application/json' \
  -d '{"firstName":"James","lastName":"Ryan"}'
 "id": 1,
 "firstName": "James",
 "lastName": "Ryan",
 "phoneNumber": null,
 "email": null,
 "address": null,
 "createdAt": "2015-05-15T02:50:15.000Z",
 "updatedAt": "2015-05-15T02:50:15.000Z"

With minimal effort on my part, I have a functional web application that is capable of creating, updating, and deleting application users. In addition to basic REST, WebSocket support is also provided.

I can focus on the business logic of the application rather than spend time dealing with the intricacies of building out the database and the components of the web server. This is priceless to any developer. Sails.js enables me to be more productive and will be an excellent addition to my toolbox.