我创建了一个简单的Vue3应用程序,并试图在我的机器上调用另一个本地API (在另一个端口上)。为了更好地复制生产服务器环境,我调用了一个相对的API路径。这意味着我需要在vite服务器上使用一个代理将API请求转发到正确的localhost端口进行本地开发。我在我的vite.config.ts
文件中像这样定义了我的vite代理:
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import basicSsl from '@vitejs/plugin-basic-ssl'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
basicSsl(),
vue()
],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
server: {
https: true,
proxy: {
'/api': {
target: 'https://localhost:44326', // The API is running locally via IIS on this port
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '') // The local API has a slightly different path
}
}
}
});
我正在成功地从Vue应用程序调用API,但在运行vite服务器的命令行中得到了这个错误:
5:15:14 PM [vite] http proxy error:
Error: self signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34)
at TLSSocket.emit (node:events:526:28)
at TLSSocket._finishInit (node:_tls_wrap:944:8)
at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12)
我已经尝试过添加基本ssl包了,而且我并不特别想安装另一个NPM软件包,这是在顶部投票的答案。为什么在我试图调用本地机器上的另一个API时,vite服务器会抱怨一个自签名证书?我能做些什么来解决这个问题?
发布于 2022-10-11 20:33:02
你可以试试secure: false
server: {
https: true,
proxy: {
'/api': {
target: 'https://localhost:44326', // The API is running locally via IIS on this port
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^\/api/, '') // The local API has a slightly different path
}
}
}
完整选项集可在https://github.com/http-party/node-http-proxy#options上使用。
选项
httpProxy.createProxyServer
支持以下选项:
path
传递(用于代理代理)set-cookie
标头的域。可能的价值:- `false` (default): disable cookie rewriting
- String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
- Object: mapping of domains to new domains, use `"*"` to match all domains. For example keep one domain unchanged, rewrite one domain and remove other domains: cookieDomainRewrite: { "unchanged.domain": "unchanged.domain", "old.domain": "new.domain", "\*": "" }
set-cookie
头的路径。可能的价值:- `false` (default): disable cookie rewriting
- String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
- Object: mapping of paths to new paths, use `"*"` to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths: cookiePathRewrite: { "/unchanged.path/": "/unchanged.path/", "/old.path/": "/new.path/", "\*": "" }
proxyRes
事件来适当地返回响应https://stackoverflow.com/questions/74033733
复制相似问题