由于遇上了一些线上问题开发环境无法复现的场景,于是便需要在本地运行起开发环境的代码。 也就是说:打包后的代码。
打包后的本地代码和生产环境的代码有个区别就是调用的后台地址不一致。
这边是使用前缀/gw来区分后台请求的。
举个例子http://test.env/#/homepage 是前端页面请求的url
而http://test.env/gw/api/xxx是后端接口,如果在本地直接把打包后的dist的文件夹丢给一个静态服务器,肯定会报接口异常。
这边整理了几个简单的解决方法实现代理
1.nodeJs http-server 利用npm的一个包http-server简单好用明了。
项目打包后:
cd dist
http-server . -p ${portNumber} --proxy ${your-proxy-website}
运行上面的命令,就可以启动一个静态代理服务器了
2.nginx实现路由转发
homebrew安装
brew install nginx
找到nginx对应的配置文件,由homebrew下载的地址如下:
/usr/local/etc/nginx
修改nginx.conf文件:
# 添加此行配置
location /gw {
proxy_pass http://test.env;
}
location / {
root html;
index index.html index.htm;
}
将dist文件夹覆盖/usr/local/opt/nginx/html
启动nginx
nginx
打开网页查看效果