Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js HTTP Methods and Headers Redirection Headers in Node.js

Carla Kroll
Carla Kroll
7,906 Points

page 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

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