Open kimown opened 7 years ago
Would be a nice feature IMO!
If the PR merged, you can run pm2 start ecosystem.config.js
like this:
ecosystem.config.js
const detect = require('detect-port');
function getPorts(basePort, number) {
let ports = [];
return new Promise((resolve) => {
const cb = (err, _port) => {
if (err) {
console.log(err);
}
ports.push(_port)
if (ports.length < number) {
detect(_port + 1, cb)
} else {
resolve(ports)
}
}
detect(basePort, cb);
})
}
const apps = [
{
name: 'web1',
script: 'web.js'
},
{
name: 'web2',
script: 'web.js'
}
];
function getApps() {
return new Promise((resolve) => {
getPorts(8080, apps.length).then((data) => {
const appCopy = apps.map((v, k) => {
return Object.assign(v,
{
name : v.name + " :" + data[k]
},
{
env: {
PORT: data[k]
}
}
)
})
resolve(appCopy)
})
})
}
module.exports = getApps()
web.js
var express = require('express')
var app = express();
const openBrowser = require('react-dev-utils/openBrowser');
const PORT = process.env.PORT;
app.get('/', function (req, res) {
res.send('web.js '+PORT)
})
app.listen(PORT, function () {
console.log(`Example app listening on port !${PORT}`);
const openUrl = `http://localhost:${PORT}`;
openBrowser(openUrl)
})
Also, you can use args
in ecosystem.config.js to pass the port parameter .
args: [
'--env.port=2333',
],
then in web.js
const argv = require('yargs').argv;
const PORT = argv.env.port
http://pm2.keymetrics.io/docs/usage/application-declaration/
Still not possible? I would start apps based on database entries
@cKehres
http://pm2.keymetrics.io/docs/usage/pm2-api/
use pm2 as node module, it works well, I use it in my company project.
I hope this becomes a feature soon! I've been using child_process execSync to get stuff inside a consistent Docker container. Here's an example:
const child_process = require("child_process");
const ip = child_process.execSync("curl http://api.ipify.org").toString()
// use ip wherever
Hi, I am using
pm2 start ecosystem.config.js
to start my multiple express projects, I use detect-port in each express project to avoid using used ports. Because free port is a dynamic number, so I want to show port in App name when I runpm2 list
I searched some issues, the only way I found doing this is change the name before run
pm2 start ecosystem.config.js
.So if pm2 can support promise config, my code can look like this:
Or can you give me other suggestions, thanks.