Skip to content

Instantly share code, notes, and snippets.

@nrstott
Created August 6, 2010 17:59
Show Gist options
  • Save nrstott/511701 to your computer and use it in GitHub Desktop.
Save nrstott/511701 to your computer and use it in GitHub Desktop.
var
Q = require('promised-io/promise'),
when = Q.when,
HttpClient = require('promised-io/http-client').Client,
sys = require('sys');
exports.proxy = function(from, to, nextApp) {
var
client = new HttpClient();
if (from.constructor === String) {
from = new RegExp('^' + from);
}
return function(jsgiReq) {
var proxyReq;
console.log("PathInfo: " + jsgiReq.pathInfo);
if (jsgiReq.pathInfo.match(from)) {
proxyReq = {};
for (var key in jsgiReq) {
proxyReq[key] = jsgiReq[key];
}
proxyReq.url = to;
console.log('Request to '+sys.inspect(proxyReq));
return when(client.request(proxyReq), function(resp) {
return resp;
});
} else {
nextApp(jsgiReq);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment