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 Dell'Ova
Courses Plus Student 6,283 PointsProfile HTML does not write and is not shown
I don't know whats up with my code but I can't search for users because it just puts me right back to the home screen.
I have some console.log's to debug this and I noticed that the "http.createServer" function runs twice.
Here is my app.js:
const router = require("./router.js");
var http = require("http");
http.createServer(function(request, response) {
router.home(request, response);
console.log("router.home run");
router.user(request, response);
console.log("router.user run");
}).listen(3000);
console.log("Server running at http://<workspace-url>/");
Here is my router.js:
const Profile = require("./profile.js");
var renderer = require("./renderer.js");
const commonHeader = {'Content-type': 'text/html'}
function home(request, response) {
if(request.url === "/"){
response.writeHead(200, commonHeader);
renderer.view("header", {}, response);
renderer.view("search", {}, response);
renderer.view("footer", {}, response);
response.end();
}
}
function user(request, response){
let username = request.url.replace("/", "");
console.log("Request URL:", request.url);
console.log("Username Length:", username.length);
if(username.length > 0){
response.writeHead(200, commonHeader);
renderer.view("header", {}, response);
var studentProfile = new Profile(username);
//console.dir(studentProfile);
studentProfile.on("end", function(profileJSON){
//show profile
conosole.dir(profileJSON);
//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();
});
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;
When I run the server and enter the website this is what my console shows:
Server running at http://<workspace-url>/
router.home run
Request URL: /
Username Length: 0
router.user run
router.home run
Request URL: /favicon.ico
Username Length: 11
Profile function from profile.js run
router.user run
I tried changing search.html to get instead of post and when clicking the search button it shows the correct username as the id in the URL. Another weird thing is when I have the form on POST and refresh the page chrome gives me this message:
Confirm Form Resubmission
The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?
What is the problem here and how do i fix it?
Jonathan Dell'Ova
Courses Plus Student 6,283 PointsLink to the snapshot:
Steven Parker
231,172 PointsSteven Parker
231,172 PointsYou can make it much easier to replicate and investigate the issue if you make a snapshot of your workspace and post the link to it here.