Tuesday 16 May 2017

get response from CasperJS in Node.JS

I have a script which is calling another CasperJS script.

    var exec = require('child_process').exec;
    exec("casperjs webCheck/thisWebCheck.js",puts);

I need a way to get a response from this script back to the calling script. It just has to be a Boolean value, just yes the webcheck worked, or no the website is down. Then depending on that response I will execute one piece of code or another.

I have searched all through web posts, blog posts and stack exchange for an answer to this and have come up empty.

When I try to use the exit code, it never comes back with what I told it to exit with.

When i use STDOUT and try to validate against it such as

thisWebCheck.js
///////////////
...
if(failed) {
  console.log("failed");
} else {
  console.log("success")
}


Main.js
///////////////
var puts = function(error, stdout, stderr){
  if (stdout == "failed"){
    doSomething();
  } else if (stdout == "success") {
      doSomethingElse();
  } else {
    console.log(stdout);
  }
};

The main.js will console.log() the stdout but won't validate against the strings.

TL/DR: I just need some way to communicate between the two scripts. Also, if there is a better way to call the casper file, let me know.



via Cody Nichols

No comments:

Post a Comment