Do I need the Nitro server directory in nxut3 for direct server api-call in ssr front-end app?

43 views Asked by At

I need to have a wrapper around my API calls in nuxt3 app. our backend has micro-service architecture and our URLs are like this:

`${microservice-domain}/api/${entity}/${route}` // car/api/report/topModels

and I need to have a specific config for each microservice and each kind of method(POST, GET, ...).

Now do I have to write my API calls in the server directory? does it send my request to the nitro server and then in the nitro server I have to call my backend API via $fetch? is this best practice?

I have another idea to write a wrapper function to handle all these different routes and then I call them inside of this function and return it to my pages and components.

Can somebody help me to do the best practice?

1

There are 1 answers

0
Ellrohir On

Personally I wouldn't introduce Nuxt server routes just to re-fetch from (open) external sources. You can easilly do this directly in dedicated client side composables, typically Pinia stores. I find server routes useful when I want to leverage some server-side-only features or to expose my own data endpoints externally. And last but not least when there are connection secrets (passwords, API keys) not to be exposed publicly. Then doing it server-side is a must. But many integrations like Supabase only require public key, so it is not always a concern.