简体   繁体   English

启动多个本地 node.js 服务器的正确方法是什么?

[英]What is the proper way to start multiple local node.js servers?

I'm creating an example project for an open source framework.我正在为一个开源框架创建一个示例项目。 For my demo to run, some of it's dependencies must be running local servers on other ports.要运行我的演示,它的一些依赖项必须在其他端口上运行本地服务器。

I'd rather just provide them a single command to run instead of telling them to open multiple terminals and run multiple commands in each.我宁愿只为他们提供一个命令来运行,而不是告诉他们打开多个终端并在每个终端中运行多个命令。

What is the best/most proper/most elegant way to go about this?解决此问题的最佳/最合适/最优雅的方法是什么?

This is how I accomplish this for two web servers.这就是我为两个 Web 服务器完成此操作的方式。 You should be able to play with more & 's and fg 's to get more servers.您应该能够使用更多&fg来获得更多服务器。

package.json :包.json :

{
    "scripts": {
        "start": "node node_modules/something/server.js & node server.js && fg
    }
}

So the user only has to run npm install and then npm start to run two servers in one terminal and ctrl-c kills both.所以用户只需要运行npm install然后npm start在一个终端中运行两台服务器, ctrl-c两者都杀死。

Breakdown:细分:
node node_modules/something/server.js & run this server in the background node node_modules/something/server.js &在后台运行这个服务器
node server.js && run my server in the foreground node server.js &&在前台运行我的服务器
fg move the most recently backgrounded shell into the foreground fg将最近背景的 shell 移到前台

If you use the npm package call 'concurrently' set up your package.json file as below如果你使用 npm 包调用 'concurrently' 设置你的 package.json 文件如下

you can use the following 3 commands run only server您可以使用以下 3 个命令只运行服务器

npm run server npm 运行服务器

run only client只运行客户端

npm run client npm 运行客户端

run both运行两者

npm run dev npm 运行开发

  "scripts": {
    "server": "nodemon server.js --ignore client",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },

For those who want this case:对于那些想要这个案例的人:

If you want to run a single script that will open multiple terminals and run different nodejs servers on each you can do something like (this is for windows.. for other os you can change command) :如果您想运行一个脚本来打开多个终端并在每个终端上运行不同的 nodejs 服务器,您可以执行以下操作(这适用于 Windows .. 对于其他操作系统,您可以更改命令)

You can write a single nodejs file that will start all your other servers in different terminal windows您可以编写一个 nodejs 文件,该文件将在不同的终端窗口中启动所有其他服务器

startAllServers.js: startAllServers.js:

const child_process = require('child_process');

// commands list
const commands = [
    {
        name: 'Ap1-1',
        command: 'cd ./api1 && start nodemon api1.js'
    },
    {
        name: 'Ap1-2',
        command: 'cd ./api2 && start nodemon api2.js'
    }
];

// run command
function runCommand(command, name, callback) {
    child_process.exec(command, function (error, stdout, stderr) {
        if (stderr) {
            callback(stderr, null);
        } else {
            callback(null, `Successfully executed ${name} ...`);
        }
    });
}

// main calling function
function main() {
    commands.forEach(element => {
        runCommand(element.command, element.name, (err, res) => {
            if (err) {
                console.error(err);
            } else {
                console.log(res);
            }
        });
    });
}

// call main
main();

Use concurrently npm package.同时使用 npm 包。

concurrently "node server.js" "node client.js"

This allows you to write multiple commands with clean output in one go.这允许您一次性编写多个具有干净输出的命令。 And they don't just have to be node servers.而且它们不仅必须是节点服务器。 You can combine any bash commands.您可以组合任何 bash 命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM