【问题标题】:How to stop a nodejs LineInputStream?如何停止节点js LineOutputStream?
【发布时间】:2018-04-15 08:12:10
【问题描述】:

我正在处理一个 csv 文件,当我发现一个验证错误并希望脚本停止处理换行符时。

router.use(upload.single('csv')).route('/agency/:agencyId/properties/import').post(function (req, res) {

  var stream = LineInputStream(fs.createReadStream(req.file.path, {flags: 'r'}));
  stream.setDelimiter("\n");

  stream.on('line', function(line) {
    var row = line.split(',');
    if (row.length!=15) {
      stream.end(); // this does not stop the processing of new lines
      return res.status(400).json({error: 'File should have 15 columns.'});
    }
  });

});

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    在行处理函数中使用stream.destroy(),然后将错误处理程序附加到返回错误响应的流。

    参考How to close a readable stream (before end)?

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2020-02-18
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多