Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Last active August 29, 2015 14:04
Show Gist options
  • Save mwmitchell/18d6159afc687a959d3e to your computer and use it in GitHub Desktop.
Save mwmitchell/18d6159afc687a959d3e to your computer and use it in GitHub Desktop.
proxy-express
// "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