Node.js FTP Client Tutorial

Introduction

FTP stands for File Transfer Protocol. Using an FTP client is a method to download, upload and manage files on our server. To build an FTP client in node we have to use some third-party library as there is no built-in library available for FTP client.

Here we are going to use pretty famous third party library which is node-ftp to create FTP Client in Node.

node-ftp is an FTP client module for node.js that provides an asynchronous interface for communicating with an FTP server.

Let’s get start.

To use node-ftp library, we have to install it using following command.

npm install ftp

Download file using ftp client in Node.js

Download remote file ‘foo.txt’ and save it to the local file system:

const FTPClient = require('ftp');
let ftp_client = new FTPClient();
const fs = require("fs");
let ftpConfig = {
     host: "localhost",
     port: 21,
     user: 'test',
     password: 'test',
}
//create a connection to ftp server
ftp_client.connect(ftpConfig);

ftp_client.on('ready', function() {
    ftp_client.get('foo.txt', function(err, stream) {
      if (err) throw err;
      stream.once('close', function() { ftp_client.end(); });
      stream.pipe(fs.createWriteStream('foo.local-copy.txt'));
    });
  });

Upload file using ftp client in Node.js

Upload local file ‘foo.txt’ to the FTP server:

const FTPClient = require('ftp');
let ftp_client = new FTPClient();
let ftpConfig = {
     host: "localhost",
     port: 21,
     user: 'test',
     password: 'test',
}
//create a connection to ftp server
ftp_client.connect(ftpConfig);

ftp_client.on('ready', function() {
    ftp_client.put('foo.txt', 'foo.remote-copy.txt', function(err) {
      if (err) throw err;
      ftp_client.end();
    });
  });

List directory of remote ftp server in Node.js

Get a directory listing of the current (remote) working directory:

const FTPClient = require('ftp');
let ftp_client = new FTPClient();
let ftpConfig = {
     host: "localhost",
     port: 21,
     user: 'test',
     password: 'test',
}
//create a connection to ftp server
ftp_client.connect(ftpConfig);

ftp_client.on('ready', function() { 
    ftp_client.list(function(err, list) {
      if (err) throw err;
      console.dir(list);
      ftp_client.end();
    });
  });

Events supported in node-ftp:

  • greeting(< string >msg) – Emitted after connection. msg is the text the server sent upon connection.
  • ready() – Emitted when connection and authentication were sucessful.
  • close(< boolean >hadErr) – Emitted when the connection has fully closed.
  • end() – Emitted when the connection has ended.
  • error(< Error >err) – Emitted when an error occurs. In case of protocol-level errors, err contains a ‘code’ property that references the related 3-digit FTP response code.

That is all about the FTP client. With help of this open-source library, you can build your own FTP client written in Nodejs. Thanks for reading😊

#Happy Coding #Topcode #topcode.in

#ftp client node js #ftp-client node js example #install ftp-client in node js #node js ftp client upload #ftp nodejs #ftp using node js #ftp node js example #ftp client nodejs #sftp client node js #node js express ftp-client #ftp server using node.js #ftp server node js #ftp client javascript #javascript ftp client example #javascript ftp client browser #javascript ftp client library #html javascript ftp client #javascript client side ftp #javascript ftp client stream #sftp client javascript #javascript sftp client example #ftp client in javascript #javascript tftp client ftp node js example #ftp node js server #basic ftp node js #ftp-watcher node js #node js ftp server example #node js ftp upload example #node js ftp upload #node js ftp file upload #ftp node js #ftp using node js #node.js basic-ftp #node js ftp client #node js ftp client example #node js create ftp server #node js express ftp-client #node js ftp client upload #node js download ftp file #node js ftp download example #node js ftp download #sftp node.js #sftp node js example #node.js sftp upload example #node js sftp server #node.js sftp download example #node js connect to sftp server #sftp to s3 node js #node js synchronous sftp #node js ftp-srv example #node js express ftp #node js read ftp file #node js ftp multiple files #node js fs ftp #node js ftp stream #node js simple ftp server #node js ftp get #ftp in node js #ftp server in node js #ftp connection in node js #install node js ftp #node.js ftp list #node js ftp module #node js ftp passive mode #node js ftp connect #ftp server using node.js #node js ftp put #node js proxy ftp #node.js ftp request #node js simple ftp #node js connect to ftp server #servidor ftp node js #connect to ftp node js #node js upload to ftp #tftp node js #node js tftp server #node js ftp upload file #connect ftp using node js #ftp with node js #ftp server with node js

, , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site