Send non blocking call to another API after serving an api request in Nuxt3?

167 views Asked by At

I want to make a request to a logging service after serving the request. For eg you hit an endpoint '/hello' i want to increment count in redis but after serving the request. Something similar to res.on('final') in express.js.

I tried searching the docs to something similar to res.on('final') but can't find any solution.

1

There are 1 answers

1
devnik On

Nuxt is usinig ofetch library. You can add a callback after the call is done and response is parsed https://github.com/unjs/ofetch#onresponse-request-options-response-

Also usable within useFetch() wrapper see in examples https://nuxt.com/docs/api/composables/use-fetch#example

If you consider using $fetch or any composable as wrapper like useFetch() consider this

We recommend to use useFetch or useAsyncData + $fetch to prevent double data fetching when fetching the component data.

https://nuxt.com/docs/api/utils/dollarfetch