我尝试启动 nuxt3 程序,现在我想设置服务器代理。对 http://localhost:3000/api/v1 的请求应该会从http://39.98.58.238:8594上的后端服务器返回响应,但现在它给了我一个 404 页面。
首先,我按照 vite.js 文档设置 nuxt.config.js 文件
nuxt.config.js
export default defineNuxtConfig({
...
vite: {
server: {
proxy: {
'/api': {
target: 'http://39.98.58.238:8594',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
},
}
},
})
Run Code Online (Sandbox Code Playgroud)
页
<script setup>
async function test() {
await usefetch('/api/v1/xxx')
}
</script>
<template>
<div>
<button @click="test">check</button>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
它不起作用,我的请求返回了 404 页面。然后我尝试遵循这个问题:text,不要使用vite代理
nuxt.config.js
export default defineNuxtConfig({
nitro: {
devProxy: {
'/api/': {
target: 'http://39.98.58.238:8594/',
changeOrigin: true
}
}
} …Run Code Online (Sandbox Code Playgroud)