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 Creating a Simple Server in Node.js Creating a Simple Server

Ruben Sarkisyan
Ruben Sarkisyan
8,473 Points

Not working on local machine (mac)

Has anyone worked through this tutorial on their local machine? When I try to do it on mine, the user object is not being accessed through the console. The response comes back as an error. It seems that I am not able to access user information through the treehouse API.

6 Answers

Jesus Mendoza
Jesus Mendoza
23,288 Points

Can you post your code?

Ruben Sarkisyan
Ruben Sarkisyan
8,473 Points

APP.JS

//Problem: We need a simple way to look at a users badge count //and JavaScript point from a web browser //Solution: Use Node.js to perform the profile look ups and //serve our template via HTTP

//1. Create a web server

var http = require('http'); http.createServer(function(request, response) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000); console.log('Server running at http://<workspace-url>');

//2. Handle HTTP route GET / and POST / i.e. Home //if the url == '/' && GET //show search //if url == '/' && POST //redirect to /:username

//3. Handle HTTP route GET / :username i.e. /chalkers //if url == '/....' //get json from Treehouse //on 'end' //showprofile //on 'error' //show error

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

Ruben Sarkisyan
Ruben Sarkisyan
8,473 Points

EXAMPLE_PROFILE.JS

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

var studentProfile = new Profile("chalkers");

/**

  • When the JSON body is fully recieved the
  • the "end" event is triggered and the full body
  • is given to the handler or callback **/ studentProfile.on("end", console.dir);

/**

  • If a parsing, network or HTTP error occurs an
  • error object is passed in to the handler or callback **/ studentProfile.on("error", console.error);
Ruben Sarkisyan
Ruben Sarkisyan
8,473 Points

PROFILE.JS

var EventEmitter = require("events").EventEmitter; var http = require("http"); var util = require("util");

/**

  • An EventEmitter to get a Treehouse students profile.
  • @param username
  • @constructor */ function Profile(username) {

    EventEmitter.call(this);

    profileEmitter = this;

    //Connect to the API URL (https://teamtreehouse.com/username.json) var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response) { var body = "";

    if (response.statusCode !== 200) {
        request.abort();
        //Status Code Error
        profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
    }
    
    //Read the data
    response.on('data', function (chunk) {
        body += chunk;
        profileEmitter.emit("data", chunk);
    });
    
    response.on('end', function () {
        if(response.statusCode === 200) {
            try {
                //Parse the data
                var profile = JSON.parse(body);
                profileEmitter.emit("end", profile);
            } catch (error) {
                profileEmitter.emit("error", error);
            }
        }
    }).on("error", function(error){
        profileEmitter.emit("error", error);
    });
    

    }); }

util.inherits( Profile, EventEmitter );

module.exports = Profile;

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Out of curiosity, what happens when you try changing it to use https instead of http?

From this:

http.get("http://teamtreehouse.com/" + username + ".json", function(response)

To this:

https.get("https://teamtreehouse.com/" + username + ".json", function(response)

You may also have to change this:

var http = require('http'); 

To this:

var http = require('https'); 

Interested to know if any of this helps! :sparkles:

Ruben Sarkisyan
Ruben Sarkisyan
8,473 Points

that seemed to have fixed the issue...