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


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>/                                                                   
        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'});
  //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'});

        //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,
            //Simple reponse

        //on "end"
        //show profile 
      //on "error"
        studentProfile.on("error", function(error){
        reponse.write(error.message + "\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