Teakit supports Life Cycle events, which trigger at specific moments.
A Middleware or Hook acts as an event listener, allowing you to "hook" into these events. This Hook capability enables you to modify data as it moves through the data pipeline.
Whether you aim to implement a custom body parser, generate a tailored response based on your handler, or set up an authentication guard, the Hook empowers you to harness the full potential of Teakit.
The life cycle events in Teakit consists of:
Although the hook is designed to be assignable to many events, not all event is listenable:
The listenable event consists of:
The hook is separated into 2 types:
The global hook can listen to all events in the listenable list, which will be applied to all handlers in scope.
To listen to an event, use .on[EventName](callback)
The local hook can only access event AFTER routing as it's assigned to a specific path handler only.
Which consists of:
To use a local hook, pass it as the third argument in the handler
A local hook can accept an array of functions.
The best practice is to break down a function into a smaller reusable function.