Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Support func matcher #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down