Getting started with Sails.js: General Overview

sails_img

What is Sails.js?

Sails is a Node.js framework that makes it easy to build custom, enterprise-grade Node.js apps. As described on the Sail.js homepage, It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps: data-driven APIs with a scalable, service-oriented architecture. It’s especially good for building chat, real-time dashboards, or multiplayer games; but you can use it for any web application project – top to bottom.

Why Use Sails.js?

Sails.js boasts a number of awesome features which will help you to build custom, enterprise-ready applications in little time. Some of these features are listed and explained below as described on the Sails homepage:

  • Auto-generate REST API’s: Sails comes with blueprints that help jumpstart your app’s backend without writing any code. Just run sails generate api dentist and you’ll get an API that lets you search, paginate, sort, filter, create, destroy, update, and associate dentists. Since these blueprint actions are built on the same underlying technology as Sails, they also work with WebSockets and any supported database out of the box.
  • Any Database: Sails bundles a powerful ORM, Waterline, which provides a simple data access layer that just works, no matter what database you’re using. In addition to a plethora of community projects, officially supported adapters exist for MySQL, MongoDB, PostgreSQL, Redis, and local disk.
  • Front-end Agnostic: Sails is compatible with any front-end strategy; whether it’s Angular, Backbone, iOS/ObjC, Android/Java, Windows Phone, or something else that hasn’t been invented yet. Plus it’s easy to serve up the same API to be consumed by another web service or community of developers.
  • Powerful Associations: Sails offers a new take on the familiar relational model, aimed at making data modeling more practical. You can assign different models to different databases, and your associations/joins will still work— even across NoSQL and relational boundaries. Sails have no problem implicitly/automatically joining a MySQL table with a Mongo collection and vice versa.
  • 100% Javascript: Sails is built with an emphasis on developer happiness and a convention-over-configuration philosophy. But Node.js takes this principle to the next level. Building on top of Sails means your app is written entirely in JavaScript. Since you spend less time context-shifting, you’re able to write code in a more consistent style, which makes development more productive and fun.
  • Built-in Web Sockets: In the past, adding real-time/”comet” features meant maintaining two separate code bases. But since the request interpreter in Sails translates incoming socket messages for you, they’re automatically compatible with every route in your Sails app, as well as any existing Express routes/middleware. Normalization of parameters, the session, and the streaming interface are all taken care of. In other words, the code you write works with WebSockets and HTTP, without doing any extra work.

We will learn more about some of these features in subsequent hands-on sessions.

Installing Sails.js

To install Sails on your computer, first download and install the latest version of Node and NPM or update the copy you have to the latest version. This is necessary as legacy Node versions lack some needed components.

To install the latest stable release of Sails with the command-line tool, run the following command:

sudo npm -g install sails

To install the On Windows (or Mac OS with Homebrew), just run the command without sudo:

npm -g install sails

Creating a New Sails.js Project

To create a new Sails App with the command-line tool, run the following command:

sails new testApp

screen-shot-2016-12-31-at-1-05-02-pm

A new project directory with the name testApp will be created, inside this directory, you can go to the project directory from the command-line to see all the files and directories that were generated by default.

cd testApp/

screen-shot-2016-12-31-at-1-05-17-pm

Running a Sails.js Project

To run a Sails project, open up your terminal, go to the project directory and then run the following command:

sails lift

This command is used to lift the Sails Application. Sails will initialize the application, bind to the configured port and start to listen for HTTP requests.

screen-shot-2016-12-31-at-1-05-33-pm

At this point,  visit http://localhost:1337 and you will see the Sails.js default home page. If this is so, then you have successfully created and initialized your testAPP.

screen-shot-2016-12-31-at-1-24-24-pm

Note: When your application is lifted, the terminal window will be in blocked state. You can press Control + C to terminate the application and return to the command prompt.

Final words

In this post, I have tried to explain at a very basic level what Sail.js is and how to get started using Sails to build your enterprise-grade Node.js apps. In part 2 of this post, I will cover a whole lot of stuff like Rest API Auto-generation, Routing, Views, Controller, Model etc. Part 2 will be more hands-on as we will be building a simple Sails.js App to explain all the concepts in this post and in Part 2.

Show Buttons
Hide Buttons