Skip to content

Instantly share code, notes, and snippets.

@dweidenfeld
Created November 27, 2015 09:20
Show Gist options
  • Save dweidenfeld/385c817886d339aed188 to your computer and use it in GitHub Desktop.
Save dweidenfeld/385c817886d339aed188 to your computer and use it in GitHub Desktop.
Proxy Transforming HTML Documents on the Fly
var http = require('http');
var cheerio = require('cheerio');
http.createServer(function (req, res) {
console.log(req.url);
http.get(req.url, function (r) {
r.setEncoding('utf-8');
var content = '';
r.on('data', function (chunk) {
content += chunk;
});
r.on('end', function () {
var $ = cheerio.load(content);
var title = $('h1').text();
$('head').append('<meta name="title" content="' + title + '"/>');
res.end($.html());
});
}, function (err) {
console.error(err);
});
}).listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment