-
-
Save csmr/6416194 to your computer and use it in GitHub Desktop.
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
// 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