admin管理员组

文章数量:1122846

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

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

Share Improve this question edited Nov 23, 2024 at 8:09 Ostoura 2,5232 gold badges27 silver badges45 bronze badges asked Nov 22, 2024 at 15:41 user28435498user28435498 1
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: How to get the Full Host in Server Api Nuxt 3Stack Overflow