Server-Hooks in Adonis V6 | Running actions after response globally/for all endpoints #4873
-
Hey Team, I have the usecase/requirement that I need to log certain infos about every request that reached the server, both successful and failed ones.
In Adonis V5, i used the
function to get access to the data after the response was sent to the client, which worked great. Apparently the Server-Hooks don't exist anymore though in Adonis V6. I did find this section in the Docs about running actions after the response finishes (https://docs.adonisjs.com/guides/basics/response#running-actions-after-response-finishes), which could be partly suitable for my usecase. However the docs make it seem that i would need to include that snippet into every single endpoint, which would certainly lead to errors down the line when someone forgets it etc and also leads to code duplication... Are the server hooks really gone in V6? And if thats the case, is it possible to use the "response.onFinish()" method globally somehow for all Endpoints without having to write it into each single one? Thanks a lot |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can do it via middleware. https://docs.adonisjs.com/guides/basics/middleware#mutating-response-from-a-middleware |
Beta Was this translation helpful? Give feedback.
-
Ahhh ok, thanks:) If someone else stumbles over it in the future: the status code is accurate in the |
Beta Was this translation helpful? Give feedback.
You can do it via middleware. https://docs.adonisjs.com/guides/basics/middleware#mutating-response-from-a-middleware