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 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.