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#
Version | Release notes | Changelog | Release Date |
---|---|---|---|
0.12.13 | Release notes | 2017-03-06 | |
0.12.12 | Release notes | Changelog | 2017-03-03 |
0.12.11 | Release notes | Changelog | 2016-11-24 |
0.12.10 | Release notes | Changelog | 2016-11-17 |
0.12.9 | Release notes | Changelog | 2016-11-02 |
0.12.8 | Release notes | Changelog | 2016-10-22 |
0.12.7 | Release notes | Changelog | 2016-10-06 |
0.12.6 | Release notes | Changelog | 2016-09-28 |
0.12.5 | Release notes | Changelog | 2016-09-28 |
0.12.4 | Release notes | Changelog | 2016-08-01 |
0.12.3 | Release notes | Changelog | 2016-04-04 |
0.12.2 | Release notes | Changelog | 2016-04-02 |
0.12.1 | Release notes | Changelog | 2016-02-15 |
0.12.0 | Release notes | Changelog | 2016-02-06 |
0.11.5 | Release notes | Changelog | 2016-02-05 |
0.11.4 | Release notes | Changelog | 2016-01-06 |
0.11.3 | Release notes | Changelog | 2015-11-23 |
0.11.2 | Release notes | Changelog | 2015-09-23 |
0.11.0 | Release notes | Changelog | 2015-02-11 |
0.10.5 | Release notes | Changelog | 2014-08-30 |
0.10.4 | Release notes | 2014-08-13 | |
0.10.3 | Release notes | 2014-08-07 | |
0.10.2 | Release notes | 2014-08-06 | |
0.10.1 | Release notes | 2014-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.
- Create an empty controller file by typing
$ sails generate controller hello
- Find the new controller file at
api/controllers/HelloControllers.js
and add thehello
method to it.
module.exports = {
hello : function (req, res) {
var myName = 'Luis';
return res.view('hello' , {name : myName});
}
}
- Create a new view file under the folder
views
namedhello.ejs
with the following HTML:
<html>
<head></head>
<body>
<p>Hello {{}}.</p>
</body>
</html>
- Define a route in
config/routes.js
that calls thehello
method in theHelloController
controller.
'GET /' : 'HelloController.hello',
Now we have implemented all the code needed for this example. Let’s try it:
- Start the server
$ sails lift
-
Open the browser and type
https://localhost:1337
. If it’s not coming up, check the URL in thesails lift
output. The port may be different. -
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