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

Can somebody please explain how username works?

Hey guys,

Can somebody please explain what this means?

var username = request.url.replace("/", "");?

Why would you set username to nothing?

Immediately following this is:

if(username.length > 0) {

How could username possibly be longer than 0 if it's just been set to an empty string?

Thank you!

3 Answers

andren
andren
28,524 Points

The replace function looks through a string for the value you define in the first argument and replaces it with the value you define in the second argument.

In other words the code is essentially saying "Replace all instances of / with nothing" it isn't saying replace the string itself with nothing.

If the string in question was "hello /world/!" for example the above code would result in a string that said "hello world!".

Jeremy Castanza
Jeremy Castanza
12,080 Points

The other part to what @andren said has to do with the root. This is also an attempt to isolate the home root route from the user route. By stripping the URL of the "/", there's a distinction between the home (or root) and the user URIs.

Thank you so much!

James Barrett
James Barrett
13,253 Points

Thanks for the comment andren . Quick question, why exactly are we replacing all instances of the "/" with nothing? Thanks, James.

andren
andren
28,524 Points

It's worth nothing that it's been a while since I completed the Node course, but from skimming though the video I think I more or less understand why.

In the video Andrew is setting up the site so that when you visit a URL that contains a username (/chalkers for example) then the website will respond with a page that simply contains a header, footer and the username that was inserted in the URL.

In order to get the username that was entered he is using "request.url" which will contain the requested URL ("/chalkers" if that is the requested part of the site) and then he strips of the "/" in order to get the pure username and stores that new string "chalkers" as a variable called username, that he can then later use when he is sending out the text that the server responds with.

This means that no matter what name you enter when you visit the website (/andren for example) you will get that name sent back in the response. if he did not strip off the / then the username would include the "/" like this "/andren".