Node js - ftp-srv - Simple ftp Server Example needed

我的未来我决定 提交于 2021-02-10 05:14:29

问题


The last days Im trying to get a simple ftp-server running in Node JS.

I found a package named "ftp-srv" with some documentation here: https://www.npmjs.com/package/ftp-srv

Inspire by the code-snipptes there I wrote a small script:

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

const hostname = '0.0.0.0';
const port = 1111

const ftpServer = new FtpSvr ( '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. If I connect with a FTP-Client Software it seems to connect, but waits for the "Welcome Message" and hangs at that point.

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".

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. 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.

It seems like the client cant access the directory '/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.

I tried some things with a fs-object instead of root:[dir], but always see the same behaviour.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/46234845/node-js-ftp-srv-simple-ftp-server-example-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!