{"id":13591243,"url":"https://github.com/zy445566/before-server","last_synced_at":"2025-04-13T12:33:28.355Z","repository":{"id":42094321,"uuid":"251525527","full_name":"zy445566/before-server","owner":"zy445566","description":"方便查阅客户端(移动端，Web端)请求的前置服务","archived":false,"fork":false,"pushed_at":"2023-03-04T10:02:18.000Z","size":407,"stargazers_count":40,"open_issues_count":6,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-24T17:39:30.797Z","etag":null,"topics":["before","monitor","proxy","server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zy445566.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-03-31T07:02:53.000Z","updated_at":"2024-03-20T06:33:42.000Z","dependencies_parsed_at":"2024-01-14T04:34:55.437Z","dependency_job_id":"3e6894a5-751c-4827-b90b-a6c675b6ff3a","html_url":"https://github.com/zy445566/before-server","commit_stats":{"total_commits":122,"total_committers":2,"mean_commits":61.0,"dds":0.0901639344262295,"last_synced_commit":"d7989a29f30dae345acf8ed344d89608588cb3aa"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fbefore-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fbefore-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fbefore-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zy445566%2Fbefore-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zy445566","download_url":"https://codeload.github.com/zy445566/before-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248714734,"owners_count":21149958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["before","monitor","proxy","server"],"created_at":"2024-08-01T16:00:55.038Z","updated_at":"2025-04-13T12:33:28.010Z","avatar_url":"https://github.com/zy445566.png","language":"JavaScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# before-server\r\n方便查阅客户端(移动端，Web端SSR和小程序)请求的前置服务,前端或移动端通过链接前置服务的代理地址来转发到开发或测试服务，那我们可以不需要复杂的配置抓包工具，不需要为手机或电脑等设备单独设置代理，而直接打开前置服务的监控服务即可实现查看请求情况。解决了移动端，Web端SSR和小程序无法直接通过浏览器查看请求数据的痛点和需要为每台设备配置抓包代理的痛点。\r\n\r\n# 与传统开发的不同点\r\n传统开发是客户端直连开发机来实现通讯中间没有监控http或https的工具，所以每个人都需要配置一个抓包工具，同时还要为每台手机或电脑配置代理服务，这无疑是复杂和费时的。所以我们完全可以在开发网或测试网只部署一个直连前置服务，并通过url来区分监控请求来实现只部署一次却能服务整个团队的抓包需求功能，而这唯一的代价仅仅是前端项目修改API接口的请求地址。\r\n\r\n![图片说明](https://raw.githubusercontent.com/zy445566/zy445566.github.io/master/before-server/example.jpg)\r\n\r\n优点：\r\n* 只需部署一次，可通过URL区分拦截请求\r\n* 不需要配置复杂的抓包工具，自带http和https的请求拦截\r\n* 不需要为手机或电脑等设备单独设置代理\r\n* 既可以支持个人部署，也可以支持团队部署\r\n* 可以在相同服务器不同端口部署服务\r\n* 如果部署到服务器中，整个团队都可以使用\r\n\r\n缺点：\r\n* 前端需要修改API请求地址到代理服务地址\r\n\r\n# 使用方法\r\n## 0x1 创建`.bsrc.js`文件到工作目录中\r\n首先需要在你的工作目录中增加`.bsrc.js`文件,目录结构如下：\r\n```\r\n/your-work-dir\r\n└─.bsrc.js\r\n```\r\n最小`.bsrc.js`文件配置例子如下(和webpack-dev-server的proxyTable配置基本类似。[点此查看配置文件相对全面的例子](#配置文件相对全面的例子))：\r\n```js\r\nmodule.exports = {\r\n    proxyTable:{\r\n        '/': {\r\n            // 比如后端API接口地址为http://api.test.com:7001,这里需要配置你的后端API地址\r\n            target: 'http://api.test.com:7001', // 服务端API接口\r\n            changeOrigin: true, // 如果后端对域名有识别，可以配置这个\r\n            tag:'API服务'， // 配置标签\r\n        }\r\n    },\r\n    httpPort:8000\r\n}\r\n```\r\n## 0x2 安装全局包\r\n```sh\r\n# 这里也可以使用yarn\r\nnpm install before-server -g\r\n```\r\n## 0x3 在工作目录启动服务\r\n```sh\r\n# dir: /your-work-dir\r\nbefore-server # 或者 `npx before-server`\r\n# 也可以使用pm2启动，如 pm2 start before-server # 但注意pm2需要在配置文件目录启动\r\n```\r\n\r\n## 0x4 修改前端项目的API接口请求地址\r\n最后前端(包括移动端和web端以及小程序端)修改请求API接口地址到前置服务，默认http为8000端口,默认https为8443。\r\n\r\n比如.bsrc.js配置为如下时:\r\n```js\r\nmodule.exports = {\r\n    proxyTable:{\r\n        '/': {\r\n            // 比如后端API接口地址为http://api.test.com:7001,这里需要配置你的后端API地址\r\n            target: 'http://api.test.com:7001', // 服务端API接口\r\n            changeOrigin: true, // 如果后端对域名有识别，可以配置这个\r\n            tag:'根服务'， // 配置标签\r\n        }\r\n    },\r\n    httpPort:8000\r\n}\r\n```\r\n修改你的前端项目的请求API接口地址，如上面配置例子则修改API接口地址为8000，\r\n```sh\r\n# API_HOST:http://api.test.com:7001 # 比如原来前端项目是请求http://api.test.com:7001来获取接口数据的\r\nAPI_HOST:http://127.0.0.1:8000 # 如本地启动服务可直接访问127.0.0.1:8000，建议部署到服务器中\r\n```\r\n\r\n完成以上工作后，就可以使用浏览器打开8555端口的UI界面，来查看发送的请求。\r\n\r\n注意如果使用nginx反向代理需要配置支持8555端口使用WebStocket功能。\r\n\r\n# 功能截图\r\n\r\n![首页](https://raw.githubusercontent.com/zy445566/zy445566.github.io/master/before-server/home.png)\r\n![监控页面](https://raw.githubusercontent.com/zy445566/zy445566.github.io/master/before-server/monitor.png)\r\n\r\n# 配置文件的默认配置\r\n以下是`.bsrc.js`文件的默认配置，手动配置项会合并进默认配置项\r\n```js\r\nconst fs =require('fs');\r\nconst path =require('path');\r\nmodule.exports = {\r\n    // 这是自带的证书，一般你不需要配置\r\n    ssl: {\r\n            key: fs.readFileSync(path.join(__dirname,'keys','key.pem'), 'utf8'),\r\n            cert: fs.readFileSync(path.join(__dirname,'keys','cert.pem'), 'utf8')\r\n    },\r\n    // 默认代理墙是没有任何配置的，需要手动配置\r\n    proxyTable:{\r\n\r\n    },\r\n    httpPort:8000, // http代理服务的端口\r\n    httpsPort:8443, // https代理服务的端口\r\n    monitorPort:8555, // 监控服务端口服务的端口\r\n    HistoryNumber:300, // 总历史记录上限，存在服务内存中，重启服务内存重置\r\n}\r\n```\r\n\r\n# 配置文件相对全面的例子\r\n相对全面的`.bsrc.js`文件例子如下：\r\n```js\r\nmodule.exports = {\r\n    proxyTable:{ \r\n        // 这里代理的优先级，前者高于后者，如果想全访问，直接使用/即可\r\n        /**\r\n         * http://proxyhost/api1/1 \r\n         * 代理将转发到=\u003e \r\n         * http://www.aaa.com/api1/1\r\n         */\r\n        '/api1': {\r\n            target: 'http://www.aaa.com', //你的测试域名,当访问代理的api1开头的url要代理的开发服务\r\n            cors:true, // 如果要配置cors跨域可以在这里设置\r\n            changeOrigin: true, // 如果后端对域名有识别，可以配置这个\r\n            tag:'xxxx功能'， // 配置标签\r\n        },\r\n        /**\r\n         * http://proxyhost/api2/1 \r\n         * 此配置代理将转发到=\u003e \r\n         * http://www.aaa.com/api3/api2/1 \r\n         */\r\n        '/api2': {\r\n            target: 'https://www.bbb.com/api3', //当访问代理的api2开头的url要代理的开发服务\r\n            secure:false, // 对于自签证书则需要配置secure忽略证书无效\r\n            tag:'xxxx服务'， // 配置标签\r\n        },\r\n        '/ws/api': {\r\n            target: 'ws://www.ccc.com', //当访问/ws/api开头的url要代理的开发服务\r\n            tag:'xxxxWebSockt服务'， // 配置标签\r\n        },\r\n        '/ws2': {\r\n            target: 'ws://www.ddd.com', //当访问/ws2开头的url要代理的开发服务\r\n            tag:'xxxxWebSockt2号服务'， // 配置标签\r\n        },\r\n        '/log': {\r\n            target: 'ws://www.eee.com', //重写服务地址示例\r\n            tag:'重写服务'， // 配置标签\r\n            pathRewrite: {\r\n                \"^/log\": \"\", // 此时当访问/log/xxx时，会修改请求到/xxx\r\n            },\r\n        },\r\n        /**\r\n         * 最好配置一下根服务，这样的话如果都不能匹配到，还可以直接匹配到根服务\r\n         * before-server匹配的优先级是从上到下，所以根建议配置到最下面\r\n         * 这样监控全部就能发挥最大功力了\r\n         */\r\n        '/': {\r\n            target: 'http://www.aaa.com', \r\n            tag:'根服务'， // 配置标签\r\n        }\r\n    },\r\n    httpPort:8000, // http代理服务的端口\r\n    httpsPort:8443, // https代理服务的端口\r\n    monitorPort:8555,// 监控服务页面服务端口，可以通过这个端口打开UI界面\r\n}\r\n```\r\n\r\n# 关于\r\n通过代理请求实现请求监控的工具，解决了移动端、Web端SSR、小程序很多请求无法直接通过浏览器查看请求数据的痛点。\r\n\r\nPower By @Web Components\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzy445566%2Fbefore-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzy445566%2Fbefore-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzy445566%2Fbefore-server/lists"}