From 2f6a15fa224eb63b448de275655b96e03da8e33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=9B=E8=82=9A?= Date: Mon, 6 Aug 2018 11:38:03 +0800 Subject: [PATCH 1/4] support func match option --- index.js | 10 ++++++++-- test/index.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0b5bf8f..8fed660 100644 --- a/index.js +++ b/index.js @@ -35,11 +35,17 @@ module.exports = function(options) { // if match option supplied, restrict proxy to that match if (options.match) { - if (!this.path.match(options.match)) { + var isMatched = false; + if (typeof options.match === 'function') { + isMatched = options.match(options.math); + } else { + isMatched = this.path.match(options.match); + } + if (!isMatched) { return yield* next; } } - + var parsedBody = getParsedBody(this); var opt = { diff --git a/test/index.js b/test/index.js index 855166d..9e9318f 100644 --- a/test/index.js +++ b/test/index.js @@ -203,6 +203,30 @@ describe('koa-proxy', function() { }); }); + it('shold have option host and match function', function(done) { + var app = koa(); + app.use(proxy({ + host: 'http://localhost:1234', + match: function(str){ + return /^\/[a-z]+\.js$/.test(str) + } + })); + app.use(proxy({ + host: 'http://localhost:1234' + })); + var server = http.createServer(app.callback()); + request(server) + .get('/class.js') + .expect(200) + .expect('Content-Type', /javascript/) + .end(function (err, res) { + if (err) + return done(err); + res.text.should.startWith('define("arale/class/1.0.0/class"'); + done(); + }); + }); + it('should have option followRedirect', function(done) { var app = koa(); app.use(proxy({ From 03c6c9ac900599b4192f7550178fe8120b5b61bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=9B=E8=82=9A?= Date: Mon, 6 Aug 2018 11:52:23 +0800 Subject: [PATCH 2/4] update readme --- README.md | 12 ++++++++++++ package.json | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96bf7dd..0000e3a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +Forked from [popomore/koa-proxy](https://github.com/popomore/koa-proxy) + # koa-proxy [![Build Status](https://travis-ci.org/popomore/koa-proxy.png?branch=master)](https://travis-ci.org/popomore/koa-proxy) [![Coverage Status](https://coveralls.io/repos/popomore/koa-proxy/badge.png?branch=master)](https://coveralls.io/r/popomore/koa-proxy?branch=master) Proxy middleware for koa @@ -70,6 +72,16 @@ app.use(proxy({ })); ``` +Or you can use match of function style to exclude a specific path. +```js +app.use(proxy({ + host: 'http://alicdn.com', // proxy alicdn.com... + match: function match(path) { return /^(?!\/dontproxy\.html)/.test(path); } // ...everything except /dontproxy.html +})); +``` + + + Proxy won't send cookie to real server, you can set `jar = true` to send it. ```js diff --git a/package.json b/package.json index 14293b0..fe9696c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "koa-proxy", - "version": "0.9.0", + "name": "koa-proxy-b", + "version": "0.1.0", "description": "Proxy middleware for koa", "main": "index", "dependencies": { From 6d8a989d052b6a77461d2e6ae2100f093cf4c33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=9B=E8=82=9A?= Date: Mon, 6 Aug 2018 11:55:36 +0800 Subject: [PATCH 3/4] add user --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fe9696c..79d42ee 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "url": "https://github.com/popomore/koa-proxy" }, "homepage": "https://github.com/popomore/koa-proxy", - "author": "popomore ", + "author": "popomore , L1uD0ngq1 ", "license": "MIT", "scripts": { "test": "make test" From 770fec516f8d884b79852322d9ae26f76ebe5cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=9B=E8=82=9A?= Date: Mon, 3 Sep 2018 10:44:27 +0800 Subject: [PATCH 4/4] support func match --- README.md | 2 -- index.js | 2 +- package.json | 6 +++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0000e3a..fccb364 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -Forked from [popomore/koa-proxy](https://github.com/popomore/koa-proxy) - # koa-proxy [![Build Status](https://travis-ci.org/popomore/koa-proxy.png?branch=master)](https://travis-ci.org/popomore/koa-proxy) [![Coverage Status](https://coveralls.io/repos/popomore/koa-proxy/badge.png?branch=master)](https://coveralls.io/r/popomore/koa-proxy?branch=master) Proxy middleware for koa diff --git a/index.js b/index.js index 8fed660..9b57ba9 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ module.exports = function(options) { if (options.match) { var isMatched = false; if (typeof options.match === 'function') { - isMatched = options.match(options.math); + isMatched = options.match(this.path); } else { isMatched = this.path.match(options.match); } diff --git a/package.json b/package.json index 79d42ee..14293b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "koa-proxy-b", - "version": "0.1.0", + "name": "koa-proxy", + "version": "0.9.0", "description": "Proxy middleware for koa", "main": "index", "dependencies": { @@ -28,7 +28,7 @@ "url": "https://github.com/popomore/koa-proxy" }, "homepage": "https://github.com/popomore/koa-proxy", - "author": "popomore , L1uD0ngq1 ", + "author": "popomore ", "license": "MIT", "scripts": { "test": "make test"