Edit Page

Hooks

Status

Stability: 3 - Stable

What is a hook?

A hook is a Node module that adds functionality to the Sails core. The hook specification defines the requirements a module must meet for Sails to be able to import its code and make the new functionality available. Because they can be saved separately from the core, hooks allow Sails code to be shared between apps and developers without having to modify the framework.

Hooks vs. Services

Hooks share some common features with Sails services. They both allow developers to store commonly used code in one location, and they both make new methods available globally to a Sails app. However, there are some key differences between the two concepts:

  • Services cannot be saved independently of an app. While some types of hooks may be tied to a single app (see Project Hooks), other types can be developed independently of a Sails app and installed using npm install.
  • Hooks have their own initialization system. This allows them to be more dynamic and configure themselves when Sails lifts.
  • Hooks can add new routes to a Sails app before it lifts.

Services are still a good choice for code that is shared between multiple controllers or models in an app, but

  • is unlikely to be reused in another app
  • won't need to behave differently in different environments (e.g. development vs. production)

For all other reusable code, hooks are the way to go!

Types of hooks

There are three types of hooks available in Sails:

  1. Core hooks. These hooks provide many of the common features essential to a Sails app, such as request handling, blueprint route creation, and database integration via Waterline. Core hooks are bundled with the Sails core and are thus available to every app. You will rarely have a need to call core hook methods in your code.
  2. Project hooks. These are hooks that live in the api/hooks folder of a Sails app. Project hooks provide a way to take advantage of the features of the hook system for code that doesn’t need to be shared between apps.
  3. Installable hooks. These hooks are installed into an app’s node_modules folder using npm install. Installable hooks allow developers in the Sails community to create “plug-in”-like modules for use in Sails apps.

Read more

  • Using hooks in your app
  • 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.