简体   繁体   English

如何在nodejs中设置ftp-srv的用户名和密码?

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

I'm using ftp-srv package to create an ftp-server.我正在使用ftp-srv package 创建一个 ftp 服务器。

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!我不知道如何设置 ftp-users 的用户名和密码!

I found an event handler named "login"我找到了一个名为“login”的事件处理程序

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

but I couldn't find out, how to use it: :(但我不知道如何使用它::(

Reference the ftp-srv documentation参考 ftp-srv文档

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,将 object 与相关详细信息传递给它,例如:

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.另请注意,如果您在专用网络上进行本地测试,您可能应该使用本地 IP 或 127.0.0.1 作为主机名。 0.0.0.0 makes it use the external IP address. 0.0.0.0 使其使用外部 IP 地址。

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参考这篇文章https://stackoverrun.com/de/q/12681767

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM