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 Populating User Information

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

User code not working

The username,badges, and points not working when I add "/chalkers"

https://w.trhou.se/nrpk3hw707

1 Answer

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Leo. I'm just pasting here lines 14 to 50 of your router.js file for reference.

// Handle HTTP route GET/: username i.e. /chalkers
function user(request,response){
var username=request.url.replace("/","");
  if(username.length>0){
    response.writeHead(200,{'Content-Type':'text/plain'});
  response.write("Header\n");

    // get JSON from Treehouse
    var studentProfile = new Profile(username);
    //on end
    studentProfile.on("end", function(profileJSON){
      // show Profile

      //Store the values we need
      var values={
      avatarURL:profileJSON.gravatar_url,
        username:profileJSON.profile_name,
          badges:profileJSON.badges.length,
          javascriptPoints:profileJSON.points.JavaScript
      }
      //Simple response
       response.write(values.username+ "has " + values.badges+ "badges.\n");
    response.end("Footer\n");
    });
  //on error
    studentProfile.on("error", function(error){
    //show error

    response.end("Footer\n");
    });
    response.write(username + "\n");
    response.end("Footer\n");
  }
};

module.exports.home=home
module.exports.user=user

1: Your home route is working fine, but as soon as you activate the user route by appending /chalkers, your server is crashing. The reason is that lines 44 and 45 are duplicates and should be removed. The teacher (Andrew) is doing a lot of cutting and pasting in the video and it is very easy to lose track of where you are. So just remove these lines...

    response.write(username + "\n");
    response.end("Footer\n");

2: Now the GET request is working but you see this in your browser.

Header
chalkershas 209badges.
Footer

In line 35, the response.write entry is using concatenation and the spaces need to be inserted next to the strings, within the apostrophies. It seems you don't need a space before each '+' but it does look better if you put one there.

Old

response.write(values.username+ "has " + values.badges+ "badges.\n");

New

response.write(values.username + " has " + values.badges + " badges\n");;

Now we see...

Header
chalkers has 209 badges
Footer

You mentioned not seeing points either. In this particular video, Andrew doesn't actually do that but you can do this yourself by adding to the response.write entry we covered above.