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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Got a bit lost on the way, profile not defined.

Here's my code, I tried to follow as best as I could but I'm not sure what I'm doing wrong.

router.js

var Profile = require("./profile.js");


//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
   //if url == "/" && GET
   if(request.url ==="/") {
     //show search
      response.writeHead(200, {'Content-Type': 'text/plain'});

      response.write("Header\n");
      response.write("Search\n")
      response.end("Footer\n");


   }
   //if url == "/" && POST
     //redirect to :/username   
}

function user(request, response) {

 // if url == "/...."
 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 which we neeed
                var values = {
                    avatarUrl:  profileJSON.gravatar_url,
                    username: profile.JSON.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.write(error.message + "\n"); 
      response.end("Footer\n");

    });


 }

}

//export routes to app.js
module.exports.home = home;
module.exports.user = user;

1 Answer

"username: profile.JSON.profile_name"

From the way you accessed avatarURL and badges I assume it should be profileJSON.profile_name.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thank you! This has saved me hours, all because of a simple typo :)