watchr
watchr copied to clipboard
How to use the callback function
Hi, I have an application using the watchr module with the following features: the app is a nodejs server, which will monitor a folder. If a new html file created, the app will push this html file to the client that connected. Here is the sketch of my code, and I don't know how to implement a callback function as a bridge to connect the monitor part and the send to client part. Any suggestion and comment are welcome.
var filePath = null;
// Define our watching parameters
var path = 'C:\\Users\\fetag\\jsexample\\save';
function listener(changeType, fullPath, currentStat, previousStat) {
switch (changeType) {
case 'update':
console.log('the file', fullPath, 'was updated');
break
case 'create':
console.log('the file', fullPath, 'was created');
filePath = fullPath;
//fs.unlink(fullPath);
break
case 'delete':
console.log('the file', fullPath, 'was deleted');
break
}
}
function next(err) {
if (err) return console.log('watch failed on', path, 'with error', err)
//console.log('watch successful on', path)
}
// Watch the path with the change listener and completion callback
var stalker = watchr.open(path, listener, next)
app.use(function(req, res) {
console.log("start to send: " + filePath);
res.sendFile(filePath, {root: __dirname })
console.log("send ok!!!!");
});