server跨域代理
在vite.config.ts 文件中直接配置

export default defineConfig({
//其他配置...

//设置反向代理,跨域
  server: {
    // host: '0.0.0.0',//设置地址:http://localhost
    port: 4500, // 设置服务启动端口号
    // open: true, // 设置服务启动时是否自动打开浏览器
    // cors: true, // 允许跨域
    proxy: {
      '/api': {
        target: 'http://localhost:3001/',//代理的地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')//这里的/需要转义
      }
    }
  },
})
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