DEV Community

abdellah ht
abdellah ht

Posted on

ftp auto upload files on change

Hello world, this is my first post on dev.

Today we're going to see how to watch files for change and upload them automatically to an ftp server using node.js. This is a real world project that I worked on for a client.

This article is intentionally short, feel free to open the code on this repo in second tab to follow along.

Let's think about the problem for a sec, we have 3 sub-problems:

  1. First we need a way to be notified when a certain file changed.
  2. Second we need to figure out how to upload a file to an ftp server.
  3. And last we will tie out all together and throw in some polish for a reliable end product.

A quick look at the native fs module reveals that we have the method fs.watch(directory, watchHandler) where the watch handler takes two arguments (event, fileName) =>{...} and event is either change or rename, we are mostly interested in change here. This takes care of the first sub-problem.

For the second sub-problem, I used the polished ssh2-sftp-client npm package. Which has many great features, but we're interesting in uploading files, so it's basically connect -> upload file.

The requirement for the source and destination folders needed to be specific for each file of interest. The simplest solution I found for this is a plain text file with each file on one line relying on the order of the items to represent the credentials for the ftp server and the folders. Further details are on the example config.txt, but it essentially looks like this:
filname username host port password source_folder destination_folder.

So to answer the third sub-problem, the general flow is like this:

  1. Read the files from the config.txt file
  2. massage the data to correspond to objects we can use down the line.
  3. create file watches for each folder and source folders and respond to change events when they concern a file from the watched files, and of course some light logging to inform the user about what's going on.

I hope you feel like you've learned something new. Don't forget check the code on github and comment any suggestions/questions you might have.

Top comments (0)