Skip to content

Commit 7976de1

Browse files
committed
support old (port,host) and (options) style when using middlewares
1 parent c773eed commit 7976de1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/node-http-proxy.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ exports.getMaxSockets = function () {
112112
exports.setMaxSockets = function (value) {
113113
maxSockets = value;
114114
};
115+
115116
//
116117
// stack
117118
// adapted from https://github.com/creationix/stack
@@ -122,15 +123,16 @@ function stack (middlewares, proxy) {
122123
middlewares.reverse().forEach(function (layer) {
123124

124125
var child = handle;
125-
var next = function (err) {
126-
if (err) {
127-
throw err;
128-
//return error(req, res, err);
129-
}
130-
child(req, res);
131-
}
132-
next.__proto__ = proxy;
133126
handle = function (req, res) {
127+
var next = function (err) {
128+
if (err) {
129+
throw err;
130+
//TODO: figure out where to send errors.
131+
//return error(req, res, err);
132+
}
133+
child(req, res);
134+
}
135+
next.__proto__ = proxy;
134136
layer(req, res, next);
135137
};
136138
});
@@ -167,11 +169,6 @@ exports.createServer = function () {
167169

168170
var proxy = new HttpProxy(options);
169171

170-
if(middleware.length)
171-
//handler = callback = middleware.shift()
172-
//else if (middleware.length)
173-
handler = callback = stack(middleware, proxy);
174-
175172
if (port && host) {
176173
//
177174
// If we have a target host and port for the request
@@ -183,6 +180,8 @@ exports.createServer = function () {
183180
host: host
184181
});
185182
}
183+
if(middleware.length)
184+
middleware.push(handler)
186185
}
187186
else if (proxy.proxyTable) {
188187
//
@@ -192,13 +191,21 @@ exports.createServer = function () {
192191
handler = function (req, res) {
193192
proxy.proxyRequest(req, res);
194193
}
194+
if(middleware.length)
195+
middleware.push(handler)
195196
}
196-
else if (!handler) {
197-
//
198-
// Otherwise this server is improperly configured.
199-
//
200-
throw new Error('Cannot proxy without port, host, or router.')
201-
}
197+
198+
if(middleware.length)
199+
//handler = callback = middleware.shift()
200+
//else if (middleware.length)
201+
handler = callback = stack(middleware, proxy);
202+
203+
if (!handler) {
204+
//
205+
// Otherwise this server is improperly configured.
206+
//
207+
throw new Error('Cannot proxy without port, host, or router.')
208+
}
202209

203210
server = options.https
204211
? https.createServer(options.https, handler)

0 commit comments

Comments
 (0)