Created
June 17, 2015 13:38
-
-
Save cwygoda/c0bc8b75f5444fde2b8f to your computer and use it in GitHub Desktop.
Quick and Dirty Proxy
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
var http = require('http'), | |
httpProxy = require('http-proxy'); | |
var targetPort = 5555; | |
var serverPort = 5050; | |
var proxy = httpProxy.createProxyServer({}); | |
// To modify the proxy connection before data is sent, you can listen | |
// for the 'proxyReq' event. When the event is fired, you will receive | |
// the following arguments: | |
// (http.ClientRequest proxyReq, http.IncomingMessage req, | |
// http.ServerResponse res, Object options). This mechanism is useful when | |
// you need to modify the proxy request before the proxy connection | |
// is made to the target. | |
// | |
proxy.on('proxyReq', function(proxyReq, req, res, options) { | |
res.setHeader('X-Special-Proxy-Header', 'foobar'); | |
}); | |
var server = http.createServer(function(req, res) { | |
proxy.web(req, res, { | |
target: 'http://127.0.0.1:' + targetPort | |
}); | |
}); | |
console.log("listening on port " + serverPort); | |
server.listen(serverPort); |
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
{ | |
"name": "Workspace", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"http-proxy": "^1.11.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment