joepie91 / node-bhttp

A sane HTTP client library for Node.js with Streams2 support.
62 stars 12 forks source link

Example with FeedParser please #31

Open Ryu945 opened 7 years ago

Ryu945 commented 7 years ago

Can I please see an example of how to use this with Feed Parser https://github.com/danmactough/node-feedparser

joepie91 commented 7 years ago

It seems that feedparser is a Transform stream, so you would use it something like this:

const Promise = require("bluebird");
const bhttp = require("bhttp");
const FeedParser = require("feedparser");

Promise.try(() => {
    return bhttp.get("http://example.com/feed.xml", {stream: true});
}).then((response) => {
    let feedparser = new FeedParser();
    response.pipe(feedparser).pipe(yourTargetStream);
});

... where yourTargetStream is some kind of Transform or Writable stream. It works like every other kind of stream, so you can also listen on data events (although that's generally not recommended - expressing everything as a stream is more reliable), use a library that asynchronously buffers a stream into an array, and so on.

Note that I haven't used feedparser before, so I don't know what kind of options you would normally pass into it. You'll have to consult the feedparser documentation for that.

It's not really any different from any other input stream - however, unlike request, in bhttp you need to explicitly define {stream: true} to indicate that you want the HTTP response as a stream, and you use .pipe on the response object instead of the request object (which is for reliability reasons - request has a lot of technical issues because it tries to let you do this on the request object).