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
This video introduces the challenge relating to Importing and Exporting Modules in Node.js that you'll be working to complete.
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
[MUSIC]
0:00
Hey everyone, Guil here.
0:09
It's time for more JavaScript practice.
0:10
Practice not only makes
what you've learned stick,
0:12
it also helps you become a faster and
better developer.
0:15
In this workshop, you're going to
build up your Node.js skills by
0:17
practicing the basics of importing and
exporting modules in a simple application.
0:20
You've learned that in Node modules are a
way of bundling code to make it portable,
0:24
shareable, and
reusable across your project.
0:28
Most importantly, any code written inside
the module will not interfere with code
0:31
inside other modules, and will not
pollute the global namespace or scope.
0:35
Modules also help limit file size and make
your code easier to maintain over time.
0:40
Now, importing and exporting modules can
be a tricky topic, because like many
0:45
things in JavaScript, there are various
ways to export and import a module.
0:48
So the goal of this exercise is to not
only to get you more comfortable using
0:53
module syntax, but also understanding
what's actually happening when you
0:56
import and export modules in Node.
1:00
To get started, download the project
files with this video and
1:03
open them in your favorite text editor.
1:06
The project file is consist of a small and
1:08
simple express application with
a package.json file, one JavaScript file,
1:10
app.js and a README.md file containing
the instructions for this challenge.
1:15
Once you have the project open, navigate
to the project directory using your
1:21
terminal or console app, run npm install
to install all the project dependencies,
1:25
Then npm start to start the app.
1:32
You can view the app in the browser
on localhost port 3000.
1:36
When you open the app.js file, you will
first see two simple helper functions.
1:41
One reverses the string passed to it and
1:47
the other truncates or shortens the string
if it's greater than 50 characters.
1:50
Below the helper functions are the route
handlers, we have a home route and
1:54
a custom error route.
1:59
Below those are the error handlers,
there's a 404 error handler, and
2:01
a global error handler.
2:06
Your job in this exercise is to
break app.js up into smaller more
2:08
manageable files by abstracting
the route handlers, error handlers, and
2:12
helper functions into their own modules.
2:17
That way code from one module can be
imported and referenced in other files.
2:19
Now let's go over what you'll need
to do following the instructions in
2:26
the README file.
2:28
Starting with the routes you will need
to create a file named routes.js,
2:30
in routes.js you'll import express and
2:35
setup express.Router to
create your route handlers.
2:38
Then remove the two get methods for
the home and
2:42
error routes from app.js and
add them to routes.js.
2:46
After that,
2:52
you'll need to adjust the route handlers
to work on the router rather than app.
2:53
Then export the router and
import the routes module into app.js.
3:00
And finally, use the app.use method
3:05
to pass the new routes
module to the express app.
3:08
Next, to modularize the error handlers,
3:12
you'll first create a file
named errorHandlers.js.
3:16
Then remove the callback functions from
the 404 and global error handlers,
3:20
and app.js and
add them to the errorHandlers.js file.
3:27
Then you will need to
convert the call backs and
3:35
errorHandlers.js to named functions so
that they can be exported.
3:38
Then you will export those named
error handler functions from
3:43
errorHandlers.js and
import the module into app.js.
3:47
And finally, pass the exported function
into the app.use statements for
3:51
the 404 and global error handlers
3:56
Finally, the helper functions,
you will create a file named helpers.js.
4:05
Then remove the reverseString and
4:11
shortenString functions from app.js and
add them to helpers.js.
4:14
Then you're going to export the two
functions from helpers.js and
4:22
import the helpers module into routes.js.
4:27
And make sure that you're still able
to use each function in the home
4:30
route handler.
4:35
The two helper function calls in the route
handler should call the reverseString and
4:36
shortenedString helper functions
from the imported module.
4:40
After modularizing the app, you should be
able to navigate to the home route and
4:47
still see hello world.
4:51
The reversed reading and the shortened
description printed to the page,
4:53
as well as handling request to route or
home route in the console output.
4:56
The error route should print the custom
error message to the page and
5:02
log a message to the console.
5:06
And a non existent route, like abcd should
5:10
also print a custom not found message and
log a message to the console.
5:14
Now your console output should display
no errors other than the custom error
5:19
thrown by the error handlers.
5:23
To help you succeed in this practice
session, I suggest that you watch or
5:28
review our lessons on importing and
exporting modules in Node.
5:32
I've added links to the content as
well as additional resources and
5:36
the teacher's notes with this video.
5:39
Remember, there's more than one way
to export and import a module and
5:41
the approach you use is up to you.
5:45
So good luck, have fun, and in the next
video, I'll walk you through one solution.
5:47
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