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.
Xiaoxuan Wang4,590 Points
Differences between Homeroute and Userroute
I've confusing about homeroute and userroute through this course, though finished the code mechanically. Since there seems barely differences of using either of them showed in the video, How can I understand using homeroute or userroute? What kind of role are routes playing when we send requests and receive responses from the server?
This is actually an example of a protocol. You, as a developer, define the protocol however you want. In this case, the teacher have decided (and by convention) that if the URL is a forward slash ('/') then the client most likely want the
index.html file. If the URL contain anything beside a forward slash, the teacher have decided that it must be a request for information about a particular user.
Do distinguish between the two requests (for
index.html and the user information), the teacher have chosen two routes: home and user. Now, anyone that want to use your services must adhere to this protocol, i.e., a simple forward slash for
index.html and a username for information about a user.
So when should you use these two routes? It is actually up to you (again) how you define the protocol. You may not even need more than one route, and instead handle it through the request body, or you may want even more routes. It is up to you, but using two different routes may be simpler. To summarize: a route can be used to easily distinguish between the kind of request (e.g., for
index.html, logging in, logging out, retrieving information about a user) to send the appropriate response.
So if I type in the browser bar home address I just get general info with no user info and when I type url ending with username I get user info? This is what you are saying?
Charles Wanjohi9,235 Points
Hey, having two files for Homeroute and Userroute is simply to ensure the code is neatly arranged i.e having categories for the different groups of routes based on the purpose they serve.All the routes could have been put in a single file.
To understand the different roles each of the routes play, check on the first argument passed to the app.use method in the app.js file .e.g.
The above code tells express that any request sent to express with a url starting with ''/home" should be referred to the homeRoute Similarly any request with a url that starts with "/user" would be relayed to the userRoute eg a url of '/user/details' would be sent to the user route
Hope this helps you find the difference