Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Modular Routes

Why only the app.get and app.post methods put into the routes folder?

Is there any reason why you wouldn't put the error handlers in the routes folder? In general, do you never put the app.use() routes into the routes folder, and leave it in the app.js?

2 Answers

nico dev
nico dev
20,364 Points

I think that the routes folder is intended to modularize (is that even a word? |o| ) the routing stuff away from the actual app code, like the middleware you handle with app.use, app.listen, etc.

That makes a lot of sense, especially if you have to debug it.

The default handlers remain in the main file because they apply to the entire application (not specific to any component) and they need to be ordered last, after all other routes. The handler generating a 404 error prevents handlers on laters routes from getting evaluated. Try it out: move the handlers to routes/index.js and observe requests to /cards (whose handlers are set later) fail. That's why.