Created
October 22, 2014 15:47
-
-
Save hoehrmann/0d9bec75d93d3ba93827 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
var httpProxy = require('http-proxy'); | |
var http = require('http'); | |
var fs = require('fs'); | |
var proxy = httpProxy.createProxyServer({ | |
target:'http://localhost:9005' | |
}); | |
proxy.listen(8005); | |
// | |
// Listen for the `error` event on `proxy`. | |
proxy.on('error', function (err, req, res) { | |
res.writeHead(500, { | |
'Content-Type': 'text/plain;charset=utf-8' | |
}); | |
console.log(err); | |
res.end('Something went wrong.'); | |
}); | |
// | |
// Listen for the `proxyRes` event on `proxy`. | |
// | |
proxy.on('proxyRes', function (proxyRes, req, res) { | |
console.log('For ' + JSON.stringify(req.url) + '\n' | |
+ 'RAW Response from the target', | |
JSON.stringify(proxyRes.headers, true, 2)); | |
var contentType = proxyRes.headers['content-type']; | |
if (!contentType) | |
return; | |
contentType = contentType.replace(/\s*;\s*.*/, '').toLowerCase(); | |
if (contentType != "audio/mpeg") | |
return; | |
var filename = encodeURIComponent(req.url) + ".mpeg"; | |
var writable = fs.createWriteStream(filename); | |
proxyRes.pipe(writable); | |
}); | |
// | |
// Create your target server | |
// | |
http.createServer(function (req, res) { | |
proxy.web(req, res, { | |
target: 'http://' + req.headers.host | |
}); | |
}).listen(9005); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment