Skip to content

Instantly share code, notes, and snippets.

@AndiDittrich
Created October 12, 2015 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndiDittrich/748539dd73fa684552f0 to your computer and use it in GitHub Desktop.
Save AndiDittrich/748539dd73fa684552f0 to your computer and use it in GitHub Desktop.
Howto fetch & parse a RSS Feed with Node.js
var _request = require('request');
var _xml2js = require('xml2js').parseString;
function fetchFeed(url, cb){
_request({
url: url,
headers: {
'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0'
}
}, function (error, response, xml){
// valid response ?
if (!error && response.statusCode == 200){
// try to parse xml feed
_xml2js(xml, function (xmlerror, xmldata){
// error ?
if (xmlerror){
cb(xmlerror, null);
return;
}
cb(null, xmldata);
});
}else{
cb(error, null);
}
});
}
fetchFeed('http://de2.php.net/releases/feed.php', function(err, data){
if (err){
console.error(err);
return;
}
console.log(JSON.stringify(data));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment