user3130478
user3130478

Reputation: 217

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!"

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!

Upvotes: 0

Views: 817

Answers (1)

user94559
user94559

Reputation: 60143

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

Upvotes: 1

Related Questions