Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created June 26, 2014 05:36
Show Gist options
  • Save cmawhorter/a527a2350d5982559bb6 to your computer and use it in GitHub Desktop.
Save cmawhorter/a527a2350d5982559bb6 to your computer and use it in GitHub Desktop.
Node script to forward all http requests to another server and return the response with an access-control-allow-origin header. Follows redirects.
// Simple proxy/forwarding server for when you don't want to have to add CORS during development.
// Usage: node proxy.js
// Open browser and navigate to http://localhost:9100/[url]
// Example: http://localhost:9100/http://www.google.com
// This is *NOT* for anything outside local development. It has zero error handling among other glaring problems.
// This started as code I grabbed from this SO question: http://stackoverflow.com/a/13472952/670023
var url = require('url')
, http = require('http')
, https = require('https');
var PORT = process.argv[2] || 9100;
var server = http.createServer(function(req, res) {
var reqUrl = req.url.substr(1);
console.log('==> Making req for' + reqUrl + '\n');
req.pause();
var options = url.parse(reqUrl);
options.headers = req.headers;
options.method = req.method;
options.agent = false;
options.headers['host'] = options.host;
var connector = (options.protocol == 'https:' ? https : http).request(options, function(serverResponse) {
console.log('<== Received res for', serverResponse.statusCode, reqUrl);
console.log('\t-> Request Headers: ', options);
console.log(' ');
console.log('\t-> Response Headers: ', serverResponse.headers);
serverResponse.pause();
serverResponse.headers['access-control-allow-origin'] = '*';
switch (serverResponse.statusCode) {
// pass through. we're not too smart here...
case 200: case 201: case 202: case 203: case 204: case 205: case 206:
case 304:
case 400: case 401: case 402: case 403: case 404: case 405:
case 406: case 407: case 408: case 409: case 410: case 411:
case 412: case 413: case 414: case 415: case 416: case 417: case 418:
res.writeHeader(serverResponse.statusCode, serverResponse.headers);
serverResponse.pipe(res, {end:true});
serverResponse.resume();
break;
// fix host and pass through.
case 301:
case 302:
case 303:
serverResponse.statusCode = 303;
serverResponse.headers['location'] = 'http://localhost:'+PORT+'/'+serverResponse.headers['location'];
console.log('\t-> Redirecting to ', serverResponse.headers['location']);
res.writeHeader(serverResponse.statusCode, serverResponse.headers);
serverResponse.pipe(res, {end:true});
serverResponse.resume();
break;
// error everything else
default:
var stringifiedHeaders = JSON.stringify(serverResponse.headers, null, 4);
serverResponse.resume();
res.writeHeader(500, {
'content-type': 'text/plain'
});
res.end(process.argv.join(' ') + ':\n\nError ' + serverResponse.statusCode + '\n' + stringifiedHeaders);
break;
}
console.log('\n\n');
});
req.pipe(connector, {end:true});
req.resume();
});
console.log('Listening on http://localhost:%s...', PORT);
server.listen(PORT);
@muthurajbharathi
Copy link

Listening on http://localhost:9100...
==> Making req for

<== Received res for 302
-> Request Headers: { protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: null,
path: null,
href: '',
headers:
{ host: null,
connection: 'keep-alive',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
'accept-encoding': 'gzip, deflate, sdch',
'accept-language': 'en-US,en;q=0.8,hi;q=0.6,it;q=0.4,pt;q=0.2,de;q=0.2',
cookie: 'PYPCLOUDY=P566GmTmSboSORtPFeAgB1' },
method: 'GET',
agent: false }

-> Response Headers:  { date: 'Sun, 24 Apr 2016 15:52:41 GMT',

server: 'Apache',
location: 'https://localhost/',
vary: 'Accept-Encoding',
'content-encoding': 'gzip',
'content-length': '181',
'keep-alive': 'timeout=5',
connection: 'Keep-Alive',
'content-type': 'text/html; charset=iso-8859-1' }
-> Redirecting to http://localhost:9100/https://localhost/

==> Making req forhttps://localhost/

events.js:85
throw er; // Unhandled 'error' event
^
Error: self signed certificate
at Error (native)
at TLSSocket. (_tls_wrap.js:936:36)
at TLSSocket.emit (events.js:104:17)

at TLSSocket._finishInit (_tls_wrap.js:467:8)

i got above error , please help me how to have a proxy server in node js which opens web ui of another machine and also forwards request to the other machine.

@DakEnviy
Copy link

DakEnviy commented May 1, 2018

The best forwarding ever!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment