【问题标题】:How to configure proxy in Vite?如何在 Vite 中配置代理?
【发布时间】:2021-02-16 23:29:49
【问题描述】:

我试图关注文档并像这样创建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 存储库中创建了一个 issue,但它已关闭,我不明白结束评论。

【问题讨论】:

    标签: http-proxy vuejs3 vite


    【解决方案1】:

    原来需要将secure 标志指定为 false,如下所示:

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

    相关github issue

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 2022-08-02
      • 2021-09-09
      • 2018-01-23
      相关资源
      最近更新 更多