Created
January 5, 2013 08:06
-
-
Save camshaft/4460458 to your computer and use it in GitHub Desktop.
Express.js Embedded Forward Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"), | |
httpProxy = require("http-proxy"), | |
proxy = new httpProxy.RoutingProxy(); | |
var app = module.exports = express(); | |
app.get("/", function(req, res, next) { | |
res.send("This is the index"); | |
}); | |
app.use(function(req, res, next) { | |
// Are we connecting to the api? | |
if (req.url.indexOf("/api") === 0) { | |
// Remove the /api in the path | |
req.url = req.url.replace("/api", ""); | |
// replace the host or your network will freak out | |
req.headers.host = "www.google.com"; | |
// proxy the bad boy | |
return proxy.proxyRequest(req, res, { | |
port: 80, | |
host: 'google.com', | |
https: true | |
}); | |
} | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment