Commit 7976de11 authored by Dominic Tarr's avatar Dominic Tarr

support old (port,host) and (options) style when using middlewares

parent c773eede
...@@ -112,6 +112,7 @@ exports.getMaxSockets = function () { ...@@ -112,6 +112,7 @@ exports.getMaxSockets = function () {
exports.setMaxSockets = function (value) { exports.setMaxSockets = function (value) {
maxSockets = value; maxSockets = value;
}; };
// //
// stack // stack
// adapted from https://github.com/creationix/stack // adapted from https://github.com/creationix/stack
...@@ -122,15 +123,16 @@ function stack (middlewares, proxy) { ...@@ -122,15 +123,16 @@ function stack (middlewares, proxy) {
middlewares.reverse().forEach(function (layer) { middlewares.reverse().forEach(function (layer) {
var child = handle; var child = handle;
handle = function (req, res) {
var next = function (err) { var next = function (err) {
if (err) { if (err) {
throw err; throw err;
//TODO: figure out where to send errors.
//return error(req, res, err); //return error(req, res, err);
} }
child(req, res); child(req, res);
} }
next.__proto__ = proxy; next.__proto__ = proxy;
handle = function (req, res) {
layer(req, res, next); layer(req, res, next);
}; };
}); });
...@@ -167,11 +169,6 @@ exports.createServer = function () { ...@@ -167,11 +169,6 @@ exports.createServer = function () {
var proxy = new HttpProxy(options); var proxy = new HttpProxy(options);
if(middleware.length)
//handler = callback = middleware.shift()
//else if (middleware.length)
handler = callback = stack(middleware, proxy);
if (port && host) { if (port && host) {
// //
// If we have a target host and port for the request // If we have a target host and port for the request
...@@ -183,6 +180,8 @@ exports.createServer = function () { ...@@ -183,6 +180,8 @@ exports.createServer = function () {
host: host host: host
}); });
} }
if(middleware.length)
middleware.push(handler)
} }
else if (proxy.proxyTable) { else if (proxy.proxyTable) {
// //
...@@ -192,8 +191,16 @@ exports.createServer = function () { ...@@ -192,8 +191,16 @@ exports.createServer = function () {
handler = function (req, res) { handler = function (req, res) {
proxy.proxyRequest(req, res); proxy.proxyRequest(req, res);
} }
if(middleware.length)
middleware.push(handler)
} }
else if (!handler) {
if(middleware.length)
//handler = callback = middleware.shift()
//else if (middleware.length)
handler = callback = stack(middleware, proxy);
if (!handler) {
// //
// Otherwise this server is improperly configured. // Otherwise this server is improperly configured.
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment