R.z*_*ald 2 webkit exe node.js node-webkit
我正在使用Node Webkit作为我的网络应用程序,我真的是使用node webkit的新手.我想在我的应用程序中运行我的exe,但我甚至无法使用'child_process'打开简单的记事本.我在网站上看到了一些例子,但我发现很难运行notepad.exe,请提前帮助并非常感谢.
var execFile = require
('child_process').execFile, child;
child = execFile('C:\Windows\notepad.exe',
function(error,stdout,stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+ error.code);
console.log('Signal received: '+
error.signal);
}
console.log('Child Process stdout: '+ stdout);
console.log('Child Process stderr: '+ stderr);
});
child.on('exit', function (code) {
console.log('Child process exited '+
'with exit code '+ code);
});
Run Code Online (Sandbox Code Playgroud)
此外,我试图使用meadco-neptune插件运行exe并添加插件我把代码放在package.json文件中,但它显示无法加载插件.我的package.json文件是这样的
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "index.html",
"window": {
"toolbar": false,
"frame": false,
"resizable": false,
"show": true,
"title": " example"
},
"webkit": {
"plugin": true
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "xyz",
"license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)
在node.js中,有两种方法可以使用标准模块启动外部程序child_process
:exec
和spawn
.
使用exec
时,在外部程序退出时获取stdout和stderror信息.数据只返回node.js,正如Mi Ke Bu在评论中正确指出的那样.
但是如果你想以交互方式从外部程序接收数据(我怀疑你不打算发射notepad.exe),你应该使用另一种方法 - spawn
.
考虑这个例子:
var spawn = require('child_process').spawn,
child = spawn('C:\\windows\\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
});
Run Code Online (Sandbox Code Playgroud)
此外,您需要在路径名中使用双向后斜杠:C:\\Windows\\notepad.exe
否则您的路径将被评估为
C:windows
notepad.exe
(带行返回),这当然不存在.
或者您可以使用正斜杠,如示例中的命令行参数.