Edit Page

Routing to Controllers

By default, Sails will create a blueprint action route for each action in a controller, so that a GET request to /:controllerIdentity/:nameOfAction will trigger the action. If the example controller in the previous section was saved as api/controllers/SayController.js, then the /say/hi and /say/bye routes would be made available by default whenever the app was lifted. If the controller was saved under the subfolder /we, then the routes would be /we/say/hi and /we/say/bye. See the blueprints documentation for more information about Sails’ automatic route binding.

Besides the default routing, Sails allows you to manually bind routes to controller actions using the config/routes.js file. Some examples of when you might want to use explicit routes are:

  • When you want to use separate actions to handle the same route path, based on the HTTP method (aka verb). The aforementioned action blueprint routes bind all request methods for a path to a given action, including GET, POST, PUT, DELETE, etc.
  • When you want an action to be available at a custom URL (e.g. PUT /login, POST /signup, or a "vanity URL" like GET /:username)
  • When you want to set up additional options for how the route should be handled (e.g. special CORS configuration)

To manually bind a route to a controller action in the config/routes.js file, you can use the HTTP verb and path (i.e. the route address) as the key, and the controller name + . + action name as the value (i.e. the route target).

For example, the following manual route will cause your app to trigger the makeIt() action in api/controllers/SandwichController.js whenever it receives a POST request to /make/a/sandwich:

'POST /make/a/sandwich': 'SandwichController.makeIt'

Note:

For controller files saved in subfolders, the subfolder is part of the controller identity:

'/do/homework': 'stuff/things/HomeworkController.do'

This will cause the do() action in api/controllers/stuff/things/HomeworkController.js to be triggered whenever /do/homework is requested.

A full discussion of manual routing is out of the scope of this doc--please see the routes documentation for a full overview of the available options.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Concepts

  • Assets
    • Default Tasks
    • Disabling Grunt
    • Task Automation
  • Blueprints
    • Blueprint Actions
    • Blueprint Routes
  • Configuration
    • The local.js file
    • Using `.sailsrc` Files
  • Controllers
    • Generating Controllers
    • Routing to Controllers
  • Custom Responses
    • Adding a Custom Response
    • Default Responses
  • Deployment
    • FAQ
    • Hosting
    • Scaling
  • Extending Sails
    • Adapters
      • Available Adapters
      • Custom Adapters
    • Generators
      • Available Generators
      • Custom Generators
    • Hooks
      • Hook Specification
        • .configure()
        • .defaults
        • .initialize()
        • .routes
      • Installable Hooks
      • Project Hooks
      • Using Hooks
  • File Uploads
    • Uploading to GridFS
    • Uploading to S3
  • Globals
    • Disabling Globals
  • Internationalization
    • Locales
    • Translating Dynamic Content
  • Logging
    • Custom log messages
  • Middleware
    • Conventional Defaults
  • Models and ORM
    • Associations
      • Dominance
      • Many-to-Many
      • One Way Association
      • One-to-Many
      • One-to-One
      • Through Associations
    • Attributes
    • Lifecycle callbacks
    • Model Settings
    • Models
    • Query Language
    • Validations
  • Policies
    • Sails + Passport
  • Programmatic Usage
    • Tips and Tricks
  • Realtime
    • Multi-server environments
    • On the client
    • On the server
  • Routes
    • Custom Routes
    • URL Slugs
  • Security
    • Clickjacking
    • Content Security Policy
    • CORS
    • CSRF
    • DDOS
    • P3P
    • Socket Hijacking
    • Strict Transport Security
    • XSS
  • Services
    • Creating a Service
  • Sessions
  • Testing
  • Views
    • Layouts
    • Locals
    • Partials
    • View Engines

Built with Love

The Sails framework is maintained by a web & mobile studio in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Treeline IDE
  • Contribute
  • Logos/artwork
About:
  • The Sails Company
  • Security
  • News
  • Legal
Help:
  • Get started
  • Documentation
  • Docs
  • Enterprise
  • Hire us

© 2012-2018 The Sails Company. 
The Sails framework is free and open-source under the MIT License.