Project hooks are custom Sails hooks that reside in an application’s api/hooks
folder. They are typically useful when you want to take advantage of hook features like defaults and routes for code that is used by multiple components in a single app. If you wish to re-use a hook in more than one Sails app, see creating an installable hook instead.
To create a new project hook:
api/hooks
folder.index.js
file to that folder.index.js
in accordance with the hook specification.Your new folder may contain other files as well, which can be loaded in your hook via require
; only index.js
will be read automatically by Sails.
As an alternative to a folder, you may create a file in your app’s api/hooks
folder like api/hooks/myProjectHook.js
.
To test that your hook is being loaded by Sails, lift your app with sails lift --verbose
. If your hook is loaded, you will see a message like:
verbose: your-hook-name hook loaded successfully.
in the logs.