{"id":22347684,"url":"https://github.com/laomu1988/koa2-proxy","last_synced_at":"2025-10-06T05:14:52.985Z","repository":{"id":76746127,"uuid":"62496633","full_name":"laomu1988/koa2-proxy","owner":"laomu1988","description":"基于koa@next的代理工具，支持http和https，并且可以当做本地服务器使用","archived":false,"fork":false,"pushed_at":"2017-11-09T13:27:42.000Z","size":337,"stargazers_count":44,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T02:11:18.129Z","etag":null,"topics":["koa","koa2","proxy"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laomu1988.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-03T12:59:06.000Z","updated_at":"2024-06-22T15:06:35.000Z","dependencies_parsed_at":"2023-02-26T03:00:21.666Z","dependency_job_id":null,"html_url":"https://github.com/laomu1988/koa2-proxy","commit_stats":null,"previous_names":["laomu1988/koa-proxy"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/laomu1988/koa2-proxy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laomu1988%2Fkoa2-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laomu1988%2Fkoa2-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laomu1988%2Fkoa2-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laomu1988%2Fkoa2-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laomu1988","download_url":"https://codeload.github.com/laomu1988/koa2-proxy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laomu1988%2Fkoa2-proxy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267809697,"owners_count":24147516,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["koa","koa2","proxy"],"created_at":"2024-12-04T10:10:21.725Z","updated_at":"2025-10-06T05:14:47.946Z","avatar_url":"https://github.com/laomu1988.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 基于koa2的代理工具\n\n## 功能\n* 代理http和https\n* 转发本地请求到网络\n* 本地服务器\n* 本地模拟数据配置\n* 解析smarty模板\n* 随意修改请求和响应结果\n\n\n## 安装\n安装node之后执行\n```\nnpm install koa2-proxy\n```\n\n## 使用\n```\nvar proxy = require('koa2-proxy');\n\n// 本地静态服务器\nproxy.static(__dirname);\n\n// 本地模拟文件\nproxy.mockfile(__dirname + '/mockfile.txt');\n\n// 解析smarty模板\nproxy.smarty({ext: '.html', data: {data: 'smarty html'}});\n\n// 转发请求到指定host\nproxy.when('/api', function(ctx) {\n    ctx.request.host = 'www.test.com';\n    ctx.request.protocol = 'http';\n});\n\n// 配置代理请求结束后修改body\nproxy.when({'.html', phase: 'response'}, function(ctx) {\n    ctx.response.body += '\u003cdiv\u003etest\u003c/div\u003e';\n});\n\n// 请求开始时转发本地请求到网络\nproxy.on('start', function (ctx) {\n    console.log('start: ', ctx.request.url, ctx.isLocal());\n    ctx.request.host = 'www.koa2.com';\n});\n// 请求结束时输出状态\nproxy.on('end', function (ctx) {\n    console.log('end: ' + ctx.response.status);\n    console.log('end: ' + ctx.response.get('content-type'));\n    // console.log('end: ' + ctx.response.body);\n});\n\n// 监听端口\nproxy.listen(3010);\n```\n\n## 增加属性\n* proxy.app koa的实例:proxy.app = new koa();\n* proxy.httpServer  http服务器， 只有当http服务器启动后才会赋值（http-server-start事件）\n* proxy.httpsServer https服务器， 只有当http服务器启动后才会赋值（https-server-start事件）\n* proxy.localip 本地ip地址,listen后生效\n* proxy.localhost 本地ip地址+监听host, listen后生效\n* ctx.proxy  proxy\n* request.body 请求的form表单数据\n\n## 增加函数\n* proxy.static(root,opts) 静态文件服务器\n* proxy.when(conditions,callback) 增加中断和处理内容\n* proxy.mockfile(mockfile) 模拟文件路径\n* proxy.smarty({ext:'',data:data}) 解析smarty模板，data可以是json数据或者func,func参数为文件路径\n* proxy.listen(port|config) 启动监听端口,假如需同时启动https,可以proxy.listen({port:3000,https:true}),则会自动增加https监听\n* ctx.hasSend() 判断是否发送过数据\n* ctx.isLocal() 判断当前请求是否是请求本地文件\n* ctx.isBinary(filename) 判断本地文件是否是二进制文件\n* ctx.sendFile(filepath) 发送本地文件\n\n\n## 事件(proxy.on(event,function(data){}))\n* http-server-start http服务器启动完成后触发\n* https-server-start  https服务器启动完成后触发\n* start: 请求开始时触发\n* end: 请求结束时触发\n\n\n### 本地开发\n\n执行**npm install** 安装依赖\n执行**npm run build** 编译src到lib目录\n\n\n### 注意问题\n* ctx.request.host不能直接修改，需要通过ctx.request.header.host修改\n\n## api详细解释\n\n###  proxy.listen(3000)\n\n启动监听端口,启动http和https服务器和代理,默认不启动https代理\n\n*  {number|object} config 当为number时,表示http服务器端口号,默认3000。\n         当为object时,存在下列参数\n*  {number} config.port http服务器端口号\n*  {boolean} config.https 是否启动https代理,默认不启动\n*  {string} config.loadCertUrl 下载密匙的链接,只要配置代理后访问带有该链接的地址就可以下载密匙,默认: proxy.com\n*  {string} config.key https服务器的密匙,可省略\n*  {string} config.cert https服务器的公匙,可省略(密匙和公匙必须配对)\n*  {function} callback 启动http服务器后的回调函数,参数err\n\n\n**示例:**\n\n```\nvar proxy = require('koa2-proxy');\nproxy.static(__dirname + '/output/');\nproxy.listen(8000);\n```\n\n\n**示例:**\n\n```\n// 启动https代理,则可以如下配置\nvar proxy = require('koa2-proxy');\nproxy.static(__dirname + '/output/');\nproxy.listen({port: 8000,https: true});\n```\n\n\n###  proxy.when([condition,] callback)\n\n当请求的内容和condition匹配时,执行callback\n\n*  condition\n       {string|reg} condition url包含string或者reg.test(url)为tru时,将执行callback\n       {object} condition 匹配条件列表,其属性值可以是header的字段或者host,fullUrl,url,phase,method等\n           - {string|reg|function} condition.url 匹配url(host之后的部分)\n           - {string|reg|function} condition.fullUrl (匹配)\n           - {string} condition.phase 匹配阶段,request或者response,默认request\n           - {string|reg|function} condition.cookie\n           - {string|reg|function} ...  匹配其他任意header字段\n*  {function} callback 匹配时执行的函数,参数ctx\n\n\n**示例:** test.html的内容设置为test\n\n```\nproxy.when('test.html',function(ctx){\n    ctx.response.body = 'test';\n});\n```\n\n\n**示例:** test.html的内容增加一个div\n\n```\nproxy.when({url:'test.html',phase: 'response' },function(ctx){\n    ctx.response.body +='\u003cdiv\u003etest\u003c/div\u003e';\n});\n```\n\n\n###  proxy.index('/index.html')\n\n浏览根网址时自动跳转到指定地址\n\n*  {string} url 跳转地址,默认/index.html\n\n\n###  proxy.mockfile(mockfile, needLocal)\n\n匹配规则之后发送模拟数据,主要用来mock请求数据\n\n*  {string} mockfile 匹配规则文件路径\n 匹配规则说明\n 匹配规则对应文件只有以下列单词开头的行才会匹配规则,其他任何内容开头将作为注释\n     root folder # 指定根目录(其他文件地址相对mockfile文件地址)\n     rewrite reg rewriteUrl #匹配到正则，发送文件\n     replace reg replaceWith  #修改请求url,替换reg的内容为replaceWith\n     redirect reg redirectUrl #匹配到正则，则转发到新的url\n     exec reg execFile\n*  {boolean} needLocal 是否需要是本地请求,默认true,主要用来避免代理时污染代理请求\n\n\n**示例:** 配置某个文件作为模拟规则文件\n\n```\nproxy.mockfile(__dirname + '/server.conf');\n```\n\n\n**示例:** 部分路径不使用模拟文件配置\n\n```\nproxy.when('/test1/',function (ctx) {\n    ctx.mockfile = false;\n});\nproxy.mockfile(__dirname + '/server.conf');\n```\n\n\n###  proxy.open('/index.html')\n\n使用浏览器打开指定网页,假如不指定域名,则会使用localhost和监听端口(listen调用时的监听端口)\n\n*  {string} url 打开的网址\n\n\n###  proxy.static(root, opts)\n\n创建静态文件服务器\n\n*  {string} root 静态文件根目录\n*  {object} opts 其他可选参数\n         - opts.path {string}  匹配的路径,匹配到该路径时,匹配后剩余的路径存在文件时才发送文件,默认为空\n         - opts.index {string|array} 假如浏览的是目录,则自动发送其下存在的文件,默认为index.html,可以指定多个文件,例如[\"index.html\",\"index.htm\"]\n         - opts.list  当浏览的是目录并且不存在默认发送文件时,发送目录下文件列表\n             - {boolean}  是否发送文件列表\n             - {string} 内容不为空时发送,opt.list将一起发送\n             - {function} 自定义发送内容格式,参数(list, pathname, localFolder)\n\n\n**示例:**\n\n```\nproxy.static(__dirname + '/output', {path: '/static/', index: 'index.html'});\n```\n\n## 版本更新(使用lazy-doc打包自doc/history.md)\n* **1.1.7(2017.11.09)**\n    - fix: issues/5 文件读取函数错误；转发application/json格式\n* **1.1.6(2017.11.09)**\n    - fix: issues/4 修改protocol不生效的问题\n* **1.1.4(2017.09.15)**\n    - feat: 使用mime-types计算文件的content-type\n    - fix: 部分文件类型计算问题，form-data判定问题\n* **1.1.3(2017.08.03)**\n    - fix：上传文件bug\n* **1.1.2(2017.07.31)**\n    - fix无文件时body错误\n* **1.1.0(2017.07.27)**\n    - 静态资源文件夹路径增加斜线\n    - 使用`koa-match`替代内置when\n    - 上传文件处理\n* **1.0.17(2017.03.01)**\n    - 增加函数proxy.index,浏览根目录时自动跳转\n* **1.0.16(2017.03.01)**\n    - 增加函数proxy.open,自动打开浏览器\n* **1.0.15(2017.02.23)**\n    - 完善dependencies\n* **1.0.14(2016.12.29)**\n    - 使用.npmignore配置npm发布时目录\n* **1.0.13(2016.12.06)**\n    - mockfile第二个参数的needLocal默认值改为false\n    - ctx.mockfile设为false时将不使用模拟配置\n* **1.0.12(2016.10.18)**\n    - 取消版本检查\n    - 删除加载数据header的content-length\n    - 文件列表样式\n* **1.0.11(2016.08.18)**\n    - proxy.static增加显示文件夹内文件列表\n* **1.0.10(2016.08.12)**\n    - 使用lazy-doc生成文档\n    - when增加支持method和host等\n    - when(condition,callback)的condition可以忽略,这时所有的请求都将调用callback\n* **1.0.9(2016.08.11)**\n    - 检查是否存在新版本并提示\n    - 修复一些bug;\n* **1.0.5(2016.07.26)**\n    - proxy.smarty({ext:,root:,data:}): 完善smarty函数\n* **1.0.4(2016.07.22)**\n    - proxy.when(conditions, callback):参数conditions增加local\n    - proxy.static(root, opts): 参数opts增加path和index\n* **1.0.3(2016.07.15)**\n    - 增加request.body\n    - 请求form发送\n    - 增加proxy.localip和proxy.localhost\n    - 默认取消监听https,只有显示指示https时才启动https\n* **1.0.2** load模块bug修复\n* **1.0.1** 增加proxy.when等函数，远程加载图片内容\n* **1.0.0** 代理功能\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaomu1988%2Fkoa2-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaomu1988%2Fkoa2-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaomu1988%2Fkoa2-proxy/lists"}