D
D
Dvorak2016-11-18 01:28:56
Node.js
Dvorak, 2016-11-18 01:28:56

Why can't I send the request again?

I'm using node-feedparser . The code is something like this:

const request = require('request'),
         feedParser = require('feedparser');
feedParser.on('readable', function() {
  var stream = this, item = stream.read();
  if (item) {
    //код для работы с данными
  }
});

this.feedParser.on('end', () => {
    console.log('end');
});      

const req = this.request(url), $this = this;
req.on('response', function(res) {
   req.pipe(feedParser);
});

One request is normally sent. But if after it I try to send another one, then I see "Error: write after end" in the console. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-11-18
@allishappy

Is it really that hard to read the description of a module on npmjs.org?

const request = require('request');
const FeedParser = require('feedparser');

function readFeed(url) {
  return new Promise((resolve, reject) => {
    const $this = this;
    const feedParser = new FeedParser();
    const req = this.request(url);

    feedParser.on('readable', function() {
      var stream = this, item = stream.read();
      if (item) {
        //код для работы с данными
      }
    });

    this.feedParser.on('end', () => {
      resolve();
    });   

    feedParser.on('error', reject);

    req.on('response', function(res) {
      req.pipe(feedParser);
    });
    req.on('error', reject);
  });
}

readFeed('url').then(() => {
  console.log('end');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question