I. Brief introduction of the problem

When multiple Node.js processes need to be managed together and each requires different parameters, creating a configuration file is the best solution.


Second, solutions
1. Generate the basic version configuration file
  • Pm2 Init simple: generates the configuration file of the base version, nerb.config.js

    // ecosystem.config.js
    module.exports = {
        apps: [{
            name: "app1".script: "./app.js"}}]Copy the code
2. Generate complex versions of configuration files
  • Pm2 init is equivalent to pm2 ecosystem and generates a configuration file with the deploy attribute, file.config.js

    module.exports = {
        apps: [{
            script: 'index.js'.watch: '. '
        }, {
            script: './service-worker/'.watch: ['./service-worker']}],deploy: {
            production: {
                user: 'SSH_USERNAME'.host: 'SSH_HOSTMACHINE'.ref: 'origin/master'.repo: 'GIT_REPOSITORY'.path: 'DESTINATION_PATH'.'pre-deploy-local': ' '.'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production'.'pre-setup': ' '}}};Copy the code
  • This version of the configuration is suitable for remote configuration server use, see examples here!


3. Use a configuration file
  • Start, stop, restart, reload, and delete all items in the configuration file

    pm2 start ecosystem.config.js
    pm2 stop ecosystem.config.js
    pm2 restart ecosystem.config.js
    pm2 reload ecosystem.config.js
    pm2 delete ecosystem.config.js
    Copy the code
  • Starting a specified application

    pm2 start   ecosystem.config.js --only api-app
    pm2 start 	ecosystem.config.js --only "api-app,worker-app"
    Copy the code

Iii. Reference documents
  • PM2 manages multiple Node.js projects with configuration files!