Node.js SFTP结束连接

时间:2018-01-11 15:52:28

标签: javascript node.js sftp

我正在使用sh2-sftp-client模块对多个文件执行sftp get请求。我可以将文件拉得很好但是当我尝试拉出10个以上的文件时,我收到错误:(node:4068) Warning: Possible EventEmitter memory leak detected. 11 ready listeners added. Use emitter.setMaxListeners() to increase limitError: (SSH) Channel open failure: open failed

我正在进行一些搜索,我可以通过增加EventEmitter来阻止第一个问题:require('events').EventEmitter.defaultMaxListeners = 0;。我现在这不是解决这个问题的正确方法,因为我需要在每次请求后关闭连接。但我不确定在哪里添加sftp.end

route.js

    // Connect to server
    sftp.connect(config).then(() => {  
        //Grab file and set it as a readable stream
        sftp.get(fileName, "false", null).then((data) => {
            // This is what the file name will be
            res.setHeader('Content-disposition', 'attachment; filename=' + fileName);
            // Setting headers to correct MIME type to the header of the response object
            res.setHeader('Content-Type', 'audio/wav');
            //Pipe the respose
            data.pipe(res);
            // End connection?
            sftp.end();
        });
    }).catch((err) => {
            console.log(err, 'catch error');
    }); 

1 个答案:

答案 0 :(得分:1)

当没有更多数据时,由于数据是读取流,您可以这样做:

library(xts) sales <- xts(c(200, 400), as.Date(c('2018-01-01','2018-01-04'))) exchange <- xts(c(5, 5.5, 5.7, 6.0, 5.9), as.Date(c('2018-01-01','2018-01-02','2018-01-03','2018-01-04','2018-01-05'))) merged <- merge(sales, exchange, all=c(TRUE,FALSE)) converted <- merged$sales / merged$exchange converted # sales #2018-01-01 200 #2018-01-04 400