Pm2 designated port

can pm2 specify ports

if you directly use node server.js to specify the port number in the project, you can get
but in pm2, you will randomly assign a port number to me every time you restart
is there a way to assign the project port number

Jul.16,2021

you can set the port number in the configuration file of pm2. Please refer to the official document: http://pm2.keymetrics.io/docs.


The < H1 > problem has been solved and is going in the wrong direction. < / H1 > The port of

pm2 is not directly related to the port defined in the actual project, mainly because the setting method of getting env in the project started by pm2 and the setting of node_env are not universal

. < H1 > solution, record here so that you can step on the pit again later < / H1 >

env is used in the project to define the environment in which the project runs, where the project uses three environments

directory structure

 config
     config.development.json
     config.production.json 
     config.test.json
     index.js

index.js

module.exports = require(require('path').join(__dirname, ['config', process.env.NODE_ENV || 'development', 'json'].join('.')));

because the environment variables set in linux are environment variables in node_env, there is no problem using node server.js to run the project directly

while getting process.env.NODE_ENV in pm2 must be when pm2 starts the project-- env

the env used by the test server is test

-sharpcd ,--name --envNODE_ENV --watch
pm2 start server.js --name app --env test --watch
Menu