如何在我的Javascript中执行二进制可删除文件?

Yum*_*Cao 0 javascript node.js

我在服务器端使用node.js.现在我想运行一些从.c代码编译的二进制文件,该怎么做?

我已经试过了 var obj = new ActiveXObject("WwScript.Shell"); obj.run("myBinary");

但是不起作用......非常感谢!

xda*_*azz 7

var sys = require('sys')
var exec = require('child_process').exec;
exec("/path/to/your/Binary", function(error, stdout, stderr) { sys.puts(stdout) });
Run Code Online (Sandbox Code Playgroud)

更新: 似乎sys不推荐使用模块,util而是使用@loganfsmyth提及.

var exec  = require('child_process').exec,
    child;

child = exec('/path/to/your/Binary',
  function (error, stdout, stderr) {
    console.log('stdout:', stdout);
    console.log('stderr:', stderr);
    if (error !== null) {
      console.log('exec error:', error);
    }
});
Run Code Online (Sandbox Code Playgroud)