Reputation: 178
In the feathers generate app
command we have a folder called middleware which is used for adding any middleware functionality to the service, however hooks can achieve the same thing, why is middleware provided? Am I missing something?
Upvotes: 1
Views: 1555
Reputation: 44215
You are correct, hooks can do almost anything an Express middleware can do. The difference is that they are transport independent. This means that hooks will work no matter if you use the service internally, through a websocket (Socket.io), HTTP (Express) or any other kind of connection. I wrote more about the benefits of this transport independent design in Design patterns for modern web APIs.
Feathers is fully compatible with Express which means that normal middleware is still available to you but most Feathers apps have only very few or usually no middleware at all. The reason why you would still use Express middleware (which does not run when using a Socket.io connection) is to convert from and to things that are specific to the HTTP request and responses. That way services and hooks can process it in a way where they don't have to know about where this information is coming from. Some examples are
For anything else you can and should use a hook. That way your application will stay future proof and you won't have to change anything when moving to a different transport mechanism.
Upvotes: 6