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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsHelp! Server logs successfully to the console but no response on localhost.
I've gone through the video but im having trouble getting the home page to load. However if I type a username into the browser it'll load this along with the points and other information. I haven't a clue what I've done wrong.
I think the problem is somewhere in router.js
var Profile = require("./profile.js");
var renderer = require("./renderer.js");
var querystring = require("querystring");
//file header
var commonHeader = {'content-type': 'text/html'};
//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
//if url == "/" && GET
if(request.url === "/") {
if(request.method.toLowerCase === "get") {
//show search
response.writeHead(200, commonHeader);
renderer.view("header", {}, response);
renderer.view("search", {}, response);
renderer.view("footer", {}, response);
response.end();
} else {
//if url == "/" && POST
//get the post data from the body
request.on("data", function(postBody) {
//extract the username
var query = querystring.parse(postBody.toString());
response.write(query.username);
response.end();
//redirect to /:username
});
}
}
}
//Handle HTTP route GET /:username i.e. /chalkers
function user(request, response) {
//if url == "/...."
var username = request.url.replace("/", "");
if(username.length > 0) {
response.writeHead(200, commonHeader);
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();
});
}
}
//export routes to app.js]
module.exports.home = home;
module.exports.user = user;
2 Answers
Jennifer Nordell
Treehouse TeacherI'm not even going to call this an answer... but this line I think needs some parentheses. Note: I just got done with this particular "module" on Treehouse and it's still a bit fuzzy for me :P
if(request.method.toLowerCase === "get") {
I think that should be...
if(request.method.toLowerCase() === "get") {
Nikhil Khandelwal
9,371 PointsI think you should kill the node and again run it. Worked for me, hope it does for you.
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsYou were right! You're a star thank you! I changed this to an answer for you because your solution worked perfectly.
I'm in the same boat as you. Most of this has gone completely over my head, I've got some of the fundamentals down but the documentation scares me :) and I wish I could understand some of what is being taught but I'm definitely keeping this to study and go through piece by piece. It's been fun learning about Node but I need more work at it :)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherJonathan Grieve Oh goody! It's not just me then! By the end of it I felt like a complete moron and was wondering if I could even do simple things like:
There's a reason the project files are downloaded locally to my system for this course :P
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsWhen that happens it's probably time to take a break, but the knowledge is in there somewhere, it just needs to be coaxed out :)
It's a balance between those "A-ha" moments and the "What on earth is going on" moments ;)