Created
January 12, 2014 16:55
-
-
Save matthewphilyaw/8387274 to your computer and use it in GitHub Desktop.
Simple reverse proxy. forwards /api to a different server. Keeps path after api/ in that if you have api/somecall?test=blah it will forward has /somecall?test=blah.
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'), | |
request = require('request'), | |
url = require('url'); | |
var port = 8080; | |
var app = express(); | |
app.use(function(req, res, next) { | |
var proxyUrl = req.path.match(/^\/api(.*)$/); | |
if (proxyUrl) { | |
var apiUrl = 'http://localhost:8088' + proxyUrl[1]; | |
req.pipe(request({ | |
uri: apiUrl + url.parse(req.url).search, | |
method: req.method | |
})).pipe(res); | |
} | |
else { | |
next(); | |
} | |
}); | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment