diff --git a/README.md b/README.md index 96bf7dd..fccb364 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,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/index.js b/index.js index 0b5bf8f..9b57ba9 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(this.path); + } 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({