Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Importing and Exporting Modules in Node.js!
You have completed Practice Importing and Exporting Modules in Node.js!
Preview
This video covers one solution to the importing and exporting modules challenge.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hey, how'd it go?
0:00
Hopefully you're able to complete
this challenge successfully.
0:01
The goal was to break up app.js by
abstracting the route handlers,
0:04
error handlers and helper functions
into the respective modules.
0:08
Then you had to import and use the route
and error handlers in app.js and
0:12
use the helpers in a routes.js file.
0:17
Now, I'll show you my solution,
which you can compare to what you wrote.
0:19
You can also reference all my code
when you download the project files.
0:22
All right, let's start with the routes.
0:26
I created a file named routes.js.
0:27
And in routes.js, I imported express and
0:31
setup express.Router up top then
moved the two get method routes for
0:35
the home and error routes from app.js and
into routes.js.
0:41
I adjusted the route handlers to work
on the router rather than app, for
0:46
example router.get.
0:51
Then I exported the router using
the module.exports property.
0:53
Now module.exports instructs Node JS which
parts of the code to export from the file,
0:57
whether it's a function object,
string and so on.
1:03
That way other files are allowed
to access the exported code.
1:06
So to export the router from routes.js, I
assigned module.exports the router object.
1:10
Then I imported the routes
module into app.js.
1:18
You Import and load code from one file
into another file in Node using require.
1:22
So to import code from the routes file,
I pass the file's path to require.
1:28
Then I use the app.use method to pass
the new routes module to the Express app.
1:36
Next, to modularize the error handlers,
I created a file named errorHandler.js.
1:43
Remove those callback
functions in the 404 and
1:51
global error handlers from app.js and
added them to errorHandlers.js.
1:54
And I had to adjust the callback so
1:59
that there are named functions
that can be exported.
2:01
So I, for example, named them
handleFourOhFour and handleGlobalError.
2:03
Then I exported the named error handler
functions from errorHandlers.js
2:08
by assigning module.exports an object
that references each function.
2:13
And I imported the error
handlers module into app.js and
2:19
assigned it to the constant errorHandlers.
2:23
After that I pass the exported functions
into the app.use statements for
2:26
the 404 and global error handlers
with errorHandlers.handleFourOhFour,
2:32
and errorHandlers.handleGlobalError.
2:38
Modules also help you organize related
functions together into a file.
2:42
So I created a file named helpers.js,
2:46
which contains functions you can import
in any file that needs to use a helper.
2:49
I moved the reverseString and
shortenString functions from app.js
2:53
into helpers.js, and
similar to the error handlers,
2:58
I exported the named helper functions by
assigning module.exports an object that
3:02
references each function, and I imported
the helpers module into routes.js.
3:08
Finally, I had to replace the helper
function calls in the route or home route
3:17
handler, with calls to the exported helper
functions from the helpers.js module.
3:21
When you export a function
with a module.exports,
3:27
you can call it as you would any function,
in this case, helpers.reverseString and
3:30
helpers.shortenString.
3:34
As I mentioned earlier, there are multiple
ways to export and import a module.
3:36
So you may have used a different
approach to import or
3:44
export your error handlers and
helpers, and that's okay.
3:46
In fact, the next step of this workshop
provides a few more examples of
3:50
the various ways you could export and
import each module.
3:53
All right, so
3:56
I hope that you were able to complete this
modules practice session successfully.
3:57
If not, why not start over and try it
again without looking at my version?
4:01
You're also going to learn more
about working with modules
4:05
as you progress through our full
stack JavaScript curriculum.
4:08
Thanks everyone, and happy learning.
4:11
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up