为什么我的前端请求全部发送到后端项目?



我正在学习前端和后端接口对接。但我遇到了一个问题。


我的后端项目的端口是8081,当我http://localhost:8081/hello发送此请求时,我会收到一个响应正文hello

我的前端项目的端口是8080。当我发送请求喜欢http://localhost:8080/hello时,它的响应与http://localhost:8081/hello相同


这是我的vue.config.js

let proxyObj = {};
proxyObj['/']={
ws: false,
target: 'http://localhost:8081',
changeOrigin: true,
pathRewrite: {
'^/': ''
}
}
module.exports= {
devServer: {
host: 'localhost',
port: 8080,
proxy: proxyObj
}
}

如果我proxy: proxyObj更改为proxy: null然后http://localhost:8080/hello,我只收到一个空白。我的vue.config.js文件有问题吗?


任何帮助将不胜感激!

DevServer 代理将代理请求取决于您的代理配置:

// '/'means this rule will match all requests
proxyObj['/']={
ws: false,
// target means requests that matched will be sent to http://localhost:8081
target: 'http://localhost:8081',
changeOrigin: true,
pathRewrite: {
'^/': ''
}
}

使用此配置,您的所有请求都将发送到后端服务器,您将收到来自服务器的响应。如果将配置对象设置为 null,则请求将发送到前端项目:http://localhost:8080,因此无法收到任何响应。


有关代理配置的更多详细信息,请参阅此处。

最新更新