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

Nicolas Fernandez
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicolas Fernandez
Full Stack JavaScript Techdegree Student 17,565 Points

Profile is not defined

This is the error:

/home/treehouse/workspace/router.js:21
var studentProfile = new Profile(username);

app.js

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

//Create a web server

const http = require('http');

const hostname = '';
const port = 3000;

http.createServer((request, response) => {
  router.home(request, response);
  router.user(request, response);
}).listen(port, hostname, () => {
  console.log(`Server running at http://${port}/`);
});

// Function that handles the reading of files and merge in value

router.js

//2. Handle HTTP route GET / and POST / i.e. Home

function home(request, response) {
  if (request.url === "/"){
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.write("Header\n");
    response.write("Search\n");
    response.end('Footer\n');
  }
}

//3. Handle HTTP route GET / :username i.e. /nicolasfernandez2

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);
    studentProfile.on("end", function(profileJSON){
      //Show profile

      //Store the vaulues 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.write(error.message + "/n");
      response.end('Footer\n');
    });

  }
}

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

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

It looks like you forgot to require ./profile in router.js