2017-06-30 29 views
0

我有以下脚本:如果的NodeJS字符串包含执行脚本

function execute(){ 
require("fs").readFile("sometextfile.txt", function(err, cont) { 
    if (err) 
     throw err; 
    console.log("ALERT:"+(cont.indexOf("mystring")>-1 ? " " : " not ")+"found!"); 
}); 

} 

setInterval(execute,9000); 

我只希望如果字符串包含执行一个javascript“警告:发现”

脚本:

var Prowl = require('node-prowl'); 

var prowl = new Prowl('API'); 

prowl.push('ALERT', 'ALERT2', function(err, remaining){ 
     if(err) throw err; 
     console.log('I have ' + remaining + ' calls to the api during current hour. BOOM!'); 
}); 

帮助!

+0

你从自己的代码中得到了什么样的输出? –

回答

0

你在问怎么把两者结合起来?

const fs = require('fs'); 
const Prowl = require('node-prowl'); 

const prowl = new Prowl('API'); 

function alert() { 
    prowl.push('ALERT', 'ALERT2', function(err, remaining) { 
     if (err) throw err; 
     console.log('I have ' + remaining + ' calls to the API during current hour. BOOM!'); 
    }); 
} 

function poll() { 
    fs.readFile('sometextfile.txt', function(err, cont) { 
     if (err) throw err; 
     if (cont.indexOf('mystring') !== -1) { 
      alert(); 
     } 
    }); 
} 

setInterval(poll, 9000); 
+0

是的,非常感谢你! – user3130478

相关问题