简体   繁体   English

如何修复MaxListenersExceededWarning错误?

[英]How to fix MaxListenersExceededWarning error?

In my Node.js http web server I use ssh2-sftp-client library to load csv files from remote SFTP server. 在我的Node.js http Web服务器中,我使用ssh2-sftp-client库从远程SFTP服务器加载csv文件。 It raise error when user try to load several files and I don't understand how to fix it? 当用户尝试加载多个文件而我不知道如何解决它时,它将引发错误。

CODE : 代码

router.post('/', (req, res) => {
  const fileName = req.body.file_name

  const remotePath = '/reports/' + fileName

  const localePath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads/' + fileName)

  sftp.connect(config.sftpServer, 'on').then(() => {
    sftp.fastGet(remotePath, localePath, {}).then(() => {
      res.header('Content-type', 'text/csv; charset=windows-1251')
      res.sendFile(localePath)
      sftp.end()
    }).catch((err) => {
      sftp.end()
      console.log(err, 'fastGet method error')
    })
  }).catch((err) => {
    sftp.end()
    console.log(err, 'connect method error')
  })
})

ERROR : 错误

Error: Failed to connect to server: (SSH) Channel open failure: open failed
    at client.sftp (/node_modules/ssh2-sftp-client/src/index.js:456:18)
    at /node_modules/ssh2/lib/client.js:867:14
    at SSH2Stream.onFailure (/node_modules/ssh2/lib/client.js:1211:5)
    at Object.onceWrapper (events.js:277:13)
    at SSH2Stream.emit (events.js:189:13)
    at parsePacket (/node_modules/ssh2-streams/lib/ssh.js:3709:10)
    at SSH2Stream._transform (/node_modules/ssh2-streams/lib/ssh.js:671:13)
    at SSH2Stream.Transform._read (_stream_transform.js:190:10)
    at SSH2Stream._read (/node_modules/ssh2-streams/lib/ssh.js:253:15)
    at SSH2Stream.Transform._write (_stream_transform.js:178:12) 'connect method error'
Error: Failed to connect to server: (SSH) Channel open failure: open failed
    at client.sftp (/node_modules/ssh2-sftp-client/src/index.js:456:18)
    at /node_modules/ssh2/lib/client.js:867:14
    at SSH2Stream.onFailure (/node_modules/ssh2/lib/client.js:1211:5)
    at Object.onceWrapper (events.js:277:13)
    at SSH2Stream.emit (events.js:189:13)
    at parsePacket (/node_modules/ssh2-streams/lib/ssh.js:3709:10)
    at SSH2Stream._transform (/node_modules/ssh2-streams/lib/ssh.js:671:13)
    at SSH2Stream.Transform._read (_stream_transform.js:190:10)
    at SSH2Stream._read (/node_modules/ssh2-streams/lib/ssh.js:253:15)
    at SSH2Stream.Transform._write (_stream_transform.js:178:12) 'connect method error'
Error: Failed to connect to server: (SSH) Channel open failure: open failed
    at client.sftp (/node_modules/ssh2-sftp-client/src/index.js:456:18)
    at /node_modules/ssh2/lib/client.js:867:14
    at SSH2Stream.onFailure (/node_modules/ssh2/lib/client.js:1211:5)
    at Object.onceWrapper (events.js:277:13)
    at SSH2Stream.emit (events.js:189:13)
    at parsePacket (/node_modules/ssh2-streams/lib/ssh.js:3709:10)
    at SSH2Stream._transform (/node_modules/ssh2-streams/lib/ssh.js:671:13)
    at SSH2Stream.Transform._read (_stream_transform.js:190:10)
    at SSH2Stream._read (/node_modules/ssh2-streams/lib/ssh.js:253:15)
    at SSH2Stream.Transform._write (_stream_transform.js:178:12) 'connect method error'

Finally I found the problem. 终于我找到了问题。 Instead of on method you need to use once method. 代替on方法,您需要使用once方法。

sftp.connect(config.sftpServer, 'once').then(() => {
    // CODE
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM