Skip to content

Commit

Permalink
support poxying https request and add path inherit param
Browse files Browse the repository at this point in the history
  • Loading branch information
alanchenchen committed Apr 24, 2019
1 parent 2c780bd commit eb4b585
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 159 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# httpProxyer

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

Expand All @@ -22,6 +22,13 @@

</div>

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

## Usage

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

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

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

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

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

## license
* MIT
* Anti 996(996.ICU)
File renamed without changes.
21 changes: 12 additions & 9 deletions example/express/server.js → example/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ app.use(static({
rootPath: '../../'
}))

app.use(proxer('http://localhost:4000', {
proxyRequest(proxyReq, opt) {
proxyReq.setHeader('whoami', 'AlanChen')
// console.log(opt)
console.log('请求被拦截啦')
},
proxyError(err, from) {
console.log(err)
console.log(from)
app.use(proxer({
target: 'http://localhost:4000',
hooks: {
proxyRequest(proxyReq, opt) {
proxyReq.setHeader('whoami', 'AlanChen')
// console.log(opt)
console.log('请求被拦截啦')
},
proxyError(err, from) {
console.log(err)
console.log(from)
}
}
}))

Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions example/proxyHttpsRequest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {proxy, createProxyServer} = require('../../src/core/proxy/index')

const URL = 'https://alancc.cn'
const ins = createProxyServer({
target: URL,
inherit: true
})
ins.on('proxyRequest', (req, info) => {
console.log(info.path)
// console.log(info)
// req.setHeader('Referer', 'http://www.cnblogs.com/')
// req.setHeader('Purpose', 'prefetch')
})
.on('proxyError', (error, from) => {
console.log(error)
})
.listen(8080, '10.69.65.80', () => {
console.log('server is running at http://10.69.65.80:8080')
})

// console.log(ins)
// setTimeout(() => {
// ins.close(() => {
// console.log('proxy server has been closed')
// })
// }, 2000)
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ const http = require('http')
*/
http.createServer(async (req, res) => {
// 路径匹配成功,返回静态文件
const flag = await staticFile.start(req, res, {
rootPath: '../../',
homePage: 'package.json'
})

// 路径匹配失败,转发代理请求
if(!flag) {
httpProxyer.proxy(req, res, {
target: 'http://127.0.0.1:8080'
try {
await staticFile.start(req, res, {
rootPath: '../../',
homePage: 'package.json'
})
}
} catch (error) {
if(error) {
// 路径匹配失败,转发代理请求
httpProxyer.proxy(req, res, {
target: 'https://www.baidu.com'
})
}
}

}).listen(7000, () => {
console.log('server is running at 7000')
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@alanchenchen/httpproxyer",
"version": "0.0.7",
"version": "0.0.8",
"author": "Alan Chen",
"license": "MIT",
"license": "996.ICU",
"description": "A simple http server proxy module implented with nodejs",
"main": "src/index.js",
"directories": {
Expand Down
Loading

0 comments on commit eb4b585

Please sign in to comment.