I set up a simple ftps server in Ubuntu using the vsftpd and I was able to access the server from the filezilla without any problem. But I want to be able to connect to it from a node.js script so that I can read / write files thru it. I tried 2 libraries, but still I am not able to see any results. I used JSFTP
and ftps
libraries but I am not seein 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 configure the node.js script to be able to access the files?
But using curl (http://www.binarytides.com/vsftpd-configure-ssl-ftps/) I was able to list out the directory. But the only thing that I didn't understand was why did we use port 6003
and not 21
?
curl --ftp-ssl --insecure --ftp-port 192.168.1.3:6003 --user ftpuser:somepassword ftp://192.168.1.3
Update-- solved by using node-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're using the secure
property here but what was I doing wrong in the previous code?