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

Unable to display web page due to undefined property 'url'

I am having some trouble displaying the user data. I have followed the videos but still receive this error:

treehouse:~/workspace$ node app.js                                                                          
Server running at http://<workspace-url>/                                                                   
/home/treehouse/workspace/router.js:21                                                                      
        var username = request.url.replace("/","");                                                         
                              ^                                                                             

TypeError: Cannot read property 'url' of undefined                                                          
    at Object.user (/home/treehouse/workspace/router.js:21:24)                                              
    at Server.<anonymous> (/home/treehouse/workspace/app.js:10:9)                                           
    at emitTwo (events.js:87:13)                                                                            
    at Server.emit (events.js:172:7)                                                                        
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:528:12)                                 
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)  

I am not sure why because I have tried following the code exactly. Here is my router.js file:

var Profile = require("./profile.js");
var studentProfile = new Profile("chalkers");


//Handle HTTP route GET / and POST / i.e. Home
function home(request,response) {
    //if the URL == "/" && GET 
    //show the search field
    if(request.url ==="/"){
    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.  /ericpark
function user(request, response) {
    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 reponse
            response.write(values.username+"\n");
            response.end('Footer\n');
        });

        //on "end"
        //show profile 
      //on "error"
        studentProfile.on("error", function(error){
        reponse.write(error.message + "\n");
            response.write(username+"\n");
            response.end('Footer\n');
        });
    }
}
    //if url =="/...." 
    //get json from Treehouse 


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

Eric, it would be great if you would include a link to the challenge. Otherwise it's difficult to see what needs doing, not knowing the course or the instructions.

What's in your app.js file?

1 Answer