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 Home Route

Elena Man
Elena Man
21,092 Points

Can't understand request.url

Can someone explain in more detail what request.url does and why we set it equal to "/"? I have looked over the documentation and went through the video several times but still don't understand it.

Thank you!

2 Answers

This is how I understand it:

Say you have example.com/library

The route is considered '/library'

So, if you just want to go to the homepage (example.com), it's referred to as '/' because example.com can also be written as example.com/ (notice the last character of this url is a forward slash)

Let me know if this helps here

Elena Man
Elena Man
21,092 Points

But why are we looking only for the '/'? Why not he whole string ('example.com/')?

I think routes are built to work regardless of what the domain name is. For example, locally your domain will be different than in production.

Elena Man
Elena Man
21,092 Points

Thanks for the help! It makes more sense for me now!

wildgoosestudent
wildgoosestudent
11,274 Points

I had the same question so I added in some code to see what the url actually is

// Handle HTTP route GET and POST ex Home
function homeRoute(request, response) {
  if(request.url === "/") {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write("Header\n");
    response.write("Search\n");
    // didn't make any changes to the url
    response.write(request.url);
    // '/' is returned 
    response.end(" ");
  } else {
    // added '/banana' to the url
    response.write(request.url);
    // '/banana' is returned 
    response.end(" ");
  }
}