Node js将远程文件下载到本地并出现套接字错误

时间:2018-09-26 13:35:07

标签: node.js ftp

我尝试使用module从带有nodejs的远程FTP下载许多json文件,下载完一些文件后出现错误并且连接已关闭:

这是我尝试获取文件的方式:

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

    const  passThrough = () => {
        var passthrough = new Transform();
        passthrough._transform = function(data, encoding, done) {
            console.log(data);
            this.push(data);
            done();
        };
        return passthrough;
    }
    
    c.on('ready', () => {
        c.list(`${remoteDirectory}`, (err, list) => {
            if (err) throw err;
            
            list.map( (entry) => {
                if( entry.name.match(/FILE_NAME/) ){
                    contrats.push(entry.name);
                }
            });
            
            contrats.map((contrat) => {
                c.get(`${remoteDirectory}/${contrat}`, function(err, stream) {

                    if (err) throw err;
                    console.log(`${stream}`)
                    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,
    });
    
    

将某些文件下载到本地后返回以下错误:

0|server   | Error: Unable to make data connection
0|server   |     at Socket.<anonymous> 
(/xxx/xxx/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 (/xxx/xxx/api/node_modules/ftp/lib/connection.js:575:18)

有人知道吗?谢谢。

0 个答案:

没有答案