run a windows batch file from node.js

前端 未结 4 1328
北恋
北恋 2020-12-05 15:58

am trying to run a test.bat file inside node.js

here is the code

var exec = require(\'child_process\').execFile;

case \'/start\':
    req.on(\'data         


        
4条回答
  •  無奈伤痛
    2020-12-05 16:19

    I have found the solution for it.. and its works fine for me. This opens up a new command window and runs my main node JS in child process. You need not give full path of cmd.exe. I was making that mistake.

    var spawn = require('child_process').spawn,
    ls    = spawn('cmd.exe', ['/c', 'my.bat']);
    
    ls.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
    });
    
    ls.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
    });
    
    ls.on('exit', function (code) {
    console.log('child process exited with code ' + code);
    });
    

提交回复
热议问题