首页 > 解决方案 > 如何在 Vite 中配置代理?

问题描述

我试图遵循文档并vite.config.js像这样创建:

const config = {
  outDir: '../wwwroot/',
  proxy: {
    // string shorthand
    '/foo': 'http://localhost:4567',
    // with options
    '/api': {
      target: 'http://jsonplaceholder.typicode.com',
      changeOrigin: true,
      rewrite: path => path.replace(/^\/api/, '')
    }
  }
};

export default config;

并尝试通过以下调用对其进行测试:

fetch('/foo');
fetch('/api/test/get');

我期待有实际的请求http://localhost:4567/foohttp://jsonplaceholder.typicode.com/test/get 但他们都把我的开发服务器作为这样的来源:http://localhost:3000/foohttp://localhost:3000/api/test/get

我误会了吗?代理应该如何工作?

我还在Vite 存储库中创建了一个问题,但它已关闭,我不明白结束评论。

标签: http-proxyvuejs3vite

解决方案


事实证明,需要将secure标志指定为 false,如下所示:

 proxy: {
      '/api': {
           target: 'https://localhost:44305',
           changeOrigin: true,
           secure: false,      
           ws: true,
       }
  }

相关的github问题


推荐阅读