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 trialCarla Kroll
7,906 Pointspage not loading when I add full else statement
following the course I've come to a place where when I run the app I get a page that continues to load but never does. The issue has come up when I added the if/else statement to the home function. Here is my code for router.js
var Profile = require("./profile.js");
var renderer = require("./renderer.js");
var querystring = require("querystring");
var commonHeaders = {'Content-Type': 'text/html'};
function home(request, response){
// if url === '/' && GET
if(request.url === "/"){
if(request.method.toLowerCase() === "GET") {
// show search field
response.writeHead(200, commonHeaders);
renderer.view("header", {}, response);
renderer.view("search", {}, response);
renderer.view("footer", {}, response);
response.end();
}else {
// if url === / && Post
//get the post data from body
request.on("data", function(postBody){
//extract username
var query = querystring.parse(postBody.toString());
response.writeHead(303, {"Location": "/" + query.username});
response.end();
});
}
}
}
// Handle HTTP route GET /:username i.e. /carlakroll
function user(request, response){
//if url == "/...."
var username = request.url.replace("/", "");
if(username.length > 0){
response.writeHead(200, commonHeaders);
response.write("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,
javascript: profileJSON.points.JavaScript
}
//Simple Response
//response.write(values.username + " has " + values.badges + " badges\n");
renderer.view("header", {}, response);
renderer.view("profile", values, response);
renderer.view("footer", {}, response);
response.end();
});
// on "error"
studentProfile.on("error", function(error){
// show error
renderer.view("header", {}, response);
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;
2 Answers
Alexander La Bianca
15,959 PointsWhen you are getting your '/' route in the home function, you are never going into the if block for 'GET' because you are saying if request.method.toLowerCase() === 'GET'. That will never be true. Hence why the page is endlessly loading. Change it to 'if request.method.toUpperCase() === 'GET'.
Carla Kroll
7,906 PointsYou're my hero!