mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[minor] add middleware to node-http-proxy
This commit is contained in:
parent
25c06a3a95
commit
c773eedeb6
@ -112,6 +112,30 @@ exports.getMaxSockets = function () {
|
||||
exports.setMaxSockets = function (value) {
|
||||
maxSockets = value;
|
||||
};
|
||||
//
|
||||
// stack
|
||||
// adapted from https://github.com/creationix/stack
|
||||
//
|
||||
|
||||
function stack (middlewares, proxy) {
|
||||
var handle;
|
||||
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) {
|
||||
layer(req, res, next);
|
||||
};
|
||||
});
|
||||
return handle;
|
||||
}
|
||||
|
||||
//
|
||||
// ### function createServer ([port, host, options, handler])
|
||||
@ -127,55 +151,54 @@ exports.setMaxSockets = function (value) {
|
||||
//
|
||||
exports.createServer = function () {
|
||||
var args = Array.prototype.slice.call(arguments),
|
||||
callback = typeof args[0] === 'function' && args.shift(),
|
||||
options = {}, port, host, forward, silent, proxy, server, handler;
|
||||
callback,
|
||||
options = {}, port, host, forward, silent, proxy, server, middleware = [];
|
||||
|
||||
if (args.length >= 2) {
|
||||
port = args[0];
|
||||
host = args[1];
|
||||
options = args[2] || {};
|
||||
}
|
||||
else if (args.length === 1) {
|
||||
options = args[0] || {};
|
||||
if (!options.router && !callback) {
|
||||
throw new Error('Cannot create server with no router and no callback');
|
||||
}
|
||||
}
|
||||
args.forEach(function (arg) {
|
||||
|
||||
proxy = new HttpProxy(options);
|
||||
switch (typeof arg) {
|
||||
case 'string': host = arg; break;
|
||||
case 'number': port = arg; break;
|
||||
case 'function': middleware.push(arg); break;
|
||||
case 'object': options = arg; break;
|
||||
};
|
||||
|
||||
handler = function (req, res) {
|
||||
if (callback) {
|
||||
//
|
||||
// If we were passed a callback to process the request
|
||||
// or response in some way, then call it.
|
||||
//
|
||||
callback(req, res, proxy);
|
||||
}
|
||||
else if (port && host) {
|
||||
});
|
||||
|
||||
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
|
||||
// then proxy to the specified location.
|
||||
//
|
||||
proxy.proxyRequest(req, res, {
|
||||
port: port,
|
||||
host: host
|
||||
});
|
||||
handler = function (req, res) {
|
||||
proxy.proxyRequest(req, res, {
|
||||
port: port,
|
||||
host: host
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (proxy.proxyTable) {
|
||||
//
|
||||
// If the proxy is configured with a ProxyTable
|
||||
// instance then use that before failing.
|
||||
//
|
||||
proxy.proxyRequest(req, res);
|
||||
handler = function (req, res) {
|
||||
proxy.proxyRequest(req, res);
|
||||
}
|
||||
}
|
||||
else {
|
||||
else 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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user