1 00:00:00,370 --> 00:00:01,630 Hey, how'd it go? 2 00:00:01,630 --> 00:00:04,430 Hopefully you're able to complete this challenge successfully. 3 00:00:04,430 --> 00:00:08,640 The goal was to break up app.js by abstracting the route handlers, 4 00:00:08,640 --> 00:00:12,380 error handlers and helper functions into the respective modules. 5 00:00:12,380 --> 00:00:17,105 Then you had to import and use the route and error handlers in app.js and 6 00:00:17,105 --> 00:00:19,641 use the helpers in a routes.js file. 7 00:00:19,641 --> 00:00:22,950 Now, I'll show you my solution, which you can compare to what you wrote. 8 00:00:22,950 --> 00:00:26,190 You can also reference all my code when you download the project files. 9 00:00:26,190 --> 00:00:27,880 All right, let's start with the routes. 10 00:00:27,880 --> 00:00:31,350 I created a file named routes.js. 11 00:00:31,350 --> 00:00:35,046 And in routes.js, I imported express and 12 00:00:35,046 --> 00:00:41,001 setup express.Router up top then moved the two get method routes for 13 00:00:41,001 --> 00:00:46,670 the home and error routes from app.js and into routes.js. 14 00:00:46,670 --> 00:00:51,500 I adjusted the route handlers to work on the router rather than app, for 15 00:00:51,500 --> 00:00:53,122 example router.get. 16 00:00:53,122 --> 00:00:57,872 Then I exported the router using the module.exports property. 17 00:00:57,872 --> 00:01:03,850 Now module.exports instructs Node JS which parts of the code to export from the file, 18 00:01:03,850 --> 00:01:06,580 whether it's a function object, string and so on. 19 00:01:06,580 --> 00:01:10,240 That way other files are allowed to access the exported code. 20 00:01:10,240 --> 00:01:16,090 So to export the router from routes.js, I assigned module.exports the router object. 21 00:01:18,450 --> 00:01:22,780 Then I imported the routes module into app.js. 22 00:01:22,780 --> 00:01:28,830 You Import and load code from one file into another file in Node using require. 23 00:01:28,830 --> 00:01:34,420 So to import code from the routes file, I pass the file's path to require. 24 00:01:36,410 --> 00:01:41,830 Then I use the app.use method to pass the new routes module to the Express app. 25 00:01:43,680 --> 00:01:51,030 Next, to modularize the error handlers, I created a file named errorHandler.js. 26 00:01:51,030 --> 00:01:54,362 Remove those callback functions in the 404 and 27 00:01:54,362 --> 00:01:59,700 global error handlers from app.js and added them to errorHandlers.js. 28 00:01:59,700 --> 00:02:01,140 And I had to adjust the callback so 29 00:02:01,140 --> 00:02:03,660 that there are named functions that can be exported. 30 00:02:03,660 --> 00:02:08,860 So I, for example, named them handleFourOhFour and handleGlobalError. 31 00:02:08,860 --> 00:02:13,683 Then I exported the named error handler functions from errorHandlers.js 32 00:02:13,683 --> 00:02:18,440 by assigning module.exports an object that references each function. 33 00:02:19,700 --> 00:02:23,626 And I imported the error handlers module into app.js and 34 00:02:23,626 --> 00:02:26,747 assigned it to the constant errorHandlers. 35 00:02:26,747 --> 00:02:32,479 After that I pass the exported functions into the app.use statements for 36 00:02:32,479 --> 00:02:38,768 the 404 and global error handlers with errorHandlers.handleFourOhFour, 37 00:02:38,768 --> 00:02:42,210 and errorHandlers.handleGlobalError. 38 00:02:42,210 --> 00:02:46,570 Modules also help you organize related functions together into a file. 39 00:02:46,570 --> 00:02:49,430 So I created a file named helpers.js, 40 00:02:49,430 --> 00:02:53,620 which contains functions you can import in any file that needs to use a helper. 41 00:02:53,620 --> 00:02:58,538 I moved the reverseString and shortenString functions from app.js 42 00:02:58,538 --> 00:03:02,521 into helpers.js, and similar to the error handlers, 43 00:03:02,521 --> 00:03:08,454 I exported the named helper functions by assigning module.exports an object that 44 00:03:08,454 --> 00:03:14,241 references each function, and I imported the helpers module into routes.js. 45 00:03:17,138 --> 00:03:21,802 Finally, I had to replace the helper function calls in the route or home route 46 00:03:21,802 --> 00:03:27,520 handler, with calls to the exported helper functions from the helpers.js module. 47 00:03:27,520 --> 00:03:30,262 When you export a function with a module.exports, 48 00:03:30,262 --> 00:03:34,749 you can call it as you would any function, in this case, helpers.reverseString and 49 00:03:34,749 --> 00:03:36,265 helpers.shortenString. 50 00:03:36,265 --> 00:03:44,340 As I mentioned earlier, there are multiple ways to export and import a module. 51 00:03:44,340 --> 00:03:46,920 So you may have used a different approach to import or 52 00:03:46,920 --> 00:03:50,080 export your error handlers and helpers, and that's okay. 53 00:03:50,080 --> 00:03:53,800 In fact, the next step of this workshop provides a few more examples of 54 00:03:53,800 --> 00:03:56,450 the various ways you could export and import each module. 55 00:03:56,450 --> 00:03:57,550 All right, so 56 00:03:57,550 --> 00:04:01,700 I hope that you were able to complete this modules practice session successfully. 57 00:04:01,700 --> 00:04:05,490 If not, why not start over and try it again without looking at my version? 58 00:04:05,490 --> 00:04:08,450 You're also going to learn more about working with modules 59 00:04:08,450 --> 00:04:11,200 as you progress through our full stack JavaScript curriculum. 60 00:04:11,200 --> 00:04:12,680 Thanks everyone, and happy learning.