Skip to content

Instantly share code, notes, and snippets.

@RaccoonDev
Last active August 29, 2015 14:15
Show Gist options
  • Save RaccoonDev/44bb242f362906db2be5 to your computer and use it in GitHub Desktop.
Save RaccoonDev/44bb242f362906db2be5 to your computer and use it in GitHub Desktop.
Simple NodeJS Proxy
//Use this script as proxy where you can specify custom headers
//that will be sent to destination server
//TODO
//[X] Measure request time and write it to console
//[ ] Write measured request time to log file instead of console
//[ ] Make lookup list of possible custom headers to simulate different logins
var http = require('http');
var proxyServer = http.createServer(function(req, res) {
var options = {
hostname: 'localhost',
port: 3000,
path: req.url,
method: req.method
};
//Specify additional custom
req.headers['customAuthToken'] = '<auth>Here is my token</auth>';
options.headers = req.headers;
var timeMeasurementLabel = 'Call: ' + options.method + ' ' + options.path;
var proxyRequest = http.request(options, function(proxyResponse) {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(res);
proxyResponse.on('end', function() {
console.timeEnd(timeMeasurementLabel);
})
});
proxyRequest.on('error', function(e) { console.log('error calling destination server' + e.message); });
console.time(timeMeasurementLabel);
proxyRequest.end();
});
proxyServer.listen(3001, function() {
console.log('Proxy server setting on port 3001');
});
//Use this script to analyze headers and path that comes from proxy security server
var http = require('http');
var server = http.createServer(function(req, res) {
console.log(req.url);
console.log(req.headers);
res.writeHead(200, {'customRespHeaders':'ThisIsFromDestinationService'})
res.write('Are you here?');
res.end();
});
server.listen(3000, function() {
console.log('server listens on port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment