mscdex / node-ftp

An FTP client module for node.js
MIT License
1.13k stars 243 forks source link

Socket error after some times #227

Open rahajason opened 6 years ago

rahajason commented 6 years ago

Why this error? after some downloaded files i got following error from terminal :

`let c = new FTPClient(); const remoteDirectory = '/xxx/xxxx/'; const localDirectory = '/var/www/api/xxx/xxx/'; let contrats = [];

c.on('ready', () => {
    c.list(`${remoteDirectory}`, (err, list) => {
        if (err) throw err;

        list.map( (entry) => {
            if( entry.name.match(/CLIENTS_V/) ){
                contrats.push(entry.name);
            }
        });
        contrats.map((contrat) => {
            c.get(`${remoteDirectory}/${contrat}`, function(err, stream) {
                if (err) throw err;
                stream.once('close', function() { c.end(); });
                stream.pipe(fs.createWriteStream(`${localDirectory}${contrat}`));
            })
        })           
        c.end();
    })
})
c.connect({
    host: process.env.FTP_HOST,
    port: process.env.FTP_PORT,
    user: process.env.FTP_USER,
    password: process.env.FTP_PASSWORD,
    //keepalive:30000
});`

Error: Unable to make data connection 0|server | at Socket.<anonymous> (/var/www/api/node_modules/ftp/lib/connection.js:935:10) 0|server | at Object.onceWrapper (events.js:254:19) 0|server | at Socket.emit (events.js:164:20) 0|server | at Object.cb (/var/www/api/node_modules/ftp/lib/connection.js:575:18) 0|server | at Parser.<anonymous> (/var/www/api/node_modules/ftp/lib/connection.js:117:20) 0|server | at Parser.emit (events.js:159:13) 0|server | at Parser._write (/var/www/api/node_modules/ftp/lib/parser.js:59:10) 0|server | at doWrite (_stream_writable.js:388:12) 0|server | at writeOrBuffer (_stream_writable.js:374:5) 0|server | at Parser.Writable.write (_stream_writable.js:291:11) 0|server | at Socket.ondata (/var/www/api/node_modules/ftp/lib/connection.js:273:20) 0|server | at Socket.emit (events.js:159:13) 0|server | at addChunk (_stream_readable.js:265:12) 0|server | at readableAddChunk (_stream_readable.js:252:11) 0|server | at Socket.Readable.push (_stream_readable.js:209:10) 0|server | at TCP.onread (net.js:598:20) PM2 | App [server] with id [0] and pid [6461], exited with code [1] via signal [SIGINT]

janguianof commented 5 years ago

Same here : (

Did you try another library?

steven-tey commented 1 year ago

Ran into this issue today as well – I ended up wrapping a Promise around my c.get() and it worked like a charm!

Code:

c.on("ready", async() => {
   await new Promise((resolve) => {
      c.get(filename, (err, stream) => {
         ...
         resolve()
      })
   });
   await new Promise((resolve) => {
      c.get(filename2, (err, stream) => {
         ...
         resolve()
      })
   })
   ...
   c.end()
}