how to set username and password of ftp-srv in nodejs?

1.1k Views Asked by At

I'm using ftp-srv package to create an ftp-server.

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 Jong"]
});

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

I don't know how to set username and password of ftp-users!

I found an event handler named "login"

ftpServer.on('login', ({connection, username, password}, resolve, reject) => { ... });

but I couldn't find out, how to use it! :(

1

There are 1 best solutions below

10
On BEST ANSWER

Reference the ftp-srv documentation

ftpServer.on('login', ({connection, username, password}, resolve, reject) => { ... });

When you receive the login event, you need to call either resolve or reject based on what you decided what the result of the authentication.

If you consider the login info is correct, call resolve, passing it an object with the relevant details, for instance:

resolve({root: '/path/to/files/accessible/via/ftp'})

Also note that if you are testing locally on a private network, you should probably use you local IP or 127.0.0.1 as the hostname. 0.0.0.0 makes it use the external IP address.

Reference this answer

Like mentioned above you can check username and password like this if password and user is right call resolve otherwise call reject

  if(username === "lorem" && password === "ipsum"){
       // call resolve 
       resolve({});
    else{
      // if password and username are incorrectly then call reject
       reject({});
    }

Reference this article https://stackoverrun.com/de/q/12681767