【发布时间】: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 并流到结果中。 ¿ 有什么想法吗?
【问题讨论】: