node.jsftp-server

How to set root directory of ftp-srv in Node.js


I'm trying to create an ftp server using ftp-srv

Here is my code:

const FtpSvr = require ( 'ftp-srv' );

const hostname = '0.0.0.0';
const port = 5053

const ftpServer = new FtpSvr ({
    url:'ftp://' + hostname + ':' + port ,
    anonymous: false,
    greeting : [ "Hello user"]
});


ftpServer.on('login', (data, resolve, reject) => {
    if(data.username === "user1" && data.password === "ip1") {
        // call resolve
        return resolve({root: '/home/test1/'});
    }
    else{
        // if password and username are incorrectly then call reject
        reject({});
    }
});



ftpServer.listen()
    .then(() =>
    {
        console.log ( `Server Running at ftp://${hostname}:${port}/` );
    });

I have set the '/home/test1/' as root directory and the 'test1' permissions is set to 777, but on client side, it raise an error

an error occurred opening that folder on the ftp server make sure you have permission

Also I tried another ftp client (winscp) and it raised this error:

Could not retrieve directory listing. Command not supported

I'm really confused!


Solution

  • Finally I found the correct options of ftp-srv. And its working now:

    const FtpSvr = require ( 'ftp-srv' );
    
    const hostname = '0.0.0.0';
    const port = 5053
    
    const ftpServer = new FtpSvr ({
        url:'ftp://' + hostname + ':' + port ,
        pasv_url:'ftp://172.18.56.71' ,
        pasv_min:5054,
        pasv_max:5055,
        file_format: "ls",
        anonymous: false,
        greeting : [ "Hello user"]
    });
    
    
    ftpServer.on('login', (data, resolve, reject) => {
        if(data.username === "user1" && data.password === "ip1") {
            // call resolve
            return resolve({root: './test/'});
        }
        else{
            // if password and username are incorrectly then call reject
            reject({});
        }
    });
    
    
    
    ftpServer.listen()
        .then(() =>
        {
            console.log ( `Server Running at ftp://${hostname}:${port}/` );
        });