JS FTP Synchronizer - Static Site Generator in JavaScript

Work on Node.js, put online a local image of a website.

A PHP version of the same program is also available : PHP FTP Synchronizer.

Two operating modes are implemented:

  1. The program compares the date of the local file with the file on the server. If the local file is newer, it is updated.
  2. The program compares this file with a double, also local, in a special directory. If the size is different or if the comparison shows a difference of content, the file is updated on the site.

In all cases the file is sent if it does not exist on the site or in the backup directory.

The second solution is faster and safer, having a copy that can be placed on a removable unit reduces risks of loss.

This program uses the asynchronous JavaScript framework JSFtp to send pages to the remote site. Performing a series of such operations asynchronously is a real challenge. In particular, it is difficult to create folders when they do not exist, then move files, all this piecemeal since the server performs the operations when it suits him and not when asked.

The simplest solution was found with the following algorithm:

function ftpSend(src, rmt, rdir) {
    var connection = new JSFtp(OPTIONS)
    connection.put(src, rmt, function(err) {
        if (err)  {
            connection.raw.mkd(rdir, function(err, data) {
                connection.put(src, rmt, function(err) {
                    if(err) {
                        return console.log("Error, file not uploaded.")
                    connection.raw.quit(function() {})
        connection.raw.quit(function() {})

The file is sent in all cases, if an error occurs, it is assumed that the directory does not exist, therefore it is created. Then file is sent again. If the error persists, then only it is shown that the file is not downloaded.

How to use

The site is updated with the following command, it is more convenient to put in a batch file:

node sync.js [options/parameters] source

The source is the local directory where the files to upload are. The other required parameters are as follows.

You may add the following options.


node sync.js -lmylogin -pmypass -fftp.example.com -dremotedir localdir

Download and install

To install the program:

  1. Extract the contents of the archive.
  2. Download and install Node.js if necessary.
  3. You may install jsftp: npm install jsftp (it is already in the archive actually).