Skip to content

Instantly share code, notes, and snippets.

@srossross
Created April 5, 2013 12:19
Show Gist options
  • Save srossross/5318873 to your computer and use it in GitHub Desktop.
Save srossross/5318873 to your computer and use it in GitHub Desktop.
Simple http-proxy cli with ssl
fs = require('fs');
// Syslog = require('node-syslog');
// Syslog.init("node-syslog", Syslog.LOG_PID | Syslog.LOG_ODELAY, Syslog.LOG_LOCAL0);
parser = require("optimist")
.alias("r", "router")
.demand("router")
.boolean('https')
.describe('https', 'Use https instead of http')
.describe('key', 'Path to keyfile (https only)')
.describe('cert', 'Path to cert file (https only)')
.alias("h", "help")
.alias("p", "port")
.default('port', 8080)
args = parser.argv
if (args.help){
parser.showHelp();
process.exit(-1);
}
options = { router: require(args.router) }
if (args.https) {
if (!args.key && !args.cert){
parser.showHelp();
console.log('ERROR: Arguments --key and --cert reqruired when --https is true')
process.exit(-1);
}
options.https = {
key: fs.readFileSync(args.key, 'utf8'),
cert: fs.readFileSync(args.cert, 'utf8')
}
}
console.log(args.router);
server = require("http-proxy").createServer(options);
server.listen(args.port);
// server.proxy.on('end', function(data1) {
// console.log("The request was proxied.", data1.url);
// Syslog.log(Syslog.LOG_INFO, "Node Syslog Module output " + new Date());
// });
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
http = require('http')
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello 1\n');
res.end();
}).listen(8001);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello 2\n');
res.end();
}).listen(8002);
@srossross
Copy link
Author

To use this file you need the dependancies:

  • "http-proxy": "~0",
  • "optimist": "~0",

The routing table should be a JSON file like

{
    "localhost/foo": "127.0.0.1:8001",
    "localhost/bar": "127.0.0.1:8002"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment