【毕业论文】create-react-app 配置代理,进行前后端联调

毕业论文配置如下:

前端:http://localhost:3000  3000 端口 (技术栈:react、antd)

后端:http://localhost:8080 8080端口 (技术栈:原生node)

数据库: mysql、mysqlWorkBench

问题:

如何前端3000端口调用8080端口的接口,直接调用会出现跨域问题,react配置如下:

在src下建setupProxy.js 这个文件,

const proxy = require('http-proxy-middleware')

module.exports = function(app) {
  app.use(
    proxy(
      '/api/', {
        target: 'http://localhost:8080/',
        changeOrigin: true
      }
    )
  )
}

然后保存重启npm start就可以了

测试生不生效:在命令行里面输入:

curl http://localhost:8080/api/history/area\?year_num\=2015

有正确的数据返回,即生效成功,然后就可以随意进行联调啦~~

猜你喜欢

转载自blog.csdn.net/Luckyzhoufangbing/article/details/89041321
今日推荐