skip to Main Content

In stock

  • Linux (Debian) distribution with Node.js installed Third-party
  • Third-party Windows Server

Task

It is necessary to establish an FTP connection to Windows Server using the Node.js platform, cut out a specific folder (along with the folders and files located in it) and paste it into a specific directory on a server running Linux.

Question

Is it possible to implement the described task? If yes, what NPM packages to use and / or what program code is suitable for solving the problem?

2

Answers


  1. Chosen as BEST ANSWER
    const ftp = require('basic-ftp');
    
    ftp_connect()
     
    async function ftp_connect() {
        const client = new ftp.Client();
        client.ftp.verbose = true;
        try {
            await client.access({
                host: 'host',
                port: '21',
                user: 'domain\username',
                password: 'password',
                secure: false
            })
            console.log(await client.list())
        }
        catch(err) {
            console.log(err)
        }
        client.close()
    }
    

  2. You can use ssh2-sftp-client package from npm

    const Client = require('ssh2-sftp-client');
    
    const config = {
      host: 'example.com',
      port: 22,
      username: 'red-don',
      password: 'my-secret'
    };
    
    let sftp = new Client;
    
    sftp.connect(config)
      .then(() => {
        return sftp.list('/path/to/remote/dir');
      })
      .then(async data => {
        console.log(data);
        for(let file of data) {
            //you can check if file is directory or file
            if(file.type == 'd') {
               //recursively read files
            }else {
                //download file
                const remoteFilename = '/path/to/remote/dir/' + data.name;
                const localFilename = '/path/to/local/files/' + data.name;
                sftp.get(remoteFilename).then((stream) => {
                    stream.pipe(fs.createWriteStream(localFilename));
                });`enter code here`
            }
        } 
        //delete directory recursively after all files are download 
        return client.rmdir(/path/to/remote/dir, true); 
      })
      .then(() => {
        sftp.end();
      })
      .catch(err => {
        console.error(err.message);
      });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search