Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Our application is using a fixed username. Let's update the application so it can take in any number of usernames via command line arguments.
Documentation
Next, let's implement the feature of
getting information from multiple
0:00
Treehouse students from the command
line as arguments to our script.
0:03
Our code isn't flexible at the moment.
0:08
It's fixed to the username of chalkers.
0:11
What would we'd like to do is have
a get profile function that can take
0:13
username.
0:18
So let's delete the username variable
here at the top of the file and
0:18
instead of having the variable declared,
0:24
let's wrap the code in
a function called getProfile,
0:28
With a single argument username.
0:37
Now we can call getProfile
with my username.
0:49
And a colleague alena's username.
0:58
Let's run this and you'll notice Alena's
profile information is returned first.
1:05
This is us seeing non-blocking
in action because Alena
1:14
doesn't have as many badges as I do.
1:18
The HTTPS request response cycle finishes
before mine, so it prints it out.
1:21
In other languages and
frameworks, it would wait for
1:27
mine to be processed first and
then printout Alena's.
1:31
Because we're streaming in data and
dealing with it when we can
1:35
is an order of magnitude faster than
other solutions which is awesome.
1:39
Now let's use these usernames in
an array calling each line like this is
1:45
unnecessary repeating ourselves.
1:49
So let's create the array
users with our two usernames.
1:54
We can cycle over each of the members
of the array with a forEach method.
2:16
ForEach iterates over the array and passes
each member into a callback function.
2:25
Each member would be a user name.
2:30
We can now call getProfile
username because
2:41
the getProfile function
takes one parameter and
2:46
forEach passes one in,
we can shorten our code to this.
2:51
Let's see it in action.
3:09
It works the same.
3:14
This means we can pass
in any number of users.
3:17
Let's add Dave McFarland.
3:20
Run it again and
we see Dave's profile too.
3:34
But how would we do
something like node app.js,
3:39
chalkers, alenaholligan and davemcfarland?
3:44
Just like there's a window object
with JavaScript in the browser,
3:53
there's another global object for
node JS and that's called process.
3:56
You could explore process
by doing console.dir
4:02
sometime to see what's available
on the process object.
4:07
There is an array property called argv.
4:11
Let's log this out.
4:17
So that gives us the node binary,
our app.js and then the three usernames.
4:33
We don't want the first two so
we could use the slice method for
4:41
a slice of the array, starting at
the index that we want to start
4:46
at which is the number two,
the third member in the array.
4:51
Let's try it out.
5:15
It works.
5:27
Our application looks like it's done,
right?
5:30
We've performed everything we planned.
5:32
From making the request, reading and
parsing the data, and printing it out.
5:35
However, we have failed to handle
any every states of our application.
5:39
What if a user enters an invalid username?
5:44
Or the server doesn't
respond like we expect?
5:47
In the next section we'll perfect
our application by catching
5:50
any errors that get thrown our way.
5:53
You need to sign up for Treehouse in order to download course files.
Sign up