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
- Add an
index.jsfile to that folder.
- Write your hook code in
index.jsin accordance with the hook specification.
Your new folder may contain other files as well, which can be loaded in your hook via
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
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.
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.
- Custom Responses
- Extending Sails
- File Uploads
- Models and ORM
We wrote a book!
Get early access to the book
with promotion code: mcneildoc