Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ app.use(proxy({
}));
```

You can use function to make host dependent on request

```js
app.use(proxy({
host: (req) => req.header['x-host']
}));
```
## LICENSE

Copyright (c) 2014 popomore. Licensed under the MIT license.
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ module.exports = function(options) {
}

return function* proxy(next) {
var url = resolve(this.path, options);
var host = options.host;
if (typeof options.host === 'function') {
host = options.host(this.request);
}

var url = resolve(host, this.path, options);

if(typeof options.suppressRequestHeaders === 'object'){
options.suppressRequestHeaders.forEach(function(h, i){
Expand All @@ -39,7 +44,7 @@ module.exports = function(options) {
return yield* next;
}
}

var parsedBody = getParsedBody(this);

var opt = {
Expand All @@ -51,8 +56,8 @@ module.exports = function(options) {
body: parsedBody,
};

// set 'Host' header to options.host (without protocol prefix), strip trailing slash
if (options.host) opt.headers.host = options.host.slice(options.host.indexOf('://')+3).replace(/\/$/,'');
// set 'Host' header to host (without protocol prefix), strip trailing slash
if (host) opt.headers.host = host.slice(host.indexOf('://')+3).replace(/\/$/,'');

if (options.requestOptions) {
if (typeof options.requestOptions === 'function') {
Expand Down Expand Up @@ -104,11 +109,11 @@ module.exports = function(options) {
};


function resolve(path, options) {
function resolve(host, path, options) {
var url = options.url;
if (url) {
if (!/^http/.test(url)) {
url = options.host ? join(options.host, url) : null;
url = host ? join(host, url) : null;
}
return ignoreQuery(url);
}
Expand All @@ -121,7 +126,7 @@ function resolve(path, options) {
path = options.map(path);
}

return options.host ? join(options.host, path) : null;
return host ? join(host, path) : null;
}

function ignoreQuery(url) {
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,43 @@ describe('koa-proxy', function() {
});
});

it('should work with option host as function', function(done) {
var app = koa();
app.use(proxy({
host: function() {return 'http://localhost:1234/'}
}));
var server = http.createServer(app.callback());
request(server)
.get('/class.js')
.expect(200)
.expect('Host', 'localhost:1234')
.end(function (err, res) {
if (err)
return done(err);
res.text.should.startWith('define("arale/class/1.0.0/class"');
done();
});
});

it('should pass request to host as function', function(done) {
var app = koa();
app.use(proxy({
host: function(req) {return req.header['x-host']}
}));
var server = http.createServer(app.callback());
request(server)
.get('/class.js')
.set('x-host', 'http://localhost:1234/')
.expect(200)
.expect('Host', 'localhost:1234')
.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 host and map', function(done) {
var app = koa();
app.use(proxy({
Expand Down