What is jsftp?
jsftp is a lightweight FTP client for Node.js that allows you to interact with FTP servers. It supports a variety of FTP operations such as uploading, downloading, renaming, and deleting files, as well as creating and removing directories.
What are jsftp's main functionalities?
Connecting to an FTP server
This code demonstrates how to connect to an FTP server using jsftp. You need to provide the host, username, and password for the FTP server.
const JSFtp = require('jsftp');
const ftp = new JSFtp({
host: 'ftp.example.com',
user: 'username',
pass: 'password'
});
ftp.auth('username', 'password', (err, res) => {
if (err) return console.error(err);
console.log('Connected to FTP server');
});
Uploading a file
This code demonstrates how to upload a file from the local filesystem to the FTP server using jsftp.
ftp.put('local/path/to/file.txt', 'remote/path/to/file.txt', err => {
if (err) return console.error(err);
console.log('File uploaded successfully');
});
Downloading a file
This code demonstrates how to download a file from the FTP server to the local filesystem using jsftp.
ftp.get('remote/path/to/file.txt', 'local/path/to/file.txt', err => {
if (err) return console.error(err);
console.log('File downloaded successfully');
});
Listing files in a directory
This code demonstrates how to list files in a directory on the FTP server using jsftp.
ftp.ls('remote/path/to/directory', (err, res) => {
if (err) return console.error(err);
res.forEach(file => {
console.log(file.name);
});
});
Deleting a file
This code demonstrates how to delete a file on the FTP server using jsftp.
ftp.raw('DELE', 'remote/path/to/file.txt', (err, res) => {
if (err) return console.error(err);
console.log('File deleted successfully');
});
Other packages similar to jsftp
ftp
The 'ftp' package is another FTP client for Node.js. It provides similar functionalities to jsftp, such as connecting to an FTP server, uploading, downloading, and deleting files. However, 'ftp' is known for its simplicity and ease of use, making it a good choice for basic FTP operations.
promise-ftp
The 'promise-ftp' package is a promise-based FTP client for Node.js. It offers similar functionalities to jsftp but uses promises instead of callbacks, which can make the code cleaner and easier to manage, especially for complex workflows.
ssh2-sftp-client
The 'ssh2-sftp-client' package is an SFTP client for Node.js. While it focuses on SFTP rather than FTP, it provides similar functionalities such as uploading, downloading, and managing files on a server. It is built on top of the 'ssh2' package and is known for its reliability and performance.
jsFTP
jsFTP is a client FTP module for NodeJS that focuses on correctness, clarity and conciseness. It doesn't get in the middle of the user intentions.
jsFTP gives the user access to all the raw commands of FTP in form of methods in the Ftp
object. It also provides several convenience methods for actions that require complex chains of commands (e.g. uploading and retrieving files). When commands succeed they always pass the response of the FTP server to the callback, in the form of an object that contains two properties: code
, which is the response code of the FTP operation, and text
, which is the complete text of the response.
FTP raw (or native) commands are accessible in the form Ftp.raw["desired_command"](params, callback)
Thus, a command like QUIT
will be called like
Ftp.raw.quit(function(err, data) {
if (err)
throw err;
console.log("Bye!");
});
and a command like MKD
, which accepts parameters will look like
Ftp.raw.mkd("/new_dir", function(err, data) {
if (err)
throw err;
console.log(data.text); // Presenting the FTP response text to the user
});
Usage examples
// Initialize some common variables
var user = "johndoe";
var pass = "12345"
var ftp = new Ftp({
host: "myhost.com",
port: 21, // The port defaults to 21, but let's include it anyway.
});
// First, we authenticate the user
ftp.auth(user, pass, function(err, res) {
if (err) throw err;
// Retrieve a file in the remote server. When the file has been retrieved,
// the callback will be called with `data` being the Buffer with the
// contents of the file.
// This is a convenience method that hides the actual complexity of setting
// up passive mode and retrieving files.
ftp.get("/folder/file.ext", function(err, data) {
if (err) throw err;
// Do something with the buffer
doSomething(data);
// We can use raw FTP commands directly as well. In this case we use FTP
// 'QUIT' method, which accepts no parameters and returns the farewell
// message from the server
ftp.raw.quit(function(err, res) {
if (err) throw err;
console.log("FTP session finalized! See you soon!");
});
});
});
// The following code assumes that you have authenticated the user, just like
// I did in the code above.
// Create a directory
ftp.raw.mkd("/example_dir", function(err, res) {
if (err) throw err;
console.log(data.text);
});
// Delete a directory
ftp.raw.rmd("/example_dir", function(err, res) {
if (err) throw err;
console.log(data.text);
});
You can find more usage examples in the unit tests for it. This documentation
will grow as jsFTP evolves, I promise!
Installation
With NPM:
npm install jsftp
From GitHub:
git clone https://github.com/sergi/jsFTP.git
License
See LICENSE.