Skip to content

Commit

Permalink
add close method, optimise mocha test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
alanchenchen committed Dec 18, 2018
1 parent ad83653 commit 6d7d276
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
> Author : Alan Chen
> version: 0.0.5
> version: 0.0.6
> node >= 8.11.1
Expand Down Expand Up @@ -38,6 +38,7 @@
* `proxyError` 在代理服务器接收客户端请求或转发请求发生错误时触发,函数有2个参数
* error `[Error]` 错误对象
* from `[String]` server或client中其一种字符串。server表示错误发生在代理服务请求出错。client表示代理服务器接收客户端请求出错。
4. ProxyHttp实例还自带一个`close`方法,使用方法和node的http模块类似。可选一个回调函数,当关闭服务器后触发。

### staticServer
导出一个对象,自带1个方法`start`。返回一个promise,promise只会存在then,then返回一个布尔值,用来判断当前路径是否存在静态文件。true,会返回文件,false,表示当前路径不存在静态文件。参数如下:
Expand Down
10 changes: 6 additions & 4 deletions example/emitEvents/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ http.createServer((req, res) => {
// 自启内置服务器做代理
// const ins = HttpProxyPlugin.createProxyServer({
// target: 'http://localhost:7070'
// }).listen(3000, '192.168.0.43')
// }).listen(3000, () => {
// console.log('反向代理服务器启动成功,监听3000端口...')
// })

// ins.on('proxyRequest', (proxyReq, opt) => {
// // console.log(ins)
Expand Down Expand Up @@ -63,6 +65,6 @@ const generateHanlder = (port) => {
}
}

http.createServer(generateHanlder(PORT)).listen(PORT, () => {
console.log(`目标服务器启动成功,监听${PORT}...`)
})
// http.createServer(generateHanlder(PORT)).listen(PORT, () => {
// console.log(`目标服务器启动成功,监听${PORT}...`)
// })
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alanchenchen/httpproxyer",
"version": "0.0.5",
"version": "0.0.6",
"author": "Alan Chen",
"license": "MIT",
"description": "A simple http server proxy module implented with nodejs",
Expand Down
19 changes: 17 additions & 2 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,30 @@ class ProxyHttp {
return this
}

listen(port=80, host='localhost', cb) {
http.createServer(this.proxyServerListener).listen(port, host, () => {
listen(port=80, ...rest) {
let host = 'localhost'
let cb
if(rest.length == 1) {
cb = rest[0]
}
else if(rest.length == 2) {
host = rest[0]
cb = rest[1]
}
this.proxyServer = http.createServer(this.proxyServerListener).listen(port, host, () => {
// console.log(`proxy server http://${host}:${port} to ${this.targetServer} is running...`)
cb && cb()
})

return this
}

close(cb) {
this.proxyServer.close(() => {
cb && cb()
})
}

on(event, handler) {
switch (event) {
case 'proxyRequest':
Expand Down
53 changes: 35 additions & 18 deletions test/proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ const assert = require('assert')
const http = require('http')
const proxyPlugin = require('../src/proxy')

// target server 7070
http.createServer((req, res) => {
const reqHeaders = req.headers
// return the headers from request info
Object.entries(reqHeaders).forEach(item => {
res.setHeader(item[0], item[1])
})
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf8'
})
res.end(JSON.stringify({
port: 7070,
msg: 'success'
}))
}).listen(7070)

describe('httpProxyer', function() {
// target server 7070
const targetServer = http.createServer((req, res) => {
const reqHeaders = req.headers
// return the headers from request info
Object.entries(reqHeaders).forEach(item => {
res.setHeader(item[0], item[1])
})
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf8'
})
res.end(JSON.stringify({
port: 7070,
msg: 'success'
}))
}).listen(7070)

describe('init one inner proxy server, use createProxyServer()', function() {
let server
afterEach(function() {
server.close()
})
it('should proxy server 7070 to server 3000', function(done) {
// proxy server 3000
proxyPlugin.createProxyServer({
server = proxyPlugin.createProxyServer({
target: 'http://localhost:7070'
}).listen(3000)

Expand All @@ -43,9 +48,13 @@ describe('httpProxyer', function() {
})

describe('proxy an existing server, use proxy()', function() {
let server
afterEach(function() {
server.close()
})
it('should proxy server 7070 to server 3001', function(done) {
// proxy server 3001
http.createServer((req, res) => {
server = http.createServer((req, res) => {
proxyPlugin.proxy(req, res, {
target: 'http://localhost:7070'
})
Expand All @@ -68,9 +77,13 @@ describe('httpProxyer', function() {
})

describe('use the event hooks by on()', function() {
let server
afterEach(function() {
server.close()
})
it('should intercept the req or res data', function(done) {
// proxy server 3002
http.createServer((req, res) => {
server = http.createServer((req, res) => {
const ins = proxyPlugin.proxy(req, res, {
target: 'http://localhost:7070'
})
Expand Down Expand Up @@ -98,4 +111,8 @@ describe('httpProxyer', function() {
})
})
})
after('if all tests succeed, close the http process', function() {
targetServer.close()
})

})

0 comments on commit 6d7d276

Please sign in to comment.