Sails.js 101

Sails.js 101
Courses on Node.js, React
and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →


Sails.js (GitHub) is a convention-over-configuration type of a framework. This means that it’s similar in philosophy to Ruby on Rails. Sails.js is a true MVC framework, unlike Express.js which relies on developers for adding ORMs like Mongoose. Sails.js uses the Waterline ORM.

To get started with Sails.js:

$ npm -g install sails@0.10.5

This will give you the sails command, and you can see the list of available options with:

$ npm sails -h

Let’s use the new command to generate an app (.../sails):

$ sails new sails

After the app has been generated, start it with lift:

$ cd sails
$ sails lift

Now, if you go to the http://localhost:1337, you will see a Sails.js page with some instructions and links (Figure 1).

Figure 1. The default Sails.js page with some instructions and links.

Figure 1. The default Sails.js page with some instructions and links.

To generate resources, you can use these commands:

  • $ sails generate api NAME: Building a new model and controller api/models/NAME.js and api/controllers/NAMEController.js
  • $ sails generate model NAME [attribute1:type1, attribute2:type2 ... ]: Building a new model api/models/NAME.js with attributes (optional).
  • $ sails generate controller NAME [action1, action2, ...]: Building a new controller api/controllers/NAMEController.js with actions (optional).
  • $ sails generate adapter NAME: Building a new adapter api/adapters/NAME.
  • $ sails generate generator NAME: Building a new generator.

Each controller is structured as a module with methods. These methods are actions, e.g., /controller/action. Each action has a request and response. Their arguments are inherited from their Express.js counterparts. To illustrate this point, let’s create a controller and add some custom code to it using Express.js methods —response.json() and response.redirect().

Firstly, run this command:

$ sails generate api user

Open newly created file .../sails/api/controllers/UserController.js. In it, add two actions “json” that will output the current time, and “buy-oauth” that will utilize the redirect:

module.exports = {
  json: function (request, response) {
    response.json({time: new Date()})
  },
  'buy-oauth': function (request, response) {
    return res.redirect('https://gum.co/oauthnode');
  }
};

If you go to the http://localhost:1337/user/json, you’ll see:

{   "time": "2014-09-09T14:59:28.377Z" }

And if you go to the http://localhost:1337/user/buy-oauth, you’ll be redirected to the Introduction to OAuth with Node.js [webapplog.com, 2014] page.

So the conclusion here is that it will be easy for someone who’s already familiar with Express.js to write controllers in Sails.js. Controllers are intermediaries between views and models, and typically contain the bulk of the code and logic! For more information on Sails.js concepts and its documentation go to http://sailsjs.org/#/documentation/concepts and http://irlnathan.github.io/sailscasts.

 

[Sidenote]

Reading blog posts is good, but watching video courses is even better because they are more engaging.

A lot of developers complained that there is a lack of affordable quality video material on Node. It's distracting to watch to YouTube videos and insane to pay $500 for a Node video course!

Go check out Node University which has FREE video courses on Node: node.university.

[End of sidenote]

PS: This text is from my new book Pro Express.js.

Courses on Node.js,
React and JavaScript
Become an expert with my comprehensive
Node.js, React.js and JavaScript courses.
Learn Full Stack Javascript →

--
Azat Mardan
Azat Mardan avatar
https://www.linkedin.com/in/azatm
To contact Azat, the main author of this blog, submit the contact form.

Also, make sure to get 3 amazing resources to FREE when you sign up for the newsletter.
Simple.
Easy.
No commitment.

4 thoughts on “Sails.js 101

  1. Pingback: Sails.js 101 – SailJs

  2. Pingback: This Week’s Node News – Issue 66 | blog1

  3. Pingback: Error Handling and Running an Express.js App

  4. Pingback: Quick Announcement and a Favor

Leave a Reply

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