Sails.js is an MVC-based Node.js web framework designed for building custom, enterprise-grade applications, particularly those requiring real-time features using WebSockets. It aims to provide a Rails-like developer experience but with a data-oriented approach suitable for modern APIs. The current stable version is 1.5.17, with patch releases occurring periodically to address dependencies and minor fixes. Major version updates, like the transition to v1.0, introduced significant breaking changes, prioritizing developer experience over strict backward compatibility. Sails differentiates itself through its convention-over-configuration philosophy, automatic RESTful API generation, integrated ORM (Waterline) with multi-database support, and native Socket.io integration for real-time communication. It also embraced `async/await` syntax from v1.0 onwards, streamlining asynchronous code.
npm install sailsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a new Sails.js project using its CLI, generate basic model and controller files, lift the application, and then programmatically interact with the lifted app's models from an external script.
Refer to the official Sails.js upgrade guides (e.g., 'Upgrading to Sails v1.0') and consider using the `sails upgrade` tool. Migrate your application incrementally, addressing database configuration, model attribute definitions, and asynchronous API usage.
Pass configuration overrides directly to the `.lift()` or `.load()` methods. For `.sailsrc` specific configurations, use `require('sails/accessible/rc')('sails')` and pass them in. Disable unnecessary hooks and globals (`globals: false`) when running multiple instances or in test environments.Standardize on `async/await` for all asynchronous model methods and other operations. Ensure proper `try/catch` blocks are used. If using `.then()` chains, always include a `.catch()` to prevent unhandled promise rejections.
Always sanitize and escape user-provided or dynamic data before rendering it in views or sending it in HTTP responses to prevent Cross-Site Scripting (XSS) attacks. Utilize templating engine auto-escaping features or specific sanitization libraries.
For new logic, use 'helpers' instead of 'services'. Adopt the 'Actions2' syntax for new controller actions to benefit from automatic parameter validation and clear exit definitions.
Ensure that each request handler (action, policy, middleware) sends only one response. Use `return` before `res.send()`, `res.json()`, `res.serverError()`, etc., to prevent further code execution after a response has been dispatched. Implement robust error handling.
Check server logs for more specific error messages from individual hooks, models, or configurations. Verify database connection settings in `config/datastores.js` and model definitions for correctness. Review the `config/bootstrap.js` file for any problematic logic. Incrementally comment out hooks or custom logic to isolate the problematic component.
Before creating or updating, perform a check to see if a record with the unique value already exists. Handle the `E_UNIQUE` error specifically in `try/catch` blocks or promise `.catch()` handlers, providing appropriate feedback to the user or logic to resolve the conflict.