Created
October 2, 2015 23:57
-
-
Save spiermar/69db9cc3762c850e0fb4 to your computer and use it in GitHub Desktop.
Node.JS Express Proxy
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
var http = require('http'); | |
var app = require('express')(); | |
app.use('/proxy*', function(req, res, next) { | |
var options = { | |
host: "spiermar.github.io", | |
path: "/contact" + req.params[0] | |
}; | |
var callback = function(response) { | |
if (response.statusCode === 200) { | |
res.writeHead(200, { | |
'Content-Type': response.headers['content-type'] | |
}); | |
response.pipe(res); | |
} else { | |
res.writeHead(response.statusCode); | |
res.end(); | |
} | |
}; | |
http.request(options, callback).end(); | |
}); | |
var server = app.listen(3000, function () { | |
var host = server.address().address; | |
var port = server.address().port; | |
console.log('Example app listening at http://%s:%s', host, port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment