Intro to Sails.js

Joseph Lawson
4 min readMar 1, 2018

--

Sails.js is a fullstack Node.js framework thats is build on Express. It is opinionated, and follows the MVC framework. Sails.js is similar to Ruby on Rails, and if you have worked in Rails a lot of things in Sail.js will look familiar. I will be going over how to initialize a project in Sail, and discussing some of the unique things that come with it.

Initializing a project

The first step in creating a project is to install the Sails.js globally with the npminstall sails -g command in your terminal.

The next step is to enter the sails new command followed with your project name.

Now move into tour newly created directory and open your new application into a text editor and look around.

The application starts with an api folder that contains your models and controllers. The config folder holds routes, database information, and many other application configuration files. The View folder holds all of the default views, which are .ejs files. The next step is to start your server and look at the Sails.js hello world page. Run sails lift in your terminal.

Once your server is running go to localhost:1337 in your browser.

Blueprints

Now that our app is running we need to create a model and controller. In Sails.js this is incredibly easy because of blueprints do alot of the work behind the scenes for us. The first way to create the model and controller are with sails generate api articles, which creates them both in one command, but you cannot create attributes to to model this way.

This command will create a model for our articles with a title and body attributes.

This command will create a controller for our articles, and because of the blueprints that come with Sails.js this is all we need to do for a fully CRUD functional api. Sails.js comes with a configured noSQL database called Sails disk, but you can easily change it to Postgress, mongoDB, or any of the other supported databases. Now we will add an article to our API. First start your server with sails lift and go to localhost:1337/articles in your browser.

If you wanted to add an article to your database for testing, you can open sails console in your terminal and create a new article in your database. The second option is using the action routes that come with blueprints in Sail.js

The action routes in Sail.js allow you to use HTTP verbs and pass along data to speed up prototyping. These commands can easily be disabled by going to the Bluprints file in the config folder and setting the action routes to false. The blueprinting also gives every controller you make restful routing. Once I enter a couple of articles the Articles route will look like this.

You can view a specific post by passing along an id

Creating a view is very simple, you first have to create a route. This can be done in a controller or in the routes.js file, the syntax is the same.

Now that we have fetched the Articles from the database we need to create a view to display the information.

And this is the end result!

This basic guide has show you how quickly you can create a small application in Sails.js, but there is so much more that you can do with it. Refer to their documentation for more information https://sailsjs.com. Thank you.

--

--