如何解决MaxListenersExceededWarning错误?

时间:2019-02-15 08:30:43

标签: javascript node.js

在我的Node.js http Web服务器中,我使用ssh2-sftp-client库从远程csv服务器加载SFTP文件。当用户尝试加载多个文件而我不知道如何解决它时,它会引发错误?

代码

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: 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'

1 个答案:

答案 0 :(得分:0)

最后我找到了问题。您需要使用on方法来代替once方法。

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