The process global in node.js is helpful for getting information on, and control over, the current process. When making node.js applications chances are you are going to use at least some of it’s properties to grab at environment variables, and arguments given from the command line when a script is called directly. In addition it can also be used to set some event handlers to give control over the process.
This is thus far one of the properties of the process global that I use very often. When a module is called directly from the command line, and arguments are given, they should show up in the array provided here.
say I have an ecco.js file like this
It will log what I give to it when called
If I am doing something that involves a lot of arguments I will want to use some kind of option parser such as nopt.
This is another must know in the process global that comes in handy when I need to do anything that involves environment variables. When deploying a application to a hosting company there might be important information that I need to grab at such as a port, or password to a database, that is stored in an environment variable in the operating system environment when deploying. For example getting the port to connect to when making an app that I will be deploying to heroku, it will want to do something like this in my server.js file that will be called when it spins up.
When I deploy to heroku the server will use the port specified in the PORT environment variable, and when starting the script locally I can specify a port via the command line or else it will default to the hard coded value of 8080.
With the process global there are a bunch of events that can be used to help give control over the process including Inter-process communication I am new to this but I was able to put together a working demo pretty quickly when it comes to sending a simple message to a child-process.
I have a child.js file that makes use of the message, and disconnect events like this:
and an index.js file that starts, and kills that child process with the fork method.
When I call index.js from the command line I get what I expect.
The process global contains many more useful methods, and values I will expand on process more in the future as I work on more demos.