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.