Skip to main content

Sails.js Overview

Sails.js: An Introduction, History, Features, and Examples

Introduction

Sails.js is a modern, MVC (Model-View-Controller) framework for building scalable and real-time web applications using Node.js. It follows the convention-over-configuration philosophy, making it easy to develop and maintain applications without spending too much time on configuration. Sails.js is built on top of Express.js and incorporates features like data-driven APIs, real-time updates, and a powerful command-line interface.

History

Sails.js was developed by Mike McNeil and initially released in 2012. It was inspired by Ruby on Rails and aimed to bring similar productivity and ease of development to the Node.js ecosystem. Since its release, Sails.js has gained popularity among developers due to its robustness, scalability, and the ability to build real-time applications.

Features

1. MVC Architecture

Sails.js follows the MVC architectural pattern, which helps in organizing the codebase and separating concerns. The Model represents the data and business logic, the View handles the presentation layer, and the Controller manages the interaction between the Model and the View.

// Example: UserController.js
module.exports = {
find: async (req, res) => {
const users = await User.find();
return res.view('user/index', { users });
},

create: async (req, res) => {
const user = await User.create(req.body).fetch();
return res.created(user);
},
};

2. Real-Time Capabilities

Sails.js provides real-time capabilities out-of-the-box through the use of WebSockets and Socket.io. This allows developers to build applications that can push updates to connected clients in real-time.

// Example: ChatController.js
module.exports = {
subscribeToRoom: async (req, res) => {
const roomId = req.param('roomId');
sails.sockets.join(req, roomId);
return res.ok({ message: 'Subscribed to room.' });
},

sendMessage: async (req, res) => {
const roomId = req.param('roomId');
const message = req.param('message');
sails.sockets.broadcast(roomId, 'newMessage', { message });
return res.ok({ message: 'Message sent.' });
},
};

3. Blueprint API

Sails.js provides a powerful blueprint API that automatically generates RESTful routes and CRUD actions for your models. This makes it easy to create, read, update, and delete data without writing any additional code.

// Example: UserController.js
module.exports = {
// Automatically generated routes
create: async (req, res) => {
const user = await User.create(req.body).fetch();
return res.created(user);
},

find: async (req, res) => {
const users = await User.find();
return res.ok(users);
},

// Additional custom action
search: async (req, res) => {
const query = req.param('query');
const users = await User.search(query);
return res.ok(users);
},
};

4. Command-Line Interface (CLI)

Sails.js provides a powerful command-line interface (CLI) that helps with scaffolding, generating code, and managing the project. The CLI provides commands to generate controllers, models, services, and much more, improving developer productivity.

# Example: Generate a new Sails.js project
$ sails new myproject

# Example: Generate a new controller
$ sails generate controller user

# Example: Generate a new model
$ sails generate model user

Examples

Example 1: Real-Time Chat Application

// Example: ChatController.js
module.exports = {
subscribeToRoom: async (req, res) => {
const roomId = req.param('roomId');
sails.sockets.join(req, roomId);
return res.ok({ message: 'Subscribed to room.' });
},

sendMessage: async (req, res) => {
const roomId = req.param('roomId');
const message = req.param('message');
sails.sockets.broadcast(roomId, 'newMessage', { message });
return res.ok({ message: 'Message sent.' });
},
};

In this example, the subscribeToRoom action allows a user to subscribe to a chat room using WebSockets. The sendMessage action broadcasts a new message to all connected clients in the specified room.

Example 2: Creating a RESTful API

// Example: UserController.js
module.exports = {
create: async (req, res) => {
const user = await User.create(req.body).fetch();
return res.created(user);
},

find: async (req, res) => {
const users = await User.find();
return res.ok(users);
},
};

In this example, the create action creates a new user record in the database based on the request body. The find action retrieves all the user records from the database and returns them as a response.

For more information and detailed documentation, please visit the official website: Sails.js