Created
May 30, 2014 17:03
-
-
Save avshabanov/e617f80faee6a72fb42a to your computer and use it in GitHub Desktop.
NodeJS Simple Proxy Server
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 http = require("http"), | |
url = require("url"); | |
var port = process.argv[2] || 8888; | |
// Sample redir link: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html | |
http.createServer(function(request, response) { | |
var urlOptions = url.parse(request.url); | |
var pathname = urlOptions.pathname; | |
console.log("Serving " + pathname); | |
if (pathname.indexOf("/status") == 0) { | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("Proxy: ready to forward!\n\n"); | |
response.end(); | |
return; | |
} | |
var options = { | |
hostname: "docs.oracle.com", | |
port: 80, | |
path: pathname, | |
// headers: request.headers, | |
method: request.method, | |
agent: false | |
}; | |
console.log("=> Proxying to " + JSON.stringify(options)); | |
var connector = http.request(options, function (proxiedResponse) { | |
proxiedResponse.pipe(response, {end: true}); | |
}); | |
request.pipe(connector, {end: true}); | |
}).listen(parseInt(port, 10)); | |
console.log("Proxy HTTP server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown.\n" + | |
"Open http://localhost:8888/status to see the status page\n\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment