ENAMETOOLONG on request for xml document

限于喜欢 提交于 2019-12-12 15:11:39

问题


I am trying to fetch my RSS data feed (xml file) via node's request module as follows:

var fs = require('fs')
  , request = require('request')
  , feed = 'http://www.benchmark.pl/rss/aktualnosci-pliki.xml';

request.get(feed, function(error, response, body) {
  if (!error && response.statusCode == 200) {
    var csv = body;

    fs.createReadStream(body)
    .on('error', function (error) {
      console.error(error);
    }); 
  }
});

But I am getting the error :

{ [Error: ENAMETOOLONG, open '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>
....
</rss>

What should I do in this situation ?


回答1:


You're trying to create a ReadableStream from a file on disk by using the body of the HTTP request (ie. the content of your XML file) as the file name.

What you mean is:

request.get(feed, function(error, response) {
  if (!error && response.statusCode == 200) {
    response.pipe(someXmlParserStream);
  }
});

which takes advantage of the fact the response object is already a Readable stream, that you can pipe to another stream.

If you don't want to use streams, but instead buffer the whole body, then request can do that for you:

request.get(feed, function(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});



回答2:


That's one of the solutions to the problem I also found :

var FeedParser = require('feedparser')
   ,request = require('request');

request('http://www.benchmark.pl/rss/aktualnosci-pliki.xml')
.pipe(new FeedParser())
  .on('error', function(error) {
    // always handle errors
    //   
  })  
  .on('meta', function (meta) {
    console.log('===== %s =====', meta.title);
  })  
  .on('readable', function() {
    var stream = this, item;
    while (item = stream.read()) {
      console.log('Got article: %s', item.title || item.description);
    }   
});


来源:https://stackoverflow.com/questions/20271583/enametoolong-on-request-for-xml-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!