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.
Controllers (the C in MVC) are the principal objects in your Sails application that are responsible for responding to requests from a web browser, mobile application or any other system capable of communicating with a server. They often act as a middleman between your models and views. For many applications, the controllers will contain the bulk of your project’s business logic.
When sails initially starts using sails lift
, sails looks to see if you have
any controller defined. In our example, we have one controller, the User
controller. Sails then provides access to blueprint actions for this user
controller as if we built them in the controller ourselves. Sails also
automatically creates blueprint routes at the time of lifting the server. So even
if no routes is defined in /config/routes.js
and no action is defined in
/api/controllers/UserController.js
explicitly, after lifting the server all
these routes and actions are available to use.
Routes are rules that tell Sails what to do when faced with an incoming request.
Routes are defined in config/routes.js
. The order of the routes is significant, as routes are matched top down. This means if you have a specific route that also could be matched by a wildcard route, the specific route should be defined above the wildcard route.
When a request enters your application sails.js grabs all the parameters that came with it and makes them available for you as params
on the request object.
Properties in the route target object will be passed through to the route handler in the req.options object. The following are reserved properties that can affect the behavior of the route handler:
Property | Applicable Target Types | Data Type | Details |
---|---|---|---|
skipAssets | all | Boolean | Set to true if you don't want the route to match URLs with dots in them (e.g. myImage.jpg). This will keep your routes with wildcard notation from matching URLs of static assets. Useful when creating URL slugs. |
skipRegex | all | Regexp | If skipping every URL containing a dot is too permissive, or you need a route's handler to be skipped based on different criteria entirely, you can use skipRegex . This option allows you to specify a regular expression or array of regular expressions to match the request URL against; if any of the matches are successful, the handler is skipped. Note that unlike the syntax for binding a handler with a regular expression, skipRegex expects _actual RegExp objects, not strings. |
locals | controller, view, blueprint, response | Dictionary | Sets default local variables to pass to any view that is rendered while handling the request. |
cors | all | Dictionary or Boolean or String | Specifies how to handle requests for this route from a different origin. |
populate | blueprint | Boolean | Indicates whether the results in a "find" or "findOne" blueprint action should have associated model fields populated. Defaults to the value set in config/blueprints.js . |
skip, limit, sort, where | blueprint | Dictionary | Set criteria for "find" blueprint. |
Sails comes installed with a powerful ORM/ODM called Waterline, a datastore-agnostic tool that dramatically simplifies interaction with one or more databases. It provides an abstraction layer on top of the underlying database, allowing you to easily query and manipulate your data without writing vendor-specific integration code.