Sails.js Tutorial for Beginners

Introduction to Sails.js

Sails.js is an MVC web framework for node.js which works similar to the other MVC frameworks like Ruby on Rails. It is used to build customized enterprise-grade Node.js applications. Sails.js uses Express to handle the HTTP operations and provides WebSocket support via socket.io to handle web socket messages.

This framework has improved support for the more data-oriented modern style of creating web apps. It includes web socket messages to your routes without configuring it in the back-end or front-end layer. It also works well with Backbone and Angular.

Sails.js provides a set of convections and default configurations such that the users can quickly get a new website project started. This framework is highly configurable and allows its users to override the default convections easily.

Unlike the MEAN stack, where the user needs to generate API’s manually and have to do the same repetitive thing again and again. But sails.js is an advanced framework that allows you to create skeleton APIs automatically only by writing a single command. You will notice that the block of code has generated to get the API started.

Sails.js ORM

Sails.js comes with an ORM called Waterline which abstracts data access. Waterline allows you to use different databases such as MySQL, PostgreSQL, MongoDB, Redis, etc. and have a robust API for working with your model data.

Sails.js Features

Sails.js has many salient features which will help the users to build custom, enterprise-ready applications. Some of them are as follows:

  • Auto-generated REST APIs from your models
  • Database ORM
  • Inbuilt task runner
  • Security code
  • Built-in web sockets in routes.
  • Form MVC
  • Enhanced Plugin System
  • Policies to secure
  • Perform front-end agnostic

Understanding the Model View Controller

Sails.js has accepted the Model View Controller (MVC) design pattern at its core. If you have already learned Ruby on Rails, then, you will find that the architecture for creating a Sails app is quite familiar to Ruby.

In Model View Controller the model represents the data model, reflecting the database collections/table schema. On the other hand, the View refers to the HTML view when it is filled with data while the Controller delivers a platform where you can input all your server-side business logic. The control also links the view to the data.

  1. Models: Models request your database and return the necessary result.
  2. Views: Views are pages that render data. In Sails.js, they stored with .ejs template files.
  3. Controllers: Controllers handle user requests and retrieve data from the models and pass them on to the views.
  4. Policies: Policies are used to limit access to certain parts of your app. It can be used for anything unlike HTTP Basic Auth, 3rd party Single Sign-on, etc., or also for your custom authorization scheme.

Understanding How Sails.js Works

Sails.js web framework’s functionality contributes to Sails for being one of the best Node.js web frameworks. It has a fantastic yet straightforward work structure which is described below:

  1. File loading

With the Sails.js framework, the user can put their files under the API before starting their server for the web framework to load them automatically. Sails will also load the configuration automatically. This is very helpful when one needs to develop a prototype quickly.

  1. Web server

Sails.js uses Express 3 as a web server. It has also been working on migrating to Express 5.

  1. ORM

At first, you will get Sails with Waterline and then can change to another ORM down the line. However, for this to work, you will need to disable Waterline so that you can utilize another ORM.

  1. Web Socket

Sails use Socket.IO, which is embedded directly into the framework. To use another alternative socket, you will have to disable the Socket.IO first.

  1. Automatic REST API

Sails.js refers to these as blueprints. However, you can still configure them to enable or disable what you do not want to be generated automatically.

Advantages of Sails.js

The advantages of Sails.js are as follows:

  • The sails.js framework provides good code organization and blueprints.
  • WebSockets: It has built-in support for WebSockets.
  • ORM/ODM waterline:js has ORM waterline to support various databases.
  • Data validation: It validates the data before using, importing or processing the data.
  • Auto-Generated Code: Auto-generated code for controllers, models, and routes.
  • Security Features: Many out-of-the-box security features, e.g., CSRF and compatibility with Lusca.
  • Library Support: This framework provides the built-in file to upload library.
  • Good documentation: It ensures good documentation.
  • Modular architecture: It has a flexible and modular architecture with hooks and plugins.

Disadvantages of Sails.js

  • js doesn’t provide support for JOIN query in Waterline.
  • The development is very slow.
  • It has a steep learning curve.
  • This framework is opinionated.

Difference between Hapi.js and Sails.js

Hapi.js

Sails.js

Hapi.js has plugin-based architecture. Sails.js has a flexible and modular architecture with built-in hooks and plugins.
In this framework, the developers need to figure out the code structure on their own. This framework has auto-generated code for controllers, models, and routes.
The Endpoints of this framework are created manually. Sails.js has inbuilt API for creating endpoints.
Hapi.js has better separation of business logic and scalability with the help of Seneca and chairo plugin. The Sails.js Blueprint API powers the restful APIs that are obtained anytime when a model and controller are created.
It does not provide an ORM/ODM waterline support. This framework comes with the database ORM/ODM Waterline.
Does not support all databases. Sails.js supports various databases.

The Waterline ORM/ODM

One of the salient features of Sails.js is Waterline, a powerful ORM (Object Relational Mapper) for managing SQL-based databases and ODM (Object Document Mapper) for NoSQL document-based databases. Waterline extracts all the complexities when you are working with databases and, most importantly, with Waterline you don’t have to take the headache of choosing a database system when you’re just starting development. It also doesn’t intimidate you when your client hasn’t yet decided on the database technology to use.

You can start building your sails.js application even without a single line of configuration. Initially, you don’t have to install any database system. Thanks to the built-in sails-disk NeDB-based file database, because of which you can transparently use the file system to store and retrieve data for testing and managing your application functionality.

Real-Time Communication with Sails.js

The purpose of Socket.IO is to establish bidirectional event-based communication between the server and the client. Sails.js is integrated with Socket.IO and wrapped it up with a higher abstraction-level API to provide greater convenience and ease. As such, Sails.js is particularly suited for the creation of multiplayer games and live chat apps.

Installation and Configuration

To install sails, you must have the latest version of Node and npm installed in your system, because it needs several new things which are not there in legacy node versions. The steps to install sails.js are as follows:

  1. You have to run the following command on CRM to install it.

npm install -g sails

  1. Dependent on the speed of your internet connection, the system will take some time to install Sails.js. Once the installation completes, you can use the sails command line to create new projects.

Create a Project

To create a new project, run the following command.

sails create projectName

The above command will create a new directory on the same place from where you have run the command and place all needed files into it.

To install modules

Once everything is done, again go to the project directory and run the following command to install the modules.

npm install

Security and Web Socket

The Sails.js framework hasCORS ( Cross origin site request) and CSRF  (Cross Site Request Forgery) to prevent the attack on the code already written. You can enable or disable it by going to /config/CSRF.js and /config/CORS.js.