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
All of our code is in our app.js file and this isn't ideal. When our app grows it may be difficult to see what's going on. We'll create our own module to compartmentalize our code.
Documentation
All of our code is in the app.js file.
0:00
And this isn't ideal,
especially when our app grows,
0:03
it may be difficult to see
the flow of the application.
0:07
Wouldn't it be nice if we could create
our own modules like http and https?
0:11
To do this,
let's create a profile .js file.
0:17
And let's cut all the code from the top
0:25
of the file all the way down to where we
0:30
get the arguments in from the script.
0:35
Let's paste that into our profile.js and
0:43
let's rename getProfile to just get.
0:48
Now at the top of our app.js file,
let's require the profile.js.
0:55
We do this by using the require function.
1:01
And then the path to the filename,
1:05
which is ./ meaning the same
directory as the app.js file.
1:08
And then profile.js,
which is the name of the file.
1:13
Let's call it the constant
variable of profile.
1:20
Now we can update getProfile to
1:28
profile.get which is
the name of our function.
1:32
Let's rerun our command line app again,
1:38
And we see undefined is not a function.
1:46
That's because when you're
creating a module, you need
1:50
to explicitly state what you want to have
available to someone who requires it.
1:53
In this case we want users
to access the get method.
1:58
We do this by using module.exports
2:06
and the name of your API you
want to have accessible.
2:15
Get and then we need to set it something.
2:20
In this case, it's the get function.
2:26
They happen to have the same name but if
I renamed the function back to getProfile
2:29
and exported the module as
module.exports.get is equal to getProfile.
2:34
The API that I would have
created would be just get.
2:40
And run it to see if it works.
2:44
And it does.
2:50
Now there's a couple of things
that I want to take away from
2:52
requiring your own files.
2:56
.js is optional, but
the path to the JavaScript is mandatory.
2:58
If you don't include ./, it will not work.
3:05
We perfected our application a little further.
3:11
Now it's your chance to
improve the application.
3:13
Here's a few things you could do.
3:16
If you haven't already,
you could give friendlier error messages
3:18
when the connection fails or
when there's a parse error.
3:21
You could make the first argument
passed up to app.js a given topic area.
3:25
You could then see how many points
a student has in that topic area.
3:30
Finally you could extract all the printing
methods out into its own module.
3:34
Give them a go and
3:39
share your solutions with your fellow
students in the Treehouse community.
3:40
In the next section I'm going to give you
a challenge using all you've learned.
3:44
You need to sign up for Treehouse in order to download course files.
Sign up