Skip to content

Commit eb4b585

Browse files
committed
support poxying https request and add path inherit param
1 parent 2c780bd commit eb4b585

File tree

13 files changed

+278
-159
lines changed

13 files changed

+278
-159
lines changed

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# httpProxyer
22

3-
> 代理转发http请求的node包,无依赖,支持反向代理实现负载均衡,提供事件钩子函数拦截并重写数据
3+
> 代理转发http和https请求的node包,无依赖
44
55
> Author : Alan Chen
66
7-
> version: 0.0.7
7+
> version: 0.0.8
88
99
> node >= 8.11.1
1010
11-
> Date: 2019/1/4
11+
> Date: 2019/4/23
1212
1313
<div align="center">
1414

@@ -22,6 +22,13 @@
2222

2323
</div>
2424

25+
## Features
26+
1. 支持代理http和https请求,支持自启一个代理服务器。自启的服务器只支持http的protocol
27+
2. 支持反向代理实现负载均衡
28+
3. 提供事件钩子函数拦截并重写数据
29+
4. 提供express的中间件
30+
5. 提供静态文件服务器工具
31+
2532
## Usage
2633

2734
1. `npm install --save @alanchenchen/httpproxyer` or `yarn add @alanchenchen/httpproxyer`
@@ -37,12 +44,16 @@
3744
#### httpProxyer对象方法
3845
导出一个对象,自带2个方法,每个方法调用一次都会返回一个ProxyHttp实例。
3946

40-
1. createProxyServer 启用一个服务器事件监听器,必须调用listen方法才能开启服务器监听并代理转发。常用于正向代理。参数如下:
41-
* opts `[Object]`, 目前只支持一个target的key。值必须是代理服务器的http地址,例如:http://127.0.0.1:7070
47+
1. createProxyServer 启用一个服务器事件监听器,必须调用listen方法才监听端口并代理转发。常用于正向代理。参数如下:
48+
* opts `[Object]`
49+
* target `[String]`。必须是代理服务器的http或https地址,例如:http://127.0.0.1:7070
50+
* inherit `[String]`。可选,转发请求是否继承当前target的query和hash信息
4251
2. proxy 代理转发已有服务器的请求,可以实现反向代理和负载均衡。参数如下:
4352
* IncomingMessage `[可读流]`,Http Server类request事件的第一个参数req
4453
* ServerResponse `[可写流]`,Http Server类request事件的第二个参数res
45-
* opts `[Object]`, 目前只支持一个target的key。值必须是代理服务器的http地址,例如:http://127.0.0.1:7070
54+
* opts `[Object]`
55+
* target `[String]`。必须是代理服务器的http或https地址,例如:http://127.0.0.1:7070
56+
* inherit `[String]`。可选,转发请求是否继承当前target的query和hash信息
4657

4758
#### ProxyHttp实例
4859
1. ProxyHttp实例支持事件监听,通过`on(event, callback)`来调用,第一个参数是事件名,第二个参数是回调函数。目前支持3个事件钩子:
@@ -55,18 +66,18 @@
5566
* opts `[Object]`,包含响应头和http状态码的信息对象,只读
5667
* `proxyError` 在代理服务器接收客户端请求或转发请求发生错误时触发,函数有2个参数
5768
* error `[Error]` 错误对象
58-
* from `[String]` server或client中其一种字符串。server表示错误发生在代理服务请求出错。client表示代理服务器接收客户端请求出错。
69+
* from `[String]` server或client其中一字符串。server表示错误发生在代理服务请求出错。client表示代理服务器接收客户端请求出错。
5970
2. ProxyHttp实例还自带一个`close`方法,使用方法和node的http模块类似。可选一个回调函数,当关闭服务器后触发。
6071

6172
### staticServer
62-
导出一个类,自带1个静态方法`start`。返回一个promise,promise只会存在then,then返回一个布尔值,用来判断当前路径是否存在静态文件。true,会返回文件,false,表示当前路径不存在静态文件。参数如下:
73+
导出一个类,自带1个静态方法`start`。返回一个promise,then表示当前路径是否存在静态文件,会返回文件,reject表示当前路径不存在静态文件,会返回一个Error对象。参数如下:
6374
* IncomingMessage `[可读流]`,Http Server类request事件的第一个参数req
6475
* ServerResponse `[可写流]`,Http Server类request事件的第二个参数res
6576
* opts `[Object]`, 目前支持两个key。
6677
* rootPath `[String]` 指定文件目录作为服务器根目录,默认为'/',即进程运行的的目录
6778
* homePage `[String]` 当req的url为'/'时跳转的首页文件,默认为'index.html'
6879

