简体   繁体   English

NodeJS如果字符串包含执行脚本

[英]NodeJS if string contains execute script

I have a the following script: 我有以下脚本:

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);

I want to execute a javascript only if the string contains "Alert: found!" 我只想在字符串包含“ Alert:found!”的情况下执行javascript。

The script: 剧本:

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!' );
});

Help! 救命!

Are you asking how to combine the two? 您是在问如何将两者结合吗?

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM