browserSync如何将本地访问地址http://localshot:3000修改为自己需要的访问地址https://www.example.com:3000

后端限制了只能【https://*.example.com】能访问,前端启动本地服务是【http://localhost:3000】【http://127.0.0.1:3000】,访问不到后端接口。

需要在启动浏览器访问的时候,单独配置地址栏访问参数。

项目使用的是browsersync热加载浏览器。

browsersync中文文档:https://browsersync.devjs.cn/docs/options

1、启动https协议

2、覆盖掉locahost

3、浏览器打开

4、配置代码
//options:https://browsersync.devjs.cn/docs/options
browserSync.init({
    proxy: 'localhost:8001', //代理地址【启动的本地服务器的访问地址(http://localhost:8001)(http://127.0.0.1:8001)】
    https: true,//为本地主机开发使用https
    host: 'localhost.example.com',//设置默认打开的地址【在hosts里面需要配置当前访问地址指向本地 127.0.0.1 localhost.example.com】
    open: 'external',//使用external的方式打开地址,就是上面设置默认打开的地址
    port: 3000, //浏览器访问端口号
  });
5、访问项目,遇到问题

运行项目后,在浏览器自动打开 https://localhost.example.com:3000/ 

遇到问题1:显示【无法访问此网站】, 在hosts文件里面配置 127.0.0.1 localhost.example.com

遇到问题2:显示【502 Bad Gateway】,在hosts文件里面配置 127.0.0.1 localhost.example.com

 hosts文件的路径:C:\Windows\System32\drivers\etc

6、成功访问页面

猜你喜欢

转载自blog.csdn.net/xiaoxiong_jiaxin/article/details/141331621