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

Error in node-basics http request - Moved Permanently

I was working through the node.js basics tutorial and had the app working fine. I tried to fire it up today, and keep getting 301 error on the http request.

I console logged the username(s) and url, and they seem to look perfectly fine, so I'm a little stumped. I noticed that when I put the url into my browser, it redirected to https. I tihnk that may be the cause, but I'm not 100% sure. Has anyone else experienced this error? Thanks !

Code below:

/* app.js */
var profile = require("./profile");
var users = process.argv.slice(2);
users.forEach(profile.get);


/*profile.js*/
var http = require("http");

//Print out message
function printMessage(username, badgeCount, points) {
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript.";
    console.log(message);
}

//Print out error
function printError(error) {
    console.error(error.message);
}

function get(username) {
    //Connect ot the API URL
    var url = "http://teamtreehouse.com/" + username + ".json";
    console.log("url:", url);
    console.log("username:", username);
    var request = http.get(url, function (response) {
        var body = ""; //adding to this object
        response.on('data', function (chunk) {
            body += chunk; //concatenates
        });
        response.on('end', function () {
            if(response.statusCode === 200) {
                try {
                    //Parse the data
                    var profile = JSON.parse(body);
                    //Print the data
                    printMessage(username, profile.badges.length, profile.points.JavaScript)
                } catch(error) {
                    //Parse the error
                    printError(error);
                }
            } else {
                //Status Code Error
                printError({message: "There was an error getting a profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
            }
        });
    });
    //Connection errorr
    request.on("error", printError);
}

module.exports.get = get;

And then i run something like node app.js chalkers and get There was an error getting a profile for chalkers. (Moved Permanently)

2 Answers

Hi Patrick!

Treehouse now use https everywhere. You need to change the require to https and change the address to https.

/*profile.js*/
var http = require("https");  //here 

var url = "https://teamtreehouse.com/" + username + ".json"; // and here

This should be probably added to teacher's notes in the course if it isn't yet :)

Cheers!

Thanks, i was also going through the same problem as well.

Should the variable in the line of code in the above answer be "https" and not "http"? As in:

var https = require("https");

Thanks ! Had initially tried https in the url and got a kickback. Didn't realize there was a require for https, but now i do ! Thanks again!