NodeJS 连接FTP服务器

ftp一般是这样 ftp://帐号:密码@服务器IP(或者域名如:xxx.com):端口号。

来自https://github.com/mscdex/node-ftp

先安装 npm install ftp


var client = require('ftp');

    var fs = require('fs');

    var c = new client();

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

//和ftp服务器连接成功后,会进到这里来。接着可以上传下载文件或者做其它事情了。

         c.list(function(err, list) {//Get a directory listing of the current (remote) working directory:

               if(err) throw err;

               console.dir(list);

               c.end();

          });

       c.get('remote file path', function(err, stream) {  //Download remote file  and save it to the local file system:

              if(err) throwerr;

              stream.once('close', function() {

                          console.log('download Finish');

               });

          stream.pipe(fs.createWriteStream('local file path'));

       });

       });

var connectionProperties = {

    host:'xxx',

    user:'xxx',

    password:'xxx',

    port:xx  //默认是21,这个看自己要连接的端口

    };

   c.connect(connectionProperties);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光剑书架上的书阅读 3,943评论 2 8
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,403评论 0 6
  • 上一章节:【成长】黑白(18) 春天 连着一周都是大晴天,外面的积雪渐渐地化了,露出深褐色湿润的泥土。沉闷了一个冬...
    棠上燕阅读 269评论 0 1
  • 忆起一些往事 知道那是我的往事 怀着一些期待 相信那是我的期待 尽管对我的出生毫无印象 对我的死亡也无法预知 但我...
    不死青春阅读 289评论 0 1