Hooks

Hooks are pluggable middleware functions that can be registered before, after or on errors of a service method. You can register a single hook function or create a chain of them to create complex work-flows. Most of the time multiple hooks are registered so the examples show the “hook chain” array style registration.

A hook is transport independent, which means it does not matter if it has been called through HTTP(S) (REST), Socket.io, Primus or any other transport Feathers may support in the future. They are also service agnostic, meaning they can be used with ​any​ service regardless of whether they have a model or not.

Hooks are commonly used to handle things like validation, logging, populating related entities, sending notifications and more. This pattern keeps your application logic flexible, composable, and much easier to trace through and debug. For more information about the design patterns behind hooks see this blog post.

Quick Example

The following example adds a createdAt and updatedAt property before saving the data to the database and logs any errors on the service:

  1. const feathers = require('@feathersjs/feathers');
  2. const app = feathers();
  3. app.service('messages').hooks({
  4. before: {
  5. create(context) {
  6. context.data.createdAt = new Date();
  7. },
  8. update(context) {
  9. context.data.updatedAt = new Date();
  10. },
  11. patch(context) {
  12. context.data.updatedAt = new Date();
  13. }
  14. },
  15. error(context) {
  16. console.error(`Error in ${context.path} calling ${context.method} method`, context.error);
  17. }
  18. });

Hook functions

A hook function can be a normal or async function or arrow function that takes the hook context as the parameter and can

  • return a context object
  • return nothing (undefined)
  • return feathers.SKIP to skip all further hooks
  • throw an error
  • for asynchronous operations return a Promise that
    • resolves with a context object
    • resolves with undefined
    • rejects with an error

For more information see the hook flow and asynchronous hooks section.

  1. // normal hook function
  2. function(context) {
  3. return context;
  4. }
  5. // asynchronous hook function with promise
  6. function(context) {
  7. return Promise.resolve(context);
  8. }
  9. // async hook function
  10. async function(context) {
  11. return context;
  12. }
  13. // normal arrow function
  14. context => {
  15. return context;
  16. }
  17. // asynchronous arrow function with promise
  18. context => {
  19. return Promise.resolve(context);
  20. }
  21. // async arrow function
  22. async context => {
  23. return context;
  24. }
  25. // skip further hooks
  26. const feathers = require('@feathersjs/feathers');
  27. async context => {
  28. return feathers.SKIP;
  29. }

Hook context

The hook context is passed to a hook function and contains information about the service method call. It has read only properties that should not be modified and writeable properties that can be changed for subsequent hooks.

Pro Tip: The context object is the same throughout a service method call so it is possible to add properties and use them in other hooks at a later time.

context.app

context.app is a read only property that contains the Feathers application object. This can be used to retrieve other services (via context.app.service('name')) or configuration values.

context.service

context.service is a read only property and contains the service this hook currently runs on.

context.path

context.path is a read only property and contains the service name (or path) without leading or trailing slashes.

context.method

context.method is a read only property with the name of the service method (one of find, get, create, update, patch, remove).

context.type

context.type is a read only property with the hook type (one of before, after or error).

context.params

context.params is a writeable property that contains the service method parameters (including params.query). For more information see the service params documentation.

context.id

context.id is a writeable property and the id for a get, remove, update and patch service method call. For remove, update and patch context.id can also be null when modifying multiple entries. In all other cases it will be undefined.

Note: context.id is only available for method types get, remove, update and patch.

context.data

context.data is a writeable property containing the data of a create, update and patch service method call.

Note: context.data will only be available for method types create, update and patch.

context.error

context.error is a writeable property with the error object that was thrown in a failed method call. It is only available in error hooks.

Note: context.error will only be available if context.type is error.

context.result

context.result is a writeable property containing the result of the successful service method call. It is only available in after hooks. context.result can also be set in

  • A before hook to skip the actual service method (database) call
  • An error hook to swallow the error and return a result instead

Note: context.result will only be available if context.type is after or if context.result has been set.

context.dispatch

context.dispatch is a writeable, optional property and contains a “safe” version of the data that should be sent to any client. If context.dispatch has not been set context.result will be sent to the client instead.

Note: context.dispatch only affects the data sent through a Feathers Transport like REST or Socket.io. An internal method call will still get the data set in context.result.

context.statusCode

context.statusCode is a writeable, optional property that allows to override the standard HTTP status code that should be returned.

Hook flow

In general, hooks are executed in the order they are registered with the original service method being called after all before hooks. This flow can be affected as follows.

Throwing an error

When an error is thrown (or the promise is rejected), all subsequent hooks - and the service method call if it didn’t run already - will be skipped and only the error hooks will run.

The following example throws an error when the text for creating a new message is empty. You can also create very similar hooks to use your Node validation library of choice.

  1. app.service('messages').hooks({
  2. before: {
  3. create: [
  4. function(context) {
  5. if(context.data.text.trim() === '') {
  6. throw new Error('Message text can not be empty');
  7. }
  8. }
  9. ]
  10. }
  11. });

Setting context.result

When context.result is set in a before hook, the original service method call will be skipped. All other hooks will still execute in their normal order. The following example always returns the currently authenticated user instead of the actual user for all get method calls:

  1. app.service('users').hooks({
  2. before: {
  3. get: [
  4. function(context) {
  5. // Never call the actual users service
  6. // just use the authenticated user
  7. context.result = context.params.user;
  8. }
  9. ]
  10. }
  11. });

