At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

, , ,

Getting Started With Sails.js

  • By Nithin Kumar C N
  • August 10, 2018

Sails.js is a web framework that will help us to build REST API’s quickly and also it helps to build single page applications and web socket based real time apps like live chat and so on. Sails will work well with backbone and angular therefore the overall constructing process of Node.js app with sail would be more easier.
Sails.js is built on top of Express.js and Express.js is an important Node.js framework, which provides the “minimal interface” to create the applications. is a javascript library that enables real-time, bidirectional, event-based communication.

Some essential features Of Sails.js:

1. Auto-generated REST APIs
2. It has real-time support with WebSockets
3. It supports any database because of its powerful waterline ORM/ODM
4. Code generation is powerful because of Blueprints

It has some concerns also,

1. Waterline has no support for the join query
2. Not providing support for SQL Transactions until Sails v1.0

Getting Started with Sails.js

Hope i cleared you basic concepts and features of Sails.js. Next, we can start focusing on how to use Sails.js to create projects and run.


Before you start with Sails.js, we should ensure that the development environment is all set to work. For that, Our system must be installed with the latest version of npm and Node js.

Installing the Sails.js

After setting the working development environment, go to the terminal and run the following command.

sudo npm install sails -g

Once the installation is completed, you can use the sails command line to create new projects.
To create a new project, run the following command.

sails new sailsProject


The anatomy of a Sails app

Here is the Folder structure of the sails application.
Structure image needs to be added here
You can see the package.json file and node_modules folder there because Sails.js is a Node.js module. Also, Sails.js uses Grunt as a tool for building front-end assets.

Related: How To Deploy NodeJS App to Heroku

The Sails.js project is equipped with many configuration files and folders. Let us check some of them which we are using more often.
api/controllers: this folder holds the controllers. Controllers contains logic for interacting with models and based on that we can render the appropriate view files to the client.
api/models: Here in this directory you can see models. In Sails, model will contains the data for your app.
Api/policies: Here you can put policies of your application.
Api/responses: This will hold the server response logics such as functions to handle 404 and 500 responses.
./views: In this you can store the templates used for displaying views.
./config: This is the directory that contains all types of configuration details related to your application, which includes settings for models, policies, routes, sockets, etc.
./assets: The directory where you can put all the static files related to your application. This includes CSS, js, and images, etc.

How To Run Your Sails.js Project?

Get into the project root directory and run the following command to start your project in development server.

Sails lift

This will launch the dev server.

If you navigate to ‘localhost:1337’ in your browser, you can see the default Sails home page.

Why we need to use Waterline Models in Sails?

Next, we check how we can create models in the application. The model contains how the object in the data store appears. It will get different if we move with Relational databases or NoSQL databases. In case of Relational databases, it will have a table in database and will remain a JSON object in the model file. On the other case of NoSQL databases, it will get collected under the model.
You can create models using with Sails.js CLI.

sails generate model product

This will create model file Product.js in api/models directory. It will look like below.

* Product.js
* @description :: A model definition. Represents a database table/collection/etc.
* @docs ::
module.exports = {
attributes: {

You can fill content in your model file by adding attributes.

name: {
type: 'string',
defaultsTo: '',
required: 'true'
description: {
type: 'string',
defaultsTo: ''
quantity: {
type: 'integer'
price: {
type: 'integer'
user: { model: 'User' }

In the above example, you can see how we defined the association with the model User. Different types of associations are available in Sails.js.
It is also possible to define configurations per model adding certain properties in the model definition, which will override the default settings in the config/models.js.

Sails.js Controllers

The controller contains whole code that your application needs to drive the backend system and communicate with the view part.
The methods in Controller are called as Controller actions, which takes two parameters, request and response.

Also read: Introduction To E2E Testing In Angular CLI Using Protractor

Like models, you can create controllers using Sails.js CLI.

sails generate controller product

This will create a controller named api/controllers/ProductController.js with the following content.

* ProductController
* @description :: Server-side actions for handling incoming requests.
* @help :: See
module.exports = {

Though there are different node.js frameworks are available, Sails.js has it’s own distinctive features which makes us to mark it as best for Node.js. Therefore when the Node.js developers thrives to play with Sails.js then defienetly it would be a game changer in the world of node.js app.
Liked it? Similarly find more informative blogs on our largest blog repository, Stay updated with latest topics & tricks and don’t forget to subscribe us to get the latest updates from diverse technologies. Besides all, Post us your valuable thoughts in the comment section.
For any queries reach us via

Nithin Kumar C N

Passionate Senior Software Developer, Expert in PHP, Laravel, JQuery, Angular, With 6 years of experience in web development, he strongly committed himself in delivering authentic applications. Also, he continuously update himself with new technologies & features which drives him to come up with blogs on unique topic.