Skip to content

Instantly share code, notes, and snippets.

@midudev
Last active August 19, 2018 06:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midudev/6f05b27e16c98f887995 to your computer and use it in GitHub Desktop.
Save midudev/6f05b27e16c98f887995 to your computer and use it in GitHub Desktop.
Example of using feedparser (https://github.com/danmactough/node-feedparser) with ES6 on node.js >= 4.0.0
'use strict'
const FeedParser = require('feedparser')
const request = require('request')
let req = request('https://github.com/danmactough.atom')
let parser = new FeedParser()
req.on('error', (err) => {
// handle request error
console.log(err)
})
req.on('response', (res) => {
// check if status code is not correct
if (res.statusCode !== 200) {
return req.emit('error', new Error('Bad status code'))
}
// if the res is correct, when can pipe the response
req.pipe(parser) // pipe response to feedparser
})
parser.on('error', (err) => {
// handle parser error
console.log(err)
})
parser.on('end', () => {
// handle that we've finished reading articles
console.log('End parsing')
})
parser.on('readable', () => {
let item = parser.read()
let meta = parser.meta // get the metadata of the feed
while (item) {
// do whatever you want with the item
console.log(item)
// get the next item, if none, then item will be null next time
item = parser.read()
}
})
@midudev
Copy link
Author

midudev commented Jan 25, 2016

Used Standard Style to code the gist: https://github.com/feross/standard

@marti1125
Copy link

nice =D nodejs support es6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment