user28435498
user28435498

Reputation: 1

How to get the Full Host in Server Api Nuxt 3?

I need to get the Full Host, I am creating an API endpoint at ~/server/api/test but with event on defineEventHandler is just giving me for example only localhost host but without port.

I am trying with getRequestURL(event) too but same issue.

I need this because I am creating a wrapper for fetch and I need to send full host to an external multitenant API.

Can you help me how to get Full Host ?.

Previously I have Tried:

const { host } = getRequestURL(event) console.log(host)

Result: localhost

Expecting: localhost:3000 or domain.test

Upvotes: 0

Views: 42

Answers (1)

user28435498
user28435498

Reputation: 1

I found a solution:

Create composable: /composables/getHost.ts

So I can use useRequestURL() SSR.

export const useHost = () => { const { host } = useRequestURL() return host }

So in my component or page or store I can use useFetch()or other data fetching and call to my server endpoint /api/test as "post" method and send body with "host" value from my composable.

So now I can use the host in /server/api/test.post.ts to get data from external api.

I don't know if is a good approach but is it working at the moment.

Upvotes: 0

Related Questions