{"id":19974423,"url":"https://github.com/alanchenchen/httpproxyer","last_synced_at":"2025-07-10T20:05:57.920Z","repository":{"id":57096123,"uuid":"161465592","full_name":"alanchenchen/httpProxyer","owner":"alanchenchen","description":"A simple http server proxy module implemented with nodejs","archived":false,"fork":false,"pushed_at":"2019-12-06T03:28:37.000Z","size":103,"stargazers_count":10,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-29T22:17:37.409Z","etag":null,"topics":["file-server","nodejs","nodejs-server","proxy","proxy-cluster","proxy-server","reverse-proxy","static-file-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alanchenchen.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}},"created_at":"2018-12-12T09:39:26.000Z","updated_at":"2022-11-15T06:10:54.000Z","dependencies_parsed_at":"2022-08-22T21:40:56.818Z","dependency_job_id":null,"html_url":"https://github.com/alanchenchen/httpProxyer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/alanchenchen/httpProxyer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanchenchen%2FhttpProxyer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanchenchen%2FhttpProxyer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanchenchen%2FhttpProxyer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanchenchen%2FhttpProxyer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alanchenchen","download_url":"https://codeload.github.com/alanchenchen/httpProxyer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanchenchen%2FhttpProxyer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264647056,"owners_count":23643546,"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":["file-server","nodejs","nodejs-server","proxy","proxy-cluster","proxy-server","reverse-proxy","static-file-server"],"created_at":"2024-11-13T03:14:58.340Z","updated_at":"2025-07-10T20:05:57.871Z","avatar_url":"https://github.com/alanchenchen.png","language":"JavaScript","readme":"# httpProxyer\n\n\u003e 代理转发http和https请求的node包，无依赖\n\n\u003e Author : Alan Chen\n\n\u003e version: 0.1.0\n\n\u003e node \u003e= 8.11.1\n\n\u003e Date: 2019/12/4\n\n\u003cdiv align=\"center\"\u003e\n\n[![](assets/logo.png)](https://www.npmjs.com/package/@alanchenchen/httpproxyer)\n\n[![Build Status](https://travis-ci.org/alanchenchen/httpProxyer.svg?branch=master)](https://travis-ci.org/alanchenchen/httpProxyer)\n![](https://img.shields.io/npm/v/@alanchenchen/httpproxyer.svg)\n![](https://img.shields.io/node/v/@alanchenchen/httpproxyer.svg)\n![](https://img.shields.io/npm/dt/@alanchenchen/httpproxyer.svg)\n[![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE)\n\n\u003c/div\u003e\n\n## Features\n1. 支持代理http和https请求，支持自启一个代理服务器。自启的服务器只支持http的protocol\n2. 支持反向代理实现负载均衡\n3. 提供事件钩子函数拦截并重写数据\n4. 提供express的中间件\n5. 提供静态文件服务器工具\n\n## Usage \n\n1. `npm install --save @alanchenchen/httpproxyer` or `yarn add @alanchenchen/httpproxyer`\n2. npm包内置两个原生node编写的插件，和两个实现了express中间件的插件。\n3. npm包默认导出一个对象，包含4个key，分别是：\n    * httpProxyer\n    * staticServer\n    * proxyMiddleware\n    * staticMiddleware\n\n## Options\n### httpProxyer\n#### httpProxyer对象方法\n导出一个对象，自带2个方法，每个方法调用一次都会返回一个ProxyHttp实例。\n\n1. createProxyServer 启用一个服务器事件监听器，必须调用listen方法才监听端口并代理转发。常用于正向代理。参数如下：\n    * opts `[Object]`，\n        *  target `[String]`。必须是代理服务器的http或https地址，例如：http://127.0.0.1:7070\n        *  inherit `[String]`。可选，转发请求是否继承当前target的query和hash信息\n2. proxy 代理转发已有服务器的请求,可以实现反向代理和负载均衡。参数如下：\n    * IncomingMessage `[可读流]`，Http Server类request事件的第一个参数req\n    * ServerResponse `[可写流]`，Http Server类request事件的第二个参数res\n    * opts `[Object]`，\n        *  target `[String]`。必须是代理服务器的http或https地址，例如：http://127.0.0.1:7070\n        *  inherit `[String]`。可选，转发请求是否继承当前target的path信息,默认只取客户端的path\n\n#### ProxyHttp实例 \n1. ProxyHttp实例支持事件监听，通过`on(event, callback)`来调用，第一个参数是事件名，第二个参数是回调函数。目前支持3个事件钩子：\n    * `proxyRequest` 在转发请求之前触发，函数有2个参数\n        * proxyReq `[可写流]`，代理服务器请求目标服务器的数据，当调用`proxyReq.write()`方法或`proxyReq.end()`方法或`pipe(proxyReq)`会重写请求数据。否则默认转发客户端请求\n        * opts `[Object]`，包含请求头和请求url之类的信息对象，只读\n    * `proxyResponse` 在转发请求，目标服务器数据响应成功，返回响应数据到客户端之前触发，函数有3个参数\n        * proxyRes `[可写流]`，代理服务器返回给源请求的数据，当调用`proxyRes.write()`方法或`proxyRes.end()`方法或`pipe(proxyRes)`会重写返回数据。否则默认返回目标服务器响应数据\n        * res `[可读流]`，目标服务器返回给代理服务器的数据\n        * opts  `[Object]`，包含响应头和http状态码的信息对象，只读\n    * `proxyError` 在代理服务器接收客户端请求或转发请求发生错误时触发，函数有2个参数\n        * error `[Error]` 错误对象\n        * from `[String]` server或client其中一字符串。server表示错误发生在代理服务请求出错。client表示代理服务器接收客户端请求出错。  \n2. ProxyHttp实例自带一个`listen`方法，使用方法和node的http模块类似。\n3. ProxyHttp实例自带一个`close`方法，使用方法和node的http模块类似。可选一个回调函数，当关闭服务器后触发。\n\n### staticServer \n导出一个类，自带1个静态方法`start`。返回一个promise，then表示当前路径是否存在静态文件，会返回文件，reject表示当前路径不存在静态文件，会返回一个Error对象。参数如下：\n* IncomingMessage `[可读流]`，Http Server类request事件的第一个参数req\n* ServerResponse `[可写流]`，Http Server类request事件的第二个参数res\n* opts `[Object]`， 目前支持:\n    * rootPath `[String]` 指定文件目录作为服务器根目录，默认为'/'，即进程运行的的目录\n    * homePage `[String]` 当req的url为'/'时跳转的首页文件，默认为'index.html'\n    * returnContentType `[Boolean]` 是否返回请求对应文件的content-type响应头，默认为true\n    * gzip `[Boolean]` 是否对静态文件gzip压缩，默认为false\n    * gzipExclude `[Array\u003cstring\u003e]` 忽略gzip压缩的文件后缀名list，默认为[]\n\n\u003e 如果开启了gzip，并且目录里存在原文件和被gzip压缩的文件，则插件不会再次压缩，会直接读取gz文件。这样避免了静态服务器的性能开销。但必须保证原文件存在，例如：`index.html`和`index.html.gz`。\n\n\u003e 基于两个插件实现的express中间件`proxyMiddleware`和`staticMiddleware`用法同上面类似，可以去[example/express](./example/express/index.js)看详细例子。 \n\n## Unit tests\n* test目录里目前只有一个测试用例，分别测试了`httpProxyer`的`createProxyServer()`、`proxy()`和`on()`方法。\n* 测试框架为mocha，如果需要增加测试用例，操作如下：\n    1. `git clone git@github.com:alanchenchen/httpProxyer.git`\n    2. 在test目录里新增测试文件，约定测试文件必须是`*.test.js`后缀格式，必须在js后缀前加test后缀。\n    3. `yarn`或`npm install`安装开发依赖mocha\n    4. `npm test`在终端terminal查看测试结果\n\n## Attentions\n1. 当inherit为false时，代理转发时会取代理地址target的protocol、hostname、port以及客户端URL的path(包括query不包括hash)，当inherit为true时，代理转发的path路径会合并target的path和客户端URL的path\n2. https时，port会被强制重写为443。\n3. staticServer的rootPath合成的绝对路径会加上当前进程的绝对路径\n\n## license\n* Anti 996(996.ICU)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanchenchen%2Fhttpproxyer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falanchenchen%2Fhttpproxyer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanchenchen%2Fhttpproxyer/lists"}