简体   繁体   English

如何在本地和 hereko 服务器上使用命令行运行多个节点 js 服务。?

[英]How to run multiple node js services using command line on local and hereko server.?

I have multiple services in my node js app as below我的节点 js 应用程序中有多项服务,如下所示

.git
 domain
 public
 view
 .gitingore
 service1.js
 service2.js
 service3.js
 package.json
 package-lock.json
 Procfile
 README.md

service1.js code service1.js代码

var port = process.env.PORT_SERVICE1 || 8001;
 .....
 .....
 .....

service2.js code service2.js代码

 var port = process.env.PORT_SERVICE2 || 8002;
 .....
 .....
 .....

service3.js code service3.js代码

var port = process.env.PORT_SERVICE3 || 8003;
 .....
 .....
 .....

Currently i am running above3 services on windows as below..目前我在 windows 上运行以上 3 个服务,如下所示..

 set PORT_SERVICE1 = 8001
 set PORT_SERVICE2 = 8002
 set PORT_SERVICE3 = 8003

and opening 3 different command promt to run all services并打开 3 个不同的命令提示符来运行所有服务

 node service1.js
 node service2.js
 node service3.js

I want to configure same in heroko Procfile file.我想在 heroko Procfile 文件中进行相同的配置。 but not able to set it?但无法设置?

How to set different port for all services in node js and run them all?如何为节点js中的所有服务设置不同的端口并全部运行?

I suggest you using PM2我建议你使用PM2

It allows you create complex config file, that will start all your applications with configs you've provided.它允许您创建复杂的配置文件,该文件将使用您提供的配置启动所有应用程序。

Here's an example of how to run multiple applications using config file这是一个如何使用配置文件运行多个应用程序的示例

module.exports = {
  apps: [{
      name: 'sevice1',
      script: './services/sevice1',
      watch: true,
      env: {
        NODE_ENV: 'development',
      },
      env_production: {
        NODE_ENV: 'production',
      },
    }, {
      name: 'sevice2',
      script: './services/sevice2',
      watch: true,
      env: {
        NODE_ENV: 'development',
      },
      env_production: {
        NODE_ENV: 'production',
      },
    }],
};

Speaking of Heroku.说到Heroku。 PM2 provides Heroku integration . PM2 提供Heroku 集成 Hope this one helps!希望这个有帮助!

@number16BusShelter suggested a good option. @number16BusShelter 提出了一个不错的选择。 But if you want to do it quick then go for the below option.但是,如果您想快速完成,那么 go 可以选择以下选项。

Add below code in your Procfile:在您的 Procfile 中添加以下代码:

web: node service1.js && node service2.js && node service3.js

Reference: Heroku参考: Heroku

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

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