Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.