Sails Tutorial — Chapter 4

Kiran Chauhan
4 min readJun 24, 2024

This is the fourth article in the series. You can read the third article at this link.

We now have a working CRUD application around the Contact resource. Before we go further and add the database supports, in this article I would like to do some adjustment in the application and talk about a few bits and bytes!

Open the app.js file.

const sails = require('sails');
sails.lift();

This .lift() function accepts an object as an argument that modifies the behavior of the application. For example, I used to work with a web application in Node that usually runs on port 3000. Let’s pass an object to the lift function that changes the port to 3000.

const sails = require('sails');
sails.lift({
port: 3000,
});

Check in the terminal and you should see the URL as http://localhost:3000 printed in the terminal. Also confirm it by opening the URL in the browser.

While we’re looking at the terminal, did you notice the boat or ship in the sea? You can disable this ascii art by marking noShip to true within the log object as follows.

const sails = require('sails');
sails.lift({
port: 3000,
log: {
noShip: true,
},
});

Boat is no longer in the terminal! But, now we’re having the following warning.

debug: Warning: no session secret was set!  In development mode, we'll set this for you,
debug: but session secret must be manually specified in production.
debug: To set up a session secret, add or update it in `config/session.js`:
debug: module.exports.session = { secret: 'extremely-secure-keyboard-cat' }
debug:
debug: (Or if you don't need sessions enabled, disable the hook.)
debug:
debug: For more help:
debug: • https://sailsjs.com/config/session
debug: • https://sailsjs.com/support
debug:

Sails.js is complaining about the session secret that we haven’t set. This console message also guides us on how to resolve this warning. But, I’m planning to disable the session because we’re going to work with API and session is not needed while working with APIs (sessions are useful when dealing with MVC apps).

As mentioned in the console message, we’re going to disable the session within the hooks object.

const sails = require('sails');
sails.lift({
port: 3000,
log: {
noShip: true,
},
hooks: {
session: false,
},
});

The warning is gone! But, now the problem is, nothing is printed in the terminal. We should have some message(s) about the server/process that is running.

This .lift() function optionally accepts the second argument as a callback function and we can write some messages in it.

const sails = require('sails');
sails.lift(
{
port: 3000,
log: {
noShip: true,
},
hooks: {
session: false,
},
},
() => {
console.log('Application is up and running on port 30000');
}
);

Or we can use Sails’ logging as sails.log.info() (or warning(), debug(), etc).

const sails = require('sails');
sails.lift(
{
port: 3000,
log: {
noShip: true,
},
hooks: {
session: false,
},
},
() => {
sails.log.info('Application is up and running on port 30000');
}
);

Notice in the terminal the info: prefix is added before the message and in green color! Let’s add a blank line above and below this message.

const sails = require('sails');
sails.lift(
{
port: 3000,
log: {
noShip: true,
},
hooks: {
session: false,
},
},
() => {
sails.log.info();
sails.log.info('Application is up and running on port 30000');
sails.log.info();
}
);

Looks good! Let’s also print the Sails version that we’re using in this application for the reference.

const sails = require('sails');
sails.lift(
{
port: 3000,
log: {
noShip: true,
},
hooks: {
session: false,
},
},
() => {
sails.log.info();
sails.log.info('Application is up and running on port 30000');
sails.log.info(`Sails.js v${sails.version}`);
sails.log.info();
}
);

Much better!

There are many configuration options we can pass to this lift() function to adjust the application based on our requirements and we’ll use some of the configuration options as we proceed further. Meanwhile you can read about the configuration at this link.

Let’s complete this article by adding a few more points that are good to know.

  1. Do not create the production application using Sails in the way we’ve created in this series. Instead use the sails command to scaffold the application. This series is purely for learning purposes. After we know the basis, I’ll also use scaffolding to build the future applications in this series.
  2. As I mentioned earlier, Sails.js use Express internally. Not just Express but also other popular Express packages such as cookie-parser, ejs, compression, serve-favion, and many more. Some packages are by default there in Sails.js and used internally whereas some packages can be enabled or disabled from configurations. Finally, you can install other Express packages to add more functionalities based on requirements.
  3. There are two major versions of Sails.js — 0.12 and 1.x. I prefer the latest version with old styling, as I’m still figuring out the Node machine concept that is introduced in the 1.x version. I’ll talk about this in future articles.

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kiran Chauhan
Kiran Chauhan

Written by Kiran Chauhan

I design software with and for people.

No responses yet

Write a response