0

通过 spawn 作为控制台中的起始节点?你需要在cmd中运行node js,而不是单独运行,然后通过她的团队。

var iconv = require('iconv-lite');
var fs = require('fs');
function sendData (msg) {
    console.log('write msg ', msg);
    cmd.stdin.write(msg + "\r\n");
}
function execCommand() {
    var s = { 
        e : 'exec_command',
        d : {
          data : {}
        }
    };
    cmd = require('child_process').spawn('cmd', ['/K']);

    cmd.stdout.on('data', function (data) {
        console.log(iconv.decode(data, 'cp866'))
    });
    cmd.stderr.on('data', function (data) {
        console.log(iconv.decode(data, 'cp866'))
    });

}

execCommand();

sendData('node');
sendData(' for (var i in [1,2,3]) {console.log("HAHA", i)}');
4

1 回答 1

0

用于process.execPath获取 node.js 解释器的路径,并将其作为第一个参数传递给require('child_process').spawn.

require('child_process').spawn(process.execPath);

有关文档,请参阅http://nodejs.org/api/process.html#process_process_execpath

于 2013-09-09T18:34:42.333 回答