Skip to content

Instantly share code, notes, and snippets.

@cwygoda
Created June 17, 2015 13:38
Show Gist options
  • Save cwygoda/c0bc8b75f5444fde2b8f to your computer and use it in GitHub Desktop.
Save cwygoda/c0bc8b75f5444fde2b8f to your computer and use it in GitHub Desktop.
Quick and Dirty Proxy
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);
{
"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