Skip to content

Instantly share code, notes, and snippets.

@9re
Last active August 29, 2015 14:07
Show Gist options
  • Save 9re/56d75a92524c9825bcbb to your computer and use it in GitHub Desktop.
Save 9re/56d75a92524c9825bcbb to your computer and use it in GitHub Desktop.
http-proxy-server
var http = require('http'),
httpProxy = require('http-proxy'),
request = require('request');
httpProxy.createProxyServer({target:'http://localhost:9999'}).listen(8000);
// proxy server
http.createServer(function (req, res) {
console.log(req.method, req.url);
console.log(JSON.stringify(req.headers, true, 2));
var options = {
hostname : req.headers.host,
path : req.url.replace(req.headers.host, ''),
method : req.method
}
console.log(options);
var proxyRequest = http.request(options, function (realRes) {
var body = '';
realRes.on('data', function (data) {
body += data;
});
realRes.on('end', function () {
console.log('real response:', body);
res.writeHead(realRes.statusCode, JSON.stringify(realRes.headers));
res.write(body);
res.end();
});
});
if (req.method === 'POST') {
//proxyRequest.write();
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
proxyRequest.header('content-length', req.headers['content-length']);
proxyRequest.write(body);
proxyRequest.end();
});
} else {
proxyRequest.end();
}
}).listen(9999);
{
"name": "http-proxy",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies" : {
"http-proxy" : "*",
"request" : "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment