Created
October 12, 2015 10:36
-
-
Save AndiDittrich/748539dd73fa684552f0 to your computer and use it in GitHub Desktop.
Howto fetch & parse a RSS Feed with Node.js
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
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