Sails.js Web Framework

1.5.17 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

```bash
# Install Sails CLI globally
npm install sails -g

# Create a new Sails project
sails new my-sails-app --no-template
cd my-sails-app

# Generate a simple User model
sails generate model User name:string email:string

# Generate a UserController
sails generate controller User --actions 'find,create'

# Lift the Sails application (start the server)
sails lift

# --- Example of programmatic interaction (after lifting) ---
# In a separate script, e.g., `test.js`:
# const Sails = require('sails').constructor;
# const sailsApp = new Sails();
#
# sailsApp.lift({ log: { level: 'warn' } }, async (err) => {
#   if (err) {
#     console.error('Error lifting Sails app:', err);
#     return;
#   }
#   console.log('Sails app lifted successfully!');
#
#   try {
#     // Access a model and create a record
#     const User = sailsApp.models.user; // Access the model instance
#     const newUser = await User.create({
#       name: 'Alice Smith',
#       email: 'alice@example.com'
#     }).fetch();
#     console.log('Created user:', newUser);
#
#     // Find all users
#     const users = await User.find();
#     console.log('All users:', users);
#   } catch (queryErr) {
#     console.error('Database query error:', queryErr);
#   } finally {
#     sailsApp.lower((lowerErr) => {
#       if (lowerErr) console.error('Error lowering Sails app:', lowerErr);
#       else console.log('Sails app lowered.');
#     });
#   }
# });
```

view raw JSON →