How to use the command 'sails' (with examples)
Sails.js is a realtime enterprise level MVC framework built on top of Node.js. It provides a command-line interface (CLI) tool called ‘sails’ that allows developers to perform various actions such as starting a Sails application, creating a new Sails project, and generating Sails APIs, controllers, and models.
Use case 1: Start Sails
Code:
sails lift
Motivation: Starting a Sails application is one of the fundamental actions when working with Sails.js. This command is used to lift the Sails application and start the server, making it accessible for testing and development.
Explanation:
The ‘sails lift’ command starts the Sails application by lifting the server. It uses the default configuration files (e.g., config/env/development.js
) to determine the server settings.
Example OUTPUT:
Starting app...
Starting server in development environment...
...
Sails application launched in localhost:1337.
Use case 2: Create new Sails project
Code:
sails new projectName
Motivation: Creating a new Sails project is the first step before starting the development process. This command sets up the basic structure of a Sails project with predefined folders and files.
Explanation: The ‘sails new projectName’ command creates a new Sails project with the provided project name. It generates the necessary files and folders, including configuration files, controllers, models, views, and assets, to get started with developing a Sails application.
Example OUTPUT:
Using disk adapter (default) for new project...
Creating Sails app at: ~/projects/projectName
...
Done. Created new Sails app 'projectName' in directory: ~/projects/projectName
Use case 3: Generate Sails API
Code:
sails generate api name
Motivation: Generating a Sails API allows developers to quickly scaffold the required components (e.g., controllers, models, etc.) of an API-based module in a Sails application. This saves time and effort by automatically generating the boilerplate code.
Explanation: The ‘sails generate api name’ command generates the components necessary for an API module with the provided name. This includes creating a controller and a model with associated routes and actions, following the conventions of Sails.js.
Example OUTPUT:
Generating a new API named 'name'...
Created `api/controllers/NameController.js`
Created `api/models/Name.js`
Updated `config/routes.js`
Use case 4: Generate Sails Controller
Code:
sails generate controller name
Motivation: Generating a Sails controller helps in structuring the application’s logic by separating the handling of HTTP requests from business logic. This command automates the creation of a controller file with predefined methods.
Explanation:
The ‘sails generate controller name’ command generates a controller file with the provided name in the api/controllers
directory. The generated controller file includes template methods such as find
, create
, update
, destroy
, etc., which can be customized as per the application’s requirements.
Example OUTPUT:
Generating a new controller named 'name'...
Created `api/controllers/NameController.js`
Use case 5: Generate Sails Model
Code:
sails generate model name
Motivation: Generating a Sails model helps in defining the structure and behavior of the data that the application interacts with. This command automates the creation of a model file with the necessary attributes and associations.
Explanation:
The ‘sails generate model name’ command generates a model file with the provided name in the api/models
directory. The generated model file includes attributes, associations, and validation rules that define the schema of the data managed by the Sails application.
Example OUTPUT:
Generating a new model named 'name'...
Created `api/models/Name.js`
Conclusion:
The ‘sails’ command-line interface (CLI) tool is a powerful utility that simplifies the development process when working with Sails.js. By understanding and utilizing the various use cases, developers can efficiently create and manage Sails applications, improving productivity and reducing boilerplate code.