# 使用Proxy代理

把请求代理转发到其他服务器,用于解决前端请求跨域,或使用其它服务器的MOCK数据。

# 开始使用

//cyb.config.js
module.exports = {
  proxy: {
    context: '/api',
    options: {
      target: 'http://www.example.org',
      changeOrigin: true
    }
  }
}

在使用塞伯坦CYB开发和测试过程中,当请求http://localhost/api/foo/bar时,会被转发到http://www.example.org/api/foo/bar

# 高级使用(配置多个请求转发)

//cyb.config.js
module.exports = {
  proxy: [{
    context: '/api',
    options: {
      target: 'http://www.example.org',
      changeOrigin: true
    }
  },{
    context: '/mock',
    options: {
      target: 'http://mock.example.org:3000',
      changeOrigin: true
    }
  }]
}

塞伯坦CYB集成了http-proxy-middleware中间件,更多配置请参考:https://github.com/chimurai/http-proxy-middleware/#core-concept

使用Proxy要求cyb-cli版本>=1.3.0。