-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use strict" | ||
const utils = require('./utils') | ||
module.exports = function(app, servers){ | ||
return function(location, options){ | ||
app.emit('host', { app: app, location: location, options: options }) | ||
let host = utils.getHost(location, options) | ||
app.hosts[host.location.host] = app | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
module.exports.isset = function isset(object){ | ||
var url = require('url') | ||
var Utils = new Object() | ||
|
||
Utils.isset = function isset(object){ | ||
return (object != "undefined" && object != undefined && object != null && object != "" && typeof(object) != 'undefined') ? true : false ; | ||
} | ||
} | ||
|
||
Utils.getHost = function(inputLocation, options){ | ||
var options = !options || typeof options == 'function' ? {} : options ; | ||
var protocolName = (typeof options == 'object' && options.cert || options.key || options.ca) ? 'https' : 'http' ; | ||
|
||
// define location | ||
if(!isNaN(inputLocation)) { | ||
var location = url.parse(protocolName+'://0.0.0.0:'+inputLocation); | ||
|
||
} else if(typeof inputLocation == 'string') { | ||
var location = inputLocation.indexOf('://') == -1 ? 'http://' + inputLocation : inputLocation ; | ||
location = url.parse(location) | ||
|
||
} else if(typeof inputLocation == 'object') { | ||
var location = inputLocation; | ||
|
||
} else if(!Utils.isset(inputLocation)){ | ||
var location = url.parse(protocolName+'://0.0.0.0:80/'); | ||
} | ||
|
||
var port = location.protocol === 'http:' && (!options || (!options.cert && !options.key)) | ||
? (location.port || 80) | ||
: (location.port || 443) ; | ||
|
||
return { port: port, location: location } | ||
} | ||
|
||
module.exports = Utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var server = require('../../') | ||
var request = require('request') | ||
|
||
var proxy = server() | ||
proxy.listen('test.local.com:3000') | ||
proxy.header(function($){ | ||
request.get('http://test2.local.com:5000/', function(error, response, body){ | ||
if(error) throw error; | ||
$.send('\nPort 3000 reached\n\nResponse from http://test2.local.com/:\n') | ||
$.end(body) | ||
}); | ||
}) | ||
|
||
var app = server() | ||
app.listen('test.local.com:5000') | ||
app.host('test2.local.com:5000') | ||
app.get('/', function($){ | ||
$.end('Hello world!') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var server = require('../'); | ||
var assert = require('assert'); | ||
var request = require('request'); | ||
var subject = 'Test'.cyan+' → '.grey+ 'Events'.yellow + ': '.grey; | ||
|
||
describe(subject + 'initialize', function(){ | ||
|
||
|
||
var proxy = server() | ||
proxy.listen('test.local.com:9079') | ||
proxy.header(function($){ | ||
request.get('http://test2.local.com:9078/', function(error, response, body){ | ||
if(error) throw error; | ||
assert.equal(response.headers['content-type'], 'text/plain'); | ||
assert.equal(response.statusCode, 200); | ||
assert.equal(body, 'PROXY_HOST_TEST'); | ||
$.end(body) | ||
}); | ||
}) | ||
|
||
var app = server() | ||
app.listen('test.local.com:9078') | ||
app.host('test2.local.com:9078') | ||
app.get('/', function($){ | ||
$.end('PROXY_HOST_TEST') | ||
}) | ||
|
||
it('should test app.host()', function(done){ | ||
|
||
request.get('http://test.local.com:9079/', function(error, response, body){ | ||
if(error) throw error; | ||
assert.equal(body, 'PROXY_HOST_TEST'); | ||
assert.equal(response.headers['content-type'], 'text/plain'); | ||
assert.equal(response.statusCode, 200); | ||
done(); | ||
}); | ||
}); | ||
}) |