sails.js Hello world


Example

This example shows how to develop our first application step by step, assuming you already have Sails installed and a project created.

  1. Create an empty controller file by typing
$ sails generate controller hello
  1. Find the new controller file at api/controllers/HelloControllers.js and add the hello method to it.
module.exports = {

  hello : function (req, res) {
    var myName = 'Luis';          
    return res.view('hello' , {name : myName});
    }
}
  1. Create a new view file under the folder views named hello.ejs with the following HTML:
<html>
    <head></head>
    <body>
        <p>Hello {{}}.</p>
    </body>
</html>
  1. Define a route in config/routes.js that calls the hello method in the HelloController controller.
'GET /' : 'HelloController.hello',

Now we have implemented all the code needed for this example. Let's try it:

  1. Start the server
$ sails lift
  1. Open the browser and type http://localhost:1337. If it's not coming up, check the URL in the sails lift output. The port may be different.

  2. You should see the following output:

    Hello Luis