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 trial

JavaScript Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

Any username I type in after my localhost:(port) is not being displayed on the page

Hello I'm about 6 minutes into the video when we're testing if everything is working with a simple response where you type in localhost:(port)/chalkers to display the response.write(values.username + "has "+ values.badges + "badges\n");.

However nothing is being written to the page for me.

A few things before I post my code: I am running my own node.js environment on my local computer and not using the treehouse option and my port is: 1337. I'm pretty sure that something has been overlooked in my code because when I start the server without any searching of usernames it all shows up fine with Header, Search, Footer, but a /chalkers for example just gives me Header, Footer.

router.js:

    var Profile = require("./profile.js")


    // Handle the HTTP route GET / and POST  / i.e. Home
    function home(request, response) {
        //if url = "/" && GET
            //url is a property of the incoming message
        if (request.url === "/") {
                //show search field
            response.writeHead(200, { 'Content-Type': 'text/plain' });
            response.write("Header\n");
            response.write("Search\n");
            response.end("Footer\n")
        }    

            //if url = "/" && POST
                //redirect to /:username
    }


    // Handle the HTTP route GET / :username i.e. /mauricetafollacunningham
    function user(request, response) {
    //if url = "/....."
    var username = request.url.replace("/", "");

    if (username.length > 0) {
        response.writeHead(200, { 'Content-Type': 'text/plain' });
            response.write("Header\n");
            //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
                response.write(values.username + "has "+ values.badges + "badges\n");
                response.end("Footer\n");
            });
            //on "error"
            studentProfile.on("error", function(error){
            // show error
                response.end("Footer\n")
            });

        }

    }

    module.exports.home = home;
    module.exports.user = user;

app.js

//Problem: We need a simple way to look at a user's badge count and JavaScript points from a web browser
//Solution: Use node.js to perform the profile lookups and serve our template via HTTP

var router = require("./router.js");

//Create a web server
const http = require('http');

const hostname = '127.0.0.1';
const port = 1337;

http.createServer((request, response) => {
  router.home(request, response);
  router.user(request, response)
  //response.end('Hello World\n');
}).listen(port, hostname, () => {
  console.log("Server running at http://${hostname}:${port}/");
});



//4. Function that handles the reading of files and merge in value
    // read from file and get a string
        //merge values in to a string

2 Answers

Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

Okay so I figured out that I needed to require both http and https in profile.js and change the references on a few lines cool, but now after I enter in usernames my server eventually dies. I'm given Error: write after end in the console.

This one has me really stuck.

Are you getting any output at all? I am getting something similar.

I put a console.log in studentProfile.on("error", function(error){ // show error response.end("Footer\n") }); It looks like I am getting an error there [Error: There was an error getting the profile for favicon.ico. (Not Found)]

It looks like profile is not emitting the correct data.