Connecting to ftps server from node.js

I installed a simple ftps server in Ubuntu using vsftpd and I was able to access the server from the filesystem without any problem. But I want to be able to connect to it using a node.js script so that I can read / write files through it. I tried 2 libraries but still I don't see any results. I've used libraries JSFTP

and ftps

, but I don't see any results.

JSFTP

var JSFtp = require("jsftp");

var Ftp = new JSFtp({
  host: "192.168.1.3",
  port : 21,
  user: "ftpuser", // defaults to "anonymous" 
  pass: "somepassword", // defaults to "@anonymous" 
  debugMode : true
});

Ftp.ls("./files", function(err, res) {
    console.log('the result ' + res + err);//prints nothing
  res.forEach(function(file) {
    console.log(file.name);
  });
});

      

ftps (I installed the lftp module before running the code)

var FTPS = require('ftps');
var ftps = new FTPS({
  host: '192.168.1.3', // required
  username: 'ftpuser', // required
  password: 'somepassword', // required
  protocol: 'ftps', // optional, values : 'ftp', 'sftp', 'ftps',... default is 'ftp'
  // protocol is added on beginning of host, ex : sftp://domain.com in this case
  port: 21 // optional
  // port is added to the end of the host, ex: sftp://domain.com:22 in this case
});

ftps.raw('ls -l').exec(function(err,res) {
    console.log(err);//nothing
    console.log(res);//nothing
});

ftps.cd('./files').ls().exec(function(err,res){
    console.log(err);//nothing
    console.log(res);//nothing
})

      

How do I set up a node.js script to access files?

But using curl ( http://www.binarytides.com/vsftpd-configure-ssl-ftps/ ) I was able to list the directory. But the only thing I didn't understand is why we were using the port 6003

instead of 21

?

curl --ftp-ssl --insecure --ftp-port 192.168.1.3:6003 --user ftpuser:somepassword ftp://192.168.1.3

      

Refresh - withnode-ftp

  var Client = require('ftp');
  var fs = require('fs');
  var c = new Client();
  c.on('ready', function() {
    c.list("./files",function(err, list) {
      if (err) throw err;
      console.dir(list);
      c.end();
    });
  });
  // connect to localhost:21 as anonymous

//var c = new Client();
  c.on('ready', function() {
    c.get('./files/iris.data.txt', function(err, stream) {
      if (err) throw err;
      stream.once('close', function() { c.end(); });
      stream.pipe(fs.createWriteStream('foo.local-copy.txt'));
    });
  });
  c.connect(
    {
  host: "192.168.1.3",
  port : 21,
  user: "ftpuser", // defaults to "anonymous" 
  password: "somepassword", // defaults to "@anonymous" 
  secure : true,
  pasvTimeout: 20000,
  keepalive: 20000,
  secureOptions: { rejectUnauthorized: false }
});

      

I understand that we are using a property secure

here, but what was I doing wrong in the previous code?

+3


source to share





All Articles