Skip to content

Instantly share code, notes, and snippets.

@csmr
Forked from mattd/proxy.js
Created September 2, 2013 19:03
Show Gist options
  • Save csmr/6416194 to your computer and use it in GitHub Desktop.
Save csmr/6416194 to your computer and use it in GitHub Desktop.
// 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) {
req.url = proxyUrl + req.url;
req.pipe(request(req.url)).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