Mevia
Mevia

Reputation: 1564

Communicate through command line with active process

Very simple Node.js application. Application on start reads a file called config.js.

app.js

const config = require("./config");

const doStuff = (req, res, next) => {
    // using config.b or config.a or config.c
};

router.post("/", doStuff);

module.exports = router;

config.js

require("dotenv").config();

module.exports = {
    a: 5,
    b: 10,
    c: 11
};

Lets say i am starting my application with PM2

pm2 start app.js

Is it possible to communicate with app.js process from command line (terminal) and make it so it reloads config.js file. It's possible for example for nginx or php. Is it possible for Node.js process?

The important is so its done through command line and doesnt interupt the process flow or cause downtime.

Upvotes: 0

Views: 217

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21364

If you really only need to trigger a reload of the config, then you can use signals:

const config = require("./config");

const doStuff = (req, res, next) => {
    // using config.b or config.a or config.c
};

router.post("/", doStuff);

module.exports = router;

process.on('SIGUSR1', () => {
  console.log('Received SIGUSR1. Reloading config.');
  reloadConfig();
});

And then:

kill -SIGUSR1 PID

where PID is the process id of your running node process.

Upvotes: 1

Related Questions