在Webpack项目中开发时,遇到axios 请求接口会报 CERT_HAS_EXPIRED Error。
Error 如下 :
Proxyerror: Could not proxy request /api/islogged fromlocalhost:3000 to
https://www.example.com.Seehttps://nodejs.org/api/errors.html#errors_common_system_errors for
more information (CERT_HAS_EXPIRED).
原因
Wepback 中的 http-porxy 插件,默认情况下,不接受运行在HTTPS上,并且使用了无效证书的后端服务。
解决方法
如果你想要接受,只要设置 secure: false 就行。或者让后端服务使用正确的https证书。
修改配置如下:
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://target/api/url',
secure: false
}
}
}
};
解决跨域
若是遇到 跨域问题,可以查看 http-proxy 中有一个参数 changeOrigin参数, 设置为true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求。
修改配置如下:
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://target/api/url',
changeOrigin: true,
}
}
}
};