Edit Page

Using Hooks in a Sails App

Using a project hook

To use a project hook in your app, first create the api/hooks folder if it doesn’t already exist. Then create the project hook or copy the folder for the hook you want to use into api/hooks.

Using an installable hook

To use an installable hook in your app, simply run npm install with the package name of the hook you wish to install (e.g. npm install sails-hook-autoreload). You may also manually copy or link an installable hook folder that you've created directly into your app’s node_modules folder.

Calling hook methods

Any methods that a hook exposes are available in the sails.hooks[<hook-name>] object. For example, the sails-hook-email hook provides a sails.hooks.email.send() method (note that the sails-hook- prefix is stripped off). Consult a hook’s documentation to determine which methods it provides.

Configuring a hook

Once you’ve added an installable hook to your app, you can configure it using the regular Sails config files like config/local.js, config/env/development.js, or a custom config file you create yourself. Hook settings are typically namespaced under the hook’s name, with any sails-hook- prefix stripped off. For example, the from setting for sails-hook-email is available as sails.config.email.from. The documentation for the installable hook should describe the available configuration options.

Changing the way Sails loads an installable hook

On rare occassions, you may need to change the name that Sails uses for an installable hook, or change the configuration key that the hook uses. This may be the case if you already have a project hook with the same name as an installable hook, or if you’re already using a configuration key for something else. To avoid these conflicts, Sails provides the sails.config.installedHooks.<hook-identity> configuration option. The hook identity is always the name of the folder that the hook is installed in.

// config/installedHooks.js
module.exports.installedHooks = {
   "sails-hook-email": {
      // load the hook into sails.hooks.emailHook instead of sails.hooks.email
      "name": "emailHook",
      // configure the hook using sails.config.emailSettings instead of sails.config.email
      "configKey": "emailSettings"
   }
};

Note: you may have to create the config/installedHooks.js file yourself.

  • Hooks overview
  • The hook specification
  • Creating a project hook
  • Creating an installable hook

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.