Returning feathers.SKIP

require('@feathersjs/feathers').SKIP can be returned from a hook to indicate that all following hooks should be skipped. If returned by a before hook, the remaining before hooks are skipped; any after hooks will still be run. If it hasn’t run yet, the service method will still be called unless context.result is set already.

Asynchronous hooks

When the hook function is async or a Promise is returned it will wait until all asynchronous operations resolve or reject before continuing to the next hook.

Important: As stated in the hook functions section the promise has to either resolve with the context object (usually done with .then(() => context) at the end of the promise chain) or with undefined.

async/await

When using Node v8.0.0 or later the use of async/await is highly recommended. This will avoid many common issues when using Promises and asynchronous hook flows. Any hook function can be async in which case it will wait until all await operations are completed. Just like a normal hook it should return the context object or undefined.

The following example shows an async/await hook that uses another service to retrieve and populate the messages user when getting a single message:

  1. app.service('messages').hooks({
  2. after: {
  3. get: [
  4. async function(context) {
  5. const userId = context.result.userId;
  6. // Since context.app.service('users').get returns a promise we can `await` it
  7. const user = await context.app.service('users').get(userId);
  8. // Update the result (the message)
  9. context.result.user = user;
  10. // Returning will resolve the promise with the `context` object
  11. return context;
  12. }
  13. ]
  14. }
  15. });

Returning promises

The following example shows an asynchronous hook that uses another service to retrieve and populate the messages user when getting a single message.

  1. app.service('messages').hooks({
  2. after: {
  3. get: [
  4. function(context) {
  5. const userId = context.result.userId;
  6. // context.app.service('users').get returns a Promise already
  7. return context.app.service('users').get(userId).then(user => {
  8. // Update the result (the message)
  9. context.result.user = user;
  10. // Returning will resolve the promise with the `context` object
  11. return context;
  12. });
  13. }
  14. ]
  15. }
  16. });

Note: A common issue when hooks are not running in the expected order is a missing return statement of a promise at the top level of the hook function.

Important: Most Feathers service calls and newer Node packages already return Promises. They can be returned and chained directly. There is no need to instantiate your own new Promise instance in those cases.

Converting callbacks

When the asynchronous operation is using a callback instead of returning a promise you have to create and return a new Promise (new Promise((resolve, reject) => {})) or use util.promisify.

The following example reads a JSON file converting fs.readFile with util.promisify:

  1. const fs = require('fs');
  2. const util = require('util');
  3. const readFile = util.promisify(fs.readFile);
  4. app.service('messages').hooks({
  5. after: {
  6. get: [
  7. function(context) {
  8. return readFile('./myfile.json').then(data => {
  9. context.result.myFile = data.toString();
  10. return context;
  11. });
  12. }
  13. ]
  14. }
  15. });

Pro Tip: Other tools like Bluebird also help converting between callbacks and promises.

Registering hooks

Hook functions are registered on a service through the app.service(<servicename>).hooks(hooks) method. There are several options for what can be passed as hooks:

  1. // The standard all at once way (also used by the generator)
  2. // an array of functions per service method name (and for `all` methods)
  3. app.service('servicename').hooks({
  4. before: {
  5. all: [
  6. // Use normal functions
  7. function(context) { console.log('before all hook ran'); }
  8. ],
  9. find: [
  10. // Use ES6 arrow functions
  11. context => console.log('before find hook 1 ran'),
  12. context => console.log('before find hook 2 ran')
  13. ],
  14. get: [ /* other hook functions here */ ],
  15. create: [],
  16. update: [],
  17. patch: [],
  18. remove: []
  19. },
  20. after: {
  21. all: [],
  22. find: [],
  23. get: [],
  24. create: [],
  25. update: [],
  26. patch: [],
  27. remove: []
  28. },
  29. error: {
  30. all: [],
  31. find: [],
  32. get: [],
  33. create: [],
  34. update: [],
  35. patch: [],
  36. remove: []
  37. }
  38. });
  39. // Register a single hook before, after and on error for all methods
  40. app.service('servicename').hooks({
  41. before(context) {
  42. console.log('before all hook ran');
  43. },
  44. after(context) {
  45. console.log('after all hook ran');
  46. },
  47. error(context) {
  48. console.log('error all hook ran');
  49. }
  50. });

Pro Tip: When using the full object, all is a special keyword meaning this hook will run for all methods. all hooks will be registered before other method specific hooks.

Pro Tip: app.service(<servicename>).hooks(hooks) can be called multiple times and the hooks will be registered in that order. Normally all hooks should be registered at once however to see at a glance what the service is going to do.

Application hooks

To add hooks to every service app.hooks(hooks) can be used. Application hooks are registered in the same format as service hooks and also work exactly the same. Note when application hooks will be executed however:

  • before application hooks will always run before all service before hooks
  • after application hooks will always run after all service after hooks
  • error application hooks will always run after all service error hooks

Here is an example for a very useful application hook that logs every service method error with the service and method name as well as the error stack.

  1. app.hooks({
  2. error(context) {
  3. console.error(`Error in '${context.path}' service method '${context.method}'`, context.error.stack);
  4. }
  5. });