Created
July 21, 2014 15:28
-
-
Save ben-bradley/990a56973f0c694a2bd2 to your computer and use it in GitHub Desktop.
Reverse Proxy
This file contains hidden or 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
// This could probably be accomplished with the basic http module, but I was lazy & leaned on Express | |
var request = require('request'), | |
express = require('express'); | |
var proxy = express(); | |
var apps = { | |
app1 : 'http://10.10.10.10:8000', | |
app2 : 'http://10.10.10.10:8001', | |
app3 : 'http://10.10.10.10:8002' | |
}; | |
proxy.use(function(req, res, next) { | |
var parts = req.url.split('/'), | |
app = parts[1] || false; | |
parts.splice(0, 2); | |
var url = (apps[app]) ? apps[app]+'/'+parts.join('/') : false; | |
// requests sent to "http://this.host/app1" are redirected to "http://this.host/app1/" | |
if (!parts.length) | |
res.redirect(req.originalUrl+'/'); | |
// requests sent to "http://this.host/app1/" are proxied to "http://10.10.10.10:8000" | |
else if (url) | |
req.pipe(request(url)).pipe(res); | |
// requests for which there is no app in the table are 404'd | |
else | |
res.send(404, 'No app specified'); | |
}); | |
proxy.listen(80); | |
console.log('listening on :80'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment