Edit Page

URL slugs

A common use case for explicit routes is the design of slugs or vanity URLs. For example, consider the URL of a repository on Github, http://www.github.com/balderdashy/sails. In Sails, we might define this route at the bottom of our config/routes.js file like so:

'get /:account/:repo': {
  controller: 'RepoController',
  action: 'show',
  skipAssets: true
}

In your RepoController's show action, we'd use req.param('account') and req.param('repo') to look up the data for the appropriate repository, then pass it in to the appropriate view as locals. The skipAssets option ensures that the vanity route doesn't accidentally match any of our assets (e.g. /images/logo.png), so they are still accessible.

Slugs that might contain slashes

There is one particular case where the simple implementation of URL pattern variables (e.g. :foo) is not enough.

If your application will tolerate slugs containing unescaped forward slash (/) characters, then instead of addressing the dynamic parts of your route address path using pattern variables like :foo, you will need to use a URL wildcard suffix (*).

For example:

'get /admin/email-template-previews/*': {
  action: 'admin/view-email-template-preview',
  skipAssets: true
}

To receive the runtime value corresponding with this wildcard (*) in a modern Sails action, use urlWildcardSuffix at the top level of your action definition to indicate the name of the input you would like to use to represent the dynamic value:

urlWildcardSuffix: 'template',
inputs: {
  template: {
    description: 'The relative path to an EJS template within our `views/emails/` folder -- WITHOUT the file extension.',
    extendedDescription: 'Use strings like "foo" or "foo/bar", but NEVER "foo/bar.ejs" or "/foo/bar".  For example, '+
      '"internal/email-contact-form" would send an email using the "views/emails/internal/email-contact-form.ejs" template.',
    example: 'email-reset-password',
    type: 'string',
    required: true
  },
},
fn: async function({ template }) {
  // …
}

Notes

  • Alternatively, in a classic (req,res) action, you can use req.param('0') to access the dynamic value of a route's URL wildcard suffix (*).
  • For more background, see https://www.npmjs.com/package/machine-as-action

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 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.

Sailsconf 2022 June 22 - 24: Learn more at the Sailsconf website

Tweet Follow @sailsjs

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Concepts

  • Actions and controllers
    • Generating actions and controllers
    • Routing to actions
  • Assets
    • Default tasks
    • Disabling Grunt
    • Task automation
  • Blueprints
    • Blueprint actions
    • Blueprint routes
  • Configuration
    • The local.js file
    • Using `.sailsrc` files
  • Deployment
    • FAQ
    • Hosting
    • Scaling
  • E-commerce
  • Extending Sails
    • Adapters
      • Available adapters
      • Custom adapters
    • Custom responses
      • Adding a custom response
    • Generators
      • Available generators
      • Custom generators
    • Hooks
      • Available hooks
      • Events
      • Hook specification
        • .configure
        • .defaults
        • .initialize()
        • .registerActions()
        • .routes
      • Installable hooks
      • Project hooks
      • Using hooks
  • File uploads
    • Uploading to GridFS
    • Uploading to S3
  • Globals
    • Disabling globals
  • Helpers
    • Example helper
  • Internationalization
    • Locales
    • Translating dynamic content
  • Logging
    • Custom log messages
  • Middleware
    • Conventional defaults
  • Models and ORM
    • Associations
      • Many-to-many
      • One way association
      • One-to-many
      • One-to-one
      • Reflexive associations
      • Through associations
    • Attributes
    • Errors
    • Lifecycle callbacks
    • Model settings
    • Models
    • Query language
    • Records
    • Standalone Waterline usage
    • Validations
  • Policies
    • Access Control and Permissions
  • 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
  • Sessions
  • Shell scripts
  • Testing
  • Views
    • Layouts
    • Locals
    • Partials
    • View engines

Built with Love

The Sails framework is built by a web & mobile shop 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?
  • Community
  • News
  • For business
About:
  • Our company
  • Security
  • Legal
  • Logos/artwork
Help:
  • Get started
  • Documentation
  • Docs
  • Contribute
  • Take a class

© 2012-2021 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.