Install Sails globally:
npm install -g sails
A quick reference guide for Sails.js, covering core concepts, commands, configurations, and best practices for building scalable Node.js applications.
|
Install Sails globally:
|
|
Create a new Sails project:
|
|
Lift Sails server:
|
|
Generate a model:
|
|
Generate a controller:
|
|
Generate an API (model and controller):
|
|
Models |
Represent the data structure. Define attributes and their types (e.g., String, Number, Boolean). |
|
Controllers |
Handle incoming requests and define actions to be performed. |
|
Views |
Templates rendered to the client. Supports various templating engines (e.g., EJS, Jade). |
|
Routes |
Map URLs to controllers and actions. Can be configured in |
|
Policies |
Middleware functions that run before actions. Used for authentication, authorization, etc. |
|
Services |
Reusable logic. Can be injected into controllers, models, or other services. |
|
Models are defined in Example:
|
|
Attribute Types: |
|
Create |
|
|
Find |
|
|
Update |
|
|
Destroy |
|
|
findOne |
|
|
count |
|
|
Controllers are defined in Example:
|
|
Each function within |
|
Configuration |
Routes are configured in |
|
Example - Explicit Route |
|
|
Example - Shortcut Route |
|
|
Example - RESTful Route |
Sails automatically generates RESTful routes for APIs. For example, |
|
Custom Routes |
You can also define custom routes with parameters:
|
|
req (Request) |
Represents the HTTP request and has properties like |
|
res (Response) |
Represents the HTTP response and has methods like |
|
Views are located in Example:
|
|
Use
|
|
Location |
Assets (CSS, JavaScript, images) are stored in the |
|
Pipeline |
Sails uses Grunt to manage and pipeline assets. Configure in |
|
Linking |
Link assets in your views using |
|
Example |
|
|
Policies are middleware functions that run before your controller actions. They are located in Example:
|
|
Apply policies to controllers/actions in |