Skip to content

Instantly share code, notes, and snippets.

@wenshin
Created August 26, 2016 11:33
Show Gist options
  • Save wenshin/208df80ff761c626d310923f2197072c to your computer and use it in GitHub Desktop.
Save wenshin/208df80ff761c626d310923f2197072c to your computer and use it in GitHub Desktop.
/**
* Usage:
* ```
* $ node reverseproxy.js
*
* ```
* */
const request = require('request'); // https://github.com/request/request
const http = require('http');
const PROXY_URL = 'http://your.proxyserver.com';
var TARGET_HOST = 'http://your.domain.com';
console.log(`PROXY: ${PROXY_URL}`);
console.log('TARGET_HOST: ', TARGET_HOST);
http.createServer((req, res) => {
const targetUrl = `http://${TARGET_HOST}${req.url}`;
console.log(`[${req.method}]`, targetUrl);
const reqProxy = request({url: targetUrl, proxy: PROXY_URL})
.on('error', (e) => console.log(e));
req.pipe(reqProxy).pipe(res);
}).listen(SERVER_PORT);
console.log('server on 127.0.0.1:' + SERVER_PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment