Built.io Blog

How to Get Started with Sails.js


As described on the Sails.js homepage:

Sails makes it easy to build custom, enterprise-grade Node.js apps. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture. It's especially good for building chat, realtime dashboards, or multiplayer games; but you can use it for any web application project - top to bottom.

Today we will learn how to do the following:

  1. Setup Sails.js and get it running
  2. Creating a database ready app

Lets begin!

NOTE: This tutorial assumes that you have previously installed Node.js on your machine.

Installing Sails.js

Sails.js is available as a NPM package and can be directly downloaded using the npm install command. You will have to install the package at the global level.

$ sudo npm install sails -g

Create a new Sails.js app

The new project can be created using a simple command:

$ sails new my-project
info: Created a new Sails app `my-project`!

Now that the project has been created, let's see the what's happening behind the scenes:

$ cd my-project
~/my-project $ ls -la
total 40
drwxr-xr-x 7 4096 Aug 27 16:53 api
-rw-r--r-- 1 1656 Aug 27 16:53 app.js
drwxr-xr-x 6 4096 Aug 27 16:53 assets
drwxr-xr-x 4 4096 Aug 27 16:53 config
-rw-r--r-- 1 2134 Aug 27 16:53 Gruntfile.js
drwxr-xr-x 2 4096 Aug 27 16:53 node_modules
-rw-r--r-- 1  924 Aug 27 16:53 package.json
-rw-r--r-- 1   56 Aug 27 16:53 README.md
drwxr-xr-x 4 4096 Aug 27 16:53 tasks
drwxr-xr-x 2 4096 Aug 27 16:53 views

Anatomy of a Sails.js App can help you understand what each of these files are for.

Create a User Resource for your app

Creating a user resource for your app can be done using this command:

~/my-project $ sails generate api User
info: Created a new model ("User") at api/models/User.js!
info: Created a new controller ("User") at api/controllers/UserController.js!
info: REST API generated @ http://localhost:1337/User
info: and will be available the next time you run `sails lift`

The above command created two files, one inside the api/models directory and other inside the api/controllers directory. If you navigate to those files you see that they contain an empty module.exports call. This happened because we did not specify any attributes for our model or actions for our controller. If you do know the attributes, you could create a user resource as follows:

~/my-project $ sails generate model User name:string email:string password:string
~/my-project $ sails generate controller User index create show edit delete

Lets get the app running

The Sails.js app can be started using this command:

~/my-project $ sails lift

When running this command, you may think you got an error. Its not actually an error, it's a prompt message. This message occurred because while creating models we did not specify whether we want Sails.js to automatically rebuild the tables, collections, sets, etc. in our schema. You can answer the prompt message with 1 and the server will start up. Now your application is ready to use.

But, wait a second, we don't have a database yet! Which leads us to...

Database Connectivity

Open the file config/connections.js in your favorite editor. You might see some connection attributes already listed in that file. Just select the one that you want, MySQL, MongoDB or PostgreSQL and specify the connection parameters.

We will be using MongoDB in this tutorial.

sailsMongoDBServer: {
  adapter: 'sails-mongo',
  host: 'localhost',
  port: 27017,
  database: 'mySailsDB'
  // user: 'username',
  // password: 'password'

After you have specified your connection parameters, open the file config/models.js. You may see the following commented out:

// connection: 'localDiskDb',

Uncomment this line, and edit the configuration key you specified in config/connections.js, In our case it's connection: 'sailsMongoDBServer'.

Don't forget to install the corresponding NPM for whichever adapter you configure. Sails.js will need it to create a connection. Lets install the sails-mongo NPM:

~/my-project $ npm install sails-mongo

Now, lets run our app:

~/my-project $ sails lift

The app (hopefully) ran successfully! But, we don't know if the database was established properly...

Create an entry in the User Model

If we take a look at our UserController, we can clearly see that the actions just respond with a TODO message. We should change that. Waterline is bundled by default along with Sails.js. Lets add a create action to our controller:

create: function(req, res){
  var params = req.params.all()
  User.create({name: params.name}).exec(function createCB(err,created){
    return res.json({
      notice: 'Created user with name ' + created.name

We will also have to add a route for this. Open the file config/routes.js in an editor. The routes are added in this file in a key-value pattern. Add this line to the orginal key-value set:

'post /User': 'UserController.create'

Now, lets restart the server and send a POST call to the URL, http://localhost:1337/User, and pass the parameter, name.

curl -X POST --data 'name="raw engineering"' http://localhost:1337/User

After doing this, you can go the database console and confirm that a user with the name raw engineering exists.


Here's what we just did:

  1. Created a Sails.js application
  2. Created resources with custom fields
  3. Modified database connection parameters
  4. Added a custom route
  5. Created an entry in the database

That's it for part 1 of this tutorial. In the Part 2, we will learn about how use templates in a Sails app. Check the Built.io blog for more tutorials!

Happy Sailing!!!

Subscribe to our blog