8000 feathers-docs/hooks at master · rayroad/feathers-docs · GitHub
[go: up one dir, main page]

Skip to content

Directory actions

More options

Directory actions

More options

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

readme.md

Feathers Hooks

feathers-hooks is a powerful plugin that allows to register composable middleware functions before or after a Feathers service method executes. This makes it easy to decouple things like authorization, data pre- or post processing or sending notifications like emails or text messages after something happened from the actual service logic.

A hook is provider independent which means it does not matter if it has been called through REST, Socket.io, Primus or any other provider Feathers may support in the future.

If you would like to learn more about the design patterns behind hooks read up in API service composition with hooks. In this chapter we will look at the usage of hooks, some examples and the built-in hooks.

Getting Started

To install from NPM run:

$ npm install feathers-hooks

Then, to use the plugin in a Feathers app:

const feathers = require('feathers');
const hooks = require('feathers-hooks');

const app = feathers().configure(hooks());

Now we can register a hook for a service:

const service = require('feathers-memory');

// Initialize our service
app.use('/users', service());

// Get our initialized service to that we can bind hooks
const userService = app.service('/users');

// Set up our before hook
userService.before({
  find(hook) {
    console.log('My custom hook ran');
  }
});
0