在node.js中配置proxyTable并使用axios实现跨域调用不同的服务

配置文件路径:

config/index.js

proxyTable配置:

proxyTable: {
// 这里配置 '/api' 就等价于 target , 你在链接里访问 /api === http://localhost:8089
      '/api': {
        target: 'http://localhost:8089',
// secure: true, // 如果是 https ,需要开启这个选项
// 是否是跨域请求?肯定是啊,不跨域就没有必要配置这个proxyTable了.
        changeOrigin: true,
        loglevel: 'debug',
// 这里是重写链接,比如真是接口里不包含 /api,就需要这样配置
        pathRewrite: {
          '^/api': '',
// 和下边两种写法,因人而异根据需求。
// 等价于    /api + /  ==  http://localhost:8089/
// 如果写为 '^/api' : '/api'
// 等价于   /api + /api  ==  http://localhost:8089/api
// 这里的 /api ==  http://localhost:8089
        }
      },
      '/sp': {
        target: 'http://localhost:8090',
        changeOrigin: true,
        loglevel: 'debug',
        pathRewrite: {
          '^/sp': '',
        }
      }
    }

methods方法中的服务调用方式

axios.get('/api/check').then(function(res) {
})

axios.get('/ipa/he').then(function(res) {
})

springboot后端服务

    @RequestMapping("/check")
    public String check(){

        System.out.println("hello check");
        return  "hello check";
    }
    @RequestMapping("/he")
    public String hello(){

        System.out.println("hello world");
        return  "hello world";
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值