从 node.js 启动外部应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17733849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch an external application from node.js
提问by Pete Gardner
I'm writing a desktop web app that uses node.js to access the local file system. I can currently use node.js to open and copy files to different places on the hard drive. What I would also like to do is allow the user to open a specific file using the application that is associated with the file type. In other words, if the user selects "myfile.doc" in a Windows environment, it will launch MSWord with that file.
我正在编写一个使用 node.js 访问本地文件系统的桌面 Web 应用程序。我目前可以使用 node.js 打开文件并将其复制到硬盘上的不同位置。我还想做的是允许用户使用与文件类型关联的应用程序打开特定文件。换句话说,如果用户在 Windows 环境中选择“myfile.doc”,它将使用该文件启动 MSWord。
I must be a victim of terminology, because I haven't been able to find anything but the spawning of child processes that communicate with node.js. I just want to launch a file for the user to view and then let them decided what to do with it.
我一定是术语的受害者,因为除了与 node.js 通信的子进程的产生之外,我什么也找不到。我只想启动一个文件供用户查看,然后让他们决定如何处理它。
Thanks
谢谢
回答by Gntem
you can do this
你可以这样做
var cp = require("child_process");
cp.exec("document.docx"); // notice this without a callback..
process.exit(0); // exit this nodejs process
it not safe thought, to ensure that the command show no errors or any undesired output
这不是安全的想法,以确保命令显示没有错误或任何不需要的输出
you should add the callback parameter
child_process.exec(cmd,function(error,stdout,stderr){})
你应该添加回调参数
child_process.exec(cmd,function(error,stdout,stderr){})
and next you can work with events so you won't block execution of script or even make use of a external node.js script that launches and handles outputs from processes which you spawn from a "master" script.
接下来您可以处理事件,这样您就不会阻止脚本的执行,甚至不会使用外部 node.js 脚本来启动和处理您从“主”脚本生成的进程的输出。
回答by Yalamber
In below example I have used textmate "mate" command to edit file hello.js, you can run any command with child_process.exec but the application you want to open file in should provide you with command line options.
在下面的示例中,我使用 textmate“mate”命令来编辑文件 hello.js,您可以使用 child_process.exec 运行任何命令,但您要在其中打开文件的应用程序应为您提供命令行选项。
var exec = require('child_process').exec;
exec('mate hello.js');
回答by RemyG
var childProcess = require('child_process');
childProcess.exec('start Example.xlsx', function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout);
process.exit(0);// exit process once it is opened
})
Emphasis on where 'exit' is called. This executes properly in windows.
强调在何处调用“退出”。这在 Windows 中正确执行。
回答by JulianSoto
Simply call your file (any file with extension, including .exe) from the command promp, or programmatically:
只需从命令提示符或以编程方式调用您的文件(任何带有扩展名的文件,包括 .exe):
var exec = require('child_process').exec;
exec('C:\Users\Path\to\File.js', function (err, stdout, stderr) {
if (err) {
throw err;
}
})
If you want to run a file without extension, you can do almost the same, as follow:
如果你想运行一个没有扩展名的文件,你几乎可以这样做,如下:
var exec = require('child_process').exec;
exec('start C:\Users\Path\to\File', function (err, stdout, stderr) {
if (err) {
throw err;
}
})
As you can see, we use startto open the File, letting windows (or windows letting us) choose an application.
如您所见,我们使用start打开文件,让窗口(或窗口让我们)选择一个应用程序。

