问题描述
我的 Vue 项目中有以下 vue.config.js:
module.exports = {
devServer: {
proxy: {
'^/api/': {
target: 'https://example.com/api/',changeOrigin: true,logLevel: 'debug'
},}
}
}
因此所有对 /api/*
的请求都应该重定向到 https://example.com/api/*
。不幸的是,代理似乎删除了 api/
之后的部分网址:
[HPM] POST /api/api-token-auth/ -> https://example.com/api/
api-token-auth/
部分发生了什么变化?
解决方法
要根据 docs 中的语法将所有请求代理到 /api
,请创建如下规则:
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'https://example.com',changeOrigin: true,logLevel: 'debug'
},}
}
}
您不应再次将 /api
路径放在 target
中,因为原始路径中 /api
之后和包括 {{1}} 之后的所有内容都将附加到目标中。