【问题标题】:Node Streams, wrap array as object节点流,将数组包装为对象
【发布时间】:2014-09-02 03:14:10
【问题描述】:

我在表单中有一个metadata 对象

{ 
    filename: "hugearray.json",
    author: "amenadiel",
    date: "2014-07-11",
    introduction: "A huge ass array I want to send to the browser"
}

hugearray.json 是我文件夹中的一个文本文件,顾名思义,它包含一个可能无限元素的数组。

[
    [14, 17, 25, 38, 49],
    [14, 41, 54, 57, 58],
    [29, 33, 39, 53, 59],
    ...
    [03, 14, 18, 34, 37],
    [03, 07, 14, 29, 33],
    [05, 16, 19, 30, 49]
]

我想要实现的是向浏览器输出一个对象,它是原始对象,额外的键'content'是巨大的数组

{ 
    filename: "hugearray.json",
    author: "amenadiel",
    date: "2014-07-11",
    introduction: "A huge ass array I want to send to the browser",
    content: [
                [14, 17, 25, 38, 49],
                ...
                [05, 16, 19, 30, 49]
             ]
}

但是由于我不知道数组的大小,所以我不想在输出之前将整个东西存储在内存中,所以我想到了使用流。我可以用

很好地流式传输数组
var readStream = fs.createReadStream("hugearray.json");

readStream.on('open', function () {
    readStream.pipe(res);
});

当然,我可以将元数据对象发送到 res

res.json(metadata);

我尝试解构元数据,写入每个 key : value 对并打开内容键,然后通过管道传输文件结果,然后关闭花括号。它似乎不起作用:

{ 
    filename: "hugearray.json",
    author: "amenadiel",
    date: "2014-07-11",
    introduction: "A huge ass array I want to send to the browser",
    content:
}[
    [14, 17, 25, 38, 49],
    [14, 41, 54, 57, 58],
    [29, 33, 39, 53, 59],
    ...
    [03, 14, 18, 34, 37],
    [03, 07, 14, 29, 33],
    [05, 16, 19, 30, 49]
]

我想我需要将流包装在我的元数据内容键中,而不是尝试输出 json 并流到结果中。 ¿ 有什么想法吗?

【问题讨论】:

    标签: json node.js stream


    【解决方案1】:

    好吧,我的问题没有引起注意,但让我赢得了 Tumbleweed 徽章。有点意思。

    我一直在调查,并得出了一个解决方案。我希望找到一个衬里,但这个衬里也可以,到目前为止,我已经能够向浏览器输出几 MB,而我的节点进程没有明显的性能损失。

    这是我用的方法

    app.get('/node/arraystream', function (req, res) {
        var readStream = fs.createReadStream("../../temp/bigarray.json");
        var myObject = {
            filename: "hugearray.json",
            author: "amenadiel",
            date: "2014-07-11",
            introduction: "A huge ass array I want to send to the browser"
        };
    
        readStream.on('open', function () {
            console.log('readStream open');
            var myObjectstr = JSON.stringify(myObject);
            res.write(myObjectstr.substring(0, myObjectstr.length - 1) + ',"content":');
        });
    
        readStream.on('error', function (err) {
            console.log('readStream error', err);
            throw err;
        });
    
        readStream.on('close', function () {
            console.log('readStream closed');
            readStream.destroy();
            res.write('}');
            res.end();
    
        });
    
        readStream.on('data', function (data) {
            console.log('readStream received data', data.length);
            var buf = new Buffer(data, 'ascii');
            res.write(buf);
        });
    
    });
    

    基本上,我没有把我的对象变成一个流,而是把我的数组变成了一个缓冲区。

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多