N
Nuxt3w ago
Night

How to proxy in nuxt3

i useUseFetch
3 Replies
kapa.ai
kapa.ai3w ago
Hi @Night. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To proxy API requests in Nuxt 3 when using useFetch, you have a few options. Here are the recommended approaches: 1. Use Nitro's devProxy configuration: This is a simple way to set up proxying for development:
// nuxt.config.ts
export default defineNuxtConfig({
nitro: {
devProxy: {
'/api': '<http://your-api-server.com'>
}
}
})
// nuxt.config.ts
export default defineNuxtConfig({
nitro: {
devProxy: {
'/api': '<http://your-api-server.com'>
}
}
})
2. Create a Nitro proxy API endpoint: This method allows more control and works in both development and production:
// server/api/[...].ts
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const target = config.apiUrl // Set this in your runtime config

// Remove /api prefix from the URL
const url = event.node.req.url.replace(/^\/api/, '')

// Proxy the request
return await $fetch(url, {
baseURL: target,
headers: {
// Forward headers as needed
}
})
})
// server/api/[...].ts
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const target = config.apiUrl // Set this in your runtime config

// Remove /api prefix from the URL
const url = event.node.req.url.replace(/^\/api/, '')

// Proxy the request
return await $fetch(url, {
baseURL: target,
headers: {
// Forward headers as needed
}
})
})
3. Use a custom $fetch instance: You can create a custom $fetch instance with a base URL and use it with useAsyncData:
// plugins/api.ts
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig()
const api = $fetch.create({
baseURL: config.apiUrl
})

return {
provide: {
api
}
}
})

// In your component
const { $api } = useNuxtApp()
const { data } = await useAsyncData('item', () => $api('/endpoint'))
// plugins/api.ts
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig()
const api = $fetch.create({
baseURL: config.apiUrl
})

return {
provide: {
api
}
}
})

// In your component
const { $api } = useNuxtApp()
const { data } = await useAsyncData('item', () => $api('/endpoint'))
These approaches are based on the information from Nuxt 3: The BEST way to proxy your API in Nuxt and Custom useFetch in Nuxt. Remember to handle headers and cookies appropriately when proxying requests, especially for server-side rendering scenarios. The useRequestHeaders composable can be useful for this purpose. Nuxt Documentation on Data Fetching provides more detailed information on using useFetch and other data fetching methods in Nuxt 3. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@1291669794667630633> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Night
NightOP3w ago
Now i try Create a Nitro proxy API endpoint: buy response is binary

Did you find this page helpful?