sails.js

Getting started with sails.js

Remarks#

sails.js is an MVC (Model View Controller) web framework for node.js that emulates familiar MVC frameworks like Ruby on Rails. sails.js is based on Express and provides websocket support via socket.io.

sails.js provides a set of conventions and default configurations to quickly get a new website project started. It is highly configurable and allows you to easily override the default conventions.

sails.js comes with an ORM called Waterline which abstracts data access. Waterline allows you to use various datastores such as MySQL, PostgreSQL, MongoDB, Redis, etc. and have a clear API for working with your model data.

Versions#

VersionRelease notesChangelogRelease Date
0.12.13Release notes2017-03-06
0.12.12Release notesChangelog2017-03-03
0.12.11Release notesChangelog2016-11-24
0.12.10Release notesChangelog2016-11-17
0.12.9Release notesChangelog2016-11-02
0.12.8Release notesChangelog2016-10-22
0.12.7Release notesChangelog2016-10-06
0.12.6Release notesChangelog2016-09-28
0.12.5Release notesChangelog2016-09-28
0.12.4Release notesChangelog2016-08-01
0.12.3Release notesChangelog2016-04-04
0.12.2Release notesChangelog2016-04-02
0.12.1Release notesChangelog2016-02-15
0.12.0Release notesChangelog2016-02-06
0.11.5Release notesChangelog2016-02-05
0.11.4Release notesChangelog2016-01-06
0.11.3Release notesChangelog2015-11-23
0.11.2Release notesChangelog2015-09-23
0.11.0Release notesChangelog2015-02-11
0.10.5Release notesChangelog2014-08-30
0.10.4Release notes2014-08-13
0.10.3Release notes2014-08-07
0.10.2Release notes2014-08-06
0.10.1Release notes2014-08-02

Releases prior to 0.10.1 omitted from list. See earlier releases

Installation

Prerequisites

  • nodejs

To install the latest stable release of sails with the command-line tool issue following command:

$ sudo npm install sails -g

Depending on your OS you might not need to use sudo.

Creating a new project

Once you have Sails installed, just type

$ sails new <project_name>

This will create a skeleton Sails project in a new folder called <project_name>.

You can also create a new project in an empty folder by typing

$ sails new

Launch app

Once your project has been created, you can launch the app by typing

$ sails lift

By default, you can access the app in the browser on port 1337. The URL with the port is shown in the terminal.

Another way to start the Sails app is with the node command:

$ node app.js

However, you lose some development features of the lift command like auto-reloading of the app when assets and view files are modified.

For development you can also use:

$ sails console

This allows you to execute command directly in command line. It’s very useful for debugging Models.

Hello world

This example shows how to develop our first application step by step, assuming you already have Sails installed and a project created.

  1. Create an empty controller file by typing
$ sails generate controller hello
  1. Find the new controller file at api/controllers/HelloControllers.js and add the hello method to it.
module.exports = {

  hello : function (req, res) {
    var myName = 'Luis';          
    return res.view('hello' , {name : myName});
    }
}
  1. Create a new view file under the folder views named hello.ejs with the following HTML:
<html>
    <head></head>
    <body>
        <p>Hello {{}}.</p>
    </body>
</html>
  1. Define a route in config/routes.js that calls the hello method in the HelloController controller.
'GET /' : 'HelloController.hello',

Now we have implemented all the code needed for this example. Let’s try it:

  1. Start the server
$ sails lift
  1. Open the browser and type https://localhost:1337. If it’s not coming up, check the URL in the sails lift output. The port may be different.

  2. You should see the following output:

    Hello Luis

Generating sails project without frontend

If there is no need for frontend in your next project, you can run sails new with additional flag —no-frontend.

sails new NameOfProject --no-frontend

This will generate everything needed for backend and will omit view, assets and grunt files.

More about command line and sails-new: https://sailsjs.org/documentation/reference/command-line-interface/sails-new


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow