Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS passing command line arguments to forever

Tags:

node.js

Im trying to pass command line arguments to forever, that I want to parse in my app.

Currently I'm starting my app like this:

forever start -c "node --max-old-space-size=8192 --nouse-idle-notification" /home/ubuntu/node/server.js

I want to be able to read some arguments in my server.js.

My code for reading:

var arguments = process.argv.slice(2);
console.log(arguments[0]);

if(!arguments[0]) {
    console.log("Error: Missing port.");
    process.exit(1);
}

But how to I tell forever to pass arguments that can be read inside server.js?

like image 593
Alosyius Avatar asked Dec 05 '14 10:12

Alosyius


People also ask

Why do you use forever with node js?

Forever is an npm module that ensures a Node. js script continuously runs in the background on the server. It's a helpful CLI tool for the production environment because it helps manage the Node applications and their processes.

Which object holds all arguments passed after executing a script with node command?

The argument vector is an array available from process. argv in your Node. js script. The array contains everything that's passed to the script, including the Node.

What happens if you execute the node command without any arguments?

The node arguments include the Node. js executable and the script's path, as well as the filename. Even if you execute a script without any arguments, the array will still include the script file and the node executable that you have run. The example code that we have mentioned here can check the length of argv.


1 Answers

According to the forever docs, you should use this format where you append your script arguments to the very end:

forever [action] [options] SCRIPT [script-options]

So for your example, you should be able to do this (omitting the -c options to make this a little more readable):

forever start server.js 9000 anotherarg evenanother

You can add the -c and the full path to server.js back into your real call, based upon your situation.

like image 65
Matthew Bakaitis Avatar answered Nov 15 '22 09:11

Matthew Bakaitis