Node js can't upload files to FTP when deployed and running on production server

November 20, 2019 267 views
Nginx Node.js Ubuntu 16.04

I’m using Node JS (12.13.0) and NPM (6.13.19) with basic-ftp. Everything works fine and I can upload files to the remote FTP (without SSL, my remote FTP doesn’t allow this) when I run the code on my development machine from localhost.

The production server is hosted on Digital Ocean (Ubuntu 18.04.3) I have tried to disable the firewall, because I thought this might be the reason to the problem. I used sudo ufw disable and just to make sure it’s disabled I check the current status with sudo ufw statuswhich returns Status: inactive.

This is my code

async function uploadImageToFtp(fileName, path) {
    const client = new ftp.Client()
    client.ftp.verbose = true
    try {
        await client.access({
            host: process.env.FTP_HOST,
            user: process.env.FTP_USER,
            password: process.env.FTP_PASSWORD,
            secure: false
        })
        await client.uploadFrom(path, "images/bd/" + fileName)
    } catch (err) {
        console.log(err)
    }
    client.close()
}

Response on production

Connected to EXTERNAL_IP_ADDRESS < 220 server ready - login please Login
security: No encryption
> USER username < 331 password required
> PASS ###

Again on localhost everything works and we get past this step and starts uploading the file(s) to the same server and credentials.

After this I never get any response, except for a timeout with Bad Gateway 502 from my request.

Same questions is posted and asked on SO:
https://stackoverflow.com/questions/58949317/node-js-cant-upload-files-to-ftp-when-deployed-and-running-on-production-server

1 comment
  • Apparently I’m not able to connect to any FTP server from my Ubuntu Droplet. I have as mentioned earlier, tried to disable UFW. But I’m still not able to open a normal FTP connection with ftp command.

    It just returns with the following response:
    421 Service not available, remote server has closed connection
    Login failed.
    No control connection for command: Success

3 Answers

Hello, @grumsendev

Can you check if the port is open and any ftp service is listening on it? You can do this by using telnet, nmap (externaly) and using netstat (internally)

  1. Using netstat

You need to access your server via ssh and execute the following command:

netstat -plunta | grep 21

You will see if any service is listening on port 21 (FTP)

  1. Using nmap or telnet
telnet DropletIP 21

nmap DropletIP | grep 21

Let me know what is the output of this commands.

Regards,
Alex

Hi @alexdo,
Thanks for your reply.
Please see the details below.

netstat -plunta | grep 21
tcp        0    308 167.172.171.131:22      46.32.61.37:52185       ESTABLISHED -                   

telnet DropletIP 21
Trying DropletIP...
telnet: Unable to connect to remote host: Connection refused

nmap DropletIP | grep 21
Starting Nmap 7.60 ( https://nmap.org ) at 2019-11-21 09:27 UTC

DropletIP is my Droplet’s public IP.

edited by MattIPv4

It seems like that you haven’t configured any FTP service yet.

We do not provide any droplet images with FTP running by default as it is an inherently insecure protocol. SFTP is running on all new droplets on port 22 and can be accessed with your root login credentials using Filezilla. When logging in as root you will start out in the /root directory but will have access to access any directory you require.

You can also try to connect to your droplet via the terminal:

sftp root@yourIPaddress

You can check this article on how to setup vsftpd on your droplet.

https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-for-a-user-s-directory-on-ubuntu-18-04

If you want to connect from your droplet to any other FTP host, you can first try to make sure that the external FTP host is reachable and that ftp is running as well by using the telnet and the nmap commands from my previous reply.

Hope this helps.

Regards,
Alex

by Melissa Anderson
by Kathleen Juell
FTP, short for File Transfer Protocol, is a network protocol that was once widely used for moving files between a client and server. It has since been replaced by faster, more secure, and more convenient ways of delivering files. In this tutorial, we'll show you how to configure vsftpd to allow a user to upload files to his or her home directory using FTP with login credentials secured by SSL/TLS.
Have another answer? Share your knowledge.