Created
October 2, 2012 17:29
-
-
Save m4r00p/3821432 to your computer and use it in GitHub Desktop.
Proxy combining local and external web services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express(), | |
http = require('http'), | |
httpProxy = require('http-proxy'); | |
//Express config | |
//app.set('view engine', 'ejs'); | |
//app.set('views', __dirname + '/views'); | |
app.use(express.cookieParser()); | |
app.use(express.methodOverride()); | |
app.use(express.static(__dirname + '/../app/')); | |
// Express static binding | |
app.get('/', function(req, res){ | |
res.redirect('index.html'); | |
}); | |
//Mockup some data | |
//app.get('/entry.asmx', function(req, res){ | |
//res.json(200, {}); | |
//}); | |
//app.post('/entry.asmx', function(req, res){ | |
//res.json(200, {}); | |
//}); | |
app.listen(3502); | |
//Proxy logic | |
var proxy = new httpProxy.RoutingProxy(); | |
http.createServer(function (req, res) { | |
if (req.url.indexOf('asmx') != -1) { | |
console.log(" forwarded to: roxy (" +req.url+ ")"); | |
//To avoid proxy authentication | |
req.headers['origin'] = req.headers['host'] = 'your.external.host'; | |
proxy.proxyRequest(req, res, { | |
host: 'your.external.host', | |
port: 80 | |
}); | |
} else { | |
console.log(" forwarded to: localhost (" +req.url+ ")"); | |
proxy.proxyRequest(req, res, { | |
host: 'localhost', | |
port: 3502 | |
}); | |
} | |
}).listen(8001); | |
console.log('Proxy is running on localhost:8001'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment