Creating a Project Hook
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:
- Choose a name for your new hook. It must not conflict with any of the core hook names.
- Create a folder with that name in your app’s
api/hooks
folder. - Add an
index.js
file to that folder. - Write your hook code in
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
.
Testing that your hook loads properly
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.