Sails Tutorial — Chapter 2

Kiran Chauhan
2 min readJun 19, 2024

This is the second article in the series. You can read the first one at this link.

Application is up and running at http://localhost:1337 but it throws a 404 error. Let’s fix it by adding a root route. To do so, first create a config folder and then routes.js file within it.

mkdir config
touch config/route.js

Open the config/route.js file and write the following code.

module.exports.routes = {};

It exports the routes object that will have all the routes for the application.

Go ahead define a root route or GET / as follows.

module.exports.routes = {
'GET /': 'HomeController.index',
};

Meaning that when the root route (/) is requested, server run the index action (or method) from the HomeController. But, we don’t have any of this — controller and action.

Go ahead and create a folder with the name api. Within this folder, create a folder with name controllers. In this folder, we’ll put all the application controllers.

mkdir api
mkdir api/controllers

Finally, within this controllers folder, create a file with the name HelloController.js. The name of the file should be the same as the controller name defined in routes.js file.

touch api/controllers/HomeController.js

Open the api/controllers/HomeController.js file and write the following code.

module.exports = {};

It exports the object that will have all the actions we’re interested in calling from this controller.

Sails internally uses Express framework exclusively. So, the actions within the controller are essentially the callback function we usually write in express for a given route with req and res (and next) parameters.

Let’s define an index as a named function that returns a sample JSON response.

module.exports = {
index: (req, res) => {
res.json({ message: 'Home page' });
},
};

That’s it! Reload (or open) the http://localhost:1337 in the browser and you should see this JSON response instead of the 404 error page.

Before I complete this article, I want to mention a few more points.

  1. When defining a route, always put at least one space between verb and path e.g. GET / Because, Sails will parse the key using space RegEx.
  2. Name of the controller should be in PascalCase with Controller.js as a suffix. Usually, the name of the controller should be plural.
  3. You’re free to give any name you like for the actions within the controller, but it is good to follow conventions such as index for listing, show for displaying single entity, new to create a new entity and so on. Although Sails has its own suggestion on this, I still like and follow Rails conventions.
  4. In Sails documentation, you’ll see view, action and so on within object against the routes. They’re shortcuts when you don’t have to create a controller and/or action to just render the view. It is useful. But, I’m not going to cover it!
  5. You can use all the methods of req and res from Express and certainly we’re going to use it. Sails also added a few more useful methods that we’ll use in upcoming chapters.

Take a break and read the third 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