简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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