Last active
March 29, 2016 07:07
-
-
Save adambard/f11292c891bf53eb3286 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
url = require('url') | |
http = require('http') | |
https = require('https') | |
r = require('readability-node') | |
jsdom = require('jsdom').jsdom | |
function handleRequest(request, response){ | |
var uri = url.parse(request.url, true).query.url; | |
uri = url.parse(uri || "https://medium.com/@mbostock/what-makes-software-good-943557f8a488") | |
https.get(uri, function(res){ | |
console.log("Got response", res.statusCode) | |
var src = ''; | |
res.on('data', function(d){ src += d; }); | |
res.on('end', function(){ | |
console.log("Stream end"); | |
// Do something with src | |
var doc = jsdom(src, {features: { | |
FetchExternalResources: false, | |
ProcessExternalResources: false | |
} | |
}); | |
var article = new r.Readability(uri, doc).parse(); | |
response.end( | |
"<html><head><meta charset=\"UTF-8\"></head><body>" + | |
"<h1>" + article.title + "</h1>" + | |
article.content + | |
"</body></html>"); | |
}); | |
}).on('error', function(e){ | |
console.log("Got error", e.message) | |
}); | |
} | |
var server = http.createServer(handleRequest); | |
server.listen(3000, function(){ console.log("OK");}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment