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 trialSusan Rusie
10,671 PointsWhy is "Chalkers" not printing to the console?
I have tried running this code and this is not printing "Chalkers" to the console. I went into the developer tools and changed "POST" to "GET" in the html, but it doesn't print anything in the browser window. In the browser bar, it shows "localhost:3000/username+=chalkers" but that's it. What am I doing wrong? Any help on this would be greatly appreciated.
Here is my code:
var Profile = require("./profile.js");
var renderer = require("./renderer.js");
var querystring = require("querystring");
var commonHeaders = {'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, commonHeaders);
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, commonHeaders);
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;
1 Answer
Joel Kraft
Treehouse Guest TeacherHi Susan Rusie,
First off, the code you posted here has a syntax error. Line 10's if statement needs an open paren, or "(
".
But on to your question! To print a value to the console, you must use console.log()
, and that command is not present in this code. Following along with the video, it appears that code you've presented above represents the state of the file at the end of this video. In other words, console.log()
was used earlier in the video, but was then replaced with other code.
I would recommend re-watching the video, and following each of Andrew's steps closely. Be sure to start with the correct files.
- If you're using Workspaces, you can start with a fresh Workspace by deleting the one associated with this video, and then relaunching a new one.
- If you're using the downloadable project files, the folder you're looking for is "
stage_4_video_2
".
If you're still stumped after that, please let us know!
Susan Rusie
10,671 PointsSusan Rusie
10,671 PointsI just added that "(" I was missing and the program worked like it should. Thanks for your help.
Joel Kraft
Treehouse Guest TeacherJoel Kraft
Treehouse Guest TeacherGreat! Glad you got it working.