Skip to content

Commit

Permalink
support old (port,host) and (options) style when using middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Aug 2, 2011
1 parent c773eed commit 7976de1
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ exports.getMaxSockets = function () {
exports.setMaxSockets = function (value) {
maxSockets = value;
};

//
// stack
// adapted from https://github.com/creationix/stack
Expand All @@ -122,15 +123,16 @@ function stack (middlewares, proxy) {
middlewares.reverse().forEach(function (layer) {

var child = handle;
var next = function (err) {
if (err) {
throw err;
//return error(req, res, err);
}
child(req, res);
}
next.__proto__ = proxy;
handle = function (req, res) {
var next = function (err) {
if (err) {
throw err;
//TODO: figure out where to send errors.
//return error(req, res, err);
}
child(req, res);
}
next.__proto__ = proxy;
layer(req, res, next);
};
});
Expand Down Expand Up @@ -167,11 +169,6 @@ exports.createServer = function () {

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 we have a target host and port for the request
Expand All @@ -183,6 +180,8 @@ exports.createServer = function () {
host: host
});
}
if(middleware.length)
middleware.push(handler)
}
else if (proxy.proxyTable) {
//
Expand All @@ -192,13 +191,21 @@ exports.createServer = function () {
handler = function (req, res) {
proxy.proxyRequest(req, res);
}
if(middleware.length)
middleware.push(handler)
}
else if (!handler) {
//
// Otherwise this server is improperly configured.
//
throw new Error('Cannot proxy without port, host, or router.')
}

if(middleware.length)
//handler = callback = middleware.shift()
//else if (middleware.length)
handler = callback = stack(middleware, proxy);

if (!handler) {
//
// Otherwise this server is improperly configured.
//
throw new Error('Cannot proxy without port, host, or router.')
}

server = options.https
? https.createServer(options.https, handler)
Expand Down

0 comments on commit 7976de1

Please sign in to comment.