node.js(或nwjs)ftp下载

时间:2015-03-16 15:59:07

标签: node.js ftp

需要从public ftp定期下载新文件: ftp://ftp.cmegroup.com/bulletin/

但我无法将ftp模块连接到服务器。我的代码是:

var url = "ftp://ftp.cmegroup.com/bulletin/";
var path = require('path');
var fs = require('fs');
//var Promise = require('bluebird');
var Client = require('ftp');

var c = new Client();

var connectionProperties = {
    host: "ftp.cmegroup.com",
};

c.on('ready', function () {
    console.log('ready');
    c.list(function (err, list) {
        if (err) throw err;
        list.forEach(function (element, index, array) {
            //Ignore directories
            if (element.type === 'd') {
                console.log('ignoring directory ' + element.name);
                return;
            }
            //Ignore non zips
            if (path.extname(element.name) !== '.zip') {
                console.log('ignoring file ' + element.name);
                return;
            }
            //Download files
            c.get(element.name, function (err, stream) {
                if (err) throw err;
                stream.once('close', function () {
                    c.end();
                });
                stream.pipe(fs.createWriteStream(element.name));
            });
        });
    });
});


c.connect(connectionProperties); 

错误:

Uncaught Error: connect ECONNREFUSED 127.0.0.1:21

尽管我指出了连接参数,但无法理解为什么它连接到localhost。

1 个答案:

答案 0 :(得分:1)

缺少一行

c.connect(connectionProperties);