Skip to content

Commit c773eed

Browse files
committed
[minor] add middleware to node-http-proxy
1 parent 25c06a3 commit c773eed

File tree

1 file changed

+56
-33
lines changed

1 file changed

+56
-33
lines changed

lib/node-http-proxy.js

+56-33
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ exports.getMaxSockets = function () {
112112
exports.setMaxSockets = function (value) {
113113
maxSockets = value;
114114
};
115+
//
116+
// stack
117+
// adapted from https://github.com/creationix/stack
118+
//
119+
120+
function stack (middlewares, proxy) {
121+
var handle;
122+
middlewares.reverse().forEach(function (layer) {
123+
124+
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;
133+
handle = function (req, res) {
134+
layer(req, res, next);
135+
};
136+
});
137+
return handle;
138+
}
115139

116140
//
117141
// ### function createServer ([port, host, options, handler])
@@ -127,60 +151,59 @@ exports.setMaxSockets = function (value) {
127151
//
128152
exports.createServer = function () {
129153
var args = Array.prototype.slice.call(arguments),
130-
callback = typeof args[0] === 'function' && args.shift(),
131-
options = {}, port, host, forward, silent, proxy, server, handler;
154+
callback,
155+
options = {}, port, host, forward, silent, proxy, server, middleware = [];
132156

133-
if (args.length >= 2) {
134-
port = args[0];
135-
host = args[1];
136-
options = args[2] || {};
137-
}
138-
else if (args.length === 1) {
139-
options = args[0] || {};
140-
if (!options.router && !callback) {
141-
throw new Error('Cannot create server with no router and no callback');
142-
}
143-
}
144-
145-
proxy = new HttpProxy(options);
157+
args.forEach(function (arg) {
146158

147-
handler = function (req, res) {
148-
if (callback) {
149-
//
150-
// If we were passed a callback to process the request
151-
// or response in some way, then call it.
152-
//
153-
callback(req, res, proxy);
154-
}
155-
else if (port && host) {
159+
switch (typeof arg) {
160+
case 'string': host = arg; break;
161+
case 'number': port = arg; break;
162+
case 'function': middleware.push(arg); break;
163+
case 'object': options = arg; break;
164+
};
165+
166+
});
167+
168+
var proxy = new HttpProxy(options);
169+
170+
if(middleware.length)
171+
//handler = callback = middleware.shift()
172+
//else if (middleware.length)
173+
handler = callback = stack(middleware, proxy);
174+
175+
if (port && host) {
156176
//
157177
// If we have a target host and port for the request
158178
// then proxy to the specified location.
159179
//
160-
proxy.proxyRequest(req, res, {
161-
port: port,
162-
host: host
163-
});
180+
handler = function (req, res) {
181+
proxy.proxyRequest(req, res, {
182+
port: port,
183+
host: host
184+
});
185+
}
164186
}
165187
else if (proxy.proxyTable) {
166188
//
167189
// If the proxy is configured with a ProxyTable
168190
// instance then use that before failing.
169191
//
170-
proxy.proxyRequest(req, res);
192+
handler = function (req, res) {
193+
proxy.proxyRequest(req, res);
194+
}
171195
}
172-
else {
196+
else if (!handler) {
173197
//
174198
// Otherwise this server is improperly configured.
175199
//
176200
throw new Error('Cannot proxy without port, host, or router.')
177201
}
178-
};
179-
202+
180203
server = options.https
181204
? https.createServer(options.https, handler)
182205
: http.createServer(handler);
183-
206+
184207
server.on('close', function () {
185208
proxy.close();
186209
});

0 commit comments

Comments
 (0)