ENAMETOOLONG 应要求提供 xml 文档 [英] ENAMETOOLONG on request for xml document

查看:53
本文介绍了ENAMETOOLONG 应要求提供 xml 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 node 的请求模块获取我的 RSS 数据提要(xml 文件),如下所示:

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);
    }); 
  }
});

但我收到错误:

{ [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>

这种情况我该怎么办?

推荐答案

您正在尝试使用 HTTP 请求的正文(即 XML 文件的内容)从磁盘上的文件创建 ReadableStream文件名.

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.

你的意思是:

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

利用了 response 对象已经是可读流的事实,您可以通过管道传输到另一个流.

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

如果你不想使用流,而是缓冲整个主体,那么 request 可以为你做到这一点:

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);
  }
});

这篇关于ENAMETOOLONG 应要求提供 xml 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