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 trialChristopher Lebbano
15,338 PointsWhat does this line of code actually do...(Node: request.url.replace())
So in this video, he creates a new route to handle when a user adds something after the home route "/", and to do that he adds a new function with this variable "username".
function userRoute(request, response) {
var username = request.url.replace("/", "");
if(username.length > 0) {
response.writeHead(200, {'Content-Type': 'text/plan'});
response.write(username + "\n");
response.end("End\n");
}
}
So What I'm trying to figure out is, how is replacing "/" with en empty string does anything to the variable username. The way I would initially understand it, is that we are declaring a variable and then setting it to an empty string. In the demonstration, he's able to change the URL at the top of the page and end it with "/chalkers" and it writes out "chalkers" on the actual web page.
Or, my other thought would be, at it would replace ALL of the "/"'s in the URL so the username would actually be something like "port-3000-7ih2ribli1.treehouse-app.comchalkers".
In other words, how does it now to leave out the first part of the string and only define username as everything after the "/".
Thanks.
2 Answers
andren
28,558 PointsYour second interpretation is closer too correct. The replace function simply replaces all instances of /
with nothing. It does not replace the entire string with nothing.
Your confusion stems from the fact that you are mistaken about what the request.url
string actually contains. It does not contain the full path of the current URL. It only contains the request URL. Which is the part of the URL that follows the host/domain. In other words when you connect to /something
the request.url
string only contains that exact thing /something
. If you had requested /something/somethingElse
then the request string would contain /something/somethingElse
.
So the replace
method in this instance essentially just removes the first /
in the request URL so that you are left with the word following the /
.
Daniel Breen
14,943 PointsI'm not sure I'm following you, but let me give it a shot.
First, let's demonstrate this in word or google docs. Go into one of those programs and type a word or two with multiple forward-slashes in it. Now, use the Find and Replace tool (usually under the Edit menu item) and replace / with nothing. Click *replace all or the equivalent. All the forward-slashes should be removed with the rest of the characters left in tact.
I think the part confusing you is the process. Let's break it down.
If you don't have the replace, you just have
var username = request.url
which will result in /chalkers
if I'm following you (I haven't done this course)
Next, you add the replace
method to remove the slash. .replace("/","")
.
You could break this into three parts
var username;
username = request.url; // /chalkers
username = username.replace("/",""); // chalkers
I hope this helps. Let me know if you need further explanation.
Christopher Lebbano
15,338 PointsThank you this does help.
Christopher Lebbano
15,338 PointsChristopher Lebbano
15,338 PointsOk thank you that mostly clears it up. I'll continue the course to see it in action.