使用 node.js 启动另一个节点应用程序
Start another node application using node.js?
我有两个独立的节点应用程序。我希望其中一个能够在代码中的某个点启动另一个。我将如何做到这一点?
使用 child_process.fork()
.它与spawn()
类似,但用于创建V8的整个新实例。因此,它专门用于运行 Node 的新实例。如果只是执行命令,请使用 spawn()
或 exec()
。
var fork = require('child_process').fork;
var child = fork('./script');
请注意,使用 fork()
时,默认情况下,stdio
流与父流相关联。这意味着所有输出和错误都将显示在父进程中。如果不希望流与父级共享,可以在选项中定义 stdio
属性:
var child = fork('./script', [], {
stdio: 'pipe'
});
然后,您可以将进程与主进程的流分开处理。
child.stdin.on('data', function(data) {
// output from the child process
});
另请注意,该过程不会自动退出。您必须从生成的 Node 进程中调用process.exit()
才能退出。
您可以使用
child_process模块,它将允许执行外部进程。
var childProcess = require('child_process'),
ls;
ls = childProcess.exec('ls -l', 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); });
ls.on('exit', function (code) { console.log('Child process exited with exit code '+code); });
http://docs.nodejitsu.com/articles/child-processes/how-to-spawn-a-child-process
相关文章:
- 获取docker容器中运行的节点应用程序的IP地址
- 只需添加一个'允许跨来源请求'到我的节点应用程序工作
- 节点应用程序无法识别功能
- 超级简单的节点应用程序,ä的问题ö等等字符
- 是否有离线词库?它应该可以通过节点应用程序进行搜索
- 如何使用 Restify 在同一端口上运行多节点应用程序
- 开发两个单独的节点应用程序来提供 Web 服务和使用 Web 服务以在浏览器上呈现它是一个好主意吗?
- 如何在节点应用程序中获取选定的下拉值
- 将节点应用程序中的表单数据重定向到 PHP 文件
- 如何在流打开时保持节点应用程序运行
- 无法在节点应用程序上获取/文件名错误
- Socket.io 两个节点应用程序
- 意外的输入结束 - 快速/节点应用程序上的 curl JSON
- 多变量是节点应用程序.js
- 将代理服务器(如 node-http-proxy )用于一个端口上具有单个应用程序的节点应用程序有什么意义
- 节点应用程序.js如何使用其推荐的功能
- 节点应用程序自动重新加载,自动编译咖啡脚本和自动编译更少的css
- 在我的机器上托管节点应用程序
- 使用 node.js 启动另一个节点应用程序
- 在浏览器或节点应用程序中的 JavaScript 中反序列化 UTF8 编码的 Byte[].js