简体   繁体   中英

how to list ftp files in nodejs?

I have this code to connect to an ftp server via nodejs using the node-ftp ( https://www.npmjs.com/package/ftp ) package.

Node version: v10.13.0 I tried to change the next line to false.

socket.setKeepAlive(true);
var Client = require('ftp');
const fs = require('fs');
const moment = require('moment');

var c = new Client();
var date;
var filename;
c.on('error', (err) => console.log(err));

c.on('ready', function() {

    c.list((err, list) => {
        if (err) console.dir(err);
        console.dir(list);
        c.end();
    });

});

c.connect({ host: 'hostname', user: 'username', password: 'pass', debug: console.log.bind(console) });

the message you're printing on the console is as follows:

[connection] < '220-FileZilla Server version 0.9.41 beta\r\n'
[connection] < '220 Welcome to ComexPerú FTP Server\r\n'
[parser] < '220-FileZilla Server version 0.9.41 beta\r\n220 Welcome to ComexPerú FTP Server\r\n'
[parser] Response: code=220, buffer='FileZilla Server version 0.9.41 beta\r\nWelcome to ComexPerú FTP Server'
[connection] > 'USER username'
[connection] < '331 Password required for username\r\n'
[parser] < '331 Password required for username\r\n'
[parser] Response: code=331, buffer='Password required for username'
[connection] > 'PASS pass'
[connection] < '230 Logged on\r\n'
[parser] < '230 Logged on\r\n'
[parser] Response: code=230, buffer='Logged on'
[connection] > 'FEAT'
[connection] < '211-Features:\r\n'
[connection] < ' MDTM\r\n REST STREAM\r\n SIZE\r\n MLST type*;size*;modify*;\r\n MLSD\r\n'
[connection] < ' UTF8\r\n CLNT\r\n MFMT\r\n'
[connection] < '211 End\r\n'
[parser] < '211-Features:\r\n MDTM\r\n REST STREAM\r\n SIZE\r\n MLST type*;size*;modify*;\r\n MLSD\r\n UTF8\r\n CLNT\r\n MFMT\r\n211 End\r\n'
[parser] Response: code=211, buffer='Features:\r\n MDTM\r\n REST STREAM\r\n SIZE\r\n MLST type*;size*;modify*;\r\n MLSD\r\n UTF8\r\n CLNT\r\n MFMT\r\nEnd'
[connection] > 'TYPE I'
[connection] < '200 Type set to I\r\n'
[parser] < '200 Type set to I\r\n'
[parser] Response: code=200, buffer='Type set to I'
[connection] > 'PASV'

{ Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }

FTP works by keeping the command connection open until the client (in your case your node program) closes it. So do keep it alive. And, do remember to close it when you no longer need ftp access.

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