javascript 如何从主 node.js 脚本运行多个 node.js 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27560968/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:38:06  来源:igfitidea点击:

How can I run multiple node.js scripts from a main node.js scripts?

javascriptjquerynode.js

提问by Sagar Karira

I am totally new to node.js .I have two node.js script that I want to run . I know I can run them separately but I want to create a node.js script that runs both the scripts .What should be the code of the main node.js script?

我对 node.js 完全陌生。我有两个要运行的 node.js 脚本。我知道我可以单独运行它们,但我想创建一个运行这两个脚本的 node.js 脚本。主 node.js 脚本的代码应该是什么?

采纳答案by Mehran Hatami

All you need to do is to use the node.js module format, and export the module definition for each of your node.js scripts, like:

您需要做的就是使用 node.js 模块格式,并导出每个 node.js 脚本的模块定义,例如:

//module1.js
var colors = require('colors');

function module1() {
  console.log('module1 started doing its job!'.red);

  setInterval(function () {
    console.log(('module1 timer:' + new Date().getTime()).red);
  }, 2000);
}

module.exports = module1;

and

//module2.js
var colors = require('colors');

function module2() {
  console.log('module2 started doing its job!'.blue);

  setTimeout(function () {

    setInterval(function () {
      console.log(('module2 timer:' + new Date().getTime()).blue);
    }, 2000);

  }, 1000);
}

module.exports = module2;

The setTimeoutand setIntervalin the code are being used just to show you that both are working concurrently. The first module once it gets called, starts logging something in the console every 2 second, and the other module first waits for one second and then starts doing the same every 2 second.

代码中的setTimeoutsetInterval只是为了向您展示两者同时工作。第一个模块一旦被调用,就会每 2 秒开始在控制台中记录一些东西,另一个模块首先等待一秒钟,然后每 2 秒开始做同样的事情。

I have also used npm colors packageto allow each module print its outputs with its specific color(to be able to do it first run npm install colorsin the command). In this example module1prints redlogs and module2prints its logs in blue. All just to show you that how you could easily have concurrency in JavaScript and Node.js.

我还使用了npm 颜色包来允许每个模块使用其特定颜色打印其输出(以便能够首先npm install colors在命令中运行)。在这个例子中module1打印red日志并将module2其日志打印在blue. 一切只是为了向您展示如何在 JavaScript 和Node.js.

At the end to run these two modules from a main Node.jsscript, which here is named index.js, you could easily do:

最后从主Node.js脚本运行这两个模块,这里命名为index.js,您可以轻松地执行以下操作:

//index.js
var module1 = require('./module1'),
  module2 = require('./module2');

module1();
module2();

and execute it like:

并像这样执行它:

node ./index.js

Then you would have an output like:

然后你会得到如下输出:

enter image description here

在此处输入图片说明

回答by lwang135

You can use child_process.spawnto start up each of node.js scripts in one. Alternatively, child_process.forkmight suit your need too.

您可以使用一个child_process.spawn来启动每个 node.js 脚本。或者,child_process.fork也可能适合您的需要。

Child Process Documentation

子进程文档