- Another important object that's available
to us globally is the process object.
So within my files I'm going to create a new file
and we're going to call the global process-dot-js.
So the process object can be accessed globally
and it contains information about
the current process as well as tools
to allows us to interact with that process.
So for instance, we can use the process object
to get the process ID.
Or we can also use the process object
to get the current version of Node.js
that's being used to run the process.
Process-dot-versions-dot-node.
And I can go ahead and open up my terminal
and run this file by typing node global process
and we can see the process ID as well as
the version of Node.js that's being used
to run this process.
With the process object, we can
get environment information, read environment variables,
communicate with the terminal or parent processes
through standard input and standard output.
We can also use it to exit the current process.
One of the things we can do with the process object
is collect information from the terminal
when we load the application.
There's a variable called process-dot-A-R-G-V.
I'll go ahead and delete what we have here.
And log the process-dot-A-R-G-V.
So these are the argument variables
that are sent to the process when we run it.
So I'm going to come down here to the terminal
and I will run my global process again
by running the command node global process.
So as we can see here the process-dot-A-R-G-V value
is an array.
It contains everything that we typed
to run the process.
The first command that we typed to run this process
was node.
So as you can see we see the full path
to where that node process lives.
That's where we've executed Node.js from.
The second argument is a path
to the current module that we're using.
So as you can see we have the full path
to the global process.
So anything we type...
when we run a node file
gets added to this process-dot-A-R-G-V array.
For instance I could add my name
and we notice that the array gets
a little bit longer and Alex Banks
is the next thing that we see.
Now because process-dot-A-R-G-V is an array,
we can work with it the same way
we work with arrays.
For instance I could use array destructuring.
If I wanted to get my first name and my last name,
I really don't care about the first two values
in the array.
But I can set the third value
in our argument's array to first name
and the fourth value to last name.
And here we've just destructured
the A-R-G-V array.
And I can say you're name is first name,
last name.
Great.
So we'll go ahead and save this
and come down to the terminal and run it
by typing node global.
Should we have a lot of stuff here
on the terminal, so I'm going to clear
the terminal by typing clear.
If you're using DOS the CLS command
will work it.
So now I'm going to type node global process,
Alex Banks, and you can see that
we've destructured my name from the array.
So we can use the process-dot-A-R-G-V array
to send arguments to our Node.js module
when we run it.
So typically we wouldn't just put our name
in all willy nilly like we just did here.
What we actually want to do is provide flags
so that we know what type of variables
are being passed.
So I can say node global process
and we can send a user flag.
This time, the user's name will be Ned
and a greeting flag.
And then the greeting flag, we'll go ahead
and add Hidely Hoe.
Hidely Hoe.
So what we're doing here is we are providing flags
to let us know what variables we are passing.
You'll also notice that we used quotation marks
around Hidely Hoe because we don't want
the space between Hidely and Hoe
to create another value within
that process-dot-A-R-G-V array.
Using quotation marks will treat
that entire value as one string.
So if I run this and says your name
is user Ned because we haven't changed
the code up here.
So let me come back up in here
and I will get rid of the code
that we currently have.
So lets say I wanted to grab the greeting.
I could set a variable greeting
and we could use a grab function.
Say we created a function and within this function
we can just send in the flag that we want to obtain
the value for.
Greeting.
So lets say we also wanted to find
the user.
We can also use a grab function,
and then send in our user flag.
So remember the process-dot-A-R-G-V
is an array.
What we want to do is find the index
of dash-dash user or find the index
of dash-dash greeting and then return
the next value.
So lets go ahead and build the grab function
to do this.
So we'll use a arrow function
and the arrow function will grab our flag
that we send to it.
That's the argument dash-dash greeting
or dash-dash user.
And what we're going to do is find the index
after the flag using the process-dot-A-R-G-V array.
And because it's an array, we can just use
the javascript index of command
and we will search for the flag
that will give us the index of the flag.
Since we want the value, we will go ahead
and add one to that.
And that should give us the next index.
Which we can then use to return that value.
So once we find the flag, we will increment
the index one and then return
the value.
So now let's go ahead and see what we have.
And we will use backticks to create
a template string.
So go ahead and include our greeting value.
And then our user value.
Let's go ahead and save this and we can come
to the terminal and run the exact same command.
I'm just going to push the up arrow
to get it.
And we can say that we're going to
run node global process with the user node
and the greeting Hidely Hoe.
And we see the greeting Hidely Hoe.
So if you're asking yourself why we used
a function to grab the values
of these arguments that we're passing in,
this is because it will allow users
to pass arguments in in any particular order.
Lets say if we added the greeting first,
this time it'll be hello and the user
this time that'll be Alex.
Our application will still work
because we're finding the index of the greeting
and grabbing that value and the index
of the user and grabbing that value.
So the process object is an important object
that contains information about
the current process.
And in this example we used the process-dot-A-R-G-V array
to pass arguments to our Node.js module
when we run the process.