Skip to content

Routing ​

Defining routes ​

In The Boring JavaScript Stack, all of your application's routes are defined server-side. This means that you don't need Vue Router or React Router.

Instead, you can simply define Sails routes and return an Inertia responses for SPA or return a server-side view for server-side rendering from those routes.

For example let's say we want to create a /users route, we can define the route in config/routes.js:

js
module.exports.routes = {
  'GET /users': 'user/view-users'
}

The above route definition means that when a user requests the /users page of your app, the view-users action of the user controller will handle that request. Let's implement user/view-users.

TIP

Run npx sails generate action user/view-users to scaffold the action.

js
module.exports = {
  inputs: {},
  exits: {
    success: {
      responseType: 'inertia'
    }
  },
  fn: async function (inputs) {
    const users = await User.find()
    return { page: 'users/index', props: { users } }
  }
}

INFO

Note the responseType: 'inertia' in the success exit.

All open source projects are released under the MIT License.