I'm running a node.js application which is acting as a man-in-the-middle proxy to proxy all of the requests I'm making through a PhantomJS headless testing environment. I want to spin up this proxy from either my PhantomJS test script (though I've looked into it and it seems that phantom does not have an exec()
command for executing shell commands) or a small shell script which manages both processes. Ideally, it would do something like
#!/bin/bash
node proxy.js
phantomjs runTests.js
kill node process here
Is there any way that I can do this?
Moments after asking this question, I found a much better way to execute the phantom program from within my node app by using a child process. I placed my phantom script within the folder which contained my node app, and then used exec like this:
var exec = require('child_process').exec;
var _phantom = exec('phantomjs runTests.js',function(error,stdout,stderr){
console.log(stdout);
};
_phantom.on('exit',function(code,sig){
process.exit(code);
});
This means I could spin up my proxy server, then execute the child process. The _phantom.on('exit')
block allows me to detect when the process exits on a code. Then, it's very simple to signal the node app to quit, using process.exit
.