69-
> 基于两个插件实现的express中间件`proxyMiddleware``staticMiddleware`用法同上面类似,可以去[example/express](./example/express/server.js)看详细例子。
80+
> 基于两个插件实现的express中间件`proxyMiddleware``staticMiddleware`用法同上面类似,可以去[example/express](./example/express/index.js)看详细例子。
7081
7182
## Unit tests
7283
* test目录里目前只有一个测试用例,分别测试了`httpProxyer``createProxyServer()``proxy()``on()`方法。
@@ -77,4 +88,4 @@
7788
4. `npm test`在终端terminal查看测试结果
7889

7990
## license
80-
* MIT
91+
* Anti 996(996.ICU)
File renamed without changes.

example/express/server.js renamed to example/express/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ app.use(static({
1111
rootPath: '../../'
1212
}))
1313

14-
app.use(proxer('http://localhost:4000', {
15-
proxyRequest(proxyReq, opt) {
16-
proxyReq.setHeader('whoami', 'AlanChen')
17-
// console.log(opt)
18-
console.log('请求被拦截啦')
19-
},
20-
proxyError(err, from) {
21-
console.log(err)
22-
console.log(from)
14+
app.use(proxer({
15+
target: 'http://localhost:4000',
16+
hooks: {
17+
proxyRequest(proxyReq, opt) {
18+
proxyReq.setHeader('whoami', 'AlanChen')
19+
// console.log(opt)
20+
console.log('请求被拦截啦')
21+
},
22+
proxyError(err, from) {
23+
console.log(err)
24+
console.log(from)
25+
}
2326
}
2427
}))
2528

File renamed without changes.

example/proxyHttpsRequest/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const {proxy, createProxyServer} = require('../../src/core/proxy/index')
2+
3+
const URL = 'https://alancc.cn'
4+
const ins = createProxyServer({
5+
target: URL,
6+
inherit: true
7+
})
8+
ins.on('proxyRequest', (req, info) => {
9+
console.log(info.path)
10+
// console.log(info)
11+
// req.setHeader('Referer', 'http://www.cnblogs.com/')
12+
// req.setHeader('Purpose', 'prefetch')
13+
})
14+
.on('proxyError', (error, from) => {
15+
console.log(error)
16+
})
17+
.listen(8080, '10.69.65.80', () => {
18+
console.log('server is running at http://10.69.65.80:8080')
19+
})
20+
21+
// console.log(ins)
22+
// setTimeout(() => {
23+
// ins.close(() => {
24+
// console.log('proxy server has been closed')
25+
// })
26+
// }, 2000)

example/staticFileServer/main.js renamed to example/staticFileServer/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ const http = require('http')
1515
*/
1616
http.createServer(async (req, res) => {
1717
// 路径匹配成功,返回静态文件
18-
const flag = await staticFile.start(req, res, {
19-
rootPath: '../../',
20-
homePage: 'package.json'
21-
})
22-
23-
// 路径匹配失败,转发代理请求
24-
if(!flag) {
25-
httpProxyer.proxy(req, res, {
26-
target: 'http://127.0.0.1:8080'
18+
try {
19+
await staticFile.start(req, res, {
20+
rootPath: '../../',
21+
homePage: 'package.json'
2722
})
28-
}
23+
} catch (error) {
24+
if(error) {
25+
// 路径匹配失败,转发代理请求
26+
httpProxyer.proxy(req, res, {
27+
target: 'https://www.baidu.com'
28+
})
29+
}
30+
}
31+
2932
}).listen(7000, () => {
3033
console.log('server is running at 7000')
3134
})

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@alanchenchen/httpproxyer",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"author": "Alan Chen",
5-
"license": "MIT",
5+
"license": "996.ICU",
66
"description": "A simple http server proxy module implented with nodejs",
77
"main": "src/index.js",
88
"directories": {

0 commit comments

Comments
 (0)