easy-ftp
- Easy control FTP or SFTP
- 간단한 설정만으로 편리하게 FTP 혹은 SFTP 의 기능을 이용할 수 있습니다.
- 이 모듈은 ftp-simple 와 ssh2 모듈을 참조하였습니다.
Caution
If Node.js version is 8 or higher, use 0.4.0
or higher. Otherwise, use version 0.3.44
.
Install
If Node.js version is 8 or higher.
npm install easy-ftp
Otherwise
npm install easy-ftp@0.3.44
Usage
var EasyFtp = require('easy-ftp');
var ftp = new EasyFtp();
var config = {
host: '',
port: 21,
username: '',
password: '',
type : 'ftp'
};
ftp.connect(config);
ftp.cd("/", function(err, path){});
ftp.rm("/filename", function(err){});
ftp.mkdir("/directory", function(err){});
ftp.mv("/filename", "/newFilename", function(err, newPath){});
ftp.ls("/directory", function(err, list){});
ftp.pwd(function(err, path){});
ftp.exist("/filename", function(exist){});
ftp.upload("/test.txt", "/test.txt", function(err){});
ftp.upload("/test.txt", "/test123.txt", function(err){});
ftp.upload("/test.txt", "/", function(err){});
ftp.upload("/directory", "/", function(err){});
var arr = [{local:"/test.txt", remote:"/test.txt"}, {local:"/test1.txt", remote:"/abcd/test2.txt"}, {local:"/directory", remote:"/"}];
ftp.upload(arr, function(err){});
var arr = ["/test.txt", "/abcd/test2.txt", "/directory"];
ftp.upload(arr, "/", function(err){});
var arr = [{local:"/test.txt", remote:"/directory/test.txt"}, "/abcd/test2.txt", "/directory"];
ftp.upload(arr, "/", function(err){});
ftp.download("/test.txt", "/test.txt", function(err){});
ftp.download("/test.txt", "/test123.txt", function(err){});
ftp.download("/test.txt", "/", function(err){});
ftp.download("/directory", "/", function(err){});
var arr = [{remote:"/test.txt", local:"/test.txt"}, {remote:"/test1.txt", local:"/abcd/test2.txt"}, {remote:"/directory", local:"/"}];
ftp.download(arr, function(err){});
var arr = ["/test.txt", "/abcd/test2.txt", "/directory"];
ftp.download(arr, "/", function(err){});
var arr = [{remote:"/test.txt", local:"/directory/test.txt"}, "/abcd/test2.txt", "/directory"];
ftp.download(arr, "/", function(err){});
ftp.close();
API
Methods
-
connect(< object >config)
- host - string - server domain or ip Default: 'localhost'
- port - number - port (default : 21)
- type - string - ftp type. 'ftp' or 'sftp' (default : 'ftp')
- username - string - username for authentication Default: 'anonymous',
- password - string - password for authentication. Default: 'anonymous@'
- privateKey - string - (only sftp)string that contains a private key for either key-based or hostbased user authentication (OpenSSH format) Default: none
- path - string - start path.
- secure - boolean - (only ftp) Explicit FTPS over TLS, default: false
- secureOptions - object - (only ftp) Options for TLS, same as for
tls.connect()
in Node.js.
-
cd(< string >path, < function >callback) - Changes the working directory. callback has 1 parameter: < Error >err.
-
rm(< string >path, < function >callback) - Deletes a file or directory(include child files) path on the server. callback has 1 parameter: < Error >err.
-
mkdir(< string >path, < function >callback) - Creates a new directory recursive. callback has 1 parameter: < Error >err.
-
mv(< string >oldPath, < string >newPath, < function >callback) - Renames or Move oldPath to newPath on the server. callback has 2 parameter: < Error >err, < String >newPath.
-
ls(< string >path, < function >callback) - Retrieves the directory listing of path. callback has 2 parameter: < Error >err, < Array >list.
- name - string - file name
- size - number - file size
- type - string - file type. 'd' => directory, 'f' => file
- date - date - file last modified date
-
pwd(< function >callback) - Retrieves the current working directory. callback has 2 parameters: < Error >err, < string >cwd.
-
exist(< function >callback) - whether a file or direcotry exists. callback has 1 parameters: < boolean >exist.
-
upload(< mixed >localPath, < string >remotePath, < function >callback) - Sends data to the server to be stored as remotePath. If direcotry path, include self directory and child files. If you want only child files, localPath is "/directory/**". callback has 1 parameter: < Error >err.
- file - ex) upload("/test.txt", "/a/b/test.txt", ...) => result : /a/b/test.txt
- directory - ex) upload("/directory", "/a/b", ...) => result : /a/b/directory
- only child files - ex) upload("/directory/**", "/a/b", ...) => result : /a/b/child files...
- array - ex) upload(["/directory/**", "/test.txt"], "/a/b", ...) => result : "/a/b/test.txt" and "/a/b/child files..."
-
download(< mixed >remotePath, < string >localPath, < function >callback) - Retrieves a file or directory at path from the server. If directory path, include child files. callback has 1 parameter: < Error >err.
- file - ex) download("/test.txt", "/a/b/test.txt", ...) => result : /a/b/test.txt
- directory - ex) download("/directory", "/a/b", ...) => result : /a/b/directory
- only child files - ex) download("/directory/**", "/a/b", ...) => result : /a/b/child files...
- array - ex) download(["/directory/**", "/test.txt"], "/a/b", ...) => result : "/a/b/test.txt" and "/a/b/child files..."
-
close() - Closes the connection to the server after any/all enqueued commands have been executed.
Event
-
open(< FTPClient >client) - Emitted when connection and authentication were sucessful.
-
close - Emitted when the connection has fully closed.
-
error(< Error >err) - Emitted when the connection has fully closed.
-
upload(< string >uploadedRemotePath) - Emitted when file or directory uploaded.
-
uploading(< object >data) - (sftp only) Emitted when file was transferred.
-
download(< string >downloadedLocalPath) - Emitted when file or directory downloaded.
-
downloading(< object >data) - (sftp only) Emitted when file was transferred.
Examples
var ftp = new EasyFTP();
var config = {
host: 'localhost',
port: 21,
username: 'id',
password: 'password',
type : 'ftp'
};
ftp.connect(config);
var ftp = new EasyFTP();
ftp.connect({...});
ftp.upload("/test/test.txt", "/test.txt", function(err){
ftp.close();
});
var ftp = new EasyFTP();
ftp.connect({...});
ftp.upload("/test/**", "/", function(err){
ftp.close();
});
var ftp = new EasyFTP();
ftp.connect({...});
ftp.upload("/test", "/", function(err){
ftp.close();
});
var arr = [{local:"/test.txt", remote:"/test.txt"}, {local:"/test1.txt", remote:"/abcd/test2.txt"}, {local:"/directory", remote:"/"}];
ftp.upload(arr, function(err){ftp.close();});
var arr = ["/test.txt", "/abcd/test2.txt", "/directory"];
ftp.upload(arr, "/", function(err){ftp.close();});
var arr = [{local:"/test.txt", remote:"/directory/test.txt"}, "/abcd/test2.txt", "/directory"];
ftp.upload(arr, "/", function(err){ftp.close();});
var ftp = new EasyFTP();
ftp.connect({...});
ftp.download("/test/test.txt", "/test.txt", function(err){
ftp.close();
});
var ftp = new EasyFTP();
ftp.connect({...});
ftp.download("/test", "/", function(err){
ftp.close();
});
var arr = [{remote:"/test.txt", local:"/test.txt"}, {remote:"/test1.txt", local:"/abcd/test2.txt"}, {remote:"/directory", local:"/"}];
ftp.download(arr, function(err){ftp.close();});
var arr = ["/test.txt", "/abcd/test2.txt", "/directory"];
ftp.download(arr, "/", function(err){ftp.close();});
var arr = [{remote:"/test.txt", local:"/directory/test.txt"}, "/abcd/test2.txt", "/directory"];
ftp.download(arr, "/", function(err){ftp.close();});