Search code examples
node.jsexpresscmdchild-processspawn

Unable to spawn a custom .exe from express.js on Windows


I am using node.js/express.js on windows and I have a command I execute when a user takes a image and uploads up from there phone. Once it is uploaded I run myApp.exe to perform some openCV image processing and I output the updated images to a output directory that is a argument in the command below.

I am able to kick this off from my webapp using child_process.exec, but the performance is 60x slower if I run it at command line by itself. To increase the performance I was hoping to use Spawn, but I don't know if this is an accurate assumption, please let me know if it is not.

var exec = require('child_process').exec; 
var child = exec('C:\\opt\\package_v030_package\\myApp.exe 
--user="C:\\opt\\package_v030_package\\Phone\\'+file.filename+'" 
--mv="C:\\opt\\package_v030_package\\mv\\'+req.body.detectionString+'.bmp" 
--outPath="C:\\opt\\package_v030_package\\output" 
--outputScaled 
--outputScaledOverlaid');

  child.stdout.on('data', function(data) {
      console.log('stdout: ' + data);
  });
  child.stderr.on('data', function(data) {
      console.log('stdout: ' + data);
  });
  child.on('close', function(code) {
      console.log('closing code: ' + code);
      //res.json("success")
  });

I have tried to kick it off using spawn, but it fails to execute with the following: "error child process exited with code 4294967295". The code is below:

  var spawn = require('child_process').spawn;
  var cmd = spawn('cmd', ['/s',
  '/c',
  'C:\\opt\\package_v030_package\\myApp.exe',
  '--user="C:\\opt\\package_v030_package\\Phone\\'+file.filename+'"',
  '--mv="C:\\opt\\package_v030_package\\mv\\'+req.body.detectionString+'.bmp"',
  '--outPath="C:\\opt\\package_v030_package\\output"',
  '--outputScaled',
  '--outputScaledOverlaid'
  ]);

  cmd.stdout.on('data', (data) => {
   console.log(`stdout: ${data}`);
  });

  cmd.stderr.on('data', (data) => {
   console.log(`stderr: ${data}`);
  });

  cmd.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });

It seems I am able to execute just myApp.exe from spawn because when I add any of my arguments it fails. Even when I hard code the variables that I inject. Is there an issue with my arguments or am I spawning myApp.exe incorrectly?

Update 1

I placed the command in a .bat and was able to execute it from node.js using spawn. It does not increase performance which leads me to believe that the decrease in performance is a limitation of node.js on the windows platform.

In addition, I performed a few tests using postman to see if I could optimize the call without anything else happening, but I did not succeed. I will leave this question open in the event this changes and node.js is able to better handle performance of a CPU intensive child process.

Update 2 & Answer

I was able to fix this by placing the command that we run at the command line into a java class taking in the detectionString as a parameter. Then from node I use spawn to kick off the .jar file. This caused the speed to increase significantly and run as if I was running it myself at command line.


Solution

  • I was able to fix this by placing the command that we run at the command line into a java class taking in the detectionString as a parameter. Then from node I use spawn to kick off the .jar file. This caused the speed to increase significantly and run as if I was running it myself at command line.