微信公众号本地调试

微信公众号本地调试

Q: 每次微信公众号开发都要上传到测试服务器上才能测试,特别麻烦和不便。

步骤:

1.修改host文件,将本地ip (比如:http://127.0.0.1:3300) 映射到服务器IP

修改host文件需要权限,可手动直接修改原文件,也可借助工具进行修改。
我这里是借助 Hozz 修改,这样做相对方便管理,在不需要使用时可以及时删除。

2.代理服务器

npm install http-proxy

index.js如下:

const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});

const server = http.createServer((req, res) => {
    const isLocal = req.url.indexOf('/local') === 0; //这里是举例,具体按照你项目
    if (isLocal) proxy.web(req, res, { target: 'http://127.0.0.1:3300' })
    else proxy.web(req, res, { target: '目标IP' }); 
})

server.listen(80);

3.启动

sudo node index.js

你可能感兴趣的:(微信公众号本地调试)