5 min read

In this article by Shahid Shaikh, author of the book Sails.js Essentials, you will learn a few basics of Sails.js. Sails.js is modern production-ready Node.js framework to develop Node.js web application by following the MVC pattern. If you are not looking to reinvent wheel as we do in MongoDB, ExpressJS, AngularJS, and NodeJS (MEAN) stack and focus on business logic, then Sails.js is the answer.

Sails.js is a powerful enterprise-ready framework, you can write your business logic and deploy the application with the surety that the application won’t fail due to other factors.

Sails.js uses Express.js as a web framework and Socket.io to handle the web socket messages. It is integrated and coupled in the Sails.js, therefore, you don’t need to install and configure them separately.

Sails.js also has all the popular database supports such as MySQL, MongoDB, PostgreSQL, and so on. It also comes up with autogenerated API feature that let’s you create API on the go.

(For more resources related to this topic, see here.)

Brief about MVC

We know that Model-View-Controller (MVC) is a software architecture pattern coined by Smalltalk engineers. MVC separates the application in three internal components and data is passed via each component. Each component is responsible for their task and they pass their result to the next component. This separation provides a great opportunity of code reusability and loose coupling.

MVC components

The following are the components of MVC architecture:

Model

The main component of MVC is model. Model represents knowledge. It could be a single object or nested structure of objects. The model directly manages the data (store the data as well), logic, and rules of the application.

View

View is the visual representation of model. View takes data from the model and presents it (in browser or console and so on). View gets updated as soon as the model is changed. An ideal MVC application must have a system to notify other components about the changes. In a web application, view is the HTML Embedded JavaScript (EJS) pages that we present in a web browser.

Controller

As the name implies, task of a controller is to accept input and convert it into a proper command for model or view. Controller can send commands to the model to make changes or update the state and it can also send commands to the view to update the information.

For example, consider Google Docs as an MVC application, View will be the screen where you type. As you can see in the defined interval, it automatically saves the information in the Google server, which is a controller that notifies the model (Google backend server) to update the changes.

Installing Sails.js

Make sure that you have the latest version of Node.js and npm installed in your system before installing Sails.js.

You can install it using the following command:

npm install -g sails

You can then use Sails.js as a command-line tool, as shown in the following:

Creating new project

You can create a new project using Sails.js command-line tool. The command is as follows:

sails create appName

Once the project is created, you need to install the dependencies that are needed by it. You can do so by running the following command:

npm install

Adding database support

Sails.js provides object-relational mapping (ORM) drivers for widely-used databases such as MySQL, MongoDB, and PostgreSQL. In order to add these to your project, you need to install the respective packages such as sails-mysql for MySQL, sails-mongo for MongoDB, and so on.

Once it is added, you need to change the connections.js and models.js file located in the /config folder.

Add the connection parameters such as host, user, password, and database name in the connections.js file. For example, for MySQL, add the following:

module.exports.connections = {

  mysqlAdapter: {

    adapter: 'sails-mysql',

    host: 'localhost,

    user: 'root',

    password: '',

    database: 'sampleDB'

  }

};

In the models.js file, change the connection parameter to this one. Here is a snippet for that:

module.exports.models = {

  connection: 'mysqlAdapter'

};

Now, Sails.js will communicate with MySQL using this connection.

Adding grunt task

Sails.js uses grunt as a default task builder and provides effective way to add or remove existing tasks. If you take a look at the tasks folder in the Sails.js project directory, you will see that there is the config and register folder, which holds the task and registers the task with a grunt runner.

In order to add a new task, you can create new file in the /config folder and add the grunt task using the following snippet as default:

module.exports = function(grunt) {

  // Your grunt task code

};

Once done, you can register it with the default task runner or create a new file in the /register folder and add this task using the following code:

module.exports = function (grunt) {

  grunt.registerTask('task-name', [ 'your custom task']);

};

Run this code using grunt <task-name>.

Summary

In this article, you learned that you can develop rich a web application very effectively with Sails.js as you don’t have to do a lot of extra work for configuration and set up. Sails.js also provides autogenerated REST API and built-in WebSocket integration in each routes, which will help you to develop the real-time application in an easy way.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here