diff --git a/README.md b/README.md index 96bf7dd..16c806b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 0b5bf8f..a58872d 100644 --- a/index.js +++ b/index.js @@ -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){ @@ -39,7 +44,7 @@ module.exports = function(options) { return yield* next; } } - + var parsedBody = getParsedBody(this); var opt = { @@ -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') { @@ -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); } @@ -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) { diff --git a/test/index.js b/test/index.js index 855166d..28f9799 100644 --- a/test/index.js +++ b/test/index.js @@ -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({