微信开发本地调试

本次项目需要支持在微信中运作,由于在微信后台有配置可信域名,如果直接在微信里打开本地页面可能会导致某些诡异的错误,所以想法就是开发的时候在微信里访问 test-lebz.le.com ,然后代理到本地项目,这样就不用每次小修改都提交git,然后到测试服务器部署一遍。

现有条件

  • 现在已经能够通过 10.112.41.200 绑定代理,把相应域名代理到开发者本地ip。但是众所周知这个服务不能绑定本地具体某个端口,比如gameStatic项目的8010端口。所以我们下面要做的就是怎样让本地支持不带端口访问。

现有基础上拓展

一、本地安装nginx
1、安装和启动可以参考 https://www.cnblogs.com/meng1314-shuai/p/8335140.html
2、配置nginx
a.修改nginx.conf如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
keepalive_timeout 65;

include /usr/local/etc/nginx/conf.d/*.conf;
}

b.找到 nginx 配置文件放置文件夹,新建conf.d目录,在里面添加 game.conf 文件,内容如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 80;
server_name _;
charset utf-8;
add_header Set-Cookie 'client_ip=$proxy_add_x_forwarded_for;domain=lebz.le.com;path=/;';
large_client_header_buffers 4 32k;

location ~ ^/gameApi {
proxy_pass http://127.0.0.1:8888;
}

# 商城代理
location ~ ^/store {
proxy_pass http://127.0.0.1:3002;
}

# 本地调试gameStatic项目
location ~ {
proxy_pass http://127.0.0.1:8010;
}

index index.html;
root /letv/www;
}

3、重启nginx服务

1
sudo nginx -s reload

4、测试
本地正常启动gameStatic/storeClient项目,然后去掉8010端口,如果能访问,则说明正确。

二、其他
1、至此gameStatic项目就可以了,直接在微信里打开 http://test-lebz.le.com 就能访问本地代码,每次本地修改的代码也能直接反映在微信网页上。
2、如果想调试水果大转盘和摇一摇,则还需要把gameApi项目clone到本地,然后启动项目(首次执行一次就行)。

1
2
npm run build:test
pm2 start pm2.json

三、上面准备工作都可以了,接下来在手机上绑定代理,方法有两种,原理差不多。
1、方法1:10.112.41.200 设置代理
注意不要和别人已有的设置冲突,设置好代理后,在手机上绑定host。
2、方法2:比方法1更好,可以用spy-debugger调试
a.本地绑定host (127.0.0.1 test-lebz.le.com)
b.本地开启spy-debugger
c.手机绑本地spy-debugger代理:本地ip + 端口 9888

坚持原创技术分享,您的支持将鼓励我继续创作!