Created
November 27, 2015 09:20
-
-
Save dweidenfeld/385c817886d339aed188 to your computer and use it in GitHub Desktop.
Proxy Transforming HTML Documents on the Fly
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 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