为什么我的文件流不执行错误功能?

时间:2018-08-21 18:27:43

标签: node.js stream pngcrush

我正在尝试使用以下代码crush a PNG

var pngcrush = new PngCrush(['-rem','allo', '-m', '0']); //methods 1-10

  const pathPreCrush = 'temp/' + randomstring.generate(20) + '.png';
  const pathCrushed = 'temp/' + randomstring.generate(20) + '.png';


    //crush png
    fs.createReadStream(pathPreCrush).pipe(pngcrush).on('error', function(e){
      return callback('error crushing file');
    }).pipe(fs.createWriteStream(pathCrushed)).on('finish', function () {
      return callback(null, pathCrushed);
    });

有时它可以完美工作并返回带有新路径的回调。但是有时它不起作用。它不会返回错误,只是什么也不会做。它显示了要创建的pathCrushed文件,但具有0个字节。它挂在此功能上,直到超时为止都不会消失。

我有什么想念的东西吗?

编辑:我在代码的顶部声明了第一条pngcrush行,该代码在每次页面加载时都会执行,但是将其移到页面中,现在似乎没有发生。可以吗?

0 个答案:

没有答案