简体   繁体   English

Node js - ftp-srv - 需要简单的 ftp 服务器示例

[英]Node js - ftp-srv - Simple ftp Server Example needed

The last days I'm trying to get a simple ftp-server running in NodeJS.最后几天我试图在 NodeJS 中运行一个简单的 ftp-server。

I found a package named "ftp-srv" with some documentation here: https://www.npmjs.com/package/ftp-srv我在这里找到了一个名为“ftp-srv”的包,里面有一些文档: https : //www.npmjs.com/package/ftp-srv

Inspire by the code-snipptes there I wrote a small script:受到代码片段的启发,我写了一个小脚本:

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

const hostname = '0.0.0.0';
const port = 1111

const ftpServer = new FtpSrv ( 'ftp://' + hostname + ':' + port,
{ anonymous: true, greeting : [ "Hello Jong", "Wie gehts?" ] } );

ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  console.log ( 'data: '    + data );
  console.log ( 'resolve: ' + resolve );
  console.log ( 'reject: '  + reject );

});

ftpServer.on ( 'client-error', (connection, context, error) =>
{
  console.log ( 'connection: ' +  connection );
  console.log ( 'context: '    +  context );
  console.log ( 'error: '      +  error );
});


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

I can start the Script with "node ftpserver.js" and it runs without a Problem.我可以用“node ftpserver.js”启动脚本,它运行没有问题。 If I connect with a FTP-Client Software it seems to connect, but waits for the "Welcome Message" and hangs at that point.如果我与 FTP 客户端软件连接,它似乎已连接,但等待“欢迎消息”并在那时挂起。

I provided the "greeting"-Variable but that text is not send to the client.我提供了“问候语”-变量,但该文本不会发送给客户端。

I searched a lot in google but couldnt find any working Examples for "ftp-srv".我在谷歌搜索了很多,但找不到任何“ftp-srv”的工作示例。

I think the point where I have to fill in some code is here:我认为我必须填写一些代码的要点在这里:

ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  // HERE
});

It would help me a lot if anyone could provide some Example-code to get a working client connection and to overcome the greeting message.如果有人可以提供一些示例代码来获得有效的客户端连接并克服问候消息,那将对我有很大帮助。

--- Edit --- - - 编辑 - -

The Advice from jcaron helped a lot, Im one step further now. jcaron 的建议帮助很大,我现在更进了一步。 These are my changes:这些是我的变化:

[...]
ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  resolve ( { root: '/home/peter/apps/ftpfiles' } );
});
[...]

When I connect with a client now, the client tries to read the remote directory.当我现在与客户端连接时,客户端尝试读取远程目录。 After 3 tries the client get a timeout. 3 次尝试后,客户端超时。

It seems like the client cant access the directory '/home/peter/apps/ftpfiles'.客户端似乎无法访问目录“/home/peter/apps/ftpfiles”。 I can say that it exists and has read/write permissions by the user which I start my "ftpserver.js"-Script.我可以说它存在并且具有我启动“ftpserver.js”脚本的用户的读/写权限。

I tried some things with a fs-object instead of root:[dir], but always see the same behaviour.我用 fs-object 而不是 root:[dir] 尝试了一些东西,但总是看到相同的行为。

Can anyone help?任何人都可以帮忙吗?

--- Edit --- - - 编辑 - -

In addition simply changed另外干脆改了

const hostname = '0.0.0.0';
#to local address:
const hostname = '192.x.y.z';

and it worked for me.它对我有用。

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.当您收到login事件时,您需要根据您决定的身份验证结果来调用resolvereject

If you consider the login info is correct, call resolve , passing it an object with the relevant details, for instance:如果您认为登录信息是正确的,请调用resolve ,将包含相关详细信息的对象传递给它,例如:

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 地址。

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

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