Skip to content

Instantly share code, notes, and snippets.

@mattd
Last active December 19, 2015 16:48
Show Gist options
  • Save mattd/5986178 to your computer and use it in GitHub Desktop.
Save mattd/5986178 to your computer and use it in GitHub Desktop.
Stupid simple proxy from your dev environment to any arbitrary remove server.
// Usage from some other file:
//
// var app = require('./proxy')({proxyUrl: '//your-remote.com', proxyBase: '/api'})
var path = require('path'),
request = require('request'),
express = require('express'),
app = express();
app.use(express.logger('dev'));
app.use(express.favicon());
app.use(express.bodyParser());
app.use(app.router);
module.exports = function (options) {
var port = options.port || 8000,
staticRoot = options.staticRoot || 'app',
proxyUrl = options.proxyUrl || 'http://localhost',
proxyPath = options.proxyBase + '/*';
app.use(express.static(path.join(__dirname, staticRoot)));
app.all(proxyPath, function (req, res) {
var apiRequest;
req.url = proxyUrl + req.url;
apiRequest = request({
method: req.method,
uri: req.url
}).form(req.body);
req.pipe(apiRequest).pipe(res);
});
app.listen(port, function () {
console.log('Now listening on port ' + port);
});
return app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment