-
-
Save yalamber/8035e853f0508edb060e to your computer and use it in GitHub Desktop.
file watching in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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