Last active
August 29, 2015 14:04
-
-
Save mwmitchell/18d6159afc687a959d3e to your computer and use it in GitHub Desktop.
proxy-express
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
// "dependencies": { | |
// "http-server": "0.6.1", | |
// "express": "4.6.1", | |
// "request": "2.37.0", | |
// "errorhandler": "1.0.1" | |
// } | |
var express = require('express'); | |
var request = require('request'); | |
var app = express(); | |
var http = require('http'); | |
var path = require('path'); | |
app.set('port', 3333); | |
app.use(express.static(path.join(__dirname, 'app'))); | |
app.use('/api', function(req, res) { | |
var url = "http://blah.com" + req.url; | |
console.log('Proxying to ' + url); | |
console.log('req.query:', req.query); | |
req.pipe(request({ | |
"url": url, | |
"method": req.method | |
}, function(err, errRes){ | |
if(err){ | |
console.log("Proxy =>" + err); | |
res.send('Proxy => ' + err); | |
} | |
})).pipe(res); | |
}); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log('Dev server listening on port ' + app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment