Test Drive Sails.js

We have been using Express.js for all our projects so far and there are have been no regrets. But it was time that we developed a Bolierplate template that consists of all the dependencies and repeated stuff like Session management, CSRF, File Uploads etc. that are often needed for every project. So we started working on it, but stumbled upon Sails.js. It’s true that Sails.js has been out there from a long time and we should have looked at it earlier, but we are glad that we worked on Express.js first, since Sails.js is built tightly on top of Express.js which makes it easier to understand the inner workings of Sails.js

One of the features that we thought was very cool is a very neat and tidy Models & ORM which works out of box with most of the SQL and No-SQL database. Sails.js makes use of Waterline adapter as its default ORM which can be used for data storage and retrieval for Node.js with support for MySQL, MongoDB, PostgreSQL, Redis, and more.

So, with all the nice features and bells and whistles ready to be used lets get started. This tutorial shows how to install and start your first app.

Installing Sails.js

Assuming you have Node.js and NPM installed you can install Sails.js with the following command

$ npm install sails -g

Once sails is install you can create your sails app with

$ sails new my-app

*Note: You can change my-app to the name of your project. A new project directory with the name supplied will be created

Starting your app

Once your project has been created you can start your app with the following command

$ sails lift

Once the app has started you will see the output as below in your terminal.


Your app will now started on the frameworks default port 1337

The main file where the app is spawned is app.js. So you can run the app with node, forever or any other way in situations where Sails CLI is not relevant or useful. We like to work with nodemon as it gives a nice way to watch file changes and restart the app automatically to speed up the development.

You can install nodemon by

$ npm install -g nodemon

The following command will start the app with nodemon

$ nodemon app.js

Once the nodemon starts the app, you will see the same app output as above and the app starts on port 1337. Now in your browser type http://localhost:1377 where you will see the default Sails.js home page. We will look where it comes from and how its handles in a little bit.

Level: Intermediate

Technologies: NodeJS

post via Codincafe