-
-
Save mren/ebb9a5052575b0a3f41f to your computer and use it in GitHub Desktop.
nodejs proxy for http
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
#!/usr/bin/env node | |
var http = require('http'); | |
var config = { | |
port : 9091, | |
targetHost: "localhost", | |
targetPort: 8080 | |
}; | |
http.createServer(function(req, res) { | |
var url = req.url; | |
if (url.indexOf("/proxy") > 0) { | |
var newurl = url.replace("/proxy", "", "i"); | |
var options = { | |
host: config.targetHost, | |
port: config.targetPort, | |
method: req.method, | |
path: newurl, | |
headers: req.headers | |
}; | |
var proxyReq = http.request(options, function(proxyRes) { | |
console.log("StatusCode " + proxyRes.statusCode + ": pipe"); | |
res.writeHead(proxyRes.statusCode); | |
if (proxyRes.statusCode == 401) { | |
delete proxyRes.headers["www-authenticate"]; | |
} | |
proxyRes.pipe(res); | |
}); | |
req.pipe(proxyReq); | |
proxyReq.end(); | |
} else { | |
res.writeHead(400); // Bad Request | |
res.end(); | |
} | |
}).listen(config.port); | |
console.log("Proxy Server started"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment