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

Elena Man
Elena Man
20,146 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
20,146 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
20,146 Points

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

wildgoosestudent
wildgoosestudent
11,111 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(" ");
  }
}