Runtime Hooks
app.hooks.on(name, handler) is used to observe or lightweight patch framework runtime life cycle. It is suitable for cross-cutting logic such as request auditing, request records after verification, response header patch, outbound call monitoring, service call tracking, OpenAPI document patching, etc.
app.hooks.on() returns the logout function. app.hooks is a reserved property and cannot be overridden with app.extend("hooks", ...).
Common scenarios
Only record requests that pass the verification
If you want to log requests in a middleware, but exclude requests that are rejected by parameter validation, there is no need to manually catch VextValidationError. Using validation:success is more straightforward:
Add header before sending response
response:before is a synchronous life cycle and cannot return Promise.
Track service calls
Service hook is also a synchronous life cycle. If you need asynchronous reporting, it is recommended to write to the queue or use log transmission that does not block the main call.
Monitor outbound requests and proxies
Modify OpenAPI documentation
Execution strategy
Available Hooks
More references
app.hooksAPI- [Register runtime hooks in plug-ins](/guide/plugins#apphookson--Register runtime life cycle-hook)
- Fetch / Proxy hooks
- OpenAPI hooks