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 Creating a Basic Template Engine in Node.js Reading from Files

home route never used. server loads the error route unless a username is entered.

Here are my routes:

var Profile = require("./profile.js"); var renderer = require("./renderer.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'}); renderer.view("header", {}, response); renderer.view("search", {}, response); renderer.view("footer", {}, response); response.end(); } //if url === "/" && POST //redirect to /:username } //Handle HTTP route GET /:username i.e. /ericmoody function user(request, response) { // if url === "/...." var username = request.url.replace("/", ""); if(username.length > 0){ response.writeHead(200, {'Content-Type': 'text/plain'}); renderer.view("header", {}, response); //get json 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;

if(request.url === "/") seems to be false even with a url such as 127.0.0.1:3000/ and if(username.length > 0) seem to be evaluating to true with the same url.

url 127.0.0.1:3000 also causes the error route to load

the error occurs is .length > 1 as well.

1 Answer

Careful copy and pasting... search.html file had the error message.