Skip to content

Instantly share code, notes, and snippets.

@mren

mren/proxy.js Secret

Created March 31, 2011 10:44
Show Gist options
  • Save mren/ebb9a5052575b0a3f41f to your computer and use it in GitHub Desktop.
Save mren/ebb9a5052575b0a3f41f to your computer and use it in GitHub Desktop.
nodejs proxy for http
#!/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