Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/0d9bec75d93d3ba93827 to your computer and use it in GitHub Desktop.
Save hoehrmann/0d9bec75d93d3ba93827 to your computer and use it in GitHub Desktop.
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