Published on

How to create a Sails.Js start app

Authors

Hello folks.

For this post i think doing something different. I will try to write this post in English. Any question, please, chat me. Thanks.

What is Sails.JS?

Sails makes it easy to build custom, enterprise-grade Node.js apps. (http://sailsjs.org/)

First of all, we will need node and npm installed, so, follow this guide to do it: https://nodejs.org/en/download/package-manager/ . Now, we can install Sails.JS:

npm -g install sails

This step maybe need a while.

To verify if Sails is installed, you can do that:

sails --version

If you see something like "0.12.3", Well done, Sails is installed. Next step is create an app, to do this, you must run that:

sails new sailsapp

This command will take a while, because it will create a folder with basic structure of Sails and install your dependencies. When the command ends, you can see follow message and the app folder:

info: Created a new Sails app sailsapp!

In this new folder, will can see the files created by Sails. To run our app, you need to execute this command:

sails lift

This time, the command will be executed fast, so, quickly you can see a message saying something like that:

... debug: Environment : development debug: Port : 1337 ...

The most important part of this message is the Sails port, to check your new app, you just need access this URL:

http://localhost:1337/

Now we can add a simple endpoint just running this command (before run this command, you need stop Sails server with a "Ctrl+C"):

sails generate api Hello

This command will generate two files:

  • HelloController.js (app/controllers)
    • A common empty controller file.
  • Hello.js (api/models/)
    • A common empty model file.

To create a new endpoint, you need to edit the HelloController.js and add the code bellow:

module.exports = {
  action: function (req, res) {
    return res.send('Hello!')
  },
}

In this code, "action" is the name of the endpoint. The endpoint will be composed by: Controller name (without 'Controller') and the action name, in our case, will be:

http://localhost:1337/hello/action

Now you can start Sails again, but this time, it will ask you what to do with Model. To this example, you just need answer "2", then, your app will start normally.

Now, go to

http://localhost:1337/hello/action
, and you will see the "Hello!" message.

For now, this is it! Thank you!

References