Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.