Skip to content

Instantly share code, notes, and snippets.

@allex
Created January 15, 2015 08:18
Show Gist options
  • Save allex/38b85a838c3f02041d91 to your computer and use it in GitHub Desktop.
Save allex/38b85a838c3f02041d91 to your computer and use it in GitHub Desktop.
/**
* A simple proxy for http request forwoads.
*
* @author Allex Wang ([email protected])
*/
var http = require('http')
, util = require('util')
, request = require('request')
var log = function(s) {
if (s) { arguments[0] = '[' + Date.now() + '] ' + s }
console.log.apply(console, arguments)
};
var error = function(s) {
log(s);
}
var server = http.createServer(function(req, res) {
var url = req.url
try {
var r = request(url)
r.on('end', function() {
log('%s %s', req.method, url, r.response.statusCode)
})
req.pipe(r).pipe(res)
} catch (e) {
var msg = 'fatal: ' + e.message
res.writeHead(500)
res.end(msg)
error(msg)
}
})
process.title = 'http-proxy'
process.on('uncaughtException', function (err) {
error('Unexcpted Error: ' + err);
})
var port = 8080
server.listen(port);
log('Proxy server running on port %s.', port)
// vim: set fdm=marker ts=2 sw=2 sts=2 tw=85 et :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment