Skip to content

Instantly share code, notes, and snippets.

@yalamber
Last active December 17, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yalamber/8035e853f0508edb060e to your computer and use it in GitHub Desktop.
Save yalamber/8035e853f0508edb060e to your computer and use it in GitHub Desktop.
file watching in nodejs
'use strict';
var request = require('request');
var fs = require('fs');
var Inotify = require('inotify').Inotify;
var inotify = new Inotify();
var return_watch_dir = function (dir_path) {
var return_data, headers, data_string;
return_data = {
path: dir_path,
watch_for: Inotify.IN_CLOSE_WRITE,
callback: function (event) {
var request_url, mask, file_loc, r, str;
request_url = 'http://example.com/usefile';
mask = event.mask;
file_loc = dir_path + '/' + event.name;
if (mask & Inotify.IN_CLOSE_WRITE) {
r = request.put({url : request_url + '?file_name=' + event.name});
fs.createReadStream(file_loc).pipe(r);
str = '';
r.on('data', function (chunk) {
str += chunk;
});
r.on('end', function () {
console.log(str);
});
}
}
};
return return_data;
};
//test watcher
inotify.addWatch(return_watch_dir('/home/user/test'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment