Beginner’s guide to Sails.js

Sails.js is node.js framework that allows you to build enterprise-ready, custom MVC ( model, view, controller ) application on-the-go. Sails.js has built-in features such as an API creator, and its socket integration in every route and database ORM makes it very useful and helps speed up development.

 

Features of Sails

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

 

Unlike MEAN.io, you don’t need to make APIs manually in Sails.JS. Just a single command and you’ll have your code. Sails.js provides database drivers for every major database systems such as MySQL, MongoDB, PostgreSQL. Sails.js uses Grunt task runner as the default task runner, and also provides an effective way to manage custom grunt tasks.

 

Plus, CORS and CSRF already included in Sails. You just have to turn them on from config file. It uses Express to handle http requests and Socket.io for handling web sockets.

 

How can I install Sails?

Before installing Sails in your system make sure you’ve latest versions of Node installed.
Run following command and that’s it. it’ll install Sails in your system:

npm install -g sails

 

Once installation finished, you can use sails command to create Sail project.
Followin these commands to create and start your project:

1. sails new <project_name>

1-new

2. cd <project_name>

3. npm install

4. sails lift

2-lift

5. Open localhost:1337 to open your app

 

 

Folder Structure

The controllers and model folders are the most important.

Controller contains every piece of the code your application needs to drive a backend system and communicate with the View or User interface.

Model contains how the object in data store will look like. So, if you are using a relational database, then it will be a table in database and JSON object in model file, or if you are using NoSQL databases such as MongoDB, then it will be collection under model.

Suppose you want to configure the database say, MySQL in your Sails project. You can do this in steps:

  1. Install the driver. Configure the connection
  2. Install the module using the following command:
    npm install --save sails-mysql

 

Once this done, configure MySQL settings in config/conenctions.js file

someMysqlServer: {
    adapter: 'sails-mysql',
    host: 'localhost',
    user: 'codeportal',
    password: 'codeportal',
    database: 'codeportal'
}

So, that’s it for introduction. I hope you found SailsJS amazing. Keep in touch for my next post on How to start with APIs in SailsJS? 

Check out more about Sails here.

Thanks!