Вот моя проблема, с которой я столкнулся.
В моем проекте узла есть три файла с именем app.js
, app-8080.js
, app-8090.js
.
Три файла почтито же самое, кроме порта.
In app.js
server.listen(config.port0, () => {
console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port0));
});
In app-8080.js
// the above is the same as app.js
server.listen(config.port1, () => {
console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port1));
});
In app-8090.js
// the above is the same as app.js
server.listen(config.port2, () => {
console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port2));
});
Когда я обновляю app.js
, мне приходится выполнять одну и ту же работу три раза.
Теперь я хочу изменить, как показано ниже.
In app.js
let port = (/^\d+$/.test(process.argv[2])) ? process.argv[2] : config.port0;
server.listen(port, () => {
console.log(utilL.format('app is listening at http://%s:%s', config.host, port));
});
В app-8080.js
const childProcess = require('child_process');
const config = require('./config/config');
let port = config.port1;
childProcess.fork('./app.js', [port]);
Это нормально?И я использую PM2 для запуска команды app-8080.js
, как показано ниже:
$ pm2 start app-8080.js
Если в app.js
произойдет ошибка, будет ли PM2 перезапускать этот процесс?
И есть ли другие проблемы?Или какое лучшее решение?
Я буду очень рад вашему ответу!