简体   繁体   中英

PM2 Config with Multiple Processes

I am trying to configure a PM2 config file to start two services when its starts. I want to start React on port 80 ad Node api server on 8080. When it runs the API runs on 80 and nothing runs on 8080. What am I missing in my file. I used the default create react setup to create my React structure and I'm not sure what file the script should point to. Here is the file I created:

 module.exports = { apps : [ { name: 'REACTJS', script: 'client/src/index.js', args: "port=80 sitename='React.js Website'", instances: 0, autorestart: true, watch: true, max_memory_restart: '1G', env: { NODE_ENV: 'development' }, env_production: { NODE_ENV: 'production' } }, { name: 'NODEJS', script: 'server/node.js', args: "port=8080 sitename='Node.js API Server'", // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/ instances: 0, autorestart: true, watch: true, exec_mode: 'cluster' } ] }; 

in your config, you can pass PORT as well may be you can pass in args or you can pass in ENV [recomended] , and for that, in your node application, where you are doing something like this: app.listen(<PORT>) . make sure, you are taking PORT from ENV , like this : process.env.PORT

in this case your config should look like this:

//process.js

module.exports={
  apps : [
    {
      name: 'REACTJS',
      script: 'client/src/index.js',
      instances: 1, 
      autorestart: true,
      watch: true,
      max_memory_restart: '1G',
      env: {
        NODE_ENV: 'development',
        port:80,
        sitename:'React.js Website'
      },
      env_production: {
        NODE_ENV: 'production',
        port:80,
        sitename:'React.js Website'
      }
    },
    {
      name: 'NODEJS',
      script: 'server/node.js',
      instances: 1,
      autorestart: true,
      watch: true,
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'development',
        PORT:8080,
        //... all you ENV vars goes here for development mode
      },
      env_production: {
        NODE_ENV: 'production',
        PORT:8080,
         //... all you ENV vars goes here for production mode
      }
  }
]
};

To run,

DEV: pm2 start process.js //by default it considers development mode

PROD: pm2 start process.js --env production

NOTE: Make sure, in your node app you are taking PORT from env, (eg process.env.PORT )

once again,in your react app, that is client/src/index.js , please check wheather you are taking port number like this process.env.port or processs.env.PORT and change in the pm2 config accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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