Skip to content

Instantly share code, notes, and snippets.

@maggiben
Created November 26, 2013 20:33
Show Gist options
  • Save maggiben/7665708 to your computer and use it in GitHub Desktop.
Save maggiben/7665708 to your computer and use it in GitHub Desktop.
nodejs super proxy for CORS workaround
console.log('DEV_SERVER.JS:STARTED');
var port = 3000;
var wsProxy = '$instance$.smxecho.com';
var wsPort = 8507;
var echoProxy = '$instance$.smxecho.com';
var echoPort = 8507;
// var wsProxy = 'localhost';
// var wsPort = 9000;
var config = require('./config').forBuild(process.argv.splice(2)[0]),
compiler = require('./template-compiler'),
path = require('path'),
express = require('express'),
http = require('http'),
httpProxy = require('http-proxy'),
url = require('url');
var server = express();
var proxy = new httpProxy.RoutingProxy();
// urls routes from config
config.urls.map(function(route) {
server.get(
route.url,
function(req, res) {
var html = compiler.render(config, route);
res.set('Content-Type', 'text/html');
res.end(html);
}
);
});
// map static folders
config.static_folders.map(function(pathInfo) {
var relativePath = '/' + pathInfo.path + '/' + (pathInfo.versioned ? config.build : '');
var srcPath = path.join(__dirname, config.srcPath, pathInfo.path);
console.log(" .static folder: ", relativePath + ' -> ' + srcPath);
server.use(relativePath, express.static(srcPath));
});
// map static folder as is
server.use('/static/', express.static(path.join(__dirname, config.srcPath, '/static/')));
// redirect from default login returnUrl
server.get('/Report/Default.aspx', function(req, res) {
res.redirect('/');
});
server.get('/', function(req, res) {
res.redirect('/dashboard');
});
// map any other request to the WS server
server.all('/ws/*', function(req, res) {
var instance = req.host.split('.')[0];
var newHost = wsProxy.replace('$instance$', instance);
req.headers['host'] = newHost;
console.log(" .ws proxy: ", newHost + ':' + wsPort + req.url);
proxy.proxyRequest(req, res, {
host: newHost,
port: wsPort
});
});
// login
server.all('/login.aspx', function(req, res) {
var instance = req.host.split('.')[0];
var newHost = echoProxy.replace('$instance$', instance);
req.url = '/login.aspx?returnUrl=' + (req.query['ReturnUrl'] || ('http://' + instance + '.localhost:' + echoPort + '/'));
req.headers['host'] = newHost;
console.log(" .login proxy: ", newHost + ':' + wsPort + req.url);
proxy.proxyRequest(req, res, {
host: newHost,
port: echoPort
});
});
// benja test2
// login
server.all('/test2/*', function(req, res) {
var instance = req.host.split('.')[0];
var newHost = echoProxy.replace('$instance$', instance);
req.url = "http://socialmetrix-lat.localhost";
req.headers['host'] = newHost;
console.log(" .login proxy: ", newHost + ':' + wsPort + req.url);
proxy.proxyRequest(req, res, {
host: newHost,
port: 8000
});
});
// benja email templates
// mock ping service
server.all('/test/*', function(req, res) {
var resource = req.url.replace("/test/", "") // extract test url
req.pause();
console.log('request ' + req.url);
var options = url.parse("http://socialmetrix-lat.localhost:8000/" + resource);
console.log("options", options)
options.headers = req.headers;
options.method = req.method;
options.agent = false;
var connector = http.request(options, function(serverResponse) {
serverResponse.pause();
res.writeHeader(serverResponse.statusCode, serverResponse.headers);
serverResponse.pipe(res);
serverResponse.resume();
});
req.pipe(connector);
req.resume();
});
// mock ping service
server.all('/Report/Ping.ashx', function(req, res) {
res.set('Content-Type', 'text/json');
res.end('{"status":"ok"}');
});
// map WS.legacy calls (echo.net)
server.all('/Report/*', function(req, res) {
var instance = req.host.split('.')[0];
var newHost = echoProxy.replace('$instance$', instance);
req.url = '/Report/'+ req.params[0];
req.headers['host'] = newHost;
console.log(" .ws.legacy proxy: ", newHost + ':' + echoPort + req.url);
proxy.proxyRequest(req, res, {
host: newHost,
port: echoPort
});
});
// map WS.legacy calls (echo.net)
server.all('/DATA/JSON/*', function(req, res) {
var instance = req.host.split('.')[0];
var newHost = echoProxy.replace('$instance$', instance);
req.url = '/DATA/JSON/'+ req.params[0];
req.headers['host'] = newHost;
console.log(" .ws.legacy proxy: ", newHost + ':' + echoPort + req.url);
proxy.proxyRequest(req, res, {
host: newHost,
port: echoPort
});
});
// start server
server.listen(port);
console.log('running on port ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment