Skip to content

Instantly share code, notes, and snippets.

@camshaft
Created January 5, 2013 08:06
Show Gist options
  • Save camshaft/4460458 to your computer and use it in GitHub Desktop.
Save camshaft/4460458 to your computer and use it in GitHub Desktop.
Express.js Embedded Forward Proxy
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