I try to start a nuxt3 program, now I want to set server proxy. A request to http://localhost:3000/api/v1 is supposed to return a response from our backend server on http://39.98.58.238:8594 , but now it gives me a 404 page.
At first, I follow the vite.js docs to set nuxt.config.js file
nuxt.config.js
export default defineNuxtConfig({
...
vite: {
server: {
proxy: {
'/api': {
target: 'http://39.98.58.238:8594',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
},
}
},
})
page
<script setup>
async function test() {
await usefetch('/api/v1/xxx')
}
</script>
<template>
<div>
<button @click="test">check</button>
</div>
</template>
It didn't work, my request returned a 404 page. Then I try to follow this issue: text, do not use vite proxy
nuxt.config.js
export default defineNuxtConfig({
nitro: {
devProxy: {
'/api/': {
target: 'http://39.98.58.238:8594/',
changeOrigin: true
}
}
}
})
But it's still not work. What can I do to solve the problem? Thanks!!!
/api/
to/api/v1
in your devProxy config inside nuxt.config.js. Look here – Pomiferous