【问题标题】:Why my Front-End requests are all sended to Back-End project?为什么我的前端请求都发送到后端项目?
【发布时间】:2019-12-11 09:55:33
【问题描述】:

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


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

而我的前端项目的端口是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 文件有什么问题吗?


任何帮助将不胜感激!

【问题讨论】:

    标签: vue.js frontend


    【解决方案1】:

    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,因此您将无法收到任何响应。


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

    【讨论】:

    • 谢谢!但是我从课程视频中复制了这些代码,但是我的老师可以使用http://localhost:8080/#/访问前端页面,但是我无法使用相同的url访问,响应状态为200但页面为空白。
    猜你喜欢
    • 2019-07-19
    • 2019-07-17
    • 2020-05-17
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多