Sails Tutorial — Chapter 1

Aug 18, 2023

We are going to build the sample Sails.js or Sails application from the scratch with some unusual steps (for learning). We will end up with a small, functional CRM application.. Without further ado, let’s get started.

Create a folder with name crm.

mkdir crm

Go inside the created crm folder.

cd crm

Create a package.json file with defaults (-y).

npm init -y

Install the Sails framework (sails) as the dependency in this project.

npm i -E sails

Also install the Nodemon (nodemon) to restart the server on changes as development dependency (-D).

npm i  -E -D nodemon

Go ahead and create a new file with name app.js.

touch app.js

Open this created app.js file and write the following code.

const sails = require("sails");

sails.lift();

We first required the sails package and then called the .lift method. That’s it (in terms of code setup to use the Sails)!

Open package.json file and add two scripts — one to run app.js with node and second to run app.js with nodemon (I will add dev first and then start to write the scripts in alphabetic order).

{
  "name": "crm",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon app.js",
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "sails": "1.5.7"
  },
  "devDependencies": {
    "nodemon": "3.0.1"
  }
}

Also, change the main file from index.js to app.js.

{
  "name": "crm",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "dev": "nodemon app.js",
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "sails": "1.5.7"
  },
  "devDependencies": {
    "nodemon": "3.0.1"
  }
}

In the terminal, run the dev script.

npm run dev

The application is running at http://localhost:1337. Open this URL in the browser and you should see ‘Not Found’ with 404 status code. This is obvious as we haven’t write any code for the root route. But, the important thing is you have the Sails application up and running with just two lines of code in app.js file. Rest of the things were setup!

Take a break and read the second article in this series at this link.

Tags: sails