Created
February 6, 2016 02:30
-
-
Save toddhgardner/58c49bd1f6b4fee5fd30 to your computer and use it in GitHub Desktop.
Debugging Proxy Server to host a subset of production resources locally for troubleshooting.
This file contains 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
// NOTE Before Starting, be sure you have an entry in your hosts file like: | |
// 127.0.0.1 domain.you.are.intercepting.com | |
'use strict'; | |
var _ = require('lodash'); | |
var express = require('express'); | |
var fs = require('fs'); | |
var httpProxy = require('http-proxy'); | |
var https = require('https'); | |
module.exports = { | |
/** | |
* listen | |
* Starts the CDN listening on a port. | |
* @param options.port {Number} | |
* @param options.sslPort {Number} | |
* @param options.originHost {String} Hostname to proxy as | |
* @param options.originIP {String} IP Address to proxy through to | |
*/ | |
listen: function (options) { | |
options = _.extend({ | |
port: 80, | |
sslPort: 443, | |
originHost: "localhost", | |
originIP: "127.0.0.1" | |
}, options); | |
// NOTE: Generate Self-Signed certificate and reference here. When you start debugging, | |
// be sure to hit the URL once directly first to accept the security warning, or | |
// the browser will silently block the insecure request. | |
var sslOptions = { | |
key: fs.readFileSync(__dirname + "config/keys/cert.key"), | |
cert: fs.readFileSync(__dirname + "config/keys/cert.crt") | |
}; | |
var app = express(); | |
var proxy = httpProxy.createProxyServer(); | |
proxy.on('proxyReq', function(proxyReq) { | |
console.log("Running intercepting proxy for " + options.originHost); | |
proxyReq.setHeader('host', options.originHost); | |
ipAddressUrl = "http://72.21.91.8"; | |
} | |
}); | |
// NOTE Update this path to the path you want re-written for debugging. You'll | |
// be serving your local files instead here. | |
app.use('/path/to/debugging', express.static(__dirname)); | |
// NOTE Everything else should be proxied through to the original host and returned | |
// normally. | |
app.use(function (req, res, next) { | |
proxy.web(req, res, { | |
target: options.originIP, | |
changeOrigin: true, | |
}); | |
}); | |
app.listen(options.port); | |
https.createServer(sslOptions, app).listen(options.sslPort); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment