execute a batch file from nodejs

后端 未结 3 1858
难免孤独
难免孤独 2020-12-17 01:58

Would it be possible to run a batch file from a nodejs application?

After googling for some time we can use child_process to execute the commands. Tried

3条回答
  •  清酒与你
    2020-12-17 02:16

    An easier way I know for executing that is the following code :

    function Process() {
        const process = require('child_process');   
        var ls = process.spawn('script.bat');
        ls.stdout.on('data', function (data) {
          console.log(data);
        });
        ls.stderr.on('data', function (data) {
          console.log(data);
        });
        ls.on('close', function (code) {
           if (code == 0)
                console.log('Stop');
           else
                console.log('Start');
        });
    };
    
    Process();
    

提交回复
热议问题