我有这段代码,可以使用node-ftp(https://www.npmjs.com/package/ftp)软件包通过nodejs连接到ftp服务器。
节点版本:v10.13.0 我试图将下一行更改为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) });
您要在控制台上打印的消息如下:
[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' }
答案 0 :(得分:0)
FTP的工作方式是保持命令连接打开,直到客户端(在您的情况下为节点程序)关闭它为止。因此,请保持它的生命。而且,切记在不再需要ftp访问时将其关闭。