Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Nickibrochner/4597cbb8418745b4c98bbc2f043f687c to your computer and use it in GitHub Desktop.
Save Nickibrochner/4597cbb8418745b4c98bbc2f043f687c to your computer and use it in GitHub Desktop.
Trying to call drawchar.js via GET
app.get('/test', function(req, res) {
var childProcess = require('child_process');
function runScript(scriptPath, callback) {
// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;
var process = childProcess.fork(scriptPath);
// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});
// execute the callback once the process has finished running
process.on('exit', function (code) {
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error('exit code ' + code);
callback(err);
});
}
// Now we can run a script and invoke a callback when complete, e.g.
runScript('./js/drawchart.js', function (err) {
if (err) throw err;
jsonResponse.push({ "text": "finished running drawchart.js" });
res.send(jsonResponse);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment