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! While you're at it, check out some resources Treehouse students have shared here.

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 Build a Simple Dynamic Site with Node.js Handling Routes in Node.js User Route

Xiaoxuan Wang
Xiaoxuan Wang
4,590 Points

Differences between Homeroute and Userroute

Hi guys,

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?

Thanks,

3 Answers

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.

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,324 Points

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 Wanjohi
Charles Wanjohi
9,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.

app.use('/home',homeRoute);
app.use('/user',userRoute);

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