Skip to content

Instantly share code, notes, and snippets.

@adparadise
Created November 2, 2012 23:04
Show Gist options
  • Save adparadise/4004879 to your computer and use it in GitHub Desktop.
Save adparadise/4004879 to your computer and use it in GitHub Desktop.
Proxy server, serve transformed HTML
var Transform = require('readable-stream/transform.js');
function serveTransformedHTML (httpRequest, httpResponse) {
var remoteRequest, transform, remoteURL;
console.log("transforming: " + httpRequest.url);
transform = new Transform();
transform._transform = function (chunk,
outputFunction,
callback) {
var chunkString, chunkBuffer;
chunkString = chunk.toString();
chunkString = chunkString.replace('</body>',
'<script src="/js/debug.js"></script></body>');
chunkBuffer = new Buffer(chunkString);
outputFunction(chunkBuffer);
callback();
};
remoteURL = 'http://' + remoteHost + httpRequest.url;
remoteRequest = request(remoteURL);
remoteRequest.pipe(transform)
transform.pipe(httpResponse);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment