1

I need to run the set of node js files which contains the configuration information where It has to run typically port number and IP address then using the forever in node.js I need to run the script in the terminal with the configuration without having any manual input.

4 Answers 4

3

For Programmatic approach , you can use Forever-Moniter

var forever = require('forever-monitor');

  var child = new (forever.Monitor)('your-filename.js', {
    max: 3,
    silent: true,
    options: []
  });

  child.on('exit', function () {
    console.log('your-filename.js has exited after 3 restarts');
  });

  child.start();
Sign up to request clarification or add additional context in comments.

Comments

2

You could make use of the child_process module. Check the doc, there're some useful information there: http://nodejs.org/api/child_process.html

To give a brief example

var exec = require('child_process').exec;
exec('forever', function callback(error, stdout, stderr){
    // cb
});

If you don't need a callback / don't want to wait for the execution:

var exec = require('child_process').exec('forever').unref();

Was that helpful?

Best Marc

Edit: Ok, not sure if I really got the point of your question, but my answer combined with https://stackoverflow.com/a/23871739/823851 might offer a good solution.

Comments

0

Usage:

  • forever start hello.js to start a process.
  • forever list to see list of all processes started by forever
  • forever stop hello.js to stop the process, or forever stop 0 to stop the process with index 0 (as shown by forever list).

Comments

-2

node-config is a good module for managing different configurations of a Node.js app.

For example, to make a "production" config, /config/production.json would contain:

{
  "port" : "3000",
  "ip" : "192.168.1.1"
}

In one of your node application JS files:

config = require('config')
....
var port = config.port;
var ip = config.ip;

And to launch the app in this configuration, just first set your NODE_ENV to production from the shell before running your app.

export NODE_ENV=production
forever start app.js

Make additional config JSON files as needed for each of your environments. default.json is used when no environment is specified.

2 Comments

This is not a programmatically approach .
node-config is a great module, but it doesn't answer the question here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.