【问题标题】:When using res.send the entire file is not being sent使用 res.send 时,不会发送整个文件
【发布时间】:2015-11-09 18:48:27
【问题描述】:

在我的后端,我收到一个流,其中包含来自网站的数据,其中包含的信息比从我的路线发送的信息多得多。从此代码:

feedparser.on('readable', function(){
    var stream = this;
    var meta = this.meta;
    var item;

    while(item=stream.read()){
        theNews = item;
        console.log(JSON.stringify(theNews));
    }
});

news.post('/', function(req, res){
    var currentNews = JSON.stringify(theNews);
    console.log(currentNews);
    res.send(currentNews);
});

feedparser.on 中的 console.log 返回一个包含许多新闻文章的对象。当我在该范围内进行控制台登录时,它会显示所有文章属性,例如标题、描述、链接等……它占用了整个 cmd 控制台日志,给了我超过 80,000 个字符,因为它包含多篇文章。

但是,来自 news.post 的 console.log 及其发送的响应只是流中最旧的对象,即一篇文章。为什么?

编辑:NM 我能弄明白。我必须将 theNews 设置为一个数组,然后将项目按原样推入其中。

【问题讨论】:

    标签: node.js feedparser


    【解决方案1】:

    流在抓取数据时正在对数据进行流式传输,并且执行速度非常快,因此输出是多个流,看起来像一个文件。实际上,我只是将变量设置为最后一个流式传输的项目。通过使用数组并将其推入其中,我能够获取所有信息。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2020-05-13
      • 2016-10-07
      • 2013-04-24
      • 1970-01-01
      • 2020-02-26
      • 2015-01-14
      • 1970-01-01
      相关资源
      最近更新 更多