Last active
August 29, 2015 14:07
-
-
Save 9re/56d75a92524c9825bcbb to your computer and use it in GitHub Desktop.
http-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'), | |
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); |
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
{ | |
"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