This is a Node.js framework used for building enterprise-ready and custom MVC applications. It has features such as socket integration for every route, API creator, and database ORM which improves its usability and makes the speed of development to be high. The following are some of the features provided by this framework:

  • Built-in web sockets in routes
  • Database ORM
  • Auto generated API
  • Security code
  • Inbuilt task runner

With Sails.js, one can create skeleton APIs automatically unlike what happens in MEAN stack whereby we have to repeat the same thing as we manually create the APIs. With a single command, you will be given the necessary block of code that will enable you to get started. Also, with Sails.JS, you will be provided with all of the appropriate database drivers which will enable you work with databases such as MongoDB and other NoSQL databases. Drivers for SQL databases will also be provided. The Grunt task runner is used as the default runner in Sails.JS, and this provides the developer with the appropriate way  to manage grunt tasks.

In all cases, the security parameters such as CSRF and CORS are provided in a Sails project. You only have to enable them from your respective files and the latest standards will be applied to your app so as to make it secure. In Sail.JS, Express is used for the purpose of handling HTTP operations while is used for the purpose of handling web socket messages. Web socket messages for the routes are provided without having to perform a configuration on the front-end or back-end layer.

Installation and Configuration of Sails.JS

For one to install Sails into their system, they have to ensure that they have the latest version of npm and Node installed, since most of the features it needs are not available in the old versions of these. Just execute the following command on your terminal:

[code lang=”text”]
npm install -g sails


After execution of the above command, it will take some time for the installation to be done, but this will all depend on your internet connection. After the installation has been completed, one can make use of the Sails command line for the purpose of creating new projects.

If you are in need of creating a new project, just execute the command given below:

[code lang=”text”]
sails create projectName


With the above command, a new directory will be created where the command has been executed from, and this is where you will be required to place all of your files. After the above command, it will be time for you to install the necessary modules. Just navigate to the project directory, and then execute the following command:

[code lang=”text”]
npm install


With the above command, all the new packages which are needed for you to execute the project will be installed.

Executing the Project

For you to execute the project, you have to open up your terminal and then navigate to the directory for the project. From there, execute the command given below:

[code lang=”text”]
sails lift


To view how the app looks like, just open the browser and view “localhost:1337.”

The folder view for the application should be as follows:

├── api
│  ├── controllers
│  ├── models
│  ├── policies
│  ├── responses
│   └── services
├── assets
│  ├── images
│  ├── js
│   │   └── dependencies
│  ├── styles
│   └── templates
├── config
│  ├── env
│   └── locales
├── node_modules
│  ├── ejs
│  ├── grunt
│  ├── grunt-contrib-clean
│  ├── rc
│  ├── sails
│   └── sails-disk
├── tasks
│  ├── config
│   └── register
└── views

The folders for “model” and “controllers” are the most important ones. The piece of code that the app needs to drive the backend system and then communicate with the User Interface or the View is stored in the Controller.

The model has the data on what the object in the data store will look like. For those using a NoSQL database such as MongoDB, this will just be a collection under a model, while for those using a relational database such as SQL, this will be a table under in a database.

Configuration of the Database

Suppose that you are need of configuration of a MySQL database in your Sails project. A number of steps have to be followed for this to be successful.

The first step should involve installation of the driver and configuration of the connection. In the case of MySQL, you should first install sails-mysql and then tell the Sails to make use of it. The following are the steps:

Use the following command to install the module:

[code lang=”text”]
npm install –save sails-mysql


After installation of everything, navigate to the directory “config/connections.js” and then configure the settings for MySQL, by setting the proper username and the password. This is shown in the code given below:

[code lang=”text”]
adapter: ‘sails-mysql’,
password: ‘MYSQL_PASSWORD’,
database: ‘MYSQL_DATABASE’


It is time for us to tell the Sails to make use of this, and this can be done by opening the file “config/models.js” and then change the driver to the following:

[code lang=”text”]
connection: ‘aMysqlServer’,   migrate : ‘alter’


The above connection will be used by Sail for performing a database connection.

How to Create an Auto-generated API

Sails.js provides you with a new feature for generation of new APIs on the go. To create one, just launch the terminal and then execute the following command:

[code lang=”text”]
sails generate api myAPI


With the above command, routes and models files will be created in the controller directory. In the “api/controllers” folder, a file named “MyAPIController.js” will be created, and this will have all the routes.

In our Model folder, a new file named “myAPI.js” will be created. The following code should then  be added to the file:

[code lang=”text”]
* MyAPI.js
* @description :: TODO: A short summary about the functionality of the model can be added here.
* @docs        ::!documentation/models
module.exports = {
attributes: {     userName : {         type : ‘string’         },     password : {         type : ‘string’     }   } };


Also, you should ensure that you have already created a database, and that it has been specified in the connection file. If you need to run the code, the following code can be used for lifting the app:

[code lang=”text”]
sails lift


At this point, just launch the API simulator and then test the API.

Configuration of Task Runner

In Sails, a grunt which has been configured to run the task has already been configured, and for you to configure it, navigate to “/tasks/register” and then add your tasks there as shown below:

[code lang=”text”]
module.exports = function (grunt) {     grunt.registerTask(‘default’, [‘compAssets’, ‘linkAssets’,  ‘watch’]);     };


For a new task to be added, a task file has to be created in the directory “tasks/config” and then the module exported. This is shown in the code given below:

module.exports = function(grunt) { // The grunt task };

It should then be added to the register by creation of a new file, and then these should be added to the grunt task. It can also be hooked up to the default grunt file. You can choose the method that you want.


Write your comment