http-proxy - 如何在 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/foo
,http://jsonplaceholder.typicode.com/test/get
但他们都把我的开发服务器作为这样的来源:http://localhost:3000/foo
和http://localhost:3000/api/test/get
我误会了吗?代理应该如何工作?
我还在Vite 存储库中创建了一个问题,但它已关闭,我不明白结束评论。
解决方案
事实证明,需要将secure
标志指定为 false,如下所示:
proxy: {
'/api': {
target: 'https://localhost:44305',
changeOrigin: true,
secure: false,
ws: true,
}
}
推荐阅读
- python - openCV 不会打开 mp4 文件
- azure - 如何访问属于不同租户的密钥库
- javascript - AJAX 发布请求后在 Django 视图中呈现模板
- c - 基本用户名/密码代码在 C 中不起作用 - 分段错误
- r - 无论如何,在 Docker 容器中丢失了大部分 r 服务器文件代码来恢复?
- flutter - 当变量和数据发生变化时,setState() 是否会像 stream 和 streambuilder 一样触发自己?
- python - 如何从 Python 中的正则表达式捕获动态文本?
- makefile - 在Raspberry Pi3上运行Makefile,安装问题
- html - CSS Instagram 图片框布局
- javascript - 数组故障中的过滤数组