Created
July 14, 2017 08:43
-
-
Save Nickibrochner/4597cbb8418745b4c98bbc2f043f687c to your computer and use it in GitHub Desktop.
Trying to call drawchar.js via GET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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