webpack解析php,webpack http-proxy配置

配置http-proxy

在webpack的配置文件(webpack.config.js)中进行配置

module.exports = {

...此处省略一万字

// webpack-dev-server的配置

devServer: {

historyApiFallback: true,

hot: true,

inline: true,

progress: true,

port: 3000,

host: '10.0.0.9',

proxy: {

'/test/*': {

target: 'http://localhost',

changeOrigin: true,

secure: false

}

}

},

...此处省略一万字

};

上述配置中,关于http-proxy的只是 proxy: {...} 中的值

调用接口

为了方便起见,下面使用jquery封装好的ajax函数进行示范

$.ajax({

// url: 'http://10.0.0.9:3000/test/testFetch/Login.php', // 这样不行

url: '/test/testFetch/Login.php', // 这样行

type: 'post',

data: {

app_id: '13751313169',

password: '123456',

user_name: 'Nicholas'

},

success: function(data) {

console.log(data);

}

});

作者:NicholasNC

链接:http://www.jianshu.com/p/3bdff821f859

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

proxy中的部分参数说明

'/test/*' 以及 target: 'http://localhost'

从名字就能看出,这个实际上是将匹配 '/test/*' 这种格式的API的域名重定向为 'http://localhost'

结合上面的 “调用接口” 可以看出, url: '/test/testFetch/Login.php' 这句,实际上会自动补充前缀,也就是说,url: '/test/testFetch/Login.php' 等价于 url: 'http://10.0.0.9:3000/test/testFetch/Login.php'

但是,我们使用了http-proxy进行重定向,这样的话,url: '/test/testFetch/Login.php' 等价于 url: 'http://localhost/test/testFetch/Login.php'

changeOrigin

true/false, Default: false - changes the origin of the host header to the target URL

本地会虚拟一个服务端接收你的请求并代你发送该请求——这个是别人的说法

我试了一下,就算这个参数设置成 false 也有部分情况是可以的,具体原因不详,所以还是将其设置成 true 吧

secure

true/false, if you want to verify the SSL Certs

pathRewrite

例子: pathRewrite: {'^/api': ''}

Object-keys will be used as RegExp to match paths

我猜,这里是将 '^/api' 使用 '' 代替(只是我猜,没是成功,估计是我的正则表达式写得不行)

附上使用Fetc

作者:NicholasNC

链接:http://www.jianshu.com/p/3bdff821f859

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值