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 HTTP Methods and Headers Sending Content Type Headers in Node.js

Anthony Ho
Anthony Ho
10,228 Points

I'm not getting the username to display after search

node js has updated since the course began

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


function home(request, response) {
  if (request.url === "/") {
  response.statusCode = 200;
  response.setHeader('Content-Type', 'text/html');
  renderer.view("header", {}, response);
  renderer.view("search", {}, response);
  renderer.view("footer", {}, response);
  response.end();
  }
}



function user(request, response) {
  var username = request.url.replace("/", "");
    //if url == "/..."
  if (username.length > 0 ) {
    response.statusCode = 200;
    response.setHeader('Content-Type', 'text/html');
    renderer.view("header", {}, response);

    //getJSON from treehouse
    var studentProfile = new Profile(username);
    //on "end"
    studentProfile.on("end", function(profileJSON){
      //show profile
      //Store the values which we need
      var values ={
        avatarUrl: profileJSON.gravatar_url,
        username: profileJSON.profile_name,
        badges: profileJSON.badges.length,
        javascriptPoints: profileJSON.points.JavaScript
      }
      //Simple response
      renderer.view("profile", values, response);
      renderer.view("footer", {}, response);
      response.end();

    });
      //on "error"
    studentProfile.on("error", function(error){
          //show error
      renderer.view("error", {errorMessage: error.message}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);
      response.end();
    });





  }
}

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

1 Answer

rydavim
rydavim
18,814 Points

There's a good chance your problem is not in your router.js file, but in your profile.js file instead. Sometime after this course was created, Treehouse changed their protocol from http to https. You will now need to use both in your profile.js file.

You'll need to make the following changes to your profile.js file:

var https = require("https"); // You'll need to include https in addition to http.

  // When you make your request, you'll need to use https instead of http.
  var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
    // .  .  .
  }

I think that should do it, but let me know if you still run into trouble. Happy coding!