Node.js - Organising code and closures - SFTP/Inotify

I was hoping I could get some advice on why my nodejs program is behaving in the way it is.

I am using two modules, node-sftp and node-inotify. I have setup node-inotify to watch a directory and call a function when something is written there, the function being an sftp upload.

Now the problem I have is that processing one file at a time is fine but when I drop 4 files in one go there, the function is called four times but only one sftp upload goes through.

Do I need to order my code in a particular way to ensure that the sftp upload occurs x times, is this something to do with closures perhaps?

This is a basic version of my code...

  • "event_handler" is called when something happens on a "watched" directory
  • "check_event" figures out if this type of event is one we want, in this case it's a "write"
  • "ftp_to_server" prepare connection details
  • "do_ftp" basically uses the node-sftp module to perform the sftp upload

    event_handler = function(event){
        var supplier;
        check_event(event, supplier, type, ftp_to_server);


    function check_event(event, handler)
        if (event.type === 'xxxxxx') {
            var file_to_process_name = 'abc';
            var file_to_process_dir = 'abc';
            var remote_dir = 'abc';
            handler(file_to_process_name, file_to_process_dir, remote_dir);

    function ftp_to_server(file_to_process_name, file_to_process_dir, remote_dir) {
        var connection_details = conf.ftp.connections
        do_ftp(connection_details, file_to_process_name, file_to_process_dir, remote_dir);    

    function do_ftp(connection_details, file_to_process_name, file_to_process_dir, remote_dir) {

        var credentials = {
            // FTP settings here
        var local_file = file_to_process_dir + file_to_process_name;
        var remote_file = remote_dir + file_to_process_name;

        connection = new sftp(credentials, function(err) {
            if (err){
                throw err;
            connection.writeFile(remote_file, fs.readFileSync(local_file, "utf8"), null, function(err) {
                if (err) {
                    throw err;

      'FTP PUT DONE');



Your "connection = new sftp(credentials, function(err) {" should be var connection = new sftp(credentials, function(err) {

The way you currently have it coded, "connection" is a global and you are writing over it.

Need Your Help

Unable to get dynamically created textbox.text data on button click event using c# and XAML

c# wpf

I'm having trouble getting text from a textbox in c#/xaml. I am running 2 methods - the first which creates a stackpanel and adds 2 textboxes to it and the second is intended to just take the text...

Can someone please explain this code to me (Python )

python email hotmail

I have found the following code below from a website explaining how to send emails (Hotmail/live) using python. I can understand some of the code but most of it is totally confusing. For example what

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.