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 Dealing with the POST Body

Profile 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");


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);

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);


    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);

    studentProfile.on("error", function(error){
      //show error
      renderer.view("error", {errorMessage: error.message}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);


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?

Steven Parker
Steven Parker
220,937 Points

You 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.

Link to the snapshot: