Skip to content

Instantly share code, notes, and snippets.

@matthewphilyaw
Created January 12, 2014 16:55
Show Gist options
  • Save matthewphilyaw/8387274 to your computer and use it in GitHub Desktop.
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.
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