Sails JS

Sails JS 16Jan, 2015

Let’s start this new year with a small introduction about Sails JS. It is one of the most popular frameworks of Node JS which follows the MVC structure and is a Real-time Framework. It’s very much good for building real-time dashboards, chat or multi-player games, but you can use it for any web application project – top to bottom.

Some of the key features of sails are:

  • It is 100% Javascript.
  • It can be used with any DB, as it is bundled with the powerful ORM called Waterline.
  • It has easy WebSocket Support
  • Sails provides basic security and role-based access control by default in the form of Policies.
  • Flexible asset pipeline – When you’re ready to go into production, your assets are minified and gzipped automatically.

If anyone of you have worked with frameworks like Zend, Laravel, CodeIgniter, Cake, Grails, Django, ASP.NET MVC, or Rails,then you will find Sails pretty similar to these frameworks. Not only that, but you can look at a Sails project and know- how to code up the basic patterns they’ve implemented over and over again in the past; whether their background is in PHP, Ruby, Java, C#, or Node.js.

Some more information before you start working in Sails JS:

  • With the help of Sails JS, you can easily create apps with Node.js. By using the Model-View-Controller pattern, you can organize your code more efficiently & maintain it well.
  • You can create yours app more quickly, and save most of your time by using the commands which can automate the creation of models and controllers (not views).
  • Sails.js has various “adapters”, which will help you to use any database with your app. This is different from other MVC frameworks which allow you to use only MongoDB.

Installing Node.js on an Ubuntu

  1. Installing the pre-requisites:
    sudo apt-get install python-software-properties python g++ make
    sudo apt-get install software-properties-common
  2. Add PPA Repository, which is recommended by maintainers of Node.js:
    sudo add-apt-repository ppa:chris-lea/node.js
  3. Now update package list:
    sudo apt-get update
  4. To install Node.js:
    sudo apt-get install nodejs

Installing Of Sails.js

To install the latest stable release of sails, you have to run the command:
sudo npm install sails -g

The -g flag indicates that sails is now installed globally and can be used as a command-line tool.
Creat The Sails App

If you want to move to the directory, where you you want to locate your appe.g. /var/www and then run:
sails new projectname

This will add the files to your project, and will create a directory named as projectname.

Starting up the Sails.js server

To view your app, you will need to change the directory into the project directory, and then start the server:
cd projectname
then:
sails lift

This will create a server running at http://localhost:1337, and the page may look something like this:

If you want to install a particular version of sails:

  • open package.json file
  • change version of sails in dependencies
  • run npm install in folder directory

Now your version will be updated.

To Create Controller

Just same as Baking Cake in CakePHP, we can use CLI to create models and controllers in sails. To create a controller called user with the methods “index, add, edit, delete”, just run the following command:

sails generate controller user index add edit delete

This will create a file in api/controllers called UserController.js

To Creating Model

It is as simple as creating a controller in Sail.js. You don’t have to be afraid of the database migrations, as Sails.js will look that for you. Now, it is easy to use the default in-file database, MySQL or many other database types via “adapters”.

You can also specify the fields in the model during the creation, in the format of [name]:[type]. You will see that the database tables will be created according to the type specified in the model.

e.g. To create a model called user with the fields “email, password”, you have to run the following command:

sails generate model user email:string password:string

If at any point of time you need to create/edit/delete a field in any table of the Database, you need to specify it in model file only. Sails will automatically change your database accordingly.

Creating Views

Creating a view also follow some simple steps.

  • For user view, create a folder named user in views folder.
  • Create template files with extension .ejs and call them in view. For example if you have index function in UserController.js, then template file will be index.ejs. You can also call different template file by defining it in the controller function.
  • Now you can view this page at http://localhost:1337/user/

So, this is a quick tutorial for all of you, so that you start up working in Sails JS. I hope this will be helpful to you and will save your time in the initial working with Sails.

Posted by: Sandeep Kaur / In: Node JS and Tagged ,
Cam

Leave a Reply

Your email address will not be published. Required fields are marked *