使用 Node.js 管道化多个文件流 [英] Piping multiple file streams using Node.js

查看:53
本文介绍了使用 Node.js 管道化多个文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个文件一个接一个地流式传输到浏览器.为了说明这一点,请考虑将多个 CSS 文件连接在一起交付.

I want to stream multiple files, one after each other, to the browser. To illustrate, think of having multiple CSS files which shall be delivered concatenated as one.

我使用的代码是:

var directory = path.join(__dirname, 'css');
fs.readdir(directory, function (err, files) {
  async.eachSeries(files, function (file, callback) {
    if (!endsWith(file, '.css')) { return callback(); } // (1)

    var currentFile = path.join(directory, file);
    fs.stat(currentFile, function (err, stats) {
      if (stats.isDirectory()) { return callback(); } // (2)

      var stream = fs.createReadStream(currentFile).on('end', function () {
        callback(); // (3)
      });
      stream.pipe(res, { end: false }); // (4)
    });
  }, function () {
    res.end(); // (5)
  });
});

我的想法是

  1. 过滤掉所有没有文件扩展名 .css 的文件.
  2. 过滤掉所有目录.
  3. 在完全读取文件后继续处理下一个文件.
  4. 将每个文件通过管道传输到响应流而不关闭它.
  5. 在通过管道传输所有文件后结束响应流.

问题是只有第一个 .css 文件被管道传输,而所有剩余的文件都丢失了.就好像(3)会在第一个(4)之后直接跳到(5).

The problem is that only the first .css file gets piped, and all remaining files are missing. It's as if (3) would directly jump to (5) after the first (4).

有趣的是,如果我将第 (4) 行替换为

The interesting thing is that if I replace line (4) with

stream.on('data', function (data) {
  console.log(data.toString('utf8'));
});

一切正常:我看到多个文件.如果我然后将此代码更改为

everything works as expected: I see multiple files. If I then change this code to

stream.on('data', function (data) {
  res.write(data.toString('utf8'));
});

期望第一个文件再次丢失.

all files expect the first are missing again.

我做错了什么?

PS:使用 Node.js 0.8.7 和使用 0.8.22 都会发生错误.

PS: The error happens using Node.js 0.8.7 as well as using 0.8.22.

更新

好的,如果你修改代码如下:

Okay, it works if you change the code as follows:

var directory = path.join(__dirname, 'css');
fs.readdir(directory, function (err, files) {
  var concatenated = '';
  async.eachSeries(files, function (file, callback) {
    if (!endsWith(file, '.css')) { return callback(); }

    var currentFile = path.join(directory, file);
    fs.stat(currentFile, function (err, stats) {
      if (stats.isDirectory()) { return callback(); }

      var stream = fs.createReadStream(currentFile).on('end', function () {
        callback();
      }).on('data', function (data) { concatenated += data.toString('utf8'); });
    });
  }, function () {
    res.write(concatenated);
    res.end();
  });
});

但是:为什么?为什么我不能多次调用 res.write 而不是先总结所有的块,然后一次写它们?

But: Why? Why can't I call res.write multiple times instead of first summing up all the chunks, and then write them all at once?

推荐答案

代码完全没问题,就是单元测试出错了...

The code was perfectly fine, it was the unit test that was wrong ...

解决了这个问题,现在它就像一个魅力:-)

Fixed that, and now it works like a charme :-)

这篇关于使用 Node.js 管道化多个文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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